sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/scope/fargate.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 scope 18 19 import ( 20 "context" 21 22 awsclient "github.com/aws/aws-sdk-go/aws/client" 23 "github.com/go-logr/logr" 24 "github.com/pkg/errors" 25 "k8s.io/klog/v2/klogr" 26 "sigs.k8s.io/controller-runtime/pkg/client" 27 28 infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1" 29 ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/controlplane/eks/api/v1beta1" 30 expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/exp/api/v1beta1" 31 "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud" 32 "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/throttle" 33 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 34 "sigs.k8s.io/cluster-api/util/conditions" 35 "sigs.k8s.io/cluster-api/util/patch" 36 ) 37 38 // FargateProfileScopeParams defines the input parameters used to create a new Scope. 39 type FargateProfileScopeParams struct { 40 Client client.Client 41 Logger *logr.Logger 42 Cluster *clusterv1.Cluster 43 ControlPlane *ekscontrolplanev1.AWSManagedControlPlane 44 FargateProfile *expinfrav1.AWSFargateProfile 45 ControllerName string 46 Endpoints []ServiceEndpoint 47 Session awsclient.ConfigProvider 48 49 EnableIAM bool 50 } 51 52 // NewFargateProfileScope creates a new Scope from the supplied parameters. 53 // This is meant to be called for each reconcile iteration. 54 func NewFargateProfileScope(params FargateProfileScopeParams) (*FargateProfileScope, error) { 55 if params.ControlPlane == nil { 56 return nil, errors.New("failed to generate new scope from nil AWSFargateProfile") 57 } 58 if params.Logger == nil { 59 log := klogr.New() 60 params.Logger = &log 61 } 62 63 managedScope := &ManagedControlPlaneScope{ 64 Logger: *params.Logger, 65 Client: params.Client, 66 Cluster: params.Cluster, 67 ControlPlane: params.ControlPlane, 68 controllerName: params.ControllerName, 69 } 70 71 session, serviceLimiters, err := sessionForClusterWithRegion(params.Client, managedScope, params.ControlPlane.Spec.Region, params.Endpoints, *params.Logger) 72 if err != nil { 73 return nil, errors.Errorf("failed to create aws session: %v", err) 74 } 75 76 helper, err := patch.NewHelper(params.FargateProfile, params.Client) 77 if err != nil { 78 return nil, errors.Wrap(err, "failed to init patch helper") 79 } 80 81 return &FargateProfileScope{ 82 Logger: *params.Logger, 83 Client: params.Client, 84 Cluster: params.Cluster, 85 ControlPlane: params.ControlPlane, 86 FargateProfile: params.FargateProfile, 87 patchHelper: helper, 88 session: session, 89 serviceLimiters: serviceLimiters, 90 controllerName: params.ControllerName, 91 enableIAM: params.EnableIAM, 92 }, nil 93 } 94 95 // FargateProfileScope defines the basic context for an actuator to operate upon. 96 type FargateProfileScope struct { 97 logr.Logger 98 Client client.Client 99 patchHelper *patch.Helper 100 101 Cluster *clusterv1.Cluster 102 ControlPlane *ekscontrolplanev1.AWSManagedControlPlane 103 FargateProfile *expinfrav1.AWSFargateProfile 104 105 session awsclient.ConfigProvider 106 serviceLimiters throttle.ServiceLimiters 107 controllerName string 108 109 enableIAM bool 110 } 111 112 // ManagedPoolName returns the managed machine pool name. 113 func (s *FargateProfileScope) ManagedPoolName() string { 114 return s.FargateProfile.Name 115 } 116 117 // ServiceLimiter returns the AWS SDK session. Used for creating clients. 118 func (s *FargateProfileScope) ServiceLimiter(service string) *throttle.ServiceLimiter { 119 if sl, ok := s.serviceLimiters[service]; ok { 120 return sl 121 } 122 return nil 123 } 124 125 // ClusterName returns the cluster name. 126 func (s *FargateProfileScope) ClusterName() string { 127 return s.Cluster.Name 128 } 129 130 // EnableIAM indicates that reconciliation should create IAM roles. 131 func (s *FargateProfileScope) EnableIAM() bool { 132 return s.enableIAM 133 } 134 135 // AdditionalTags returns AdditionalTags from the scope's FargateProfile 136 // The returned value will never be nil. 137 func (s *FargateProfileScope) AdditionalTags() infrav1.Tags { 138 if s.FargateProfile.Spec.AdditionalTags == nil { 139 s.FargateProfile.Spec.AdditionalTags = infrav1.Tags{} 140 } 141 142 return s.FargateProfile.Spec.AdditionalTags.DeepCopy() 143 } 144 145 // RoleName returns the node group role name. 146 func (s *FargateProfileScope) RoleName() string { 147 return s.FargateProfile.Spec.RoleName 148 } 149 150 // ControlPlaneSubnets returns the control plane subnets. 151 func (s *FargateProfileScope) ControlPlaneSubnets() *infrav1.Subnets { 152 return &s.ControlPlane.Spec.NetworkSpec.Subnets 153 } 154 155 // SubnetIDs returns the machine pool subnet IDs. 156 func (s *FargateProfileScope) SubnetIDs() []string { 157 return s.FargateProfile.Spec.SubnetIDs 158 } 159 160 // IAMReadyFalse marks the ready condition false using warning if error isn't 161 // empty. 162 func (s *FargateProfileScope) IAMReadyFalse(reason string, err string) error { 163 severity := clusterv1.ConditionSeverityWarning 164 if err == "" { 165 severity = clusterv1.ConditionSeverityInfo 166 } 167 conditions.MarkFalse( 168 s.FargateProfile, 169 expinfrav1.IAMFargateRolesReadyCondition, 170 reason, 171 severity, 172 err, 173 ) 174 if err := s.PatchObject(); err != nil { 175 return errors.Wrap(err, "failed to mark role not ready") 176 } 177 return nil 178 } 179 180 // PatchObject persists the control plane configuration and status. 181 func (s *FargateProfileScope) PatchObject() error { 182 return s.patchHelper.Patch( 183 context.TODO(), 184 s.FargateProfile, 185 patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{ 186 expinfrav1.EKSFargateProfileReadyCondition, 187 expinfrav1.EKSFargateCreatingCondition, 188 expinfrav1.EKSFargateDeletingCondition, 189 expinfrav1.IAMFargateRolesReadyCondition, 190 }}) 191 } 192 193 // Close closes the current scope persisting the control plane configuration and status. 194 func (s *FargateProfileScope) Close() error { 195 return s.PatchObject() 196 } 197 198 // InfraCluster returns the AWS infrastructure cluster or control plane object. 199 func (s *FargateProfileScope) InfraCluster() cloud.ClusterObject { 200 return s.ControlPlane 201 } 202 203 // ClusterObj returns the cluster object. 204 func (s *FargateProfileScope) ClusterObj() cloud.ClusterObject { 205 return s.Cluster 206 } 207 208 // Session returns the AWS SDK session. Used for creating clients. 209 func (s *FargateProfileScope) Session() awsclient.ConfigProvider { 210 return s.session 211 } 212 213 // ControllerName returns the name of the controller that 214 // created the FargateProfile. 215 func (s *FargateProfileScope) ControllerName() string { 216 return s.controllerName 217 } 218 219 // KubernetesClusterName is the name of the EKS cluster name. 220 func (s *FargateProfileScope) KubernetesClusterName() string { 221 return s.ControlPlane.Spec.EKSClusterName 222 }