Skip to content

Commit

Permalink
crd/ContourDeployment: Add field 'podLabels' for contour (#2)
Browse files Browse the repository at this point in the history
* add pod labels field to contourDeployment
  • Loading branch information
izturn authored and yangyy93 committed Oct 19, 2023
1 parent dfb9aef commit 9d708a5
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 9 deletions.
12 changes: 12 additions & 0 deletions apis/projectcontour/v1alpha1/contourdeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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/
Expand Down
14 changes: 14 additions & 0 deletions apis/projectcontour/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions changelogs/unreleased/5543-izturn-small.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add Kubernetes labels configurability to ContourDeployment resource. to enable customize pod labels for pod/contour & pod/envoy
14 changes: 14 additions & 0 deletions examples/contour/01-crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions examples/render/contour-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions examples/render/contour-gateway-provisioner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions examples/render/contour-gateway.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions examples/render/contour.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions internal/provisioner/controller/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 != "" {
Expand Down
8 changes: 8 additions & 0 deletions internal/provisioner/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
},
}
}
Expand Down Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions internal/provisioner/model/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion internal/provisioner/objects/dataplane/dataplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
14 changes: 14 additions & 0 deletions internal/provisioner/objects/dataplane/dataplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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",
}
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions internal/provisioner/objects/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions internal/provisioner/objects/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 9d708a5

Please sign in to comment.