Skip to content

Commit 057332f

Browse files
authored
Merge pull request #2594 from k8s-infra-cherrypick-robot/cherry-pick-2444-to-release-1.4
[release-1.4] When creating AKS clusters using autoscaler enabled, do not make an update api call to agentpool service based on difference in node count
2 parents 08c04b3 + d407486 commit 057332f

File tree

6 files changed

+81
-12
lines changed

6 files changed

+81
-12
lines changed

azure/const.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@ const (
2828
// See https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
2929
// for annotation formatting rules.
3030
RGTagsLastAppliedAnnotation = "sigs.k8s.io/cluster-api-provider-azure-last-applied-tags-rg"
31+
32+
// ReplicasManagedByAutoscalerAnnotation is set to true in the corresponding capi machine pool
33+
// when an external autoscaler manages the node count of the associated machine pool.
34+
ReplicasManagedByAutoscalerAnnotation = "cluster.x-k8s.io/replicas-managed-by-autoscaler"
3135
)

azure/scope/managedmachinepool.go

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,28 @@ func NewManagedMachinePoolScope(ctx context.Context, params ManagedMachinePoolSc
6969
return nil, errors.Wrap(err, "failed to init patch helper")
7070
}
7171

72+
capiMachinePoolPatchHelper, err := patch.NewHelper(params.MachinePool, params.Client)
73+
if err != nil {
74+
return nil, errors.Wrap(err, "failed to init patch helper")
75+
}
76+
7277
return &ManagedMachinePoolScope{
73-
Client: params.Client,
74-
Cluster: params.Cluster,
75-
ControlPlane: params.ControlPlane,
76-
MachinePool: params.MachinePool,
77-
InfraMachinePool: params.InfraMachinePool,
78-
patchHelper: helper,
79-
ManagedClusterScoper: params.ManagedControlPlaneScope,
78+
Client: params.Client,
79+
Cluster: params.Cluster,
80+
ControlPlane: params.ControlPlane,
81+
MachinePool: params.MachinePool,
82+
InfraMachinePool: params.InfraMachinePool,
83+
patchHelper: helper,
84+
capiMachinePoolPatchHelper: capiMachinePoolPatchHelper,
85+
ManagedClusterScoper: params.ManagedControlPlaneScope,
8086
}, nil
8187
}
8288

8389
// ManagedMachinePoolScope defines the basic context for an actuator to operate upon.
8490
type ManagedMachinePoolScope struct {
85-
Client client.Client
86-
patchHelper *patch.Helper
91+
Client client.Client
92+
patchHelper *patch.Helper
93+
capiMachinePoolPatchHelper *patch.Helper
8794

8895
azure.ManagedClusterScoper
8996
Cluster *clusterv1.Cluster
@@ -231,7 +238,7 @@ func (s *ManagedMachinePoolScope) UpdateDeleteStatus(condition clusterv1.Conditi
231238
}
232239
}
233240

234-
// UpdatePutStatus updates a condition on the AzureManagedControlPlane status after a PUT operation.
241+
// UpdatePutStatus updates a condition on the AzureManagedMachinePool status after a PUT operation.
235242
func (s *ManagedMachinePoolScope) UpdatePutStatus(condition clusterv1.ConditionType, service string, err error) {
236243
switch {
237244
case err == nil:
@@ -243,7 +250,7 @@ func (s *ManagedMachinePoolScope) UpdatePutStatus(condition clusterv1.ConditionT
243250
}
244251
}
245252

246-
// UpdatePatchStatus updates a condition on the AzureManagedControlPlane status after a PATCH operation.
253+
// UpdatePatchStatus updates a condition on the AzureManagedMachinePool status after a PATCH operation.
247254
func (s *ManagedMachinePoolScope) UpdatePatchStatus(condition clusterv1.ConditionType, service string, err error) {
248255
switch {
249256
case err == nil:
@@ -254,3 +261,32 @@ func (s *ManagedMachinePoolScope) UpdatePatchStatus(condition clusterv1.Conditio
254261
conditions.MarkFalse(s.InfraMachinePool, condition, infrav1.FailedReason, clusterv1.ConditionSeverityError, "%s failed to update. err: %s", service, err.Error())
255262
}
256263
}
264+
265+
// PatchCAPIMachinePoolObject persists the capi machinepool configuration and status.
266+
func (s *ManagedMachinePoolScope) PatchCAPIMachinePoolObject(ctx context.Context) error {
267+
return s.capiMachinePoolPatchHelper.Patch(
268+
ctx,
269+
s.MachinePool,
270+
)
271+
}
272+
273+
// UpdateCAPIMachinePoolReplicas updates the associated MachinePool replica count.
274+
func (s *ManagedMachinePoolScope) UpdateCAPIMachinePoolReplicas(replicas *int32) {
275+
s.MachinePool.Spec.Replicas = replicas
276+
}
277+
278+
// UpdateCAPIMachinePoolAnnotations updates the associated MachinePool annotation.
279+
func (s *ManagedMachinePoolScope) UpdateCAPIMachinePoolAnnotations(key, value string) {
280+
s.MachinePool.Annotations[key] = value
281+
}
282+
283+
// RemoveCAPIMachinePoolAnnotations removes the associated MachinePool annotation.
284+
func (s *ManagedMachinePoolScope) RemoveCAPIMachinePoolAnnotations(key string) {
285+
delete(s.MachinePool.Annotations, key)
286+
}
287+
288+
// GetCAPIMachinePoolAnnotations gets the associated MachinePool annotation.
289+
func (s *ManagedMachinePoolScope) GetCAPIMachinePoolAnnotation(key string) (bool, string) {
290+
value, ok := s.MachinePool.Annotations[key]
291+
return ok, value
292+
}

azure/services/agentpools/agentpools.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2021-05-01/containerservice"
25+
"github.com/Azure/go-autorest/autorest/to"
2526
"github.com/google/go-cmp/cmp"
2627
"github.com/pkg/errors"
2728
infrav1alpha4 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
@@ -43,6 +44,10 @@ type ManagedMachinePoolScope interface {
4344
SetAgentPoolProviderIDList([]string)
4445
SetAgentPoolReplicas(int32)
4546
SetAgentPoolReady(bool)
47+
UpdateCAPIMachinePoolReplicas(replicas *int32)
48+
UpdateCAPIMachinePoolAnnotations(key, value string)
49+
GetCAPIMachinePoolAnnotation(key string) (bool, string)
50+
RemoveCAPIMachinePoolAnnotations(key string)
4651
}
4752

4853
// Service provides operations on Azure resources.
@@ -125,6 +130,25 @@ func (s *Service) Reconcile(ctx context.Context) error {
125130
},
126131
}
127132

133+
// When autoscaling is set, the count of the nodes differ based on the autoscaler and should not depend on the
134+
// count present in MachinePool or AzureManagedMachinePool, hence we should not make an update API call based
135+
// on difference in count.
136+
if to.Bool(profile.EnableAutoScaling) && existingProfile.Count != nil {
137+
if ok, _ := s.scope.GetCAPIMachinePoolAnnotation(azure.ReplicasManagedByAutoscalerAnnotation); !ok {
138+
s.scope.UpdateCAPIMachinePoolAnnotations(azure.ReplicasManagedByAutoscalerAnnotation, "true")
139+
}
140+
141+
if to.Int32(existingProfile.Count) != to.Int32(normalizedProfile.Count) {
142+
s.scope.UpdateCAPIMachinePoolReplicas(existingProfile.Count)
143+
}
144+
normalizedProfile.Count = existingProfile.Count
145+
}
146+
147+
// set ReplicasManagedByAutoscalerAnnotation to false as it is disabled by the user.
148+
if ok, _ := s.scope.GetCAPIMachinePoolAnnotation(azure.ReplicasManagedByAutoscalerAnnotation); !to.Bool(profile.EnableAutoScaling) && ok {
149+
s.scope.RemoveCAPIMachinePoolAnnotations(azure.ReplicasManagedByAutoscalerAnnotation)
150+
}
151+
128152
// Diff and check if we require an update
129153
diff := cmp.Diff(normalizedProfile, existingProfile)
130154
if diff != "" {

config/rbac/role.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ rules:
7070
verbs:
7171
- get
7272
- list
73+
- patch
74+
- update
7375
- watch
7476
- apiGroups:
7577
- cluster.x-k8s.io

exp/controllers/azuremachinepool_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (ampr *AzureMachinePoolReconciler) SetupWithManager(ctx context.Context, mg
149149
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremachinepools/status,verbs=get;update;patch
150150
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremachinepoolmachines,verbs=get;list;watch;create;update;patch;delete
151151
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremachinepoolmachines/status,verbs=get
152-
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machinepools;machinepools/status,verbs=get;list;watch
152+
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machinepools;machinepools/status,verbs=get;list;watch;update;patch
153153
// +kubebuilder:rbac:groups="",resources=events,verbs=get;list;watch;create;update;patch
154154
// +kubebuilder:rbac:groups="",resources=secrets;,verbs=get;list;watch
155155
// +kubebuilder:rbac:groups=core,resources=nodes,verbs=get;list;watch

exp/controllers/azuremanagedmachinepool_controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ func (ammpr *AzureManagedMachinePoolReconciler) Reconcile(ctx context.Context, r
225225
if err := mcpScope.PatchObject(ctx); err != nil && reterr == nil {
226226
reterr = err
227227
}
228+
if err := mcpScope.PatchCAPIMachinePoolObject(ctx); err != nil && reterr == nil {
229+
reterr = err
230+
}
228231
}()
229232

230233
// Handle deleted clusters

0 commit comments

Comments
 (0)