Skip to content

Commit

Permalink
prefer CAPI replicas-managed-by annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfrancis committed Jan 10, 2023
1 parent 0f03b33 commit b8f29c0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
4 changes: 0 additions & 4 deletions azure/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,4 @@ const (
// See https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
// for annotation formatting rules.
RGTagsLastAppliedAnnotation = "sigs.k8s.io/cluster-api-provider-azure-last-applied-tags-rg"

// ReplicasManagedByAutoscalerAnnotation is set to true in the corresponding capi machine pool
// when an external autoscaler manages the node count of the associated machine pool.
ReplicasManagedByAutoscalerAnnotation = "cluster.x-k8s.io/replicas-managed-by-autoscaler"
)
3 changes: 2 additions & 1 deletion azure/scope/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"sigs.k8s.io/cluster-api/controllers/noderefutil"
capierrors "sigs.k8s.io/cluster-api/errors"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -735,7 +736,7 @@ func (m *MachinePoolScope) UpdateCAPIMachinePoolReplicas(ctx context.Context, re

// HasReplicasExternallyManaged returns true if the externally managed annotation is set on the CAPI MachinePool resource.
func (m *MachinePoolScope) HasReplicasExternallyManaged(ctx context.Context) bool {
return m.MachinePool.Annotations[azure.ReplicasManagedByAutoscalerAnnotation] == "true"
return annotations.ReplicasManagedByExternalAutoscaler(m.MachinePool)
}

// ReconcileReplicas ensures MachinePool replicas match VMSS capacity if replicas are externally managed by an autoscaler.
Expand Down
5 changes: 3 additions & 2 deletions azure/services/agentpools/agentpools.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"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/util/tele"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

const serviceName = "agentpools"
Expand Down Expand Up @@ -84,10 +85,10 @@ func (s *Service) Reconcile(ctx context.Context) error {
}
// When autoscaling is set, add the annotation to the machine pool and update the replica count.
if to.Bool(agentPool.EnableAutoScaling) {
s.scope.SetCAPIMachinePoolAnnotation(azure.ReplicasManagedByAutoscalerAnnotation, "true")
s.scope.SetCAPIMachinePoolAnnotation(clusterv1.ReplicasManagedByAnnotation, "true")
s.scope.SetCAPIMachinePoolReplicas(agentPool.Count)
} else { // Otherwise, remove the annotation.
s.scope.RemoveCAPIMachinePoolAnnotation(azure.ReplicasManagedByAutoscalerAnnotation)
s.scope.RemoveCAPIMachinePoolAnnotation(clusterv1.ReplicasManagedByAnnotation)
}
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions azure/services/agentpools/agentpools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import (
"github.com/golang/mock/gomock"
. "github.com/onsi/gomega"
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/agentpools/mock_agentpools"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/async/mock_async"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
gomockinternal "sigs.k8s.io/cluster-api-provider-azure/internal/test/matchers/gomock"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

var (
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestReconcileAgentPools(t *testing.T) {
expect: func(s *mock_agentpools.MockAgentPoolScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder) {
s.AgentPoolSpec().Return(&fakeAgentPoolSpec)
r.CreateOrUpdateResource(gomockinternal.AContext(), &fakeAgentPoolSpec, serviceName).Return(fakeAgentPoolWithAutoscalingAndCount(true, 1), nil)
s.SetCAPIMachinePoolAnnotation(azure.ReplicasManagedByAutoscalerAnnotation, "true")
s.SetCAPIMachinePoolAnnotation(clusterv1.ReplicasManagedByAnnotation, "true")
s.SetCAPIMachinePoolReplicas(fakeAgentPoolWithAutoscalingAndCount(true, 1).Count)
s.UpdatePutStatus(infrav1.AgentPoolsReadyCondition, serviceName, nil)
},
Expand All @@ -84,7 +84,7 @@ func TestReconcileAgentPools(t *testing.T) {
expect: func(s *mock_agentpools.MockAgentPoolScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder) {
s.AgentPoolSpec().Return(&fakeAgentPoolSpec)
r.CreateOrUpdateResource(gomockinternal.AContext(), &fakeAgentPoolSpec, serviceName).Return(fakeAgentPoolWithAutoscalingAndCount(false, 1), nil)
s.RemoveCAPIMachinePoolAnnotation(azure.ReplicasManagedByAutoscalerAnnotation)
s.RemoveCAPIMachinePoolAnnotation(clusterv1.ReplicasManagedByAnnotation)

s.UpdatePutStatus(infrav1.AgentPoolsReadyCondition, serviceName, nil)
},
Expand Down

0 comments on commit b8f29c0

Please sign in to comment.