Skip to content

Commit

Permalink
Support install label and annotations on velero CLI installation reso…
Browse files Browse the repository at this point in the history
…urces

Signed-off-by: Tiger Kaovilai <tkaovila@redhat.com>
  • Loading branch information
kaovilai committed Apr 15, 2023
1 parent 65f99c1 commit 59937b5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/00000-tkaovila
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support install label and annotations on velero CLI installation resources (#4982)
8 changes: 8 additions & 0 deletions pkg/cmd/cli/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
19 changes: 19 additions & 0 deletions pkg/install/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 59937b5

Please sign in to comment.