diff --git a/apis/projectcontour/v1alpha1/contourdeployment.go b/apis/projectcontour/v1alpha1/contourdeployment.go index 42853998249..902644f95ba 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 a0180e31da0..bf6899d909f 100644 --- a/apis/projectcontour/v1alpha1/zz_generated.deepcopy.go +++ b/apis/projectcontour/v1alpha1/zz_generated.deepcopy.go @@ -373,6 +373,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. @@ -644,6 +651,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 800eb9b8c7a..3bdd2bd47aa 100644 --- a/examples/contour/01-crds.yaml +++ b/examples/contour/01-crds.yaml @@ -1341,6 +1341,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 @@ -3329,6 +3336,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 37f7453bc66..71d3e7b5f0c 100644 --- a/examples/render/contour-deployment.yaml +++ b/examples/render/contour-deployment.yaml @@ -1554,6 +1554,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 @@ -3542,6 +3549,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 6d040d03942..38d1525f092 100644 --- a/examples/render/contour-gateway-provisioner.yaml +++ b/examples/render/contour-gateway-provisioner.yaml @@ -1355,6 +1355,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 @@ -3343,6 +3350,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 49c8eb3dafe..05235553bc2 100644 --- a/examples/render/contour-gateway.yaml +++ b/examples/render/contour-gateway.yaml @@ -1560,6 +1560,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 @@ -3548,6 +3555,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 3aa4fcaaf3c..c16b42ca84e 100644 --- a/examples/render/contour.yaml +++ b/examples/render/contour.yaml @@ -1554,6 +1554,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 @@ -3542,6 +3549,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 d7f487520ea..319596174bc 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 4faab9645e5..ebad780bb57 100644 --- a/internal/provisioner/model/model.go +++ b/internal/provisioner/model/model.go @@ -71,6 +71,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{}, }, } } @@ -215,6 +217,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 f558756636f..e0d8dd95041 100644 --- a/internal/provisioner/objects/dataplane/dataplane.go +++ b/internal/provisioner/objects/dataplane/dataplane.go @@ -524,9 +524,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 5fd13f0ee0d..adbe7154fde 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", } @@ -337,6 +350,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 6020824b04b..e5d2952bd03 100644 --- a/site/content/docs/main/config/api-reference.html +++ b/site/content/docs/main/config/api-reference.html @@ -6104,6 +6104,21 @@
podLabels
+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.
podLabels
+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