Skip to content

Commit

Permalink
Merge pull request #2167 from zmalik/zm/unify-sdk-agentpool-def
Browse files Browse the repository at this point in the history
use converters for AKS SDK agentpool definition
  • Loading branch information
k8s-ci-robot authored Mar 29, 2022
2 parents 63375d6 + 3a91d97 commit d120f2d
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 36 deletions.
72 changes: 72 additions & 0 deletions azure/converters/managedagentpool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package converters

import (
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2021-05-01/containerservice"
"github.com/Azure/go-autorest/autorest/to"
"sigs.k8s.io/cluster-api-provider-azure/azure"
)

// AgentPoolToManagedClusterAgentPoolProfile converts a AgentPoolSpec to an Azure SDK ManagedClusterAgentPoolProfile used in managedcluster reconcile.
func AgentPoolToManagedClusterAgentPoolProfile(pool azure.AgentPoolSpec) containerservice.ManagedClusterAgentPoolProfile {
return containerservice.ManagedClusterAgentPoolProfile{
Name: &pool.Name,
VMSize: &pool.SKU,
OsType: containerservice.OSTypeLinux,
OsDiskSizeGB: &pool.OSDiskSizeGB,
Count: &pool.Replicas,
Type: containerservice.AgentPoolTypeVirtualMachineScaleSets,
OrchestratorVersion: pool.Version,
VnetSubnetID: &pool.VnetSubnetID,
Mode: containerservice.AgentPoolMode(pool.Mode),
EnableAutoScaling: pool.EnableAutoScaling,
MaxCount: pool.MaxCount,
MinCount: pool.MinCount,
NodeTaints: &pool.NodeTaints,
AvailabilityZones: &pool.AvailabilityZones,
MaxPods: pool.MaxPods,
OsDiskType: containerservice.OSDiskType(to.String(pool.OsDiskType)),
NodeLabels: pool.NodeLabels,
EnableUltraSSD: pool.EnableUltraSSD,
}
}

// AgentPoolToContainerServiceAgentPool converts a AgentPoolSpec to an Azure SDK AgentPool used in agentpool reconcile.
func AgentPoolToContainerServiceAgentPool(pool azure.AgentPoolSpec) containerservice.AgentPool {
return containerservice.AgentPool{
ManagedClusterAgentPoolProfileProperties: &containerservice.ManagedClusterAgentPoolProfileProperties{
VMSize: &pool.SKU,
OsType: containerservice.OSTypeLinux,
OsDiskSizeGB: &pool.OSDiskSizeGB,
Count: &pool.Replicas,
Type: containerservice.AgentPoolTypeVirtualMachineScaleSets,
OrchestratorVersion: pool.Version,
VnetSubnetID: &pool.VnetSubnetID,
Mode: containerservice.AgentPoolMode(pool.Mode),
EnableAutoScaling: pool.EnableAutoScaling,
MaxCount: pool.MaxCount,
MinCount: pool.MinCount,
NodeTaints: &pool.NodeTaints,
AvailabilityZones: &pool.AvailabilityZones,
MaxPods: pool.MaxPods,
OsDiskType: containerservice.OSDiskType(to.String(pool.OsDiskType)),
NodeLabels: pool.NodeLabels,
EnableUltraSSD: pool.EnableUltraSSD,
},
}
}
157 changes: 157 additions & 0 deletions azure/converters/managedagentpool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package converters

import (
"testing"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2021-05-01/containerservice"
"github.com/Azure/go-autorest/autorest/to"
. "github.com/onsi/gomega"
"sigs.k8s.io/cluster-api-provider-azure/azure"
)

func Test_AgentPoolToManagedClusterAgentPoolProfile(t *testing.T) {
cases := []struct {
name string
pool azure.AgentPoolSpec
expect func(*GomegaWithT, containerservice.ManagedClusterAgentPoolProfile)
}{
{
name: "Should set all values correctly",
pool: azure.AgentPoolSpec{
SKU: "Standard_D2s_v3",
OSDiskSizeGB: 100,
Replicas: 2,
Version: to.StringPtr("1.22.6"),
VnetSubnetID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-123/providers/Microsoft.Network/virtualNetworks/vnet-123/subnets/subnet-123",
Mode: "User",
EnableAutoScaling: to.BoolPtr(true),
MaxCount: to.Int32Ptr(5),
MinCount: to.Int32Ptr(2),
NodeTaints: []string{"key1=value1:NoSchedule"},
AvailabilityZones: []string{"zone1"},
MaxPods: to.Int32Ptr(60),
OsDiskType: to.StringPtr(string(containerservice.OSDiskTypeManaged)),
NodeLabels: map[string]*string{
"custom": to.StringPtr("default"),
},
Name: "agentpool1",
},

expect: func(g *GomegaWithT, result containerservice.ManagedClusterAgentPoolProfile) {
g.Expect(result).To(Equal(containerservice.ManagedClusterAgentPoolProfile{
Name: to.StringPtr("agentpool1"),
VMSize: to.StringPtr("Standard_D2s_v3"),
OsType: containerservice.OSTypeLinux,
OsDiskSizeGB: to.Int32Ptr(100),
Count: to.Int32Ptr(2),
Type: containerservice.AgentPoolTypeVirtualMachineScaleSets,
OrchestratorVersion: to.StringPtr("1.22.6"),
VnetSubnetID: to.StringPtr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-123/providers/Microsoft.Network/virtualNetworks/vnet-123/subnets/subnet-123"),
Mode: containerservice.AgentPoolModeUser,
EnableAutoScaling: to.BoolPtr(true),
MaxCount: to.Int32Ptr(5),
MinCount: to.Int32Ptr(2),
NodeTaints: to.StringSlicePtr([]string{"key1=value1:NoSchedule"}),
AvailabilityZones: to.StringSlicePtr([]string{"zone1"}),
MaxPods: to.Int32Ptr(60),
OsDiskType: containerservice.OSDiskTypeManaged,
NodeLabels: map[string]*string{
"custom": to.StringPtr("default"),
},
}))
},
},
}

for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
result := AgentPoolToManagedClusterAgentPoolProfile(c.pool)
c.expect(g, result)
})
}
}

func Test_AgentPoolToAgentPoolToContainerServiceAgentPool(t *testing.T) {
cases := []struct {
name string
pool azure.AgentPoolSpec
expect func(*GomegaWithT, containerservice.AgentPool)
}{
{
name: "Should set all values correctly",
pool: azure.AgentPoolSpec{
Name: "agentpool1",
SKU: "Standard_D2s_v3",
OSDiskSizeGB: 100,
Replicas: 2,
Version: to.StringPtr("1.22.6"),
VnetSubnetID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-123/providers/Microsoft.Network/virtualNetworks/vnet-123/subnets/subnet-123",
Mode: "User",
EnableAutoScaling: to.BoolPtr(true),
MaxCount: to.Int32Ptr(5),
MinCount: to.Int32Ptr(2),
NodeTaints: []string{"key1=value1:NoSchedule"},
AvailabilityZones: []string{"zone1"},
MaxPods: to.Int32Ptr(60),
OsDiskType: to.StringPtr(string(containerservice.OSDiskTypeManaged)),
NodeLabels: map[string]*string{
"custom": to.StringPtr("default"),
},
},

expect: func(g *GomegaWithT, result containerservice.AgentPool) {
g.Expect(result).To(Equal(containerservice.AgentPool{
ManagedClusterAgentPoolProfileProperties: &containerservice.ManagedClusterAgentPoolProfileProperties{
VMSize: to.StringPtr("Standard_D2s_v3"),
OsType: containerservice.OSTypeLinux,
OsDiskSizeGB: to.Int32Ptr(100),
Count: to.Int32Ptr(2),
Type: containerservice.AgentPoolTypeVirtualMachineScaleSets,
OrchestratorVersion: to.StringPtr("1.22.6"),
VnetSubnetID: to.StringPtr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-123/providers/Microsoft.Network/virtualNetworks/vnet-123/subnets/subnet-123"),
Mode: containerservice.AgentPoolModeUser,
EnableAutoScaling: to.BoolPtr(true),
MaxCount: to.Int32Ptr(5),
MinCount: to.Int32Ptr(2),
NodeTaints: to.StringSlicePtr([]string{"key1=value1:NoSchedule"}),
AvailabilityZones: to.StringSlicePtr([]string{"zone1"}),
MaxPods: to.Int32Ptr(60),
OsDiskType: containerservice.OSDiskTypeManaged,
NodeLabels: map[string]*string{
"custom": to.StringPtr("default"),
},
},
}))
},
},
}

for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
result := AgentPoolToContainerServiceAgentPool(c.pool)
c.expect(g, result)
})
}
}
24 changes: 2 additions & 22 deletions azure/services/agentpools/agentpools.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2021-05-01/containerservice"
"github.com/Azure/go-autorest/autorest/to"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
infrav1alpha4 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/converters"
"sigs.k8s.io/cluster-api-provider-azure/util/maps"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)
Expand Down Expand Up @@ -73,27 +73,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
defer done()

agentPoolSpec := s.scope.AgentPoolSpec()
profile := containerservice.AgentPool{
ManagedClusterAgentPoolProfileProperties: &containerservice.ManagedClusterAgentPoolProfileProperties{
VMSize: &agentPoolSpec.SKU,
OsType: containerservice.OSTypeLinux,
OsDiskSizeGB: &agentPoolSpec.OSDiskSizeGB,
Count: &agentPoolSpec.Replicas,
Type: containerservice.AgentPoolTypeVirtualMachineScaleSets,
OrchestratorVersion: agentPoolSpec.Version,
VnetSubnetID: &agentPoolSpec.VnetSubnetID,
Mode: containerservice.AgentPoolMode(agentPoolSpec.Mode),
EnableAutoScaling: agentPoolSpec.EnableAutoScaling,
MaxCount: agentPoolSpec.MaxCount,
MinCount: agentPoolSpec.MinCount,
NodeTaints: &agentPoolSpec.NodeTaints,
AvailabilityZones: &agentPoolSpec.AvailabilityZones,
MaxPods: agentPoolSpec.MaxPods,
OsDiskType: containerservice.OSDiskType(to.String(agentPoolSpec.OsDiskType)),
NodeLabels: agentPoolSpec.NodeLabels,
EnableUltraSSD: agentPoolSpec.EnableUltraSSD,
},
}
profile := converters.AgentPoolToContainerServiceAgentPool(agentPoolSpec)

existingPool, err := s.Client.Get(ctx, agentPoolSpec.ResourceGroup, agentPoolSpec.Cluster, agentPoolSpec.Name)
if err != nil && !azure.ResourceNotFound(err) {
Expand Down
16 changes: 2 additions & 14 deletions azure/services/managedclusters/managedclusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/klog/v2"
infrav1alpha4 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/converters"
"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"
Expand Down Expand Up @@ -264,20 +265,7 @@ func (s *Service) Reconcile(ctx context.Context) error {

for i := range managedClusterSpec.AgentPools {
pool := managedClusterSpec.AgentPools[i]
profile := containerservice.ManagedClusterAgentPoolProfile{
Name: &pool.Name,
VMSize: &pool.SKU,
OsDiskSizeGB: &pool.OSDiskSizeGB,
Count: &pool.Replicas,
Type: containerservice.AgentPoolTypeVirtualMachineScaleSets,
VnetSubnetID: &managedClusterSpec.VnetSubnetID,
Mode: containerservice.AgentPoolMode(pool.Mode),
AvailabilityZones: &pool.AvailabilityZones,
MaxPods: pool.MaxPods,
OrchestratorVersion: pool.Version,
OsDiskType: containerservice.OSDiskType(to.String(pool.OsDiskType)),
EnableUltraSSD: pool.EnableUltraSSD,
}
profile := converters.AgentPoolToManagedClusterAgentPoolProfile(pool)
*managedCluster.AgentPoolProfiles = append(*managedCluster.AgentPoolProfiles, profile)
}

Expand Down

0 comments on commit d120f2d

Please sign in to comment.