|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2025-present Datadog, Inc. |
| 5 | + |
| 6 | +//go:build kubeapiserver && test |
| 7 | + |
| 8 | +package nginx |
| 9 | + |
| 10 | +import ( |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + |
| 16 | + logmock "github.com/DataDog/datadog-agent/comp/core/log/mock" |
| 17 | + appsecconfig "github.com/DataDog/datadog-agent/pkg/clusteragent/appsec/config" |
| 18 | + |
| 19 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 20 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 21 | + "k8s.io/apimachinery/pkg/runtime" |
| 22 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 23 | + dynamicfake "k8s.io/client-go/dynamic/fake" |
| 24 | + "k8s.io/client-go/tools/record" |
| 25 | + "k8s.io/client-go/util/workqueue" |
| 26 | +) |
| 27 | + |
| 28 | +func newTestConfigMapReconciler(t *testing.T, objects ...runtime.Object) (*configMapReconciler, *dynamicfake.FakeDynamicClient) { |
| 29 | + t.Helper() |
| 30 | + |
| 31 | + logger := logmock.New(t) |
| 32 | + scheme := runtime.NewScheme() |
| 33 | + client := dynamicfake.NewSimpleDynamicClientWithCustomListKinds(scheme, |
| 34 | + map[schema.GroupVersionResource]string{ |
| 35 | + configMapGVR: "ConfigMapList", |
| 36 | + ingressClassGVR: "IngressClassList", |
| 37 | + }, |
| 38 | + objects..., |
| 39 | + ) |
| 40 | + |
| 41 | + config := appsecconfig.Config{ |
| 42 | + Product: appsecconfig.Product{ |
| 43 | + Nginx: appsecconfig.Nginx{ |
| 44 | + ModuleMountPath: "/modules_mount", |
| 45 | + }, |
| 46 | + }, |
| 47 | + Injection: appsecconfig.Injection{ |
| 48 | + CommonLabels: map[string]string{ |
| 49 | + "app.kubernetes.io/part-of": "datadog", |
| 50 | + "app.kubernetes.io/component": "datadog-appsec-injector", |
| 51 | + "app.kubernetes.io/managed-by": "datadog-cluster-agent", |
| 52 | + }, |
| 53 | + CommonAnnotations: map[string]string{ |
| 54 | + "annotation": "value", |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + return &configMapReconciler{ |
| 60 | + client: client, |
| 61 | + logger: logger, |
| 62 | + config: config, |
| 63 | + eventRecorder: eventRecorder{ |
| 64 | + recorder: record.NewFakeRecorder(100), |
| 65 | + }, |
| 66 | + }, client |
| 67 | +} |
| 68 | + |
| 69 | +func TestReconcilerReconcile_Success(t *testing.T) { |
| 70 | + ctx := t.Context() |
| 71 | + originalName := "ingress-nginx-controller" |
| 72 | + ddName := ddConfigMapName(originalName) |
| 73 | + originalCM := &unstructured.Unstructured{ |
| 74 | + Object: map[string]interface{}{ |
| 75 | + "apiVersion": "v1", |
| 76 | + "kind": "ConfigMap", |
| 77 | + "metadata": map[string]interface{}{ |
| 78 | + "name": originalName, |
| 79 | + "namespace": "ingress-nginx", |
| 80 | + "uid": "original-uid", |
| 81 | + "labels": map[string]interface{}{ |
| 82 | + watchedConfigMapLabel: "true", |
| 83 | + }, |
| 84 | + "annotations": map[string]interface{}{ |
| 85 | + ddConfigMapAnnotation: ddName, |
| 86 | + }, |
| 87 | + }, |
| 88 | + "data": map[string]interface{}{ |
| 89 | + mainSnippetKey: "worker_connections 4096;", |
| 90 | + httpSnippetKey: "keepalive_timeout 75;", |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | + existingDDCM := &unstructured.Unstructured{ |
| 95 | + Object: map[string]interface{}{ |
| 96 | + "apiVersion": "v1", |
| 97 | + "kind": "ConfigMap", |
| 98 | + "metadata": map[string]interface{}{ |
| 99 | + "name": ddName, |
| 100 | + "namespace": "ingress-nginx", |
| 101 | + "resourceVersion": "1", |
| 102 | + }, |
| 103 | + "data": map[string]interface{}{ |
| 104 | + mainSnippetKey: "stale-main-snippet", |
| 105 | + }, |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + r, client := newTestConfigMapReconciler(t, originalCM, existingDDCM) |
| 110 | + queue := workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedItemBasedRateLimiter[reconcileItem]()) |
| 111 | + defer queue.ShutDown() |
| 112 | + |
| 113 | + item := reconcileItem{namespace: "ingress-nginx", originalName: originalName} |
| 114 | + queue.Add(item) |
| 115 | + received, quit := queue.Get() |
| 116 | + require.False(t, quit) |
| 117 | + require.Equal(t, item, received) |
| 118 | + |
| 119 | + r.reconcile(ctx, queue, received) |
| 120 | + assert.Equal(t, 0, queue.NumRequeues(item)) |
| 121 | + |
| 122 | + ddCM, err := client.Resource(configMapGVR).Namespace("ingress-nginx").Get(ctx, ddName, metav1.GetOptions{}) |
| 123 | + require.NoError(t, err) |
| 124 | + assert.Equal(t, "value", ddCM.GetAnnotations()["annotation"]) |
| 125 | + assert.Equal(t, "datadog", ddCM.GetLabels()["app.kubernetes.io/part-of"]) |
| 126 | + assert.Equal(t, string(appsecconfig.ProxyTypeIngressNginx), ddCM.GetLabels()[appsecconfig.AppsecProcessorProxyTypeAnnotation]) |
| 127 | + data, found, err := unstructured.NestedStringMap(ddCM.UnstructuredContent(), "data") |
| 128 | + require.NoError(t, err) |
| 129 | + require.True(t, found) |
| 130 | + assert.Contains(t, data[mainSnippetKey], "load_module /modules_mount/ngx_http_datadog_module.so;") |
| 131 | + assert.Contains(t, data[mainSnippetKey], "worker_connections 4096;") |
| 132 | + assert.Contains(t, data[httpSnippetKey], "datadog_appsec_enabled on;") |
| 133 | + assert.Contains(t, data[httpSnippetKey], "keepalive_timeout 75;") |
| 134 | +} |
| 135 | + |
| 136 | +func TestReconcilerReconcile_MissingAnnotation(t *testing.T) { |
| 137 | + ctx := t.Context() |
| 138 | + originalName := "ingress-nginx-controller" |
| 139 | + originalCM := &unstructured.Unstructured{ |
| 140 | + Object: map[string]interface{}{ |
| 141 | + "apiVersion": "v1", |
| 142 | + "kind": "ConfigMap", |
| 143 | + "metadata": map[string]interface{}{ |
| 144 | + "name": originalName, |
| 145 | + "namespace": "ingress-nginx", |
| 146 | + "labels": map[string]interface{}{ |
| 147 | + watchedConfigMapLabel: "true", |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + r, client := newTestConfigMapReconciler(t, originalCM) |
| 154 | + queue := workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedItemBasedRateLimiter[reconcileItem]()) |
| 155 | + defer queue.ShutDown() |
| 156 | + |
| 157 | + item := reconcileItem{namespace: "ingress-nginx", originalName: originalName} |
| 158 | + queue.Add(item) |
| 159 | + received, quit := queue.Get() |
| 160 | + require.False(t, quit) |
| 161 | + |
| 162 | + r.reconcile(ctx, queue, received) |
| 163 | + assert.Equal(t, 0, queue.NumRequeues(item)) |
| 164 | + |
| 165 | + _, err := client.Resource(configMapGVR).Namespace("ingress-nginx").Get(ctx, ddConfigMapName(originalName), metav1.GetOptions{}) |
| 166 | + assert.Error(t, err) |
| 167 | +} |
| 168 | + |
| 169 | +func TestReconcilerReconcile_AnnotationMismatch(t *testing.T) { |
| 170 | + ctx := t.Context() |
| 171 | + originalName := "ingress-nginx-controller" |
| 172 | + mismatchedDDName := ddConfigMapName("different-original") |
| 173 | + originalCM := &unstructured.Unstructured{ |
| 174 | + Object: map[string]interface{}{ |
| 175 | + "apiVersion": "v1", |
| 176 | + "kind": "ConfigMap", |
| 177 | + "metadata": map[string]interface{}{ |
| 178 | + "name": originalName, |
| 179 | + "namespace": "ingress-nginx", |
| 180 | + "labels": map[string]interface{}{ |
| 181 | + watchedConfigMapLabel: "true", |
| 182 | + }, |
| 183 | + "annotations": map[string]interface{}{ |
| 184 | + ddConfigMapAnnotation: mismatchedDDName, |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + } |
| 189 | + existingDDCM := &unstructured.Unstructured{ |
| 190 | + Object: map[string]interface{}{ |
| 191 | + "apiVersion": "v1", |
| 192 | + "kind": "ConfigMap", |
| 193 | + "metadata": map[string]interface{}{ |
| 194 | + "name": mismatchedDDName, |
| 195 | + "namespace": "ingress-nginx", |
| 196 | + "resourceVersion": "1", |
| 197 | + }, |
| 198 | + "data": map[string]interface{}{ |
| 199 | + mainSnippetKey: "unchanged", |
| 200 | + }, |
| 201 | + }, |
| 202 | + } |
| 203 | + |
| 204 | + r, client := newTestConfigMapReconciler(t, originalCM, existingDDCM) |
| 205 | + queue := workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedItemBasedRateLimiter[reconcileItem]()) |
| 206 | + defer queue.ShutDown() |
| 207 | + |
| 208 | + item := reconcileItem{namespace: "ingress-nginx", originalName: originalName} |
| 209 | + queue.Add(item) |
| 210 | + received, quit := queue.Get() |
| 211 | + require.False(t, quit) |
| 212 | + |
| 213 | + r.reconcile(ctx, queue, received) |
| 214 | + assert.Equal(t, 0, queue.NumRequeues(item)) |
| 215 | + |
| 216 | + ddCM, err := client.Resource(configMapGVR).Namespace("ingress-nginx").Get(ctx, mismatchedDDName, metav1.GetOptions{}) |
| 217 | + require.NoError(t, err) |
| 218 | + data, found, err := unstructured.NestedStringMap(ddCM.UnstructuredContent(), "data") |
| 219 | + require.NoError(t, err) |
| 220 | + require.True(t, found) |
| 221 | + assert.Equal(t, "unchanged", data[mainSnippetKey]) |
| 222 | +} |
0 commit comments