Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lagt til støtte for å sende inn annotations til argocd create applica… #91

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/pkg/params/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions mg/cmd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params.Labels, "labels", []string{}, "Provide extra labels as key=value pairs, seperated by ,")

argocdCreateApplicationCmd.Flags().StringSliceVar(&params.Annotations, "annotations", []string{}, "Provide extra annotations as key=value pairs, seperated by ','")
_ = argocdCreateCmd.MarkPersistentFlagRequired("namespace")
_ = argocdCreateApplicationCmd.MarkFlagRequired("project")
_ = argocdCreateApplicationCmd.MarkFlagRequired("repo")
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/generators/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
28 changes: 20 additions & 8 deletions pkg/modules/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -189,19 +189,18 @@ 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

}
}

return outputevars, err
}


func GetBuildEnvVars(mg *metagraf.MetaGraf, mgp metagraf.MGProperties) []corev1.EnvVar {
var envs []corev1.EnvVar

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Loading