diff --git a/changelogs/unreleased/00000-tkaovila b/changelogs/unreleased/00000-tkaovila new file mode 100644 index 0000000000..8a708589c9 --- /dev/null +++ b/changelogs/unreleased/00000-tkaovila @@ -0,0 +1 @@ +Support install label and annotations on velero CLI installation resources (#4982) \ No newline at end of file diff --git a/pkg/cmd/cli/install/install.go b/pkg/cmd/cli/install/install.go index 722473b4f8..56f09aef1c 100644 --- a/pkg/cmd/cli/install/install.go +++ b/pkg/cmd/cli/install/install.go @@ -47,6 +47,8 @@ type InstallOptions struct { BucketName string Prefix string ProviderName string + InstallAnnotations flag.Map + InstallLabels flag.Map PodAnnotations flag.Map PodLabels flag.Map ServiceAccountAnnotations flag.Map @@ -90,6 +92,8 @@ func (o *InstallOptions) BindFlags(flags *pflag.FlagSet) { flags.BoolVar(&o.NoDefaultBackupLocation, "no-default-backup-location", o.NoDefaultBackupLocation, "Flag indicating if a default backup location should be created. Must be used as confirmation if --bucket or --provider are not provided. Optional.") flags.StringVar(&o.Image, "image", o.Image, "Image to use for the Velero and node agent pods. Optional.") flags.StringVar(&o.Prefix, "prefix", o.Prefix, "Prefix under which all Velero data should be stored within the bucket. Optional.") + flags.Var(&o.InstallAnnotations, "install-annotations", "Annotations to add to installation resources. Optional. Format is key1=value1,key2=value2") + flags.Var(&o.InstallLabels, "install-labels", "Labels to add to installation resources. Optional. Format is key1=value1,key2=value2") flags.Var(&o.PodAnnotations, "pod-annotations", "Annotations to add to the Velero and node agent pods. Optional. Format is key1=value1,key2=value2") flags.Var(&o.PodLabels, "pod-labels", "Labels to add to the Velero and node agent pods. Optional. Format is key1=value1,key2=value2") flags.Var(&o.ServiceAccountAnnotations, "sa-annotations", "Annotations to add to the Velero ServiceAccount. Add iam.gke.io/gcp-service-account=[GSA_NAME]@[PROJECT_NAME].iam.gserviceaccount.com for workload identity. Optional. Format is key1=value1,key2=value2") @@ -127,6 +131,8 @@ func NewInstallOptions() *InstallOptions { Image: velero.DefaultVeleroImage(), BackupStorageConfig: flag.NewMap(), VolumeSnapshotConfig: flag.NewMap(), + InstallAnnotations: flag.NewMap(), + InstallLabels: flag.NewMap(), PodAnnotations: flag.NewMap(), PodLabels: flag.NewMap(), ServiceAccountAnnotations: flag.NewMap(), @@ -186,6 +192,8 @@ func (o *InstallOptions) AsVeleroOptions() (*install.VeleroOptions, error) { ProviderName: o.ProviderName, Bucket: o.BucketName, Prefix: o.Prefix, + InstallAnnotations: o.InstallAnnotations.Data(), + InstallLabels: o.InstallLabels.Data(), PodAnnotations: o.PodAnnotations.Data(), PodLabels: o.PodLabels.Data(), ServiceAccountAnnotations: o.ServiceAccountAnnotations.Data(), diff --git a/pkg/install/resources.go b/pkg/install/resources.go index a511374005..552bf45646 100644 --- a/pkg/install/resources.go +++ b/pkg/install/resources.go @@ -64,6 +64,19 @@ func podLabels(userLabels ...map[string]string) map[string]string { return base } +// Concatenate a list of maps into a single map. If a key is present in multiple maps, the value from the first map +func concatMapStringString(maps ...map[string]string) map[string]string { + result := make(map[string]string) + for _, m := range maps { + for k, v := range m { + if _, ok := result[k]; !ok { + result[k] = v + } + } + } + return result +} + func podAnnotations(userAnnotations map[string]string) map[string]string { // Use the default annotations as a starting point base := map[string]string{ @@ -221,6 +234,8 @@ type VeleroOptions struct { ProviderName string Bucket string Prefix string + InstallAnnotations map[string]string + InstallLabels map[string]string PodAnnotations map[string]string PodLabels map[string]string ServiceAccountAnnotations map[string]string @@ -340,5 +355,9 @@ func AllResources(o *VeleroOptions) *unstructured.UnstructuredList { appendUnstructured(resources, ds) } + for i := range resources.Items { + resources.Items[i].SetLabels(concatMapStringString(o.InstallLabels, resources.Items[i].GetLabels())) + resources.Items[i].SetAnnotations(concatMapStringString(o.InstallAnnotations, resources.Items[i].GetAnnotations())) + } return resources }