Add support for --pod-labels (#4694)

* Add support for --pod-labels
* Add changelog

Signed-off-by: James Landrein <github@j4m3s.eu>
This commit is contained in:
James Landrein
2022-02-24 10:00:14 +01:00
committed by GitHub
parent ea38f84c53
commit 77be862b9c
5 changed files with 33 additions and 9 deletions

View File

@@ -0,0 +1 @@
Add --pod-labels flag to velero install

View File

@@ -47,6 +47,7 @@ type InstallOptions struct {
Prefix string
ProviderName string
PodAnnotations flag.Map
PodLabels flag.Map
ServiceAccountAnnotations flag.Map
VeleroPodCPURequest string
VeleroPodMemRequest string
@@ -84,6 +85,7 @@ func (o *InstallOptions) BindFlags(flags *pflag.FlagSet) {
flags.StringVar(&o.Image, "image", o.Image, "Image to use for the Velero and restic server 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.PodAnnotations, "pod-annotations", "Annotations to add to the Velero and restic pods. Optional. Format is key1=value1,key2=value2")
flags.Var(&o.PodLabels, "pod-labels", "Labels to add to the Velero and restic 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")
flags.StringVar(&o.VeleroPodCPURequest, "velero-pod-cpu-request", o.VeleroPodCPURequest, `CPU request for Velero pod. A value of "0" is treated as unbounded. Optional.`)
flags.StringVar(&o.VeleroPodMemRequest, "velero-pod-mem-request", o.VeleroPodMemRequest, `Memory request for Velero pod. A value of "0" is treated as unbounded. Optional.`)
@@ -116,6 +118,7 @@ func NewInstallOptions() *InstallOptions {
BackupStorageConfig: flag.NewMap(),
VolumeSnapshotConfig: flag.NewMap(),
PodAnnotations: flag.NewMap(),
PodLabels: flag.NewMap(),
ServiceAccountAnnotations: flag.NewMap(),
VeleroPodCPURequest: install.DefaultVeleroPodCPURequest,
VeleroPodMemRequest: install.DefaultVeleroPodMemRequest,
@@ -173,6 +176,7 @@ func (o *InstallOptions) AsVeleroOptions() (*install.VeleroOptions, error) {
Bucket: o.BucketName,
Prefix: o.Prefix,
PodAnnotations: o.PodAnnotations.Data(),
PodLabels: o.PodLabels.Data(),
ServiceAccountAnnotations: o.ServiceAccountAnnotations.Data(),
VeleroPodResources: veleroPodResources,
ResticPodResources: resticPodResources,

View File

@@ -68,10 +68,9 @@ func DaemonSet(namespace string, opts ...podTemplateOption) *appsv1.DaemonSet {
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"name": "restic",
"component": "velero",
},
Labels: podLabels(c.labels, map[string]string{
"name": "restic",
}),
Annotations: c.annotations,
},
Spec: corev1.PodSpec{

View File

@@ -36,6 +36,7 @@ type podTemplateConfig struct {
envVars []corev1.EnvVar
restoreOnly bool
annotations map[string]string
labels map[string]string
resources corev1.ResourceRequirements
withSecret bool
defaultResticMaintenanceFrequency time.Duration
@@ -56,6 +57,12 @@ func WithAnnotations(annotations map[string]string) podTemplateOption {
}
}
func WithLabels(labels map[string]string) podTemplateOption {
return func(c *podTemplateConfig) {
c.labels = labels
}
}
func WithEnvFromSecretKey(varName, secret, key string) podTemplateOption {
return func(c *podTemplateConfig) {
c.envVars = append(c.envVars, corev1.EnvVar{
@@ -141,9 +148,6 @@ func Deployment(namespace string, opts ...podTemplateOption) *appsv1.Deployment
args = append(args, "--default-volumes-to-restic=true")
}
containerLabels := Labels()
containerLabels["deploy"] = "velero"
deployment := &appsv1.Deployment{
ObjectMeta: objectMeta(namespace, "velero"),
TypeMeta: metav1.TypeMeta{
@@ -154,7 +158,7 @@ func Deployment(namespace string, opts ...podTemplateOption) *appsv1.Deployment
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"deploy": "velero"}},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: containerLabels,
Labels: podLabels(c.labels, map[string]string{"deploy": "velero"}),
Annotations: podAnnotations(c.annotations),
},
Spec: corev1.PodSpec{

View File

@@ -48,6 +48,20 @@ func Labels() map[string]string {
}
}
func podLabels(userLabels ...map[string]string) map[string]string {
// Use the default labels as a starting point
base := Labels()
// Merge base labels with user labels to enforce CLI precedence
for _, labels := range userLabels {
for k, v := range labels {
base[k] = v
}
}
return base
}
func podAnnotations(userAnnotations map[string]string) map[string]string {
// Use the default annotations as a starting point
base := map[string]string{
@@ -77,7 +91,6 @@ func objectMeta(namespace, name string) metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: Labels(),
}
}
@@ -201,6 +214,7 @@ type VeleroOptions struct {
Bucket string
Prefix string
PodAnnotations map[string]string
PodLabels map[string]string
ServiceAccountAnnotations map[string]string
VeleroPodResources corev1.ResourceRequirements
ResticPodResources corev1.ResourceRequirements
@@ -265,6 +279,7 @@ func AllResources(o *VeleroOptions) *unstructured.UnstructuredList {
deployOpts := []podTemplateOption{
WithAnnotations(o.PodAnnotations),
WithLabels(o.PodLabels),
WithImage(o.Image),
WithResources(o.VeleroPodResources),
WithSecret(secretPresent),
@@ -294,6 +309,7 @@ func AllResources(o *VeleroOptions) *unstructured.UnstructuredList {
if o.UseRestic {
dsOpts := []podTemplateOption{
WithAnnotations(o.PodAnnotations),
WithLabels(o.PodLabels),
WithImage(o.Image),
WithResources(o.ResticPodResources),
WithSecret(secretPresent),