diff --git a/azure/scope/managedcontrolplane.go b/azure/scope/managedcontrolplane.go index 73caa5091298..a3e8cd4c2dca 100644 --- a/azure/scope/managedcontrolplane.go +++ b/azure/scope/managedcontrolplane.go @@ -37,7 +37,6 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/util/maps" "sigs.k8s.io/cluster-api-provider-azure/util/tele" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1" "sigs.k8s.io/cluster-api/util/conditions" "sigs.k8s.io/cluster-api/util/patch" "sigs.k8s.io/cluster-api/util/secret" @@ -54,11 +53,6 @@ type ManagedControlPlaneScopeParams struct { ManagedMachinePools []ManagedMachinePool } -type ManagedMachinePool struct { - InfraMachinePool *infrav1exp.AzureManagedMachinePool - MachinePool *clusterv1exp.MachinePool -} - // NewManagedControlPlaneScope creates a new Scope from the supplied parameters. // This is meant to be called for each reconcile iteration. func NewManagedControlPlaneScope(ctx context.Context, params ManagedControlPlaneScopeParams) (*ManagedControlPlaneScope, error) { @@ -421,15 +415,11 @@ func (s *ManagedControlPlaneScope) ManagedClusterSpec(ctx context.Context) azure } if clusterNetwork := s.Cluster.Spec.ClusterNetwork; clusterNetwork != nil { - if clusterNetwork.Services != nil { - if len(clusterNetwork.Services.CIDRBlocks) == 1 { - managedClusterSpec.ServiceCIDR = clusterNetwork.Services.CIDRBlocks[0] - } + if clusterNetwork.Services != nil && len(clusterNetwork.Services.CIDRBlocks) == 1 { + managedClusterSpec.ServiceCIDR = clusterNetwork.Services.CIDRBlocks[0] } - if clusterNetwork.Pods != nil { - if len(clusterNetwork.Pods.CIDRBlocks) == 1 { - managedClusterSpec.PodCIDR = clusterNetwork.Pods.CIDRBlocks[0] - } + if clusterNetwork.Pods != nil && len(clusterNetwork.Pods.CIDRBlocks) == 1 { + managedClusterSpec.PodCIDR = clusterNetwork.Pods.CIDRBlocks[0] } } diff --git a/azure/scope/managedmachinepool.go b/azure/scope/managedmachinepool.go index 72209061a8d2..801d609ad012 100644 --- a/azure/scope/managedmachinepool.go +++ b/azure/scope/managedmachinepool.go @@ -29,7 +29,7 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/util/futures" "sigs.k8s.io/cluster-api-provider-azure/util/tele" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" + clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1" "sigs.k8s.io/cluster-api/util/conditions" "sigs.k8s.io/cluster-api/util/patch" "sigs.k8s.io/controller-runtime/pkg/client" @@ -38,14 +38,18 @@ import ( // ManagedMachinePoolScopeParams defines the input parameters used to create a new managed // control plane. type ManagedMachinePoolScopeParams struct { + ManagedMachinePool Client client.Client Cluster *clusterv1.Cluster ControlPlane *infrav1exp.AzureManagedControlPlane - InfraMachinePool *infrav1exp.AzureManagedMachinePool - MachinePool *expv1.MachinePool ManagedControlPlaneScope azure.ManagedClusterScoper } +type ManagedMachinePool struct { + InfraMachinePool *infrav1exp.AzureManagedMachinePool + MachinePool *clusterv1exp.MachinePool +} + // NewManagedMachinePoolScope creates a new Scope from the supplied parameters. // This is meant to be called for each reconcile iteration. func NewManagedMachinePoolScope(ctx context.Context, params ManagedMachinePoolScopeParams) (*ManagedMachinePoolScope, error) { @@ -83,7 +87,7 @@ type ManagedMachinePoolScope struct { azure.ManagedClusterScoper Cluster *clusterv1.Cluster - MachinePool *expv1.MachinePool + MachinePool *clusterv1exp.MachinePool ControlPlane *infrav1exp.AzureManagedControlPlane InfraMachinePool *infrav1exp.AzureManagedMachinePool } @@ -111,6 +115,7 @@ func (s *ManagedMachinePoolScope) Close(ctx context.Context) error { return s.PatchObject(ctx) } +// AgentPoolAnnotations returns a map of annotations for the infra machine pool. func (s *ManagedMachinePoolScope) AgentPoolAnnotations() map[string]string { return s.InfraMachinePool.Annotations } @@ -121,7 +126,7 @@ func (s *ManagedMachinePoolScope) AgentPoolSpec() azure.AgentPoolSpec { } func buildAgentPoolSpec(managedControlPlane *infrav1exp.AzureManagedControlPlane, - machinePool *expv1.MachinePool, + machinePool *clusterv1exp.MachinePool, managedMachinePool *infrav1exp.AzureManagedMachinePool) azure.AgentPoolSpec { var normalizedVersion *string if machinePool.Spec.Template.Spec.Version != nil { diff --git a/azure/scope/managedmachinepool_test.go b/azure/scope/managedmachinepool_test.go index 81d16eb373a5..907ca55a31ae 100644 --- a/azure/scope/managedmachinepool_test.go +++ b/azure/scope/managedmachinepool_test.go @@ -60,8 +60,10 @@ func TestManagedMachinePoolScope_Autoscaling(t *testing.T) { SubscriptionID: "00000000-0000-0000-0000-000000000000", }, }, - MachinePool: getMachinePool("pool0"), - InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem), + ManagedMachinePool: ManagedMachinePool{ + MachinePool: getMachinePool("pool0"), + InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem), + }, }, Expected: azure.AgentPoolSpec{ @@ -91,8 +93,10 @@ func TestManagedMachinePoolScope_Autoscaling(t *testing.T) { SubscriptionID: "00000000-0000-0000-0000-000000000000", }, }, - MachinePool: getMachinePool("pool1"), - InfraMachinePool: getAzureMachinePoolWithScaling("pool1", 2, 10), + ManagedMachinePool: ManagedMachinePool{ + MachinePool: getMachinePool("pool1"), + InfraMachinePool: getAzureMachinePoolWithScaling("pool1", 2, 10), + }, }, Expected: azure.AgentPoolSpec{ Name: "pool1", @@ -150,8 +154,10 @@ func TestManagedMachinePoolScope_NodeLabels(t *testing.T) { SubscriptionID: "00000000-0000-0000-0000-000000000000", }, }, - MachinePool: getMachinePool("pool0"), - InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem), + ManagedMachinePool: ManagedMachinePool{ + MachinePool: getMachinePool("pool0"), + InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem), + }, }, Expected: azure.AgentPoolSpec{ @@ -181,10 +187,12 @@ func TestManagedMachinePoolScope_NodeLabels(t *testing.T) { SubscriptionID: "00000000-0000-0000-0000-000000000000", }, }, - MachinePool: getMachinePool("pool1"), - InfraMachinePool: getAzureMachinePoolWithLabels("pool1", map[string]string{ - "custom": "default", - }), + ManagedMachinePool: ManagedMachinePool{ + MachinePool: getMachinePool("pool1"), + InfraMachinePool: getAzureMachinePoolWithLabels("pool1", map[string]string{ + "custom": "default", + }), + }, }, Expected: azure.AgentPoolSpec{ Name: "pool1", @@ -242,8 +250,10 @@ func TestManagedMachinePoolScope_MaxPods(t *testing.T) { SubscriptionID: "00000000-0000-0000-0000-000000000000", }, }, - MachinePool: getMachinePool("pool0"), - InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem), + ManagedMachinePool: ManagedMachinePool{ + MachinePool: getMachinePool("pool0"), + InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem), + }, }, Expected: azure.AgentPoolSpec{ Name: "pool0", @@ -272,8 +282,10 @@ func TestManagedMachinePoolScope_MaxPods(t *testing.T) { SubscriptionID: "00000000-0000-0000-0000-000000000000", }, }, - MachinePool: getMachinePool("pool1"), - InfraMachinePool: getAzureMachinePoolWithMaxPods("pool1", 12), + ManagedMachinePool: ManagedMachinePool{ + MachinePool: getMachinePool("pool1"), + InfraMachinePool: getAzureMachinePoolWithMaxPods("pool1", 12), + }, }, Expected: azure.AgentPoolSpec{ Name: "pool1", @@ -329,8 +341,10 @@ func TestManagedMachinePoolScope_Taints(t *testing.T) { SubscriptionID: "00000000-0000-0000-0000-000000000000", }, }, - MachinePool: getMachinePool("pool0"), - InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem), + ManagedMachinePool: ManagedMachinePool{ + MachinePool: getMachinePool("pool0"), + InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem), + }, }, Expected: azure.AgentPoolSpec{ @@ -360,14 +374,16 @@ func TestManagedMachinePoolScope_Taints(t *testing.T) { SubscriptionID: "00000000-0000-0000-0000-000000000000", }, }, - MachinePool: getMachinePool("pool1"), - InfraMachinePool: getAzureMachinePoolWithTaints("pool1", infrav1.Taints{ - infrav1.Taint{ - Key: "key1", - Value: "value1", - Effect: "NoSchedule", - }, - }), + ManagedMachinePool: ManagedMachinePool{ + MachinePool: getMachinePool("pool1"), + InfraMachinePool: getAzureMachinePoolWithTaints("pool1", infrav1.Taints{ + infrav1.Taint{ + Key: "key1", + Value: "value1", + Effect: "NoSchedule", + }, + }), + }, }, Expected: azure.AgentPoolSpec{ Name: "pool1", @@ -423,8 +439,10 @@ func TestManagedMachinePoolScope_OSDiskType(t *testing.T) { SubscriptionID: "00000000-0000-0000-0000-000000000000", }, }, - MachinePool: getMachinePool("pool0"), - InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem), + ManagedMachinePool: ManagedMachinePool{ + MachinePool: getMachinePool("pool0"), + InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem), + }, }, Expected: azure.AgentPoolSpec{ Name: "pool0", @@ -453,8 +471,10 @@ func TestManagedMachinePoolScope_OSDiskType(t *testing.T) { SubscriptionID: "00000000-0000-0000-0000-000000000000", }, }, - MachinePool: getMachinePool("pool1"), - InfraMachinePool: getAzureMachinePoolWithOsDiskType("pool1", string(containerservice.OSDiskTypeEphemeral)), + ManagedMachinePool: ManagedMachinePool{ + MachinePool: getMachinePool("pool1"), + InfraMachinePool: getAzureMachinePoolWithOsDiskType("pool1", string(containerservice.OSDiskTypeEphemeral)), + }, }, Expected: azure.AgentPoolSpec{ Name: "pool1", diff --git a/azure/services/managedclusters/managedclusters.go b/azure/services/managedclusters/managedclusters.go index b771c289d710..b9662600a67a 100644 --- a/azure/services/managedclusters/managedclusters.go +++ b/azure/services/managedclusters/managedclusters.go @@ -20,6 +20,7 @@ import ( "context" "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2021-05-01/containerservice" + "github.com/Azure/go-autorest/autorest/to" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" @@ -85,13 +86,11 @@ func (s *Service) Reconcile(ctx context.Context) error { return errors.Errorf("%T is not a containerservice.ManagedCluster", result) } // Update control plane endpoint. - if managedCluster.ManagedClusterProperties != nil && managedCluster.ManagedClusterProperties.Fqdn != nil { - endpoint := clusterv1.APIEndpoint{ - Host: *managedCluster.ManagedClusterProperties.Fqdn, - Port: 443, - } - s.Scope.SetControlPlaneEndpoint(endpoint) + endpoint := clusterv1.APIEndpoint{ + Host: to.String(managedCluster.ManagedClusterProperties.Fqdn), + Port: 443, } + s.Scope.SetControlPlaneEndpoint(endpoint) // Update kubeconfig data // Always fetch credentials in case of rotation diff --git a/azure/services/managedclusters/spec.go b/azure/services/managedclusters/spec.go index 213b55dc50e7..6214c46387ef 100644 --- a/azure/services/managedclusters/spec.go +++ b/azure/services/managedclusters/spec.go @@ -159,18 +159,17 @@ type APIServerAccessProfile struct { var _ azure.ResourceSpecGetterWithHeaders = (*ManagedClusterSpec)(nil) -// ResourceName returns the name of the group. +// ResourceName returns the name of the AKS cluster. func (s *ManagedClusterSpec) ResourceName() string { return s.Name } -// ResourceGroupName returns the name of the group. -// Note that it is the same as the resource name in this case. +// ResourceGroupName returns the name of the resource group. func (s *ManagedClusterSpec) ResourceGroupName() string { return s.ResourceGroup } -// OwnerResourceName is a no-op for groups. +// OwnerResourceName is a no-op for managed clusters. func (s *ManagedClusterSpec) OwnerResourceName() string { return "" // not applicable } @@ -180,7 +179,7 @@ func (s *ManagedClusterSpec) CustomHeaders() map[string]string { return s.Headers } -// Parameters returns the parameters for the group. +// Parameters returns the parameters for the managed clusters. func (s *ManagedClusterSpec) Parameters(existing interface{}) (params interface{}, err error) { decodedSSHPublicKey, err := base64.StdEncoding.DecodeString(s.SSHPublicKey) if err != nil { diff --git a/exp/api/v1beta1/azuremanagedcontrolplane_webhook.go b/exp/api/v1beta1/azuremanagedcontrolplane_webhook.go index bbedfcb97fdc..ef1d2d1a64f4 100644 --- a/exp/api/v1beta1/azuremanagedcontrolplane_webhook.go +++ b/exp/api/v1beta1/azuremanagedcontrolplane_webhook.go @@ -29,7 +29,6 @@ import ( "k8s.io/apimachinery/pkg/runtime" kerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/apimachinery/pkg/util/validation/field" - "k8s.io/klog/v2" infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" "sigs.k8s.io/cluster-api-provider-azure/azure" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" @@ -50,7 +49,6 @@ func (m *AzureManagedControlPlane) SetupWebhookWithManager(mgr ctrl.Manager) err // Default implements webhook.Defaulter so a webhook will be registered for the type. func (m *AzureManagedControlPlane) Default(_ client.Client) { - klog.Info("defaulting AzureManagedControlPlane") if m.Spec.NetworkPlugin == nil { networkPlugin := "azure" m.Spec.NetworkPlugin = &networkPlugin diff --git a/exp/api/v1beta1/azuremanagedcontrolplane_webhook_test.go b/exp/api/v1beta1/azuremanagedcontrolplane_webhook_test.go index ebbe23d1e791..2e49570b3b55 100644 --- a/exp/api/v1beta1/azuremanagedcontrolplane_webhook_test.go +++ b/exp/api/v1beta1/azuremanagedcontrolplane_webhook_test.go @@ -23,7 +23,6 @@ import ( . "github.com/onsi/gomega" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/pointer" - "sigs.k8s.io/controller-runtime/pkg/client" ) func TestDefaultingWebhook(t *testing.T) { @@ -40,8 +39,7 @@ func TestDefaultingWebhook(t *testing.T) { Version: "1.17.5", }, } - var client client.Client - amcp.Default(client) + amcp.Default(nil) g.Expect(*amcp.Spec.NetworkPlugin).To(Equal("azure")) g.Expect(*amcp.Spec.LoadBalancerSKU).To(Equal("Standard")) g.Expect(*amcp.Spec.NetworkPolicy).To(Equal("calico")) @@ -66,7 +64,7 @@ func TestDefaultingWebhook(t *testing.T) { amcp.Spec.VirtualNetwork.Subnet.Name = "fooSubnetName" amcp.Spec.SKU.Tier = PaidManagedControlPlaneTier - amcp.Default(client) + amcp.Default(nil) g.Expect(*amcp.Spec.NetworkPlugin).To(Equal(netPlug)) g.Expect(*amcp.Spec.LoadBalancerSKU).To(Equal(lbSKU)) g.Expect(*amcp.Spec.NetworkPolicy).To(Equal(netPol)) @@ -243,11 +241,10 @@ func TestValidatingWebhook(t *testing.T) { t.Run(tt.name, func(t *testing.T) { g := NewWithT(t) t.Parallel() - var client client.Client if tt.expectErr { - g.Expect(tt.amcp.ValidateCreate(client)).NotTo(Succeed()) + g.Expect(tt.amcp.ValidateCreate(nil)).NotTo(Succeed()) } else { - g.Expect(tt.amcp.ValidateCreate(client)).To(Succeed()) + g.Expect(tt.amcp.ValidateCreate(nil)).To(Succeed()) } }) } @@ -306,8 +303,7 @@ func TestAzureManagedControlPlane_ValidateCreate(t *testing.T) { } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - var client client.Client - err := tc.amcp.ValidateCreate(client) + err := tc.amcp.ValidateCreate(nil) if tc.wantErr { g.Expect(err).To(HaveOccurred()) g.Expect(err).To(HaveLen(tc.errorLen)) @@ -729,8 +725,7 @@ func TestAzureManagedControlPlane_ValidateUpdate(t *testing.T) { } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - var client client.Client - err := tc.amcp.ValidateUpdate(tc.oldAMCP, client) + err := tc.amcp.ValidateUpdate(tc.oldAMCP, nil) if tc.wantErr { g.Expect(err).To(HaveOccurred()) } else { diff --git a/exp/controllers/azuremanagedmachinepool_controller.go b/exp/controllers/azuremanagedmachinepool_controller.go index 563ca576459f..9b5f2ad51a04 100644 --- a/exp/controllers/azuremanagedmachinepool_controller.go +++ b/exp/controllers/azuremanagedmachinepool_controller.go @@ -207,11 +207,13 @@ func (ammpr *AzureManagedMachinePoolReconciler) Reconcile(ctx context.Context, r // Create the scope. mcpScope, err := scope.NewManagedMachinePoolScope(ctx, scope.ManagedMachinePoolScopeParams{ - Client: ammpr.Client, - ControlPlane: controlPlane, - Cluster: ownerCluster, - MachinePool: ownerPool, - InfraMachinePool: infraPool, + Client: ammpr.Client, + ControlPlane: controlPlane, + Cluster: ownerCluster, + ManagedMachinePool: scope.ManagedMachinePool{ + MachinePool: ownerPool, + InfraMachinePool: infraPool, + }, ManagedControlPlaneScope: managedControlPlaneScope, }) if err != nil {