Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for autoscaling in azuremanagedmachinepools #1815

Merged
merged 1 commit into from
Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions azure/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -488,7 +489,7 @@ func (s *ManagedControlPlaneScope) GetAgentPoolSpecs(ctx context.Context) ([]azu
}

ammp := azure.AgentPoolSpec{
Name: *pool.Spec.Name,
Name: to.String(pool.Spec.Name),
SKU: pool.Spec.SKU,
Replicas: 1,
OSDiskSizeGB: 0,
Expand Down Expand Up @@ -528,7 +529,7 @@ func (s *ManagedControlPlaneScope) AgentPoolSpec() azure.AgentPoolSpec {
}

agentPoolSpec := azure.AgentPoolSpec{
Name: *s.InfraMachinePool.Spec.Name,
Name: to.String(s.InfraMachinePool.Spec.Name),
ResourceGroup: s.ControlPlane.Spec.ResourceGroupName,
Cluster: s.ControlPlane.Name,
SKU: s.InfraMachinePool.Spec.SKU,
Expand All @@ -547,6 +548,12 @@ func (s *ManagedControlPlaneScope) AgentPoolSpec() azure.AgentPoolSpec {
agentPoolSpec.OSDiskSizeGB = *s.InfraMachinePool.Spec.OSDiskSizeGB
}

if s.InfraMachinePool.Spec.Scaling != nil {
agentPoolSpec.EnableAutoScaling = to.BoolPtr(true)
agentPoolSpec.MaxCount = s.InfraMachinePool.Spec.Scaling.MaxSize
agentPoolSpec.MinCount = s.InfraMachinePool.Spec.Scaling.MinSize
}

return agentPoolSpec
}

Expand Down
179 changes: 179 additions & 0 deletions azure/scope/managedcontrolplane_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
Copyright 2021 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 scope

import (
"context"
"testing"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/cluster-api-provider-azure/azure"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
capiv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestManagedControlPlaneScope_Autoscaling(t *testing.T) {
scheme := runtime.NewScheme()
_ = capiv1exp.AddToScheme(scheme)
_ = infrav1.AddToScheme(scheme)

cases := []struct {
Name string
Input ManagedControlPlaneScopeParams
Expected azure.AgentPoolSpec
}{
{
Name: "Without Autoscaling",
Input: ManagedControlPlaneScopeParams{
AzureClients: AzureClients{
Authorizer: autorest.NullAuthorizer{},
},
Cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster1",
Namespace: "default",
},
},
ControlPlane: &infrav1.AzureManagedControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster1",
Namespace: "default",
},
Spec: infrav1.AzureManagedControlPlaneSpec{
SubscriptionID: "00000000-0000-0000-0000-000000000000",
},
},
MachinePool: getMachinePool("pool0"),
InfraMachinePool: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem),
PatchTarget: getAzureMachinePool("pool0", infrav1.NodePoolModeSystem),
},
Expected: azure.AgentPoolSpec{

Name: "pool0",
SKU: "Standard_D2s_v3",
Replicas: 1,
Mode: "System",
Cluster: "cluster1",
VnetSubnetID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups//providers/Microsoft.Network/virtualNetworks//subnets/",
},
},
{
Name: "With Autoscaling",
Input: ManagedControlPlaneScopeParams{
AzureClients: AzureClients{
Authorizer: autorest.NullAuthorizer{},
},
Cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster1",
Namespace: "default",
},
},
ControlPlane: &infrav1.AzureManagedControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster1",
Namespace: "default",
},
Spec: infrav1.AzureManagedControlPlaneSpec{
SubscriptionID: "00000000-0000-0000-0000-000000000000",
},
},
MachinePool: getMachinePool("pool1"),
InfraMachinePool: getAzureMachinePoolWithScaling("pool1", 2, 10),
PatchTarget: getAzureMachinePoolWithScaling("pool1", 2, 10),
},
Expected: azure.AgentPoolSpec{
Name: "pool1",
SKU: "Standard_D2s_v3",
Mode: "User",
Cluster: "cluster1",
Replicas: 1,
EnableAutoScaling: to.BoolPtr(true),
MinCount: to.Int32Ptr(2),
MaxCount: to.Int32Ptr(10),
VnetSubnetID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups//providers/Microsoft.Network/virtualNetworks//subnets/",
},
},
}

for _, c := range cases {
c := c
t.Run(c.Name, func(t *testing.T) {
g := NewWithT(t)
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(c.Input.MachinePool, c.Input.InfraMachinePool, c.Input.ControlPlane).Build()
c.Input.Client = fakeClient
s, err := NewManagedControlPlaneScope(context.TODO(), c.Input)
g.Expect(err).To(Succeed())
agentPool := s.AgentPoolSpec()
g.Expect(agentPool).To(Equal(c.Expected))
})
}
}

func getAzureMachinePool(name string, mode infrav1.NodePoolMode) *infrav1.AzureManagedMachinePool {
return &infrav1.AzureManagedMachinePool{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "default",
Labels: map[string]string{
clusterv1.ClusterLabelName: "cluster1",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "cluster.x-k8s.io/v1beta1",
Kind: "MachinePool",
Name: name,
},
},
},
Spec: infrav1.AzureManagedMachinePoolSpec{
Mode: string(mode),
SKU: "Standard_D2s_v3",
Name: to.StringPtr(name),
},
}
}

func getAzureMachinePoolWithScaling(name string, min, max int32) *infrav1.AzureManagedMachinePool {
managedPool := getAzureMachinePool(name, infrav1.NodePoolModeUser)
managedPool.Spec.Scaling = &infrav1.ManagedMachinePoolScaling{
MinSize: to.Int32Ptr(min),
MaxSize: to.Int32Ptr(max),
}
return managedPool
}

func getMachinePool(name string) *capiv1exp.MachinePool {
return &capiv1exp.MachinePool{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "default",
Labels: map[string]string{
clusterv1.ClusterLabelName: "cluster1",
},
},
Spec: capiv1exp.MachinePoolSpec{
ClusterName: "cluster1",
},
}
}
9 changes: 9 additions & 0 deletions azure/services/agentpools/agentpools.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func (s *Service) Reconcile(ctx context.Context) error {
OrchestratorVersion: agentPoolSpec.Version,
VnetSubnetID: &agentPoolSpec.VnetSubnetID,
Mode: containerservice.AgentPoolMode(agentPoolSpec.Mode),
EnableAutoScaling: agentPoolSpec.EnableAutoScaling,
MaxCount: agentPoolSpec.MaxCount,
MinCount: agentPoolSpec.MinCount,
},
}

Expand Down Expand Up @@ -110,6 +113,9 @@ func (s *Service) Reconcile(ctx context.Context) error {
Count: existingPool.Count,
OrchestratorVersion: existingPool.OrchestratorVersion,
Mode: existingPool.Mode,
EnableAutoScaling: existingPool.EnableAutoScaling,
MinCount: existingPool.MinCount,
MaxCount: existingPool.MaxCount,
},
}

Expand All @@ -118,6 +124,9 @@ func (s *Service) Reconcile(ctx context.Context) error {
Count: profile.Count,
OrchestratorVersion: profile.OrchestratorVersion,
Mode: profile.Mode,
EnableAutoScaling: profile.EnableAutoScaling,
MinCount: profile.MinCount,
MaxCount: profile.MaxCount,
},
}

Expand Down
9 changes: 9 additions & 0 deletions azure/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,13 @@ type AgentPoolSpec struct {

// Mode represents mode of an agent pool. Possible values include: 'System', 'User'.
Mode string

// Maximum number of nodes for auto-scaling
MaxCount *int32 `json:"maxCount,omitempty"`

// Minimum number of nodes for auto-scaling
MinCount *int32 `json:"minCount,omitempty"`

// EnableAutoScaling - Whether to enable auto-scaler
EnableAutoScaling *bool `json:"enableAutoScaling,omitempty"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ spec:
items:
type: string
type: array
scaling:
description: Scaling specifies the autoscaling parameters for the
node pool.
properties:
maxSize:
format: int32
type: integer
minSize:
format: int32
type: integer
type: object
sku:
description: SKU is the size of the VMs in the node pool.
type: string
Expand Down
18 changes: 18 additions & 0 deletions docs/book/src/topics/managedcluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,24 @@ spec:
- 917056a9-8eb5-439c-g679-b34901ade75h # fake admin groupId
```

### AKS Cluster Autoscaler

Azure Kubernetes Service can be configured to use cluster autoscaler by specifying `scaling` spec in the `AzureManagedMachinePool`

```
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: AzureManagedMachinePool
metadata:
name: agentpool0
spec:
mode: System
osDiskSizeGB: 512
sku: Standard_D2s_v3
scaling:
minSize: 2
maxSize: 10
```

### Use a public Standard Load Balancer

A public Load Balancer when integrated with AKS serves two purposes:
Expand Down
1 change: 1 addition & 0 deletions exp/api/v1alpha3/azuremanagedmachinepool_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (src *AzureManagedMachinePool) ConvertTo(dstRaw conversion.Hub) error { //
}

dst.Spec.Name = restored.Spec.Name
dst.Spec.Scaling = restored.Spec.Scaling

return nil
}
Expand Down
1 change: 1 addition & 0 deletions exp/api/v1alpha3/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 28 additions & 3 deletions exp/api/v1alpha4/azuremanagedmachinepool_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,45 @@ limitations under the License.
package v1alpha4

import (
apiconversion "k8s.io/apimachinery/pkg/conversion"
expv1beta1 "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

// ConvertTo converts this AzureManagedMachinePool to the Hub version (v1beta1).
func (src *AzureManagedMachinePool) ConvertTo(dstRaw conversion.Hub) error { // nolint
func (src *AzureManagedMachinePool) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*expv1beta1.AzureManagedMachinePool)

if err := Convert_v1alpha4_AzureManagedMachinePool_To_v1beta1_AzureManagedMachinePool(src, dst, nil); err != nil {
return err
}

// Manually restore data.
restored := &expv1beta1.AzureManagedMachinePool{}
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
return err
}
dst.Spec.Scaling = restored.Spec.Scaling
return Convert_v1alpha4_AzureManagedMachinePool_To_v1beta1_AzureManagedMachinePool(src, dst, nil)
}

// ConvertFrom converts from the Hub version (v1beta1) to this version.
func (dst *AzureManagedMachinePool) ConvertFrom(srcRaw conversion.Hub) error { // nolint
func (dst *AzureManagedMachinePool) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*expv1beta1.AzureManagedMachinePool)

return Convert_v1beta1_AzureManagedMachinePool_To_v1alpha4_AzureManagedMachinePool(src, dst, nil)
if err := Convert_v1beta1_AzureManagedMachinePool_To_v1alpha4_AzureManagedMachinePool(src, dst, nil); err != nil {
return err
}

// Preserve Hub data on down-conversion.
if err := utilconversion.MarshalData(src, dst); err != nil {
return err
}

return nil
}

func Convert_v1beta1_AzureManagedMachinePoolSpec_To_v1alpha4_AzureManagedMachinePoolSpec(in *expv1beta1.AzureManagedMachinePoolSpec, out *AzureManagedMachinePoolSpec, s apiconversion.Scope) error {
return autoConvert_v1beta1_AzureManagedMachinePoolSpec_To_v1alpha4_AzureManagedMachinePoolSpec(in, out, s)
}
Loading