sigs.k8s.io/cluster-api-provider-azure@v1.14.3/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 asocontainerservicev1preview "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20230202preview" 23 asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001" 24 asocontainerservicev1hub "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001/storage" 25 "github.com/Azure/azure-service-operator/v2/pkg/genruntime" 26 "k8s.io/utils/ptr" 27 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 28 "sigs.k8s.io/cluster-api-provider-azure/azure" 29 "sigs.k8s.io/cluster-api-provider-azure/azure/services/aso" 30 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 31 ) 32 33 const serviceName = "agentpools" 34 35 // AgentPoolScope defines the scope interface for an agent pool. 36 type AgentPoolScope interface { 37 aso.Scope 38 39 Name() string 40 NodeResourceGroup() string 41 AgentPoolSpec() azure.ASOResourceSpecGetter[genruntime.MetaObject] 42 SetAgentPoolProviderIDList([]string) 43 SetAgentPoolReplicas(int32) 44 SetAgentPoolReady(bool) 45 SetCAPIMachinePoolReplicas(replicas *int) 46 SetCAPIMachinePoolAnnotation(key, value string) 47 RemoveCAPIMachinePoolAnnotation(key string) 48 SetSubnetName() 49 IsPreviewEnabled() bool 50 } 51 52 // New creates a new service. 53 func New(scope AgentPoolScope) *aso.Service[genruntime.MetaObject, AgentPoolScope] { 54 svc := aso.NewService[genruntime.MetaObject](serviceName, scope) 55 svc.Specs = []azure.ASOResourceSpecGetter[genruntime.MetaObject]{scope.AgentPoolSpec()} 56 svc.ConditionType = infrav1.AgentPoolsReadyCondition 57 svc.PostCreateOrUpdateResourceHook = postCreateOrUpdateResourceHook 58 return svc 59 } 60 61 func postCreateOrUpdateResourceHook(ctx context.Context, scope AgentPoolScope, obj genruntime.MetaObject, err error) error { 62 if err != nil { 63 return err 64 } 65 var existing *asocontainerservicev1.ManagedClustersAgentPool 66 if scope.IsPreviewEnabled() { 67 existingPreview := obj.(*asocontainerservicev1preview.ManagedClustersAgentPool) 68 hub := &asocontainerservicev1hub.ManagedClustersAgentPool{} 69 if err := existingPreview.ConvertTo(hub); err != nil { 70 return err 71 } 72 stable := &asocontainerservicev1.ManagedClustersAgentPool{} 73 if err := stable.ConvertFrom(hub); err != nil { 74 return err 75 } 76 existing = stable 77 } else { 78 existing = obj.(*asocontainerservicev1.ManagedClustersAgentPool) 79 } 80 agentPool := existing 81 82 // When autoscaling is set, add the annotation to the machine pool and update the replica count. 83 if ptr.Deref(agentPool.Status.EnableAutoScaling, false) { 84 scope.SetCAPIMachinePoolAnnotation(clusterv1.ReplicasManagedByAnnotation, "true") 85 scope.SetCAPIMachinePoolReplicas(agentPool.Status.Count) 86 } else { // Otherwise, remove the annotation. 87 scope.RemoveCAPIMachinePoolAnnotation(clusterv1.ReplicasManagedByAnnotation) 88 } 89 return nil 90 }