From 4e2c23d53c2ad27145a49d1f73c8bd90fd8f8199 Mon Sep 17 00:00:00 2001 From: izturn <44051386+izturn@users.noreply.github.com> Date: Thu, 21 Sep 2023 10:43:18 +0800 Subject: [PATCH] crd/ContourDeployment: Add field 'podLabels' for contour (#2) * add pod labels field to contourDeployment --- .../v1alpha1/contourdeployment.go | 12 ++++++++ .../v1alpha1/zz_generated.deepcopy.go | 14 +++++++++ changelogs/unreleased/5543-izturn-small.md | 1 + examples/contour/01-crds.yaml | 14 +++++++++ examples/render/contour-deployment.yaml | 14 +++++++++ .../render/contour-gateway-provisioner.yaml | 14 +++++++++ examples/render/contour-gateway.yaml | 14 +++++++++ examples/render/contour.yaml | 14 +++++++++ internal/provisioner/controller/gateway.go | 8 +++++ internal/provisioner/model/model.go | 8 +++++ internal/provisioner/model/names.go | 9 ++++++ .../objects/dataplane/dataplane.go | 9 +++++- .../objects/dataplane/dataplane_test.go | 14 +++++++++ .../objects/deployment/deployment.go | 11 +++++-- .../objects/deployment/deployment_test.go | 25 ++++++++++++---- .../docs/main/config/api-reference.html | 30 +++++++++++++++++++ 16 files changed, 202 insertions(+), 9 deletions(-) create mode 100644 changelogs/unreleased/5543-izturn-small.md diff --git a/apis/projectcontour/v1alpha1/contourdeployment.go b/apis/projectcontour/v1alpha1/contourdeployment.go index abc371b5197..eb9b57b39ca 100644 --- a/apis/projectcontour/v1alpha1/contourdeployment.go +++ b/apis/projectcontour/v1alpha1/contourdeployment.go @@ -119,6 +119,12 @@ type ContourSettings struct { // the annotations for Prometheus will be appended or overwritten with predefined value. // +optional PodAnnotations map[string]string `json:"podAnnotations,omitempty"` + + // PodLabels defines labels to add to the Contour pods. + // If there is a label with the same key as in `ContourDeploymentSpec.ResourceLabels`, + // the one here has a higher priority. + // +optional + PodLabels map[string]string `json:"podLabels,omitempty"` } // DeploymentSettings contains settings for Deployment resources. @@ -185,6 +191,12 @@ type EnvoySettings struct { // +optional PodAnnotations map[string]string `json:"podAnnotations,omitempty"` + // PodLabels defines labels to add to the Envoy pods. + // If there is a label with the same key as in `ContourDeploymentSpec.ResourceLabels`, + // the one here has a higher priority. + // +optional + PodLabels map[string]string `json:"podLabels,omitempty"` + // Compute Resources required by envoy container. // Cannot be updated. // More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ diff --git a/apis/projectcontour/v1alpha1/zz_generated.deepcopy.go b/apis/projectcontour/v1alpha1/zz_generated.deepcopy.go index ff4f84765b9..26f8714fc76 100644 --- a/apis/projectcontour/v1alpha1/zz_generated.deepcopy.go +++ b/apis/projectcontour/v1alpha1/zz_generated.deepcopy.go @@ -372,6 +372,13 @@ func (in *ContourSettings) DeepCopyInto(out *ContourSettings) { (*out)[key] = val } } + if in.PodLabels != nil { + in, out := &in.PodLabels, &out.PodLabels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ContourSettings. @@ -658,6 +665,13 @@ func (in *EnvoySettings) DeepCopyInto(out *EnvoySettings) { (*out)[key] = val } } + if in.PodLabels != nil { + in, out := &in.PodLabels, &out.PodLabels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } in.Resources.DeepCopyInto(&out.Resources) if in.DaemonSet != nil { in, out := &in.DaemonSet, &out.DaemonSet diff --git a/changelogs/unreleased/5543-izturn-small.md b/changelogs/unreleased/5543-izturn-small.md new file mode 100644 index 00000000000..7d069f3f0f3 --- /dev/null +++ b/changelogs/unreleased/5543-izturn-small.md @@ -0,0 +1 @@ +Add Kubernetes labels configurability to ContourDeployment resource. to enable customize pod labels for pod/contour & pod/envoy \ No newline at end of file diff --git a/examples/contour/01-crds.yaml b/examples/contour/01-crds.yaml index 940a1b288bf..692dd1a5781 100644 --- a/examples/contour/01-crds.yaml +++ b/examples/contour/01-crds.yaml @@ -1415,6 +1415,13 @@ spec: Contour pods. the annotations for Prometheus will be appended or overwritten with predefined value. type: object + podLabels: + additionalProperties: + type: string + description: PodLabels defines labels to add to the Contour pods. + If there is a label with the same key as in `ContourDeploymentSpec.ResourceLabels`, + the one here has a higher priority. + type: object replicas: description: "Deprecated: Use `DeploymentSettings.Replicas` instead. \n Replicas is the desired number of Contour replicas. If if @@ -3420,6 +3427,13 @@ spec: Envoy pods. the annotations for Prometheus will be appended or overwritten with predefined value. type: object + podLabels: + additionalProperties: + type: string + description: PodLabels defines labels to add to the Envoy pods. + If there is a label with the same key as in `ContourDeploymentSpec.ResourceLabels`, + the one here has a higher priority. + type: object replicas: description: "Deprecated: Use `DeploymentSettings.Replicas` instead. \n Replicas is the desired number of Envoy replicas. If WorkloadType diff --git a/examples/render/contour-deployment.yaml b/examples/render/contour-deployment.yaml index 18fab7f0adf..06c54014828 100644 --- a/examples/render/contour-deployment.yaml +++ b/examples/render/contour-deployment.yaml @@ -1634,6 +1634,13 @@ spec: Contour pods. the annotations for Prometheus will be appended or overwritten with predefined value. type: object + podLabels: + additionalProperties: + type: string + description: PodLabels defines labels to add to the Contour pods. + If there is a label with the same key as in `ContourDeploymentSpec.ResourceLabels`, + the one here has a higher priority. + type: object replicas: description: "Deprecated: Use `DeploymentSettings.Replicas` instead. \n Replicas is the desired number of Contour replicas. If if @@ -3639,6 +3646,13 @@ spec: Envoy pods. the annotations for Prometheus will be appended or overwritten with predefined value. type: object + podLabels: + additionalProperties: + type: string + description: PodLabels defines labels to add to the Envoy pods. + If there is a label with the same key as in `ContourDeploymentSpec.ResourceLabels`, + the one here has a higher priority. + type: object replicas: description: "Deprecated: Use `DeploymentSettings.Replicas` instead. \n Replicas is the desired number of Envoy replicas. If WorkloadType diff --git a/examples/render/contour-gateway-provisioner.yaml b/examples/render/contour-gateway-provisioner.yaml index 7bc48cb9ae8..fa830028928 100644 --- a/examples/render/contour-gateway-provisioner.yaml +++ b/examples/render/contour-gateway-provisioner.yaml @@ -1426,6 +1426,13 @@ spec: Contour pods. the annotations for Prometheus will be appended or overwritten with predefined value. type: object + podLabels: + additionalProperties: + type: string + description: PodLabels defines labels to add to the Contour pods. + If there is a label with the same key as in `ContourDeploymentSpec.ResourceLabels`, + the one here has a higher priority. + type: object replicas: description: "Deprecated: Use `DeploymentSettings.Replicas` instead. \n Replicas is the desired number of Contour replicas. If if @@ -3431,6 +3438,13 @@ spec: Envoy pods. the annotations for Prometheus will be appended or overwritten with predefined value. type: object + podLabels: + additionalProperties: + type: string + description: PodLabels defines labels to add to the Envoy pods. + If there is a label with the same key as in `ContourDeploymentSpec.ResourceLabels`, + the one here has a higher priority. + type: object replicas: description: "Deprecated: Use `DeploymentSettings.Replicas` instead. \n Replicas is the desired number of Envoy replicas. If WorkloadType diff --git a/examples/render/contour-gateway.yaml b/examples/render/contour-gateway.yaml index 2dccfd92371..fcc18267f46 100644 --- a/examples/render/contour-gateway.yaml +++ b/examples/render/contour-gateway.yaml @@ -1637,6 +1637,13 @@ spec: Contour pods. the annotations for Prometheus will be appended or overwritten with predefined value. type: object + podLabels: + additionalProperties: + type: string + description: PodLabels defines labels to add to the Contour pods. + If there is a label with the same key as in `ContourDeploymentSpec.ResourceLabels`, + the one here has a higher priority. + type: object replicas: description: "Deprecated: Use `DeploymentSettings.Replicas` instead. \n Replicas is the desired number of Contour replicas. If if @@ -3642,6 +3649,13 @@ spec: Envoy pods. the annotations for Prometheus will be appended or overwritten with predefined value. type: object + podLabels: + additionalProperties: + type: string + description: PodLabels defines labels to add to the Envoy pods. + If there is a label with the same key as in `ContourDeploymentSpec.ResourceLabels`, + the one here has a higher priority. + type: object replicas: description: "Deprecated: Use `DeploymentSettings.Replicas` instead. \n Replicas is the desired number of Envoy replicas. If WorkloadType diff --git a/examples/render/contour.yaml b/examples/render/contour.yaml index d1e47c9fecb..fac0faa5e6e 100644 --- a/examples/render/contour.yaml +++ b/examples/render/contour.yaml @@ -1634,6 +1634,13 @@ spec: Contour pods. the annotations for Prometheus will be appended or overwritten with predefined value. type: object + podLabels: + additionalProperties: + type: string + description: PodLabels defines labels to add to the Contour pods. + If there is a label with the same key as in `ContourDeploymentSpec.ResourceLabels`, + the one here has a higher priority. + type: object replicas: description: "Deprecated: Use `DeploymentSettings.Replicas` instead. \n Replicas is the desired number of Contour replicas. If if @@ -3639,6 +3646,13 @@ spec: Envoy pods. the annotations for Prometheus will be appended or overwritten with predefined value. type: object + podLabels: + additionalProperties: + type: string + description: PodLabels defines labels to add to the Envoy pods. + If there is a label with the same key as in `ContourDeploymentSpec.ResourceLabels`, + the one here has a higher priority. + type: object replicas: description: "Deprecated: Use `DeploymentSettings.Replicas` instead. \n Replicas is the desired number of Envoy replicas. If WorkloadType diff --git a/internal/provisioner/controller/gateway.go b/internal/provisioner/controller/gateway.go index baf1b39ddf3..2f0a1369836 100644 --- a/internal/provisioner/controller/gateway.go +++ b/internal/provisioner/controller/gateway.go @@ -267,6 +267,9 @@ func (r *gatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct for k, v := range contourParams.PodAnnotations { contourModel.Spec.ContourPodAnnotations[k] = v } + for k, v := range contourParams.PodLabels { + contourModel.Spec.ContourPodLabels[k] = v + } } if gatewayClassParams.Spec.Envoy != nil { @@ -339,6 +342,11 @@ func (r *gatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct contourModel.Spec.EnvoyPodAnnotations[k] = v } + // Pod Labels + for k, v := range envoyParams.PodLabels { + contourModel.Spec.EnvoyPodLabels[k] = v + } + contourModel.Spec.EnvoyResources = envoyParams.Resources if envoyParams.LogLevel != "" { diff --git a/internal/provisioner/model/model.go b/internal/provisioner/model/model.go index cdb9d149297..656eeeb7525 100644 --- a/internal/provisioner/model/model.go +++ b/internal/provisioner/model/model.go @@ -73,6 +73,8 @@ func Default(namespace, name string) *Contour { ResourceLabels: map[string]string{}, EnvoyPodAnnotations: map[string]string{}, ContourPodAnnotations: map[string]string{}, + EnvoyPodLabels: map[string]string{}, + ContourPodLabels: map[string]string{}, }, } } @@ -217,6 +219,12 @@ type ContourSpec struct { // the annotations: "prometheus.io/scrape", "prometheus.io/port" will be overwritten with predefined value. ContourPodAnnotations map[string]string + // EnvoyPodLabels holds the labels that will be add to the envoy‘s pod. + EnvoyPodLabels map[string]string + + // ContourPodLabels holds the labels that will be add to the contour's pod. + ContourPodLabels map[string]string + // Compute Resources required by envoy container. EnvoyResources corev1.ResourceRequirements diff --git a/internal/provisioner/model/names.go b/internal/provisioner/model/names.go index 54a751d6e7c..091121fce93 100644 --- a/internal/provisioner/model/names.go +++ b/internal/provisioner/model/names.go @@ -86,6 +86,15 @@ func (c *Contour) AppLabels() map[string]string { labels[k] = v } + for k, v := range c.AppPredefinedLabels() { + labels[k] = v + } + return labels +} + +// AppPredefinedLabels returns predefined labels for a Contour resources(Deployment/DaemonSet). +func (c *Contour) AppPredefinedLabels() map[string]string { + labels := map[string]string{} labels["app.kubernetes.io/instance"] = c.Name labels["app.kubernetes.io/name"] = "contour" labels["app.kubernetes.io/component"] = "ingress-controller" diff --git a/internal/provisioner/objects/dataplane/dataplane.go b/internal/provisioner/objects/dataplane/dataplane.go index fb85a507445..c0c1bc2924c 100644 --- a/internal/provisioner/objects/dataplane/dataplane.go +++ b/internal/provisioner/objects/dataplane/dataplane.go @@ -526,9 +526,16 @@ func EnvoyPodSelector(contour *model.Contour) *metav1.LabelSelector { // envoyPodLabels returns the labels for envoy's pods func envoyPodLabels(contour *model.Contour) map[string]string { labels := EnvoyPodSelector(contour).MatchLabels - for k, v := range contour.AppLabels() { + for k, v := range model.CommonLabels(contour) { labels[k] = v } + for k, v := range contour.Spec.EnvoyPodLabels { + labels[k] = v + } + for k, v := range contour.AppPredefinedLabels() { + labels[k] = v + } + return labels } diff --git a/internal/provisioner/objects/dataplane/dataplane_test.go b/internal/provisioner/objects/dataplane/dataplane_test.go index c6918500d5e..ad83d186b04 100644 --- a/internal/provisioner/objects/dataplane/dataplane_test.go +++ b/internal/provisioner/objects/dataplane/dataplane_test.go @@ -107,6 +107,15 @@ func checkDaemonSetHasPodAnnotations(t *testing.T, ds *appsv1.DaemonSet, expecte t.Errorf("daemonset has unexpected %q pod annotations", ds.Spec.Template.Annotations) } +func checkDaemonSetHasPodLabels(t *testing.T, ds *appsv1.DaemonSet, expected map[string]string) { + t.Helper() + + if apiequality.Semantic.DeepEqual(ds.Spec.Template.ObjectMeta.Labels, expected) { + return + } + t.Errorf("daemonset has unexpected %q pod labels", ds.Spec.Template.Labels) +} + func checkContainerHasPort(t *testing.T, ds *appsv1.DaemonSet, port int32) { t.Helper() @@ -271,6 +280,10 @@ func TestDesiredDaemonSet(t *testing.T) { "prometheus.io/scrape": "false", } + cntr.Spec.EnvoyPodLabels = map[string]string{ + "sidecar.istio.io/inject": "false", + } + volTest := corev1.Volume{ Name: "vol-test-mount", } @@ -346,6 +359,7 @@ func TestDesiredDaemonSet(t *testing.T) { checkDaemonSecurityContext(t, ds) checkDaemonSetHasVolume(t, ds, volTest, volTestMount) checkDaemonSetHasPodAnnotations(t, ds, envoyPodAnnotations(cntr)) + checkDaemonSetHasPodLabels(t, ds, envoyPodLabels(cntr)) checkDaemonSetHasMetricsPort(t, ds, objects.EnvoyMetricsPort) checkDaemonSetHasResourceRequirements(t, ds, resQutoa) diff --git a/internal/provisioner/objects/deployment/deployment.go b/internal/provisioner/objects/deployment/deployment.go index 4a1688708f9..4908ca12b83 100644 --- a/internal/provisioner/objects/deployment/deployment.go +++ b/internal/provisioner/objects/deployment/deployment.go @@ -290,10 +290,17 @@ func ContourDeploymentPodSelector(contour *model.Contour) *metav1.LabelSelector } // contourPodLabels returns the labels for contour's pods, there are pod selector & -// app labels +// app & pod labels func contourPodLabels(contour *model.Contour) map[string]string { labels := ContourDeploymentPodSelector(contour).MatchLabels - for k, v := range contour.AppLabels() { + + for k, v := range model.CommonLabels(contour) { + labels[k] = v + } + for k, v := range contour.Spec.ContourPodLabels { + labels[k] = v + } + for k, v := range contour.AppPredefinedLabels() { labels[k] = v } return labels diff --git a/internal/provisioner/objects/deployment/deployment_test.go b/internal/provisioner/objects/deployment/deployment_test.go index 2d5de7ad691..8f4d8aec0b6 100644 --- a/internal/provisioner/objects/deployment/deployment_test.go +++ b/internal/provisioner/objects/deployment/deployment_test.go @@ -76,7 +76,16 @@ func checkPodHasAnnotations(t *testing.T, tmpl *corev1.PodTemplateSpec, annotati t.Errorf("pod template has unexpected %q annotations", tmpl.Annotations) } } +} + +func checkPodHasLabels(t *testing.T, tmpl *corev1.PodTemplateSpec, labels map[string]string) { + t.Helper() + for k, v := range labels { + if val, ok := tmpl.Labels[k]; !ok || val != v { + t.Errorf("pod template has unexpected %q labels", tmpl.Labels) + } + } } func checkContainerHasArg(t *testing.T, container *corev1.Container, arg string) { @@ -152,10 +161,6 @@ func TestDesiredDeployment(t *testing.T) { }, } - annotations := map[string]string{ - "key": "value", - "prometheus.io/scrape": "false", - } cntr.Spec.ContourResources = resQutoa // Change the Kubernetes log level to test --kubernetes-debug. @@ -165,9 +170,16 @@ func TestDesiredDeployment(t *testing.T) { cntr.Spec.ContourLogLevel = v1alpha1.DebugLog cntr.Spec.ResourceLabels = map[string]string{ - "key": "value", + "key": "value", + "key1": "value1", + } + cntr.Spec.ContourPodAnnotations = map[string]string{ + "key": "value", + "prometheus.io/scrape": "false", + } + cntr.Spec.ContourPodLabels = map[string]string{ + "key1": "overwritten", } - cntr.Spec.ContourPodAnnotations = annotations // Use non-default container ports to test that --envoy-service-http(s)-port // flags are added. @@ -184,6 +196,7 @@ func TestDesiredDeployment(t *testing.T) { checkDeploymentHasEnvVar(t, deploy, contourNsEnvVar) checkDeploymentHasEnvVar(t, deploy, contourPodEnvVar) checkDeploymentHasLabels(t, deploy, cntr.AppLabels()) + checkPodHasLabels(t, &deploy.Spec.Template, contourPodLabels(cntr)) checkPodHasAnnotations(t, &deploy.Spec.Template, contourPodAnnotations(cntr)) for _, port := range cntr.Spec.NetworkPublishing.Envoy.Ports { diff --git a/site/content/docs/main/config/api-reference.html b/site/content/docs/main/config/api-reference.html index b988b1ac283..f49ea2bf5c2 100644 --- a/site/content/docs/main/config/api-reference.html +++ b/site/content/docs/main/config/api-reference.html @@ -6160,6 +6160,21 @@

ContourSettings the annotations for Prometheus will be appended or overwritten with predefined value.

+ + +podLabels +
+ +map[string]string + + + +(Optional) +

PodLabels defines labels to add to the Contour pods. +If there is a label with the same key as in ContourDeploymentSpec.ResourceLabels, +the one here has a higher priority.

+ +

CustomTag @@ -7031,6 +7046,21 @@

EnvoySettings +podLabels +
+ +map[string]string + + + +(Optional) +

PodLabels defines labels to add to the Envoy pods. +If there is a label with the same key as in ContourDeploymentSpec.ResourceLabels, +the one here has a higher priority.

+ + + + resources