Skip to content

Commit

Permalink
Convert managedclusters and agentpools services to SDKv2
Browse files Browse the repository at this point in the history
  • Loading branch information
mboersma committed Aug 24, 2023
1 parent 0eb80e1 commit 566747b
Show file tree
Hide file tree
Showing 20 changed files with 563 additions and 605 deletions.
14 changes: 7 additions & 7 deletions api/v1beta1/azuremanagedmachinepool_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"testing"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-03-01/containerservice"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestAzureManagedMachinePoolUpdatingWebhook(t *testing.T) {
SKU: "StandardD2S_V3",
OSDiskSizeGB: ptr.To[int32](512),
MaxPods: ptr.To[int32](24),
OsDiskType: ptr.To(string(containerservice.OSDiskTypeEphemeral)),
OsDiskType: ptr.To(string(armcontainerservice.OSDiskTypeEphemeral)),
},
},
old: &AzureManagedMachinePool{
Expand All @@ -298,7 +298,7 @@ func TestAzureManagedMachinePoolUpdatingWebhook(t *testing.T) {
SKU: "StandardD2S_V3",
OSDiskSizeGB: ptr.To[int32](512),
MaxPods: ptr.To[int32](24),
OsDiskType: ptr.To(string(containerservice.OSDiskTypeManaged)),
OsDiskType: ptr.To(string(armcontainerservice.OSDiskTypeManaged)),
},
},
wantErr: true,
Expand Down Expand Up @@ -392,7 +392,7 @@ func TestAzureManagedMachinePoolUpdatingWebhook(t *testing.T) {
SKU: "StandardD2S_V3",
OSDiskSizeGB: ptr.To[int32](512),
MaxPods: ptr.To[int32](30),
OsDiskType: ptr.To(string(containerservice.OSDiskTypeManaged)),
OsDiskType: ptr.To(string(armcontainerservice.OSDiskTypeManaged)),
},
},
old: &AzureManagedMachinePool{
Expand All @@ -401,7 +401,7 @@ func TestAzureManagedMachinePoolUpdatingWebhook(t *testing.T) {
SKU: "StandardD2S_V3",
OSDiskSizeGB: ptr.To[int32](512),
MaxPods: ptr.To[int32](30),
OsDiskType: ptr.To(string(containerservice.OSDiskTypeManaged)),
OsDiskType: ptr.To(string(armcontainerservice.OSDiskTypeManaged)),
},
},
wantErr: false,
Expand Down Expand Up @@ -638,7 +638,7 @@ func TestAzureManagedMachinePool_ValidateCreate(t *testing.T) {
ammp: &AzureManagedMachinePool{
Spec: AzureManagedMachinePoolSpec{
MaxPods: ptr.To[int32](249),
OsDiskType: ptr.To(string(containerservice.OSDiskTypeManaged)),
OsDiskType: ptr.To(string(armcontainerservice.OSDiskTypeManaged)),
},
},
wantErr: false,
Expand Down Expand Up @@ -1292,7 +1292,7 @@ func getKnownValidAzureManagedMachinePool() *AzureManagedMachinePool {
return &AzureManagedMachinePool{
Spec: AzureManagedMachinePoolSpec{
MaxPods: ptr.To[int32](30),
OsDiskType: ptr.To(string(containerservice.OSDiskTypeEphemeral)),
OsDiskType: ptr.To(string(armcontainerservice.OSDiskTypeEphemeral)),
},
}
}
Expand Down
14 changes: 7 additions & 7 deletions azure/converters/managedagentpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ limitations under the License.
package converters

import (
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-03-01/containerservice"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4"
)

// AgentPoolToManagedClusterAgentPoolProfile converts a AgentPoolSpec to an Azure SDK ManagedClusterAgentPoolProfile used in managedcluster reconcile.
func AgentPoolToManagedClusterAgentPoolProfile(pool containerservice.AgentPool) containerservice.ManagedClusterAgentPoolProfile {
properties := pool.ManagedClusterAgentPoolProfileProperties
agentPool := containerservice.ManagedClusterAgentPoolProfile{
func AgentPoolToManagedClusterAgentPoolProfile(pool armcontainerservice.AgentPool) armcontainerservice.ManagedClusterAgentPoolProfile {
properties := pool.Properties
agentPool := armcontainerservice.ManagedClusterAgentPoolProfile{
Name: pool.Name, // Note: if converting from agentPoolSpec.Parameters(), this field will not be set
VMSize: properties.VMSize,
OsType: properties.OsType,
OsDiskSizeGB: properties.OsDiskSizeGB,
OSType: properties.OSType,
OSDiskSizeGB: properties.OSDiskSizeGB,
Count: properties.Count,
Type: properties.Type,
OrchestratorVersion: properties.OrchestratorVersion,
Expand All @@ -39,7 +39,7 @@ func AgentPoolToManagedClusterAgentPoolProfile(pool containerservice.AgentPool)
NodeTaints: properties.NodeTaints,
AvailabilityZones: properties.AvailabilityZones,
MaxPods: properties.MaxPods,
OsDiskType: properties.OsDiskType,
OSDiskType: properties.OSDiskType,
NodeLabels: properties.NodeLabels,
EnableUltraSSD: properties.EnableUltraSSD,
EnableNodePublicIP: properties.EnableNodePublicIP,
Expand Down
43 changes: 21 additions & 22 deletions azure/converters/managedagentpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,37 @@ package converters
import (
"testing"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-03-01/containerservice"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4"
. "github.com/onsi/gomega"
"k8s.io/utils/ptr"
"sigs.k8s.io/cluster-api-provider-azure/azure"
)

func Test_AgentPoolToManagedClusterAgentPoolProfile(t *testing.T) {
cases := []struct {
name string
pool containerservice.AgentPool
expect func(*GomegaWithT, containerservice.ManagedClusterAgentPoolProfile)
pool armcontainerservice.AgentPool
expect func(*GomegaWithT, armcontainerservice.ManagedClusterAgentPoolProfile)
}{
{
name: "Should set all values correctly",
pool: containerservice.AgentPool{
pool: armcontainerservice.AgentPool{
Name: ptr.To("agentpool1"),
ManagedClusterAgentPoolProfileProperties: &containerservice.ManagedClusterAgentPoolProfileProperties{
Properties: &armcontainerservice.ManagedClusterAgentPoolProfileProperties{
VMSize: ptr.To("Standard_D2s_v3"),
OsType: azure.LinuxOS,
OsDiskSizeGB: ptr.To[int32](100),
OSType: ptr.To(armcontainerservice.OSTypeLinux),
OSDiskSizeGB: ptr.To[int32](100),
Count: ptr.To[int32](2),
Type: containerservice.AgentPoolTypeVirtualMachineScaleSets,
Type: ptr.To(armcontainerservice.AgentPoolTypeVirtualMachineScaleSets),
OrchestratorVersion: ptr.To("1.22.6"),
VnetSubnetID: ptr.To("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-123/providers/Microsoft.Network/virtualNetworks/vnet-123/subnets/subnet-123"),
Mode: containerservice.AgentPoolModeUser,
Mode: ptr.To(armcontainerservice.AgentPoolModeUser),
EnableAutoScaling: ptr.To(true),
MaxCount: ptr.To[int32](5),
MinCount: ptr.To[int32](2),
NodeTaints: &[]string{"key1=value1:NoSchedule"},
AvailabilityZones: &[]string{"zone1"},
NodeTaints: []*string{ptr.To("key1=value1:NoSchedule")},
AvailabilityZones: []*string{ptr.To("zone1")},
MaxPods: ptr.To[int32](60),
OsDiskType: containerservice.OSDiskTypeManaged,
OSDiskType: ptr.To(armcontainerservice.OSDiskTypeManaged),
NodeLabels: map[string]*string{
"custom": ptr.To("default"),
},
Expand All @@ -61,24 +60,24 @@ func Test_AgentPoolToManagedClusterAgentPoolProfile(t *testing.T) {
},
},

expect: func(g *GomegaWithT, result containerservice.ManagedClusterAgentPoolProfile) {
g.Expect(result).To(Equal(containerservice.ManagedClusterAgentPoolProfile{
expect: func(g *GomegaWithT, result armcontainerservice.ManagedClusterAgentPoolProfile) {
g.Expect(result).To(Equal(armcontainerservice.ManagedClusterAgentPoolProfile{
Name: ptr.To("agentpool1"),
VMSize: ptr.To("Standard_D2s_v3"),
OsType: azure.LinuxOS,
OsDiskSizeGB: ptr.To[int32](100),
OSType: ptr.To(armcontainerservice.OSTypeLinux),
OSDiskSizeGB: ptr.To[int32](100),
Count: ptr.To[int32](2),
Type: containerservice.AgentPoolTypeVirtualMachineScaleSets,
Type: ptr.To(armcontainerservice.AgentPoolTypeVirtualMachineScaleSets),
OrchestratorVersion: ptr.To("1.22.6"),
VnetSubnetID: ptr.To("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-123/providers/Microsoft.Network/virtualNetworks/vnet-123/subnets/subnet-123"),
Mode: containerservice.AgentPoolModeUser,
Mode: ptr.To(armcontainerservice.AgentPoolModeUser),
EnableAutoScaling: ptr.To(true),
MaxCount: ptr.To[int32](5),
MinCount: ptr.To[int32](2),
NodeTaints: &[]string{"key1=value1:NoSchedule"},
AvailabilityZones: &[]string{"zone1"},
NodeTaints: []*string{ptr.To("key1=value1:NoSchedule")},
AvailabilityZones: []*string{ptr.To("zone1")},
MaxPods: ptr.To[int32](60),
OsDiskType: containerservice.OSDiskTypeManaged,
OSDiskType: ptr.To(armcontainerservice.OSDiskTypeManaged),
NodeLabels: map[string]*string{
"custom": ptr.To("default"),
},
Expand Down
6 changes: 3 additions & 3 deletions azure/scope/managedmachinepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"reflect"
"testing"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-03-01/containerservice"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4"
"github.com/google/go-cmp/cmp"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -591,7 +591,7 @@ func TestManagedMachinePoolScope_OSDiskType(t *testing.T) {
},
ManagedMachinePool: ManagedMachinePool{
MachinePool: getMachinePool("pool1"),
InfraMachinePool: getAzureMachinePoolWithOsDiskType("pool1", string(containerservice.OSDiskTypeEphemeral)),
InfraMachinePool: getAzureMachinePoolWithOsDiskType("pool1", string(armcontainerservice.OSDiskTypeEphemeral)),
},
},
Expected: &agentpools.AgentPoolSpec{
Expand All @@ -600,7 +600,7 @@ func TestManagedMachinePoolScope_OSDiskType(t *testing.T) {
Mode: "User",
Cluster: "cluster1",
Replicas: 1,
OsDiskType: ptr.To(string(containerservice.OSDiskTypeEphemeral)),
OsDiskType: ptr.To(string(armcontainerservice.OSDiskTypeEphemeral)),
VnetSubnetID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups//providers/Microsoft.Network/virtualNetworks//subnets/",
Headers: map[string]string{},
},
Expand Down
28 changes: 16 additions & 12 deletions azure/services/agentpools/agentpools.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ package agentpools
import (
"context"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-03-01/containerservice"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4"
"github.com/pkg/errors"
"k8s.io/utils/ptr"
infrav1 "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/services/async"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/asyncpoller"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)
Expand Down Expand Up @@ -52,16 +52,20 @@ type AgentPoolScope interface {
// Service provides operations on Azure resources.
type Service struct {
scope AgentPoolScope
async.Reconciler
asyncpoller.Reconciler
}

// New creates a new service.
func New(scope AgentPoolScope) *Service {
client := newClient(scope)
return &Service{
scope: scope,
Reconciler: async.New(scope, client, client),
func New(scope AgentPoolScope) (*Service, error) {
client, err := newClient(scope)
if err != nil {
return nil, err
}
return &Service{
scope: scope,
Reconciler: asyncpoller.New[armcontainerservice.AgentPoolsClientCreateOrUpdateResponse,
armcontainerservice.AgentPoolsClientDeleteResponse](scope, client, client),
}, nil
}

// Name returns the service name.
Expand All @@ -80,14 +84,14 @@ func (s *Service) Reconcile(ctx context.Context) error {
if err != nil {
resultingErr = err
} else {
agentPool, ok := result.(containerservice.AgentPool)
agentPool, ok := result.(armcontainerservice.AgentPool)
if !ok {
return errors.Errorf("%T is not a containerservice.AgentPool", result)
return errors.Errorf("%T is not an armcontainerservice.AgentPool", result)
}
// When autoscaling is set, add the annotation to the machine pool and update the replica count.
if ptr.Deref(agentPool.EnableAutoScaling, false) {
if ptr.Deref(agentPool.Properties.EnableAutoScaling, false) {
s.scope.SetCAPIMachinePoolAnnotation(clusterv1.ReplicasManagedByAnnotation, "true")
s.scope.SetCAPIMachinePoolReplicas(agentPool.Count)
s.scope.SetCAPIMachinePoolReplicas(agentPool.Properties.Count)
} else { // Otherwise, remove the annotation.
s.scope.RemoveCAPIMachinePoolAnnotation(clusterv1.ReplicasManagedByAnnotation)
}
Expand Down
Loading

0 comments on commit 566747b

Please sign in to comment.