Skip to content

Commit f9cfd7b

Browse files
feat: PAAL-123 move probe equality check to equality.go and reduce complexity
1 parent 83e6a0b commit f9cfd7b

File tree

3 files changed

+156
-34
lines changed

3 files changed

+156
-34
lines changed

internal/controller/agent_controller.go

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -767,40 +767,7 @@ func (r *AgentReconciler) servicePortsEqual(existing, desired []corev1.ServicePo
767767

768768
// probesEqual compares two readiness probes for equality
769769
func (r *AgentReconciler) probesEqual(existing, desired *corev1.Probe) bool {
770-
// Both nil - equal
771-
if existing == nil && desired == nil {
772-
return true
773-
}
774-
775-
// One nil, one not - not equal
776-
if existing == nil || desired == nil {
777-
return false
778-
}
779-
780-
// Compare probe settings
781-
if existing.InitialDelaySeconds != desired.InitialDelaySeconds ||
782-
existing.PeriodSeconds != desired.PeriodSeconds ||
783-
existing.TimeoutSeconds != desired.TimeoutSeconds ||
784-
existing.SuccessThreshold != desired.SuccessThreshold ||
785-
existing.FailureThreshold != desired.FailureThreshold {
786-
return false
787-
}
788-
789-
// Compare HTTP Get actions
790-
if existing.HTTPGet != nil && desired.HTTPGet != nil {
791-
return existing.HTTPGet.Path == desired.HTTPGet.Path &&
792-
existing.HTTPGet.Port == desired.HTTPGet.Port &&
793-
existing.HTTPGet.Scheme == desired.HTTPGet.Scheme
794-
}
795-
796-
// Compare TCP Socket actions
797-
if existing.TCPSocket != nil && desired.TCPSocket != nil {
798-
return existing.TCPSocket.Port == desired.TCPSocket.Port
799-
}
800-
801-
// Different probe handler types or one nil - not equal
802-
return (existing.HTTPGet == nil && desired.HTTPGet == nil) &&
803-
(existing.TCPSocket == nil && desired.TCPSocket == nil)
770+
return equality.ProbesEqual(existing, desired)
804771
}
805772

806773
// sanitizeAgentName sanitizes the agent name to meet environment variable naming requirements.

internal/equality/equality.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@ func getEnvFromSourceKey(s corev1.EnvFromSource) string {
3636
}
3737
return ""
3838
}
39+
40+
// ProbesEqual compares two Kubernetes readiness probes for equality.
41+
// This function handles nil cases and uses deep comparison for comprehensive equality checking.
42+
func ProbesEqual(existing, desired *corev1.Probe) bool {
43+
return cmp.Equal(existing, desired)
44+
}

internal/equality/equality_test.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/agentic-layer/agent-runtime-operator/internal/equality"
99
corev1 "k8s.io/api/core/v1"
10+
"k8s.io/apimachinery/pkg/util/intstr"
1011
)
1112

1213
// A helper function to easily get a pointer to a bool, as required by the 'Optional' field.
@@ -220,3 +221,151 @@ func TestEnvFromEqual(t *testing.T) {
220221
})
221222
}
222223
}
224+
225+
func TestProbesEqual(t *testing.T) {
226+
testCases := []struct {
227+
name string
228+
a *corev1.Probe
229+
b *corev1.Probe
230+
want bool
231+
}{
232+
{
233+
name: "should be equal for both nil",
234+
a: nil,
235+
b: nil,
236+
want: true,
237+
},
238+
{
239+
name: "should not be equal for one nil",
240+
a: nil,
241+
b: &corev1.Probe{InitialDelaySeconds: 10},
242+
want: false,
243+
},
244+
{
245+
name: "should not be equal for other nil",
246+
a: &corev1.Probe{InitialDelaySeconds: 10},
247+
b: nil,
248+
want: false,
249+
},
250+
{
251+
name: "should be equal for identical HTTP probes",
252+
a: &corev1.Probe{
253+
ProbeHandler: corev1.ProbeHandler{
254+
HTTPGet: &corev1.HTTPGetAction{
255+
Path: "/health",
256+
Port: intstr.FromInt(8000),
257+
},
258+
},
259+
InitialDelaySeconds: 10,
260+
PeriodSeconds: 5,
261+
},
262+
b: &corev1.Probe{
263+
ProbeHandler: corev1.ProbeHandler{
264+
HTTPGet: &corev1.HTTPGetAction{
265+
Path: "/health",
266+
Port: intstr.FromInt(8000),
267+
},
268+
},
269+
InitialDelaySeconds: 10,
270+
PeriodSeconds: 5,
271+
},
272+
want: true,
273+
},
274+
{
275+
name: "should be equal for identical TCP probes",
276+
a: &corev1.Probe{
277+
ProbeHandler: corev1.ProbeHandler{
278+
TCPSocket: &corev1.TCPSocketAction{
279+
Port: intstr.FromInt(8000),
280+
},
281+
},
282+
InitialDelaySeconds: 10,
283+
},
284+
b: &corev1.Probe{
285+
ProbeHandler: corev1.ProbeHandler{
286+
TCPSocket: &corev1.TCPSocketAction{
287+
Port: intstr.FromInt(8000),
288+
},
289+
},
290+
InitialDelaySeconds: 10,
291+
},
292+
want: true,
293+
},
294+
{
295+
name: "should not be equal for different HTTP paths",
296+
a: &corev1.Probe{
297+
ProbeHandler: corev1.ProbeHandler{
298+
HTTPGet: &corev1.HTTPGetAction{
299+
Path: "/health",
300+
Port: intstr.FromInt(8000),
301+
},
302+
},
303+
},
304+
b: &corev1.Probe{
305+
ProbeHandler: corev1.ProbeHandler{
306+
HTTPGet: &corev1.HTTPGetAction{
307+
Path: "/readiness",
308+
Port: intstr.FromInt(8000),
309+
},
310+
},
311+
},
312+
want: false,
313+
},
314+
{
315+
name: "should not be equal for different ports",
316+
a: &corev1.Probe{
317+
ProbeHandler: corev1.ProbeHandler{
318+
TCPSocket: &corev1.TCPSocketAction{
319+
Port: intstr.FromInt(8000),
320+
},
321+
},
322+
},
323+
b: &corev1.Probe{
324+
ProbeHandler: corev1.ProbeHandler{
325+
TCPSocket: &corev1.TCPSocketAction{
326+
Port: intstr.FromInt(3000),
327+
},
328+
},
329+
},
330+
want: false,
331+
},
332+
{
333+
name: "should not be equal for different probe types",
334+
a: &corev1.Probe{
335+
ProbeHandler: corev1.ProbeHandler{
336+
HTTPGet: &corev1.HTTPGetAction{
337+
Path: "/health",
338+
Port: intstr.FromInt(8000),
339+
},
340+
},
341+
},
342+
b: &corev1.Probe{
343+
ProbeHandler: corev1.ProbeHandler{
344+
TCPSocket: &corev1.TCPSocketAction{
345+
Port: intstr.FromInt(8000),
346+
},
347+
},
348+
},
349+
want: false,
350+
},
351+
{
352+
name: "should not be equal for different timing settings",
353+
a: &corev1.Probe{
354+
InitialDelaySeconds: 10,
355+
},
356+
b: &corev1.Probe{
357+
InitialDelaySeconds: 15,
358+
},
359+
want: false,
360+
},
361+
}
362+
363+
for _, tc := range testCases {
364+
t.Run(tc.name, func(t *testing.T) {
365+
got := equality.ProbesEqual(tc.a, tc.b)
366+
if got != tc.want {
367+
t.Errorf("ProbesEqual() = %v, want %v", got, tc.want)
368+
}
369+
})
370+
}
371+
}

0 commit comments

Comments
 (0)