Skip to content

Commit

Permalink
validate if DNS Service IP is a .10 IP
Browse files Browse the repository at this point in the history
- Removed `validateDNSServiceIP()` from the validators because the
DNS Service IP validation was being done in `validateManagedClusterNetwork()`.
- update `validateManagedClusterNetwork()` to validate if DNS Service IP is a .10 IP
- update mockClient.Get to return clusterv1.Cluster with Service CIDR : `192.168.0.0/26`
- update amcp_webhook_test with valid DNS Service IPs and mock client for webhook tests
  • Loading branch information
nawazkh committed Aug 15, 2023
1 parent 8aa0b26 commit 45ede31
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 78 deletions.
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 @@ -253,7 +253,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 @@ -271,17 +270,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 @@ -421,10 +409,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

0 comments on commit 45ede31

Please sign in to comment.