sigs.k8s.io/cluster-api-provider-azure@v1.17.0/azure/services/agentpools/agentpools.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package agentpools 18 19 import ( 20 "context" 21 22 asocontainerservicev1hub "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001/storage" 23 "github.com/Azure/azure-service-operator/v2/pkg/genruntime" 24 "k8s.io/utils/ptr" 25 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 26 "sigs.k8s.io/cluster-api-provider-azure/azure" 27 "sigs.k8s.io/cluster-api-provider-azure/azure/services/aso" 28 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 29 "sigs.k8s.io/controller-runtime/pkg/conversion" 30 ) 31 32 const serviceName = "agentpools" 33 34 // AgentPoolScope defines the scope interface for an agent pool. 35 type AgentPoolScope interface { 36 aso.Scope 37 38 Name() string 39 NodeResourceGroup() string 40 AgentPoolSpec() azure.ASOResourceSpecGetter[genruntime.MetaObject] 41 SetAgentPoolProviderIDList([]string) 42 SetAgentPoolReplicas(int32) 43 SetAgentPoolReady(bool) 44 SetCAPIMachinePoolReplicas(replicas *int) 45 SetCAPIMachinePoolAnnotation(key, value string) 46 RemoveCAPIMachinePoolAnnotation(key string) 47 SetSubnetName() 48 IsPreviewEnabled() bool 49 } 50 51 // New creates a new service. 52 func New(scope AgentPoolScope) *aso.Service[genruntime.MetaObject, AgentPoolScope] { 53 svc := aso.NewService[genruntime.MetaObject](serviceName, scope) 54 svc.Specs = []azure.ASOResourceSpecGetter[genruntime.MetaObject]{scope.AgentPoolSpec()} 55 svc.ConditionType = infrav1.AgentPoolsReadyCondition 56 svc.PostCreateOrUpdateResourceHook = postCreateOrUpdateResourceHook 57 return svc 58 } 59 60 func postCreateOrUpdateResourceHook(ctx context.Context, scope AgentPoolScope, obj genruntime.MetaObject, err error) error { 61 if err != nil { 62 return err 63 } 64 agentPool := &asocontainerservicev1hub.ManagedClustersAgentPool{} 65 if err := obj.(conversion.Convertible).ConvertTo(agentPool); err != nil { 66 return err 67 } 68 69 // When autoscaling is set, add the annotation to the machine pool and update the replica count. 70 if ptr.Deref(agentPool.Status.EnableAutoScaling, false) { 71 scope.SetCAPIMachinePoolAnnotation(clusterv1.ReplicasManagedByAnnotation, "true") 72 scope.SetCAPIMachinePoolReplicas(agentPool.Status.Count) 73 } else { // Otherwise, remove the annotation. 74 scope.RemoveCAPIMachinePoolAnnotation(clusterv1.ReplicasManagedByAnnotation) 75 } 76 return nil 77 }