Skip to content

Commit 3a8aff2

Browse files
authored
Merge pull request #3840 from nawazkh/1-9-improve_DNSServiceIP_validation
[release-1.9] improve dns service ip validation
2 parents 8aa0b26 + 45ede31 commit 3a8aff2

File tree

3 files changed

+180
-78
lines changed

3 files changed

+180
-78
lines changed

api/v1beta1/azuremachine_default_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,17 @@ func (m mockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Ob
521521
case *AzureCluster:
522522
obj.Spec.SubscriptionID = "test-subscription-id"
523523
case *clusterv1.Cluster:
524-
obj.Spec.InfrastructureRef = &corev1.ObjectReference{
525-
Kind: "AzureCluster",
526-
Name: "test-cluster",
527-
Namespace: "default",
524+
obj.Spec = clusterv1.ClusterSpec{
525+
InfrastructureRef: &corev1.ObjectReference{
526+
Kind: "AzureCluster",
527+
Name: "test-cluster",
528+
Namespace: "default",
529+
},
530+
ClusterNetwork: &clusterv1.ClusterNetwork{
531+
Services: &clusterv1.NetworkRanges{
532+
CIDRBlocks: []string{"192.168.0.0/26"},
533+
},
534+
},
528535
}
529536
default:
530537
return errors.New("unexpected object type")

api/v1beta1/azuremanagedcontrolplane_webhook.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ func (m *AzureManagedControlPlane) Validate(cli client.Client) error {
253253
validators := []func(client client.Client) error{
254254
m.validateName,
255255
m.validateVersion,
256-
m.validateDNSServiceIP,
257256
m.validateSSHKey,
258257
m.validateLoadBalancerProfile,
259258
m.validateAPIServerAccessProfile,
@@ -271,17 +270,6 @@ func (m *AzureManagedControlPlane) Validate(cli client.Client) error {
271270
return kerrors.NewAggregate(errs)
272271
}
273272

274-
// validateDNSServiceIP validates the DNSServiceIP.
275-
func (m *AzureManagedControlPlane) validateDNSServiceIP(_ client.Client) error {
276-
if m.Spec.DNSServiceIP != nil {
277-
if net.ParseIP(*m.Spec.DNSServiceIP) == nil {
278-
return errors.New("DNSServiceIP must be a valid IP")
279-
}
280-
}
281-
282-
return nil
283-
}
284-
285273
// validateVersion validates the Kubernetes version.
286274
func (m *AzureManagedControlPlane) validateVersion(_ client.Client) error {
287275
if !kubeSemver.MatchString(m.Spec.Version) {
@@ -421,10 +409,22 @@ func (m *AzureManagedControlPlane) validateManagedClusterNetwork(cli client.Clie
421409
if err != nil {
422410
allErrs = append(allErrs, field.Invalid(field.NewPath("Cluster", "Spec", "ClusterNetwork", "Services", "CIDRBlocks"), serviceCIDR, fmt.Sprintf("failed to parse cluster service cidr: %v", err)))
423411
}
424-
ip := net.ParseIP(*m.Spec.DNSServiceIP)
425-
if !cidr.Contains(ip) {
412+
413+
dnsIP := net.ParseIP(*m.Spec.DNSServiceIP)
414+
if dnsIP == nil { // dnsIP will be nil if the string is not a valid IP
415+
allErrs = append(allErrs, field.Invalid(field.NewPath("Spec", "DNSServiceIP"), *m.Spec.DNSServiceIP, "must be a valid IP address"))
416+
}
417+
418+
if dnsIP != nil && !cidr.Contains(dnsIP) {
426419
allErrs = append(allErrs, field.Invalid(field.NewPath("Cluster", "Spec", "ClusterNetwork", "Services", "CIDRBlocks"), serviceCIDR, "DNSServiceIP must reside within the associated cluster serviceCIDR"))
427420
}
421+
422+
// AKS only supports .10 as the last octet for the DNSServiceIP.
423+
// Refer to: https://learn.microsoft.com/en-us/azure/aks/configure-kubenet#create-an-aks-cluster-with-system-assigned-managed-identities
424+
targetSuffix := ".10"
425+
if dnsIP != nil && !strings.HasSuffix(dnsIP.String(), targetSuffix) {
426+
allErrs = append(allErrs, field.Invalid(field.NewPath("Spec", "DNSServiceIP"), *m.Spec.DNSServiceIP, fmt.Sprintf("must end with %q", targetSuffix)))
427+
}
428428
}
429429

430430
if errs := validatePrivateEndpoints(m.Spec.VirtualNetwork.Subnet.PrivateEndpoints, []string{m.Spec.VirtualNetwork.Subnet.CIDRBlock}, field.NewPath("Spec", "VirtualNetwork.Subnet.PrivateEndpoints")); len(errs) > 0 {

0 commit comments

Comments
 (0)