Skip to content

Commit 2398ebb

Browse files
authored
feat: create defaulter func for trafficManagerProfile (#196)
* feat: create defaulter func for trafficManagerProfile * address comment
1 parent e3743e8 commit 2398ebb

File tree

3 files changed

+154
-1
lines changed

3 files changed

+154
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
k8s.io/apimachinery v0.30.2
1313
k8s.io/client-go v0.30.2
1414
k8s.io/klog/v2 v2.130.1
15+
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0
1516
sigs.k8s.io/controller-runtime v0.18.4
1617
)
1718

@@ -70,7 +71,6 @@ require (
7071
gopkg.in/yaml.v3 v3.0.1 // indirect
7172
k8s.io/apiextensions-apiserver v0.30.2 // indirect
7273
k8s.io/kube-openapi v0.0.0-20240620174524-b456828f718b // indirect
73-
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 // indirect
7474
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
7575
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
7676
sigs.k8s.io/yaml v1.4.0 // indirect
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright (c) Microsoft Corporation.
3+
Licensed under the MIT license.
4+
*/
5+
6+
// Package defaulter provides the utils for setting default values for a resource.
7+
package defaulter
8+
9+
import (
10+
"k8s.io/utils/ptr"
11+
12+
fleetnetv1alpha1 "go.goms.io/fleet-networking/api/v1alpha1"
13+
)
14+
15+
// SetDefaultsTrafficManagerProfile sets the default values for TrafficManagerProfile.
16+
func SetDefaultsTrafficManagerProfile(obj *fleetnetv1alpha1.TrafficManagerProfile) {
17+
if obj.Spec.MonitorConfig == nil {
18+
obj.Spec.MonitorConfig = &fleetnetv1alpha1.MonitorConfig{}
19+
}
20+
21+
if obj.Spec.MonitorConfig.IntervalInSeconds == nil {
22+
obj.Spec.MonitorConfig.IntervalInSeconds = ptr.To(int64(30))
23+
}
24+
25+
if obj.Spec.MonitorConfig.Path == nil {
26+
obj.Spec.MonitorConfig.Path = ptr.To("/")
27+
}
28+
29+
if obj.Spec.MonitorConfig.Port == nil {
30+
obj.Spec.MonitorConfig.Port = ptr.To(int64(80))
31+
}
32+
33+
if obj.Spec.MonitorConfig.Protocol == nil {
34+
obj.Spec.MonitorConfig.Protocol = ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTP)
35+
}
36+
37+
// TimeoutInSeconds value depends on the IntervalInSeconds, so that the defaulter MUST handle the IntervalInSeconds first.
38+
// * If the Probing Interval is set to 30 seconds, then you can set the Timeout value between 5 and 10 seconds.
39+
// If no value is specified, it uses a default value of 10 seconds.
40+
// * If the Probing Interval is set to 10 seconds, then you can set the Timeout value between 5 and 9 seconds.
41+
// If no Timeout value is specified, it uses a default value of 9 seconds.
42+
// Reference link: https://learn.microsoft.com/en-us/azure/traffic-manager/traffic-manager-monitoring#configure-endpoint-monitoring
43+
if obj.Spec.MonitorConfig.TimeoutInSeconds == nil {
44+
if *obj.Spec.MonitorConfig.IntervalInSeconds == 30 {
45+
obj.Spec.MonitorConfig.TimeoutInSeconds = ptr.To(int64(10))
46+
} else { // assuming validation wehbook should validate the intervalInSeconds first
47+
obj.Spec.MonitorConfig.TimeoutInSeconds = ptr.To(int64(9))
48+
}
49+
}
50+
51+
if obj.Spec.MonitorConfig.ToleratedNumberOfFailures == nil {
52+
obj.Spec.MonitorConfig.ToleratedNumberOfFailures = ptr.To(int64(3))
53+
}
54+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Copyright (c) Microsoft Corporation.
3+
Licensed under the MIT license.
4+
*/
5+
6+
package defaulter
7+
8+
import (
9+
"testing"
10+
11+
"github.com/google/go-cmp/cmp"
12+
"k8s.io/utils/ptr"
13+
14+
fleetnetv1alpha1 "go.goms.io/fleet-networking/api/v1alpha1"
15+
)
16+
17+
func TestSetTrafficManagerProfile(t *testing.T) {
18+
tests := []struct {
19+
name string
20+
obj *fleetnetv1alpha1.TrafficManagerProfile
21+
want *fleetnetv1alpha1.TrafficManagerProfile
22+
}{
23+
{
24+
name: "TrafficManagerProfile with nil spec",
25+
obj: &fleetnetv1alpha1.TrafficManagerProfile{
26+
Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{},
27+
},
28+
want: &fleetnetv1alpha1.TrafficManagerProfile{
29+
Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{
30+
MonitorConfig: &fleetnetv1alpha1.MonitorConfig{
31+
IntervalInSeconds: ptr.To(int64(30)),
32+
Path: ptr.To("/"),
33+
Port: ptr.To(int64(80)),
34+
Protocol: ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTP),
35+
TimeoutInSeconds: ptr.To(int64(10)),
36+
ToleratedNumberOfFailures: ptr.To(int64(3)),
37+
},
38+
},
39+
},
40+
},
41+
{
42+
name: "TrafficManagerProfile with 10 IntervalInSeconds and nil TimeoutInSeconds",
43+
obj: &fleetnetv1alpha1.TrafficManagerProfile{
44+
Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{
45+
MonitorConfig: &fleetnetv1alpha1.MonitorConfig{
46+
IntervalInSeconds: ptr.To(int64(10)),
47+
},
48+
},
49+
},
50+
want: &fleetnetv1alpha1.TrafficManagerProfile{
51+
Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{
52+
MonitorConfig: &fleetnetv1alpha1.MonitorConfig{
53+
IntervalInSeconds: ptr.To(int64(10)),
54+
Path: ptr.To("/"),
55+
Port: ptr.To(int64(80)),
56+
Protocol: ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTP),
57+
TimeoutInSeconds: ptr.To(int64(9)),
58+
ToleratedNumberOfFailures: ptr.To(int64(3)),
59+
},
60+
},
61+
},
62+
},
63+
{
64+
name: "TrafficManagerProfile with values",
65+
obj: &fleetnetv1alpha1.TrafficManagerProfile{
66+
Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{
67+
MonitorConfig: &fleetnetv1alpha1.MonitorConfig{
68+
IntervalInSeconds: ptr.To(int64(10)),
69+
Path: ptr.To("/healthz"),
70+
Port: ptr.To(int64(8080)),
71+
Protocol: ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTPS),
72+
TimeoutInSeconds: ptr.To(int64(9)),
73+
ToleratedNumberOfFailures: ptr.To(int64(4)),
74+
},
75+
},
76+
},
77+
want: &fleetnetv1alpha1.TrafficManagerProfile{
78+
Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{
79+
MonitorConfig: &fleetnetv1alpha1.MonitorConfig{
80+
IntervalInSeconds: ptr.To(int64(10)),
81+
Path: ptr.To("/healthz"),
82+
Port: ptr.To(int64(8080)),
83+
Protocol: ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTPS),
84+
TimeoutInSeconds: ptr.To(int64(9)),
85+
ToleratedNumberOfFailures: ptr.To(int64(4)),
86+
},
87+
},
88+
},
89+
},
90+
}
91+
for _, tc := range tests {
92+
t.Run(tc.name, func(t *testing.T) {
93+
SetDefaultsTrafficManagerProfile(tc.obj)
94+
if diff := cmp.Diff(tc.want, tc.obj); diff != "" {
95+
t.Errorf("SetDefaultsTrafficManagerProfile() mismatch (-want +got):\n%s", diff)
96+
}
97+
})
98+
}
99+
}

0 commit comments

Comments
 (0)