diff --git a/internal/pkg/params/globals.go b/internal/pkg/params/globals.go index 3ec4806..91ac4cf 100644 --- a/internal/pkg/params/globals.go +++ b/internal/pkg/params/globals.go @@ -28,4 +28,7 @@ var ( // Slice of strings to hold any labels added through --labels argument. Labels []string + + // Slice of strings to hold any annotations added through --annotations argument. + Annotations []string ) diff --git a/mg/cmd/argocd.go b/mg/cmd/argocd.go index 3c3e47f..12deba7 100644 --- a/mg/cmd/argocd.go +++ b/mg/cmd/argocd.go @@ -44,7 +44,7 @@ func init() { argocdCreateApplicationCmd.Flags().BoolVar(&argocd.AppOpts.AutomatedSyncPolicyPrune, "auto-prune", false, "Automatically delete removed items") argocdCreateApplicationCmd.Flags().BoolVar(&argocd.AppOpts.AutomatedSyncPolicySelfHeal, "auto-heal", false, "Try to self heal?") argocdCreateApplicationCmd.Flags().StringSliceVar(¶ms.Labels, "labels", []string{}, "Provide extra labels as key=value pairs, seperated by ,") - + argocdCreateApplicationCmd.Flags().StringSliceVar(¶ms.Annotations, "annotations", []string{}, "Provide extra annotations as key=value pairs, seperated by ','") _ = argocdCreateCmd.MarkPersistentFlagRequired("namespace") _ = argocdCreateApplicationCmd.MarkFlagRequired("project") _ = argocdCreateApplicationCmd.MarkFlagRequired("repo") @@ -101,7 +101,7 @@ var argocdCreateApplicationCmd = &cobra.Command{ // Add labels from params app.Labels = modules.MergeLabels(app.Labels, modules.LabelsFromParams(params.Labels)) - + app.Annotations = modules.AnnotationsFromParams(params.Annotations) if params.Output { argocd.OutputApplication(app, params.Format) } diff --git a/pkg/generators/argocd/argocd.go b/pkg/generators/argocd/argocd.go index f8052cc..6a39459 100644 --- a/pkg/generators/argocd/argocd.go +++ b/pkg/generators/argocd/argocd.go @@ -160,13 +160,14 @@ func (g *ApplicationGenerator) Application(name string) argoapp.Application { APIVersion: "argoproj.io/v1alpha1", }, ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: g.Options.Namespace, - Labels: g.MetaGraf.Metadata.Labels, + Name: name, + Namespace: g.Options.Namespace, + Labels: g.MetaGraf.Metadata.Labels, + Annotations: g.MetaGraf.Metadata.Annotations, }, Spec: argoapp.ApplicationSpec{ Destination: argoapp.ApplicationDestination{ - Server: g.Options.ApplicationDestinationServer, + Server: g.Options.ApplicationDestinationServer, // todo revert to g.Options.ApplicationDestinationNamespace Namespace: g.Options.Namespace, }, diff --git a/pkg/modules/common.go b/pkg/modules/common.go index 4f1148f..2dc0313 100644 --- a/pkg/modules/common.go +++ b/pkg/modules/common.go @@ -55,8 +55,8 @@ var ( Context string // Application context root from FlagPassingHack. CreateGlobals bool // Sets the default pull policy for all metagraf modules - PullPolicy corev1.PullPolicy = corev1.PullIfNotPresent - IgnoreMissingEnv bool = false + PullPolicy corev1.PullPolicy = corev1.PullIfNotPresent + IgnoreMissingEnv bool = false ) var Variables metagraf.MGProperties @@ -189,11 +189,11 @@ func GetEnvVars(mg *metagraf.MetaGraf, inputprops metagraf.MGProperties) (output for _, output := range outputevars { outputevarsmap[output.Name] = output.Value } - for _, specenv := range specenvsmap { - if _, ok := outputevarsmap[specenv.Name]; ! ok && specenv.Required { - glog.Errorf("environment variable with name [%s] is missing or has no value", specenv.Name) - err = fmt.Errorf("environment variable with name [%s] is missing or has no value", specenv.Name) - return nil, err + for _, specenv := range specenvsmap { + if _, ok := outputevarsmap[specenv.Name]; !ok && specenv.Required { + glog.Errorf("environment variable with name [%s] is missing or has no value", specenv.Name) + err = fmt.Errorf("environment variable with name [%s] is missing or has no value", specenv.Name) + return nil, err } } @@ -201,7 +201,6 @@ func GetEnvVars(mg *metagraf.MetaGraf, inputprops metagraf.MGProperties) (output return outputevars, err } - func GetBuildEnvVars(mg *metagraf.MetaGraf, mgp metagraf.MGProperties) []corev1.EnvVar { var envs []corev1.EnvVar @@ -474,6 +473,18 @@ func LabelsFromParams(labels []string) map[string]string { return ret } +func AnnotationsFromParams(annotations []string) map[string]string { + ret := make(map[string]string) + for _, s := range annotations { + split := strings.Split(s, "=") + if len(split) != 2 { + continue + } + ret[split[0]] = split[1] + } + return ret +} + // Generate standardised labels map func Labels(name string, input map[string]string) map[string]string { // Resource labels @@ -486,6 +497,7 @@ func Labels(name string, input map[string]string) map[string]string { } // Merge two Labels maps, first input gets overriden by second input +// TODO: rename function to a more generic like MergeMaps func MergeLabels(first map[string]string, second map[string]string) map[string]string { // check if first is initialized if first == nil {