Skip to content

Commit 04240c9

Browse files
authored
Merge pull request #2095 from michalno1/enable-disable-aks-cluster-addons
enable/disable AKS cluster add-ons
2 parents 37dfc68 + 40b3fa8 commit 04240c9

14 files changed

+245
-10
lines changed

azure/scope/managedcontrolplane.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,16 @@ func (s *ManagedControlPlaneScope) ManagedClusterSpec() (azure.ManagedClusterSpe
474474
}
475475
}
476476

477+
if s.ControlPlane.Spec.AddonProfiles != nil {
478+
for _, profile := range s.ControlPlane.Spec.AddonProfiles {
479+
managedClusterSpec.AddonProfiles = append(managedClusterSpec.AddonProfiles, azure.AddonProfile{
480+
Name: profile.Name,
481+
Enabled: profile.Enabled,
482+
Config: profile.Config,
483+
})
484+
}
485+
}
486+
477487
if s.ControlPlane.Spec.SKU != nil {
478488
managedClusterSpec.SKU = &azure.SKU{
479489
Tier: string(s.ControlPlane.Spec.SKU.Tier),

azure/scope/managedcontrolplane_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,101 @@ func TestManagedControlPlaneScope_Taints(t *testing.T) {
670670
}
671671
}
672672

673+
func TestManagedControlPlaneScope_AddonProfiles(t *testing.T) {
674+
scheme := runtime.NewScheme()
675+
_ = capiv1exp.AddToScheme(scheme)
676+
_ = infrav1.AddToScheme(scheme)
677+
678+
cases := []struct {
679+
Name string
680+
Input ManagedControlPlaneScopeParams
681+
Expected azure.ManagedClusterSpec
682+
}{
683+
{
684+
Name: "Without add-ons",
685+
Input: ManagedControlPlaneScopeParams{
686+
AzureClients: AzureClients{
687+
Authorizer: autorest.NullAuthorizer{},
688+
},
689+
Cluster: &clusterv1.Cluster{
690+
ObjectMeta: metav1.ObjectMeta{
691+
Name: "cluster1",
692+
Namespace: "default",
693+
},
694+
},
695+
ControlPlane: &infrav1.AzureManagedControlPlane{
696+
ObjectMeta: metav1.ObjectMeta{
697+
Name: "cluster1",
698+
Namespace: "default",
699+
},
700+
Spec: infrav1.AzureManagedControlPlaneSpec{
701+
SubscriptionID: "00000000-0000-0000-0000-000000000000",
702+
},
703+
},
704+
MachinePool: getMachinePool("pool0"),
705+
InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem),
706+
PatchTarget: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem),
707+
},
708+
Expected: azure.ManagedClusterSpec{
709+
Name: "cluster1",
710+
VnetSubnetID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups//providers/Microsoft.Network/virtualNetworks//subnets/",
711+
},
712+
},
713+
{
714+
Name: "With add-ons",
715+
Input: ManagedControlPlaneScopeParams{
716+
AzureClients: AzureClients{
717+
Authorizer: autorest.NullAuthorizer{},
718+
},
719+
Cluster: &clusterv1.Cluster{
720+
ObjectMeta: metav1.ObjectMeta{
721+
Name: "cluster1",
722+
Namespace: "default",
723+
},
724+
},
725+
ControlPlane: &infrav1.AzureManagedControlPlane{
726+
ObjectMeta: metav1.ObjectMeta{
727+
Name: "cluster1",
728+
Namespace: "default",
729+
},
730+
Spec: infrav1.AzureManagedControlPlaneSpec{
731+
SubscriptionID: "00000000-0000-0000-0000-000000000000",
732+
AddonProfiles: []infrav1.AddonProfile{
733+
{Name: "addon1", Config: nil, Enabled: false},
734+
{Name: "addon2", Config: map[string]string{"k1": "v1", "k2": "v2"}, Enabled: true},
735+
},
736+
},
737+
},
738+
MachinePool: getMachinePool("pool0"),
739+
InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem),
740+
PatchTarget: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem),
741+
},
742+
Expected: azure.ManagedClusterSpec{
743+
Name: "cluster1",
744+
VnetSubnetID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups//providers/Microsoft.Network/virtualNetworks//subnets/",
745+
AddonProfiles: []azure.AddonProfile{
746+
{Name: "addon1", Config: nil, Enabled: false},
747+
{Name: "addon2", Config: map[string]string{"k1": "v1", "k2": "v2"}, Enabled: true},
748+
},
749+
},
750+
},
751+
}
752+
753+
for _, c := range cases {
754+
c := c
755+
t.Run(c.Name, func(t *testing.T) {
756+
g := NewWithT(t)
757+
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(c.Input.MachinePool, c.Input.InfraMachinePool, c.Input.ControlPlane).Build()
758+
c.Input.Client = fakeClient
759+
s, err := NewManagedControlPlaneScope(context.TODO(), c.Input)
760+
g.Expect(err).To(Succeed())
761+
managedCluster, err := s.ManagedClusterSpec()
762+
g.Expect(err).To(Succeed())
763+
g.Expect(managedCluster).To(Equal(c.Expected))
764+
})
765+
}
766+
}
767+
673768
func getAzureMachinePool(name string, mode infrav1.NodePoolMode) *infrav1.AzureManagedMachinePool {
674769
return &infrav1.AzureManagedMachinePool{
675770
ObjectMeta: metav1.ObjectMeta{

azure/services/managedclusters/managedclusters.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,30 @@ func computeDiffOfNormalizedClusters(managedCluster containerservice.ManagedClus
9999
}
100100
}
101101

102+
if managedCluster.AddonProfiles != nil {
103+
for k, v := range managedCluster.AddonProfiles {
104+
if propertiesNormalized.AddonProfiles == nil {
105+
propertiesNormalized.AddonProfiles = map[string]*containerservice.ManagedClusterAddonProfile{}
106+
}
107+
propertiesNormalized.AddonProfiles[k] = &containerservice.ManagedClusterAddonProfile{
108+
Enabled: v.Enabled,
109+
Config: v.Config,
110+
}
111+
}
112+
}
113+
114+
if existingMC.AddonProfiles != nil {
115+
for k, v := range existingMC.AddonProfiles {
116+
if existingMCPropertiesNormalized.AddonProfiles == nil {
117+
existingMCPropertiesNormalized.AddonProfiles = map[string]*containerservice.ManagedClusterAddonProfile{}
118+
}
119+
existingMCPropertiesNormalized.AddonProfiles[k] = &containerservice.ManagedClusterAddonProfile{
120+
Enabled: v.Enabled,
121+
Config: v.Config,
122+
}
123+
}
124+
}
125+
102126
if managedCluster.NetworkProfile != nil {
103127
propertiesNormalized.NetworkProfile.LoadBalancerProfile = managedCluster.NetworkProfile.LoadBalancerProfile
104128
}
@@ -257,6 +281,8 @@ func (s *Service) Reconcile(ctx context.Context) error {
257281
}
258282
}
259283

284+
handleAddonProfiles(managedCluster, managedClusterSpec)
285+
260286
if managedClusterSpec.SKU != nil {
261287
tierName := containerservice.ManagedClusterSKUTier(managedClusterSpec.SKU.Tier)
262288
managedCluster.Sku = &containerservice.ManagedClusterSKU{
@@ -352,6 +378,22 @@ func (s *Service) Reconcile(ctx context.Context) error {
352378
return nil
353379
}
354380

381+
func handleAddonProfiles(managedCluster containerservice.ManagedCluster, spec azure.ManagedClusterSpec) {
382+
for i := range spec.AddonProfiles {
383+
if managedCluster.AddonProfiles == nil {
384+
managedCluster.AddonProfiles = map[string]*containerservice.ManagedClusterAddonProfile{}
385+
}
386+
item := spec.AddonProfiles[i]
387+
addonProfile := &containerservice.ManagedClusterAddonProfile{
388+
Enabled: &item.Enabled,
389+
}
390+
if item.Config != nil {
391+
addonProfile.Config = *to.StringMapPtr(item.Config)
392+
}
393+
managedCluster.AddonProfiles[item.Name] = addonProfile
394+
}
395+
}
396+
355397
// Delete deletes the managed cluster.
356398
func (s *Service) Delete(ctx context.Context) error {
357399
ctx, _, done := tele.StartSpanWithLogger(ctx, "managedclusters.Service.Delete")

azure/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ type ManagedClusterSpec struct {
239239
// AADProfile is Azure Active Directory configuration to integrate with AKS, for aad authentication.
240240
AADProfile *AADProfile
241241

242+
// AddonProfiles are the profiles of managed cluster add-on.
243+
AddonProfiles []AddonProfile
244+
242245
// SKU is the SKU of the AKS to be provisioned.
243246
SKU *SKU
244247

@@ -261,6 +264,13 @@ type AADProfile struct {
261264
AdminGroupObjectIDs []string
262265
}
263266

267+
// AddonProfile - Profile of managed cluster add-on.
268+
type AddonProfile struct {
269+
Name string
270+
Config map[string]string
271+
Enabled bool
272+
}
273+
264274
// SKU - AKS SKU.
265275
type SKU struct {
266276
// Tier - Tier of a managed cluster SKU.

config/crd/bases/infrastructure.cluster.x-k8s.io_azuremanagedcontrolplanes.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,26 @@ spec:
530530
resources managed by the Azure provider, in addition to the ones
531531
added by default.
532532
type: object
533+
addonProfiles:
534+
description: AddonProfiles are the profiles of managed cluster add-on.
535+
items:
536+
properties:
537+
config:
538+
additionalProperties:
539+
type: string
540+
description: Config - Key-value pairs for configuring an add-on.
541+
type: object
542+
enabled:
543+
description: Enabled - Whether the add-on is enabled or not.
544+
type: boolean
545+
name:
546+
description: Name- The name of managed cluster add-on.
547+
type: string
548+
required:
549+
- enabled
550+
- name
551+
type: object
552+
type: array
533553
apiServerAccessProfile:
534554
description: APIServerAccessProfile is the access profile for AKS
535555
API server.

exp/api/v1alpha3/azuremanagedcontrolplane_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (src *AzureManagedControlPlane) ConvertTo(dstRaw conversion.Hub) error {
4040
dst.Spec.SKU = restored.Spec.SKU
4141
dst.Spec.LoadBalancerProfile = restored.Spec.LoadBalancerProfile
4242
dst.Spec.APIServerAccessProfile = restored.Spec.APIServerAccessProfile
43+
dst.Spec.AddonProfiles = restored.Spec.AddonProfiles
4344

4445
dst.Status.LongRunningOperationStates = restored.Status.LongRunningOperationStates
4546
dst.Status.Conditions = restored.Status.Conditions

exp/api/v1alpha3/zz_generated.conversion.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exp/api/v1alpha4/azuremanagedcontrolplane_conversion.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func (src *AzureManagedControlPlane) ConvertTo(dstRaw conversion.Hub) error {
3636
return err
3737
}
3838

39+
dst.Spec.AddonProfiles = restored.Spec.AddonProfiles
3940
dst.Status.Conditions = restored.Status.Conditions
4041

4142
return nil
@@ -52,6 +53,11 @@ func (dst *AzureManagedControlPlane) ConvertFrom(srcRaw conversion.Hub) error {
5253
return utilconversion.MarshalData(src, dst)
5354
}
5455

56+
// Convert_v1beta1_AzureManagedControlPlaneSpec_To_v1alpha4_AzureManagedControlPlaneSpec is an autogenerated conversion function.
57+
func Convert_v1beta1_AzureManagedControlPlaneSpec_To_v1alpha4_AzureManagedControlPlaneSpec(in *expv1beta1.AzureManagedControlPlaneSpec, out *AzureManagedControlPlaneSpec, s apiconversion.Scope) error {
58+
return autoConvert_v1beta1_AzureManagedControlPlaneSpec_To_v1alpha4_AzureManagedControlPlaneSpec(in, out, s)
59+
}
60+
5561
// Convert_v1beta1_AzureManagedControlPlaneStatus_To_v1alpha4_AzureManagedControlPlaneStatus is an autogenerated conversion function.
5662
func Convert_v1beta1_AzureManagedControlPlaneStatus_To_v1alpha4_AzureManagedControlPlaneStatus(in *expv1beta1.AzureManagedControlPlaneStatus, out *AzureManagedControlPlaneStatus, s apiconversion.Scope) error {
5763
return autoConvert_v1beta1_AzureManagedControlPlaneStatus_To_v1alpha4_AzureManagedControlPlaneStatus(in, out, s)

exp/api/v1alpha4/zz_generated.conversion.go

Lines changed: 6 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exp/api/v1beta1/azuremanagedcontrolplane_types.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ type AzureManagedControlPlaneSpec struct {
9797
// +optional
9898
AADProfile *AADProfile `json:"aadProfile,omitempty"`
9999

100+
// AddonProfiles are the profiles of managed cluster add-on.
101+
// +optional
102+
AddonProfiles []AddonProfile `json:"addonProfiles,omitempty"`
103+
100104
// SKU is the SKU of the AKS to be provisioned.
101105
// +optional
102106
SKU *SKU `json:"sku,omitempty"`
@@ -121,6 +125,18 @@ type AADProfile struct {
121125
AdminGroupObjectIDs []string `json:"adminGroupObjectIDs"`
122126
}
123127

128+
type AddonProfile struct {
129+
// Name- The name of managed cluster add-on.
130+
Name string `json:"name"`
131+
132+
// Config - Key-value pairs for configuring an add-on.
133+
// +optional
134+
Config map[string]string `json:"config,omitempty"`
135+
136+
// Enabled - Whether the add-on is enabled or not.
137+
Enabled bool `json:"enabled"`
138+
}
139+
124140
// AzureManagedControlPlaneSkuTier - Tier of a managed cluster SKU.
125141
// +kubebuilder:validation:Enum=Free;Paid
126142
type AzureManagedControlPlaneSkuTier string

exp/api/v1beta1/zz_generated.deepcopy.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/cluster-template-aks-multi-tenancy.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ metadata:
2323
name: ${CLUSTER_NAME}
2424
namespace: default
2525
spec:
26+
addonProfiles:
27+
- enabled: true
28+
name: azurepolicy
2629
identityRef:
2730
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
2831
kind: AzureClusterIdentity

0 commit comments

Comments
 (0)