Skip to content

Commit

Permalink
set pod annotations for contour
Browse files Browse the repository at this point in the history
Signed-off-by: Gang Liu <[email protected]>
  • Loading branch information
izturn committed May 4, 2023
1 parent 6cdbb32 commit 4176e6c
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 7 deletions.
4 changes: 4 additions & 0 deletions apis/projectcontour/v1alpha1/contourdeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ type ContourSettings struct {
// Deployment describes the settings for running contour as a `Deployment`.
// +optional
Deployment *DeploymentSettings `json:"deployment,omitempty"`

// PodAnnotations defines annotations to add to the Contour pods.
// +optional
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
}

// DeploymentSettings contains settings for Deployment resources.
Expand Down
7 changes: 7 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.

6 changes: 6 additions & 0 deletions examples/contour/01-crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,12 @@ spec:
type: object
type: array
type: object
podAnnotations:
additionalProperties:
type: string
description: PodAnnotations defines annotations to add to the
Contour pods.
type: object
replicas:
description: "Deprecated: Use `DeploymentSettings.Replicas` instead.
\n Replicas is the desired number of Contour replicas. If if
Expand Down
6 changes: 6 additions & 0 deletions examples/render/contour-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,12 @@ spec:
type: object
type: array
type: object
podAnnotations:
additionalProperties:
type: string
description: PodAnnotations defines annotations to add to the
Contour pods.
type: object
replicas:
description: "Deprecated: Use `DeploymentSettings.Replicas` instead.
\n Replicas is the desired number of Contour replicas. If if
Expand Down
6 changes: 6 additions & 0 deletions examples/render/contour-gateway-provisioner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,12 @@ spec:
type: object
type: array
type: object
podAnnotations:
additionalProperties:
type: string
description: PodAnnotations defines annotations to add to the
Contour pods.
type: object
replicas:
description: "Deprecated: Use `DeploymentSettings.Replicas` instead.
\n Replicas is the desired number of Contour replicas. If if
Expand Down
6 changes: 6 additions & 0 deletions examples/render/contour-gateway.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,12 @@ spec:
type: object
type: array
type: object
podAnnotations:
additionalProperties:
type: string
description: PodAnnotations defines annotations to add to the
Contour pods.
type: object
replicas:
description: "Deprecated: Use `DeploymentSettings.Replicas` instead.
\n Replicas is the desired number of Contour replicas. If if
Expand Down
6 changes: 6 additions & 0 deletions examples/render/contour.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,12 @@ spec:
type: object
type: array
type: object
podAnnotations:
additionalProperties:
type: string
description: PodAnnotations defines annotations to add to the
Contour pods.
type: object
replicas:
description: "Deprecated: Use `DeploymentSettings.Replicas` instead.
\n Replicas is the desired number of Contour replicas. If if
Expand Down
4 changes: 4 additions & 0 deletions internal/provisioner/controller/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ func (r *gatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
contourParams.Deployment.Strategy != nil {
contourModel.Spec.ContourDeploymentStrategy = *contourParams.Deployment.Strategy
}

for k, v := range contourParams.PodAnnotations {
contourModel.Spec.ContourPodAnnotations[k] = v
}
}

if gatewayClassParams.Spec.Envoy != nil {
Expand Down
46 changes: 46 additions & 0 deletions internal/provisioner/controller/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,52 @@ func TestGatewayReconcile(t *testing.T) {
},
},

"If ContourDeployment.Spec.Contour.PodAnnotations is specified, the Contour pods' have annotations for prometheus & user-defined": {

gatewayClass: reconcilableGatewayClassWithParams("gatewayclass-1", controller),
gatewayClassParams: &contourv1alpha1.ContourDeployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: "projectcontour",
Name: "gatewayclass-1-params",
},
Spec: contourv1alpha1.ContourDeploymentSpec{
Contour: &contourv1alpha1.ContourSettings{
PodAnnotations: map[string]string{
"key": "val",
},
},
},
},
gateway: &gatewayv1beta1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Namespace: "gateway-1",
Name: "gateway-1",
},
Spec: gatewayv1beta1.GatewaySpec{
GatewayClassName: gatewayv1beta1.ObjectName("gatewayclass-1"),
},
},
assertions: func(t *testing.T, r *gatewayReconciler, gw *gatewayv1beta1.Gateway, reconcileErr error) {
require.NoError(t, reconcileErr)

// Verify the Gateway has a "Accepted: true" condition
require.NoError(t, r.client.Get(context.Background(), keyFor(gw), gw))
require.Len(t, gw.Status.Conditions, 1)
assert.Equal(t, string(gatewayv1beta1.GatewayConditionAccepted), gw.Status.Conditions[0].Type)
assert.Equal(t, metav1.ConditionTrue, gw.Status.Conditions[0].Status)

// Verify the deployment has been created
deploy := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: "gateway-1",
Name: "contour-gateway-1",
},
}
require.NoError(t, r.client.Get(context.Background(), keyFor(deploy), deploy))
assert.Contains(t, deploy.Spec.Template.ObjectMeta.Annotations, "key")
},
},

"If ContourDeployment.Spec.Envoy.WorkloadType is set to DaemonSet," +
"an Envoy daemonset is provisioned with the strategy that come from DaemonsetSettings": {
gatewayClass: reconcilableGatewayClassWithParams("gatewayclass-1", controller),
Expand Down
8 changes: 6 additions & 2 deletions internal/provisioner/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ func Default(namespace, name string) *Contour {
MaxUnavailable: ref.To(intstr.FromString("25%")),
},
},
ResourceLabels: map[string]string{},
EnvoyPodAnnotations: map[string]string{},
ResourceLabels: map[string]string{},
EnvoyPodAnnotations: map[string]string{},
ContourPodAnnotations: map[string]string{},
},
}
}
Expand Down Expand Up @@ -208,6 +209,9 @@ type ContourSpec struct {
// EnvoyPodAnnotations holds the annotations that will be add to the envoy‘s pod.
EnvoyPodAnnotations map[string]string

// ContourPodAnnotations holds the annotations that will be add to the contour's pod.
ContourPodAnnotations map[string]string

// Compute Resources required by envoy container.
EnvoyResources corev1.ResourceRequirements

Expand Down
36 changes: 31 additions & 5 deletions internal/provisioner/objects/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,8 @@ func DesiredDeployment(contour *model.Contour, image string) *appsv1.Deployment
ObjectMeta: metav1.ObjectMeta{
// TODO [danehans]: Remove the prometheus annotations when Contour is updated to
// show how the Prometheus Operator is used to scrape Contour/Envoy metrics.
Annotations: map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": fmt.Sprintf("%d", metricsPort),
},
Labels: contourPodLabels(contour),
Annotations: contourPodAnnotations(contour),
Labels: contourPodLabels(contour),
},
Spec: corev1.PodSpec{
// TODO [danehans]: Readdress anti-affinity when https://github.com/projectcontour/contour/issues/2997
Expand Down Expand Up @@ -301,3 +298,32 @@ func contourPodLabels(contour *model.Contour) map[string]string {
}
return labels
}

// contourPodAnnotations returns the annotations for contour's pods
func contourPodAnnotations(contour *model.Contour) map[string]string {
annotations := map[string]string{}
for k, v := range contour.Spec.ContourPodAnnotations {
annotations[k] = v
}

metricsPort := metricsPort
if contour.Spec.RuntimeSettings != nil &&
contour.Spec.RuntimeSettings.Metrics != nil &&
contour.Spec.RuntimeSettings.Metrics.Port > 0 {
metricsPort = contour.Spec.RuntimeSettings.Metrics.Port
}

annotations["prometheus.io/scrape"] = "true"
annotations["prometheus.io/port"] = fmt.Sprint(metricsPort)

return annotations
}

/*
Annotations: map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": fmt.Sprintf("%d", metricsPort),
},
*/

// Annotations: envoyPodAnnotations(contour),
17 changes: 17 additions & 0 deletions internal/provisioner/objects/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ func checkDeploymentHasLabels(t *testing.T, deploy *appsv1.Deployment, expected
t.Errorf("deployment has unexpected %q labels", deploy.Labels)
}

func checkPodHasAnnotations(t *testing.T, tmpl *corev1.PodTemplateSpec, annotations map[string]string) {
t.Helper()

for k, v := range annotations {
if val, ok := tmpl.Annotations[k]; !ok || val != v {
t.Errorf("pod template has unexpected %q annotations", tmpl.Annotations)
}
}

}

func checkContainerHasArg(t *testing.T, container *corev1.Container, arg string) {
t.Helper()

Expand Down Expand Up @@ -140,6 +151,10 @@ func TestDesiredDeployment(t *testing.T) {
corev1.ResourceMemory: resource.MustParse("25Mi"),
},
}

annotations := map[string]string{
"key": "value",
}
cntr.Spec.ContourResources = resQutoa

// Change the Kubernetes log level to test --kubernetes-debug.
Expand All @@ -151,6 +166,7 @@ func TestDesiredDeployment(t *testing.T) {
cntr.Spec.ResourceLabels = map[string]string{
"key": "value",
}
cntr.Spec.ContourPodAnnotations = annotations

// Use non-default container ports to test that --envoy-service-http(s)-port
// flags are added.
Expand All @@ -167,6 +183,7 @@ func TestDesiredDeployment(t *testing.T) {
checkDeploymentHasEnvVar(t, deploy, contourNsEnvVar)
checkDeploymentHasEnvVar(t, deploy, contourPodEnvVar)
checkDeploymentHasLabels(t, deploy, cntr.AppLabels())
checkPodHasAnnotations(t, &deploy.Spec.Template, annotations)

for _, port := range cntr.Spec.NetworkPublishing.Envoy.Ports {
switch port.Name {
Expand Down
13 changes: 13 additions & 0 deletions site/content/docs/main/config/api-reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -5938,6 +5938,19 @@ <h3 id="projectcontour.io/v1alpha1.ContourSettings">ContourSettings
<p>Deployment describes the settings for running contour as a <code>Deployment</code>.</p>
</td>
</tr>
<tr>
<td style="white-space:nowrap">
<code>podAnnotations</code>
<br>
<em>
map[string]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>PodAnnotations defines annotations to add to the Contour pods.</p>
</td>
</tr>
</tbody>
</table>
<h3 id="projectcontour.io/v1alpha1.CustomTag">CustomTag
Expand Down

0 comments on commit 4176e6c

Please sign in to comment.