File size: 10,834 Bytes
36b3cb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
diff --git a/apis/datadoghq/v2alpha1/datadogagent_update_strategy_schema_test.go b/apis/datadoghq/v2alpha1/datadogagent_update_strategy_schema_test.go
new file mode 100644
index 00000000..482aa990
--- /dev/null
+++ b/apis/datadoghq/v2alpha1/datadogagent_update_strategy_schema_test.go
@@ -0,0 +1,115 @@
+package v2alpha1
+
+import (
+	"os"
+	"path/filepath"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+	"sigs.k8s.io/yaml"
+)
+
+func TestDatadogAgentOverrideUpdateStrategyIsExposedInCRDSchema(t *testing.T) {
+
+	crdPath := filepath.Join("..", "..", "..", "config", "crd", "bases", "v1", "datadoghq.com_datadogagents.yaml")
+	raw, err := os.ReadFile(crdPath)
+	require.NoError(t, err)
+
+	var crd map[string]interface{}
+	require.NoError(t, yaml.Unmarshal(raw, &crd))
+
+	var updateStrategySchema map[string]interface{}
+	for _, candidate := range findObjectProperties(crd, "updateStrategy") {
+		candidateProperties := properties(candidate)
+		rollingUpdate, hasRollingUpdate := candidateProperties["rollingUpdate"]
+		rollingProperties := properties(rollingUpdate)
+		if _, hasType := candidateProperties["type"]; hasType && hasRollingUpdate && rollingProperties["maxUnavailable"] != nil && rollingProperties["maxSurge"] != nil {
+			updateStrategySchema = candidate
+			break
+		}
+	}
+	require.NotNil(t, updateStrategySchema, "component override schema should expose updateStrategy with rollingUpdate limits")
+
+	strategyType, ok := properties(updateStrategySchema)["type"]
+	require.True(t, ok, "updateStrategy should expose a strategy type")
+	assert.Equal(t, "string", fieldString(strategyType, "type"))
+
+	rollingUpdate, ok := properties(updateStrategySchema)["rollingUpdate"]
+	require.True(t, ok, "updateStrategy should expose rollingUpdate limits")
+
+	rollingProperties := properties(rollingUpdate)
+	assertIntOrStringSchema(t, rollingProperties["maxUnavailable"], "maxUnavailable")
+	assertIntOrStringSchema(t, rollingProperties["maxSurge"], "maxSurge")
+}
+
+func findObjectProperties(node interface{}, property string) []map[string]interface{} {
+	switch value := node.(type) {
+	case map[string]interface{}:
+		var matches []map[string]interface{}
+		if props := properties(value); props != nil {
+			if found, ok := props[property].(map[string]interface{}); ok {
+				matches = append(matches, found)
+			}
+		}
+		for _, child := range value {
+			matches = append(matches, findObjectProperties(child, property)...)
+		}
+		return matches
+	case []interface{}:
+		var matches []map[string]interface{}
+		for _, child := range value {
+			matches = append(matches, findObjectProperties(child, property)...)
+		}
+		return matches
+	default:
+		return nil
+	}
+}
+
+func properties(schema interface{}) map[string]interface{} {
+	value, ok := schema.(map[string]interface{})
+	if !ok {
+		return nil
+	}
+	props, ok := value["properties"].(map[string]interface{})
+	if !ok {
+		return nil
+	}
+	return props
+}
+
+func assertIntOrStringSchema(t *testing.T, schema interface{}, field string) {
+	t.Helper()
+
+	value := requireMap(t, schema, field)
+
+	assert.Equal(t, true, value["x-kubernetes-int-or-string"], "%s should preserve Kubernetes IntOrString schema semantics", field)
+
+	branches, ok := value["anyOf"].([]interface{})
+	require.True(t, ok, "%s should allow integer and string branches", field)
+
+	seen := map[string]bool{}
+	for _, branch := range branches {
+		seen[fieldString(branch, "type")] = true
+	}
+	assert.True(t, seen["integer"], "%s should allow integer values", field)
+	assert.True(t, seen["string"], "%s should allow string values", field)
+}
+
+func requireMap(t *testing.T, value interface{}, name string) map[string]interface{} {
+	t.Helper()
+
+	result, ok := value.(map[string]interface{})
+	require.True(t, ok, "%s should be an object schema", name)
+	return result
+}
+
+func fieldString(value interface{}, field string) string {
+	object, ok := value.(map[string]interface{})
+	if !ok {
+		return ""
+	}
+	text, _ := object[field].(string)
+	return text
+}
diff --git a/controllers/datadogagent/override/daemonset_test.go b/controllers/datadogagent/override/daemonset_test.go
new file mode 100644
index 00000000..22511049
--- /dev/null
+++ b/controllers/datadogagent/override/daemonset_test.go
@@ -0,0 +1,96 @@
+package override
+
+import (
+	"encoding/json"
+	"testing"
+
+	"github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+	appsv1 "k8s.io/api/apps/v1"
+	"k8s.io/apimachinery/pkg/util/intstr"
+)
+
+func TestDaemonSetAppliesUpdateStrategyFromJSONOverride(t *testing.T) {
+	tests := []struct {
+		name                string
+		overrideJSON        string
+		expectedType        appsv1.DaemonSetUpdateStrategyType
+		expectedUnavailable *intstr.IntOrString
+		expectedSurge       *intstr.IntOrString
+	}{
+		{
+			name: "rolling update keeps percentage limits",
+			overrideJSON: `{
+				"updateStrategy": {
+					"type": "RollingUpdate",
+					"rollingUpdate": {
+						"maxUnavailable": "30%",
+						"maxSurge": "20%"
+					}
+				}
+			}`,
+			expectedType:        appsv1.RollingUpdateDaemonSetStrategyType,
+			expectedUnavailable: intOrStringPtr(intstr.FromString("30%")),
+			expectedSurge:       intOrStringPtr(intstr.FromString("20%")),
+		},
+		{
+			name: "rolling update keeps numeric limits",
+			overrideJSON: `{
+				"updateStrategy": {
+					"type": "RollingUpdate",
+					"rollingUpdate": {
+						"maxUnavailable": 3,
+						"maxSurge": 2
+					}
+				}
+			}`,
+			expectedType:        appsv1.RollingUpdateDaemonSetStrategyType,
+			expectedUnavailable: intOrStringPtr(intstr.FromInt(3)),
+			expectedSurge:       intOrStringPtr(intstr.FromInt(2)),
+		},
+		{
+			name: "non rolling strategy keeps the requested type",
+			overrideJSON: `{
+				"updateStrategy": {
+					"type": "OnDelete"
+				}
+			}`,
+			expectedType: appsv1.OnDeleteDaemonSetStrategyType,
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			daemonSet := appsv1.DaemonSet{}
+			override := componentOverrideFromJSON(t, tt.overrideJSON)
+
+			DaemonSet(&daemonSet, &override)
+
+			assert.Equal(t, tt.expectedType, daemonSet.Spec.UpdateStrategy.Type)
+			if tt.expectedUnavailable != nil || tt.expectedSurge != nil {
+				require.NotNil(t, daemonSet.Spec.UpdateStrategy.RollingUpdate)
+			}
+			if tt.expectedUnavailable != nil {
+				require.NotNil(t, daemonSet.Spec.UpdateStrategy.RollingUpdate.MaxUnavailable)
+				assert.Equal(t, *tt.expectedUnavailable, *daemonSet.Spec.UpdateStrategy.RollingUpdate.MaxUnavailable)
+			}
+			if tt.expectedSurge != nil {
+				require.NotNil(t, daemonSet.Spec.UpdateStrategy.RollingUpdate.MaxSurge)
+				assert.Equal(t, *tt.expectedSurge, *daemonSet.Spec.UpdateStrategy.RollingUpdate.MaxSurge)
+			}
+		})
+	}
+}
+
+func componentOverrideFromJSON(t *testing.T, payload string) v2alpha1.DatadogAgentComponentOverride {
+	t.Helper()
+
+	var override v2alpha1.DatadogAgentComponentOverride
+	require.NoError(t, json.Unmarshal([]byte(payload), &override))
+	return override
+}
+
+func intOrStringPtr(value intstr.IntOrString) *intstr.IntOrString {
+	return &value
+}
diff --git a/controllers/datadogagent/override/deploymentspec_test.go b/controllers/datadogagent/override/deploymentspec_test.go
index 2d6a165a..5f4218a3 100644
--- a/controllers/datadogagent/override/deploymentspec_test.go
+++ b/controllers/datadogagent/override/deploymentspec_test.go
@@ -11,10 +11,84 @@ import (
 	"github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
 	apiutils "github.com/DataDog/datadog-operator/apis/utils"
 	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
 	v1 "k8s.io/api/apps/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/apimachinery/pkg/util/intstr"
 )
 
+func TestDeploymentAppliesUpdateStrategyFromJSONOverride(t *testing.T) {
+	tests := []struct {
+		name                string
+		overrideJSON        string
+		expectedType        v1.DeploymentStrategyType
+		expectedUnavailable *intstr.IntOrString
+		expectedSurge       *intstr.IntOrString
+	}{
+		{
+			name: "rolling update keeps percentage limits",
+			overrideJSON: `{
+				"updateStrategy": {
+					"type": "RollingUpdate",
+					"rollingUpdate": {
+						"maxUnavailable": "25%",
+						"maxSurge": "15%"
+					}
+				}
+			}`,
+			expectedType:        v1.RollingUpdateDeploymentStrategyType,
+			expectedUnavailable: intOrStringPtr(intstr.FromString("25%")),
+			expectedSurge:       intOrStringPtr(intstr.FromString("15%")),
+		},
+		{
+			name: "rolling update keeps numeric limits",
+			overrideJSON: `{
+				"updateStrategy": {
+					"type": "RollingUpdate",
+					"rollingUpdate": {
+						"maxUnavailable": 1,
+						"maxSurge": 4
+					}
+				}
+			}`,
+			expectedType:        v1.RollingUpdateDeploymentStrategyType,
+			expectedUnavailable: intOrStringPtr(intstr.FromInt(1)),
+			expectedSurge:       intOrStringPtr(intstr.FromInt(4)),
+		},
+		{
+			name: "non rolling strategy keeps the requested type",
+			overrideJSON: `{
+				"updateStrategy": {
+					"type": "Recreate"
+				}
+			}`,
+			expectedType: v1.RecreateDeploymentStrategyType,
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			deployment := v1.Deployment{}
+			override := componentOverrideFromJSON(t, tt.overrideJSON)
+
+			Deployment(&deployment, &override)
+
+			assert.Equal(t, tt.expectedType, deployment.Spec.Strategy.Type)
+			if tt.expectedUnavailable != nil || tt.expectedSurge != nil {
+				require.NotNil(t, deployment.Spec.Strategy.RollingUpdate)
+			}
+			if tt.expectedUnavailable != nil {
+				require.NotNil(t, deployment.Spec.Strategy.RollingUpdate.MaxUnavailable)
+				assert.Equal(t, *tt.expectedUnavailable, *deployment.Spec.Strategy.RollingUpdate.MaxUnavailable)
+			}
+			if tt.expectedSurge != nil {
+				require.NotNil(t, deployment.Spec.Strategy.RollingUpdate.MaxSurge)
+				assert.Equal(t, *tt.expectedSurge, *deployment.Spec.Strategy.RollingUpdate.MaxSurge)
+			}
+		})
+	}
+}
+
 func TestDeployment(t *testing.T) {
 	deployment := v1.Deployment{
 		ObjectMeta: metav1.ObjectMeta{
diff --git a/mimo_test_command.sh b/mimo_test_command.sh
new file mode 100755
--- /dev/null
+++ b/mimo_test_command.sh
@@ -0,0 +1,19 @@
+#!/bin/bash
+set -euo pipefail
+cd /testbed
+
+
+
+case "${1:-}" in
+  base)
+    go test ./controllers/datadogagent/override/ -run '^TestDeployment$' -v
+    ;;
+  new)
+    go test ./apis/datadoghq/v2alpha1 -run '^TestDatadogAgentOverrideUpdateStrategyIsExposedInCRDSchema$' -v
+    go test ./controllers/datadogagent/override/ -run '^(TestDaemonSetAppliesUpdateStrategyFromJSONOverride|TestDeploymentAppliesUpdateStrategyFromJSONOverride)$' -v
+    ;;
+  *)
+    echo "usage: $0 base|new"
+    exit 2
+    ;;
+esac