Skip to content

Commit 2c4e263

Browse files
committed
Fix for Webhook Validations & ManagedCluster Spec Updation
1 parent 3f82514 commit 2c4e263

File tree

2 files changed

+72
-75
lines changed

2 files changed

+72
-75
lines changed

api/v1beta1/azuremanagedcontrolplane_webhook.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -534,27 +534,24 @@ func validateManagedClusterNetwork(cli client.Client, labels map[string]string,
534534
Name: clusterName,
535535
}
536536

537-
if err := cli.Get(ctx, key, ownerCluster); err != nil {
538-
allErrs = append(allErrs, field.InternalError(field.NewPath("Cluster", "spec", "clusterNetwork"), err))
539-
return allErrs
540-
}
541-
542-
if clusterNetwork := ownerCluster.Spec.ClusterNetwork; clusterNetwork != nil {
543-
if clusterNetwork.Services != nil {
544-
// A user may provide zero or one CIDR blocks. If they provide an empty array,
545-
// we ignore it and use the default. AKS doesn't support > 1 Service/Pod CIDR.
546-
if len(clusterNetwork.Services.CIDRBlocks) > 1 {
547-
allErrs = append(allErrs, field.TooMany(field.NewPath("Cluster", "spec", "clusterNetwork", "services", "cidrBlocks"), len(clusterNetwork.Services.CIDRBlocks), 1))
548-
}
549-
if len(clusterNetwork.Services.CIDRBlocks) == 1 {
550-
serviceCIDR = clusterNetwork.Services.CIDRBlocks[0]
537+
if err := cli.Get(ctx, key, ownerCluster); err == nil {
538+
if clusterNetwork := ownerCluster.Spec.ClusterNetwork; clusterNetwork != nil {
539+
if clusterNetwork.Services != nil {
540+
// A user may provide zero or one CIDR blocks. If they provide an empty array,
541+
// we ignore it and use the default. AKS doesn't support > 1 Service/Pod CIDR.
542+
if len(clusterNetwork.Services.CIDRBlocks) > 1 {
543+
allErrs = append(allErrs, field.TooMany(field.NewPath("Cluster", "spec", "clusterNetwork", "services", "cidrBlocks"), len(clusterNetwork.Services.CIDRBlocks), 1))
544+
}
545+
if len(clusterNetwork.Services.CIDRBlocks) == 1 {
546+
serviceCIDR = clusterNetwork.Services.CIDRBlocks[0]
547+
}
551548
}
552-
}
553-
if clusterNetwork.Pods != nil {
554-
// A user may provide zero or one CIDR blocks. If they provide an empty array,
555-
// we ignore it and use the default. AKS doesn't support > 1 Service/Pod CIDR.
556-
if len(clusterNetwork.Pods.CIDRBlocks) > 1 {
557-
allErrs = append(allErrs, field.TooMany(field.NewPath("Cluster", "spec", "clusterNetwork", "pods", "cidrBlocks"), len(clusterNetwork.Pods.CIDRBlocks), 1))
549+
if clusterNetwork.Pods != nil {
550+
// A user may provide zero or one CIDR blocks. If they provide an empty array,
551+
// we ignore it and use the default. AKS doesn't support > 1 Service/Pod CIDR.
552+
if len(clusterNetwork.Pods.CIDRBlocks) > 1 {
553+
allErrs = append(allErrs, field.TooMany(field.NewPath("Cluster", "spec", "clusterNetwork", "pods", "cidrBlocks"), len(clusterNetwork.Pods.CIDRBlocks), 1))
554+
}
558555
}
559556
}
560557
}

azure/services/managedclusters/spec.go

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ import (
2020
"context"
2121
"encoding/base64"
2222
"fmt"
23-
"net"
24-
"sort"
25-
"time"
26-
2723
asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001"
2824
asocontainerservicev1preview "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231102preview"
2925
asocontainerservicev1hub "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20240901/storage"
@@ -33,8 +29,10 @@ import (
3329
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3430
"k8s.io/apimachinery/pkg/runtime"
3531
"k8s.io/utils/ptr"
32+
"net"
3633
"sigs.k8s.io/cluster-api/util/secret"
3734
"sigs.k8s.io/controller-runtime/pkg/conversion"
35+
"sort"
3836

3937
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
4038
"sigs.k8s.io/cluster-api-provider-azure/azure"
@@ -762,57 +760,59 @@ func (s *ManagedClusterSpec) Parameters(ctx context.Context, existingObj genrunt
762760

763761
managedCluster.Spec.AgentPoolProfiles = prevAgentPoolProfiles
764762

765-
if existingObj != nil {
766-
hub := &asocontainerservicev1hub.ManagedCluster{}
767-
if err := existingObj.(conversion.Convertible).ConvertTo(hub); err != nil {
768-
return nil, err
769-
}
770-
existingMC := hub
771-
772-
ps := *existingMC.Status.ProvisioningState
773-
if ps != string(infrav1.Canceled) && ps != string(infrav1.Failed) && ps != string(infrav1.Succeeded) {
774-
return nil, azure.WithTransientError(errors.Errorf("Unable to update existing managed cluster in non-terminal state. Managed cluster must be in one of the following provisioning states: Canceled, Failed, or Succeeded. Actual state: %s", ps), 20*time.Second)
775-
}
776-
777-
if managedCluster.Spec.AddonProfiles == nil && existingMC.Spec.AddonProfiles != nil {
778-
managedCluster.Spec.AddonProfiles = map[string]asocontainerservicev1hub.ManagedClusterAddonProfile{}
779-
}
780-
781-
for key, item := range existingMC.Spec.AddonProfiles {
782-
if managedCluster.Spec.AddonProfiles == nil {
783-
managedCluster.Spec.AddonProfiles = map[string]asocontainerservicev1hub.ManagedClusterAddonProfile{}
784-
}
785-
786-
if _, ok := managedCluster.Spec.AddonProfiles[key]; !ok {
787-
addonProfile := asocontainerservicev1hub.ManagedClusterAddonProfile{
788-
Enabled: item.Enabled,
789-
}
790-
if item.Config != nil {
791-
addonProfile.Config = item.Config
792-
}
793-
managedCluster.Spec.AddonProfiles[key] = addonProfile
794-
}
795-
}
796-
797-
// Normalize the LoadBalancerProfile so the diff below doesn't get thrown off by AKS added properties.
798-
if managedCluster.Spec.NetworkProfile.LoadBalancerProfile == nil {
799-
// If our LoadBalancerProfile generated by the spec is nil, then don't worry about what AKS has added.
800-
existingMC.Spec.NetworkProfile.LoadBalancerProfile = nil
801-
} else {
802-
// If our LoadBalancerProfile generated by the spec is not nil, then remove the effective outbound IPs from
803-
// AKS.
804-
existingMC.Spec.NetworkProfile.LoadBalancerProfile.EffectiveOutboundIPs = nil
805-
}
806-
807-
// Avoid changing agent pool profiles through AMCP and just use the existing agent pool profiles
808-
// AgentPool changes are managed through AMMP.
809-
managedCluster.Spec.AgentPoolProfiles = existingMC.Spec.AgentPoolProfiles
810-
811-
diff := computeDiffOfNormalizedClusters(*managedCluster, *existingMC)
812-
if diff == "" {
813-
return nil, nil
814-
}
815-
}
763+
//if existingObj != nil {
764+
// hub := &asocontainerservicev1hub.ManagedCluster{}
765+
// if err := existingObj.(conversion.Convertible).ConvertTo(hub); err != nil {
766+
// return nil, err
767+
// }
768+
// existingMC := hub
769+
//
770+
// if existingMC.Status.ProvisioningState != nil {
771+
// ps := *existingMC.Status.ProvisioningState
772+
// if ps != string(infrav1.Canceled) && ps != string(infrav1.Failed) && ps != string(infrav1.Succeeded) {
773+
// return nil, azure.WithTransientError(errors.Errorf("Unable to update existing managed cluster in non-terminal state. Managed cluster must be in one of the following provisioning states: Canceled, Failed, or Succeeded. Actual state: %s", ps), 20*time.Second)
774+
// }
775+
// }
776+
//
777+
// if managedCluster.Spec.AddonProfiles == nil && existingMC.Spec.AddonProfiles != nil {
778+
// managedCluster.Spec.AddonProfiles = map[string]asocontainerservicev1hub.ManagedClusterAddonProfile{}
779+
// }
780+
//
781+
// for key, item := range existingMC.Spec.AddonProfiles {
782+
// if managedCluster.Spec.AddonProfiles == nil {
783+
// managedCluster.Spec.AddonProfiles = map[string]asocontainerservicev1hub.ManagedClusterAddonProfile{}
784+
// }
785+
//
786+
// if _, ok := managedCluster.Spec.AddonProfiles[key]; !ok {
787+
// addonProfile := asocontainerservicev1hub.ManagedClusterAddonProfile{
788+
// Enabled: item.Enabled,
789+
// }
790+
// if item.Config != nil {
791+
// addonProfile.Config = item.Config
792+
// }
793+
// managedCluster.Spec.AddonProfiles[key] = addonProfile
794+
// }
795+
// }
796+
//
797+
// // Normalize the LoadBalancerProfile so the diff below doesn't get thrown off by AKS added properties.
798+
// if managedCluster.Spec.NetworkProfile.LoadBalancerProfile == nil {
799+
// // If our LoadBalancerProfile generated by the spec is nil, then don't worry about what AKS has added.
800+
// existingMC.Spec.NetworkProfile.LoadBalancerProfile = nil
801+
// } else {
802+
// // If our LoadBalancerProfile generated by the spec is not nil, then remove the effective outbound IPs from
803+
// // AKS.
804+
// existingMC.Spec.NetworkProfile.LoadBalancerProfile.EffectiveOutboundIPs = nil
805+
// }
806+
//
807+
// // Avoid changing agent pool profiles through AMCP and just use the existing agent pool profiles
808+
// // AgentPool changes are managed through AMMP.
809+
// managedCluster.Spec.AgentPoolProfiles = existingMC.Spec.AgentPoolProfiles
810+
//
811+
// diff := computeDiffOfNormalizedClusters(*managedCluster, *existingMC)
812+
// if diff == "" {
813+
// return nil, nil
814+
// }
815+
//}
816816

817817
if s.Preview {
818818
prev := &asocontainerservicev1preview.ManagedCluster{}

0 commit comments

Comments
 (0)