-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create defaulter func for trafficManagerProfile (#196)
* feat: create defaulter func for trafficManagerProfile * address comment
- Loading branch information
1 parent
e3743e8
commit 2398ebb
Showing
3 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright (c) Microsoft Corporation. | ||
Licensed under the MIT license. | ||
*/ | ||
|
||
// Package defaulter provides the utils for setting default values for a resource. | ||
package defaulter | ||
|
||
import ( | ||
"k8s.io/utils/ptr" | ||
|
||
fleetnetv1alpha1 "go.goms.io/fleet-networking/api/v1alpha1" | ||
) | ||
|
||
// SetDefaultsTrafficManagerProfile sets the default values for TrafficManagerProfile. | ||
func SetDefaultsTrafficManagerProfile(obj *fleetnetv1alpha1.TrafficManagerProfile) { | ||
if obj.Spec.MonitorConfig == nil { | ||
obj.Spec.MonitorConfig = &fleetnetv1alpha1.MonitorConfig{} | ||
} | ||
|
||
if obj.Spec.MonitorConfig.IntervalInSeconds == nil { | ||
obj.Spec.MonitorConfig.IntervalInSeconds = ptr.To(int64(30)) | ||
} | ||
|
||
if obj.Spec.MonitorConfig.Path == nil { | ||
obj.Spec.MonitorConfig.Path = ptr.To("/") | ||
} | ||
|
||
if obj.Spec.MonitorConfig.Port == nil { | ||
obj.Spec.MonitorConfig.Port = ptr.To(int64(80)) | ||
} | ||
|
||
if obj.Spec.MonitorConfig.Protocol == nil { | ||
obj.Spec.MonitorConfig.Protocol = ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTP) | ||
} | ||
|
||
// TimeoutInSeconds value depends on the IntervalInSeconds, so that the defaulter MUST handle the IntervalInSeconds first. | ||
// * If the Probing Interval is set to 30 seconds, then you can set the Timeout value between 5 and 10 seconds. | ||
// If no value is specified, it uses a default value of 10 seconds. | ||
// * If the Probing Interval is set to 10 seconds, then you can set the Timeout value between 5 and 9 seconds. | ||
// If no Timeout value is specified, it uses a default value of 9 seconds. | ||
// Reference link: https://learn.microsoft.com/en-us/azure/traffic-manager/traffic-manager-monitoring#configure-endpoint-monitoring | ||
if obj.Spec.MonitorConfig.TimeoutInSeconds == nil { | ||
if *obj.Spec.MonitorConfig.IntervalInSeconds == 30 { | ||
obj.Spec.MonitorConfig.TimeoutInSeconds = ptr.To(int64(10)) | ||
} else { // assuming validation wehbook should validate the intervalInSeconds first | ||
obj.Spec.MonitorConfig.TimeoutInSeconds = ptr.To(int64(9)) | ||
} | ||
} | ||
|
||
if obj.Spec.MonitorConfig.ToleratedNumberOfFailures == nil { | ||
obj.Spec.MonitorConfig.ToleratedNumberOfFailures = ptr.To(int64(3)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
Copyright (c) Microsoft Corporation. | ||
Licensed under the MIT license. | ||
*/ | ||
|
||
package defaulter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"k8s.io/utils/ptr" | ||
|
||
fleetnetv1alpha1 "go.goms.io/fleet-networking/api/v1alpha1" | ||
) | ||
|
||
func TestSetTrafficManagerProfile(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
obj *fleetnetv1alpha1.TrafficManagerProfile | ||
want *fleetnetv1alpha1.TrafficManagerProfile | ||
}{ | ||
{ | ||
name: "TrafficManagerProfile with nil spec", | ||
obj: &fleetnetv1alpha1.TrafficManagerProfile{ | ||
Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{}, | ||
}, | ||
want: &fleetnetv1alpha1.TrafficManagerProfile{ | ||
Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{ | ||
MonitorConfig: &fleetnetv1alpha1.MonitorConfig{ | ||
IntervalInSeconds: ptr.To(int64(30)), | ||
Path: ptr.To("/"), | ||
Port: ptr.To(int64(80)), | ||
Protocol: ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTP), | ||
TimeoutInSeconds: ptr.To(int64(10)), | ||
ToleratedNumberOfFailures: ptr.To(int64(3)), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "TrafficManagerProfile with 10 IntervalInSeconds and nil TimeoutInSeconds", | ||
obj: &fleetnetv1alpha1.TrafficManagerProfile{ | ||
Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{ | ||
MonitorConfig: &fleetnetv1alpha1.MonitorConfig{ | ||
IntervalInSeconds: ptr.To(int64(10)), | ||
}, | ||
}, | ||
}, | ||
want: &fleetnetv1alpha1.TrafficManagerProfile{ | ||
Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{ | ||
MonitorConfig: &fleetnetv1alpha1.MonitorConfig{ | ||
IntervalInSeconds: ptr.To(int64(10)), | ||
Path: ptr.To("/"), | ||
Port: ptr.To(int64(80)), | ||
Protocol: ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTP), | ||
TimeoutInSeconds: ptr.To(int64(9)), | ||
ToleratedNumberOfFailures: ptr.To(int64(3)), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "TrafficManagerProfile with values", | ||
obj: &fleetnetv1alpha1.TrafficManagerProfile{ | ||
Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{ | ||
MonitorConfig: &fleetnetv1alpha1.MonitorConfig{ | ||
IntervalInSeconds: ptr.To(int64(10)), | ||
Path: ptr.To("/healthz"), | ||
Port: ptr.To(int64(8080)), | ||
Protocol: ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTPS), | ||
TimeoutInSeconds: ptr.To(int64(9)), | ||
ToleratedNumberOfFailures: ptr.To(int64(4)), | ||
}, | ||
}, | ||
}, | ||
want: &fleetnetv1alpha1.TrafficManagerProfile{ | ||
Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{ | ||
MonitorConfig: &fleetnetv1alpha1.MonitorConfig{ | ||
IntervalInSeconds: ptr.To(int64(10)), | ||
Path: ptr.To("/healthz"), | ||
Port: ptr.To(int64(8080)), | ||
Protocol: ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTPS), | ||
TimeoutInSeconds: ptr.To(int64(9)), | ||
ToleratedNumberOfFailures: ptr.To(int64(4)), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
SetDefaultsTrafficManagerProfile(tc.obj) | ||
if diff := cmp.Diff(tc.want, tc.obj); diff != "" { | ||
t.Errorf("SetDefaultsTrafficManagerProfile() mismatch (-want +got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |