Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate DNS Service IP to be a .10 IP belonging to Service CIDR #3826

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions api/v1beta1/azuremachine_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,17 @@ func (m mockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Ob
case *AzureCluster:
obj.Spec.SubscriptionID = "test-subscription-id"
case *clusterv1.Cluster:
obj.Spec.InfrastructureRef = &corev1.ObjectReference{
Kind: "AzureCluster",
Name: "test-cluster",
Namespace: "default",
obj.Spec = clusterv1.ClusterSpec{
InfrastructureRef: &corev1.ObjectReference{
Kind: "AzureCluster",
Name: "test-cluster",
Namespace: "default",
},
ClusterNetwork: &clusterv1.ClusterNetwork{
Services: &clusterv1.NetworkRanges{
CIDRBlocks: []string{"192.168.0.0/26"},
},
},
}
default:
return errors.New("unexpected object type")
Expand Down
28 changes: 14 additions & 14 deletions api/v1beta1/azuremanagedcontrolplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ func (m *AzureManagedControlPlane) Validate(cli client.Client) error {
validators := []func(client client.Client) error{
m.validateName,
m.validateVersion,
m.validateDNSServiceIP,
m.validateSSHKey,
m.validateLoadBalancerProfile,
m.validateAPIServerAccessProfile,
Expand All @@ -285,17 +284,6 @@ func (m *AzureManagedControlPlane) Validate(cli client.Client) error {
return kerrors.NewAggregate(errs)
}

// validateDNSServiceIP validates the DNSServiceIP.
func (m *AzureManagedControlPlane) validateDNSServiceIP(_ client.Client) error {
if m.Spec.DNSServiceIP != nil {
if net.ParseIP(*m.Spec.DNSServiceIP) == nil {
return errors.New("DNSServiceIP must be a valid IP")
}
}

return nil
}

// validateVersion validates the Kubernetes version.
func (m *AzureManagedControlPlane) validateVersion(_ client.Client) error {
if !kubeSemver.MatchString(m.Spec.Version) {
Expand Down Expand Up @@ -434,10 +422,22 @@ func (m *AzureManagedControlPlane) validateManagedClusterNetwork(cli client.Clie
if err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("Cluster", "Spec", "ClusterNetwork", "Services", "CIDRBlocks"), serviceCIDR, fmt.Sprintf("failed to parse cluster service cidr: %v", err)))
}
ip := net.ParseIP(*m.Spec.DNSServiceIP)
if !cidr.Contains(ip) {

dnsIP := net.ParseIP(*m.Spec.DNSServiceIP)
if dnsIP == nil { // dnsIP will be nil if the string is not a valid IP
allErrs = append(allErrs, field.Invalid(field.NewPath("Spec", "DNSServiceIP"), *m.Spec.DNSServiceIP, "must be a valid IP address"))
}

if dnsIP != nil && !cidr.Contains(dnsIP) {
allErrs = append(allErrs, field.Invalid(field.NewPath("Cluster", "Spec", "ClusterNetwork", "Services", "CIDRBlocks"), serviceCIDR, "DNSServiceIP must reside within the associated cluster serviceCIDR"))
}

// AKS only supports .10 as the last octet for the DNSServiceIP.
// Refer to: https://learn.microsoft.com/en-us/azure/aks/configure-kubenet#create-an-aks-cluster-with-system-assigned-managed-identities
targetSuffix := ".10"
if dnsIP != nil && !strings.HasSuffix(dnsIP.String(), targetSuffix) {
allErrs = append(allErrs, field.Invalid(field.NewPath("Spec", "DNSServiceIP"), *m.Spec.DNSServiceIP, fmt.Sprintf("must end with %q", targetSuffix)))
}
}

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