sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/services/eks/eks.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 eks
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/pkg/errors"
    23  
    24  	ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/controlplane/eks/api/v1beta1"
    25  	expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/exp/api/v1beta1"
    26  	"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/awserrors"
    27  	"sigs.k8s.io/cluster-api-provider-aws/pkg/record"
    28  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    29  	"sigs.k8s.io/cluster-api/util/conditions"
    30  )
    31  
    32  // ReconcileControlPlane reconciles a EKS control plane.
    33  func (s *Service) ReconcileControlPlane(ctx context.Context) error {
    34  	s.scope.V(2).Info("Reconciling EKS control plane", "cluster-name", s.scope.Cluster.Name, "cluster-namespace", s.scope.Cluster.Namespace)
    35  
    36  	// Control Plane IAM Role
    37  	if err := s.reconcileControlPlaneIAMRole(); err != nil {
    38  		conditions.MarkFalse(s.scope.ControlPlane, ekscontrolplanev1.IAMControlPlaneRolesReadyCondition, ekscontrolplanev1.IAMControlPlaneRolesReconciliationFailedReason, clusterv1.ConditionSeverityError, err.Error())
    39  		return err
    40  	}
    41  	conditions.MarkTrue(s.scope.ControlPlane, ekscontrolplanev1.IAMControlPlaneRolesReadyCondition)
    42  
    43  	// EKS Cluster
    44  	if err := s.reconcileCluster(ctx); err != nil {
    45  		conditions.MarkFalse(s.scope.ControlPlane, ekscontrolplanev1.EKSControlPlaneReadyCondition, ekscontrolplanev1.EKSControlPlaneReconciliationFailedReason, clusterv1.ConditionSeverityError, err.Error())
    46  		return err
    47  	}
    48  	conditions.MarkTrue(s.scope.ControlPlane, ekscontrolplanev1.EKSControlPlaneReadyCondition)
    49  
    50  	// EKS Addons
    51  	if err := s.reconcileAddons(ctx); err != nil {
    52  		conditions.MarkFalse(s.scope.ControlPlane, ekscontrolplanev1.EKSAddonsConfiguredCondition, ekscontrolplanev1.EKSAddonsConfiguredFailedReason, clusterv1.ConditionSeverityError, err.Error())
    53  		return errors.Wrap(err, "failed reconciling eks addons")
    54  	}
    55  	conditions.MarkTrue(s.scope.ControlPlane, ekscontrolplanev1.EKSAddonsConfiguredCondition)
    56  
    57  	// EKS Identity Provider
    58  	if err := s.reconcileIdentityProvider(ctx); err != nil {
    59  		conditions.MarkFalse(s.scope.ControlPlane, ekscontrolplanev1.EKSIdentityProviderConfiguredCondition, ekscontrolplanev1.EKSIdentityProviderConfiguredFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
    60  		return errors.Wrap(err, "failed reconciling eks identity provider")
    61  	}
    62  	conditions.MarkTrue(s.scope.ControlPlane, ekscontrolplanev1.EKSIdentityProviderConfiguredCondition)
    63  
    64  	s.scope.V(2).Info("Reconcile EKS control plane completed successfully")
    65  	return nil
    66  }
    67  
    68  // DeleteControlPlane deletes the EKS control plane.
    69  func (s *Service) DeleteControlPlane() (err error) {
    70  	s.scope.V(2).Info("Deleting EKS control plane")
    71  
    72  	// EKS Cluster
    73  	if err := s.deleteCluster(); err != nil {
    74  		return err
    75  	}
    76  
    77  	// Control Plane IAM role
    78  	if err := s.deleteControlPlaneIAMRole(); err != nil {
    79  		return err
    80  	}
    81  
    82  	// OIDC Provider
    83  	if err := s.deleteOIDCProvider(); err != nil {
    84  		return err
    85  	}
    86  
    87  	s.scope.V(2).Info("Delete EKS control plane completed successfully")
    88  	return nil
    89  }
    90  
    91  // ReconcilePool is the entrypoint for ManagedMachinePool reconciliation.
    92  func (s *NodegroupService) ReconcilePool() error {
    93  	s.scope.V(2).Info("Reconciling EKS nodegroup")
    94  
    95  	if err := s.reconcileNodegroupIAMRole(); err != nil {
    96  		conditions.MarkFalse(
    97  			s.scope.ManagedMachinePool,
    98  			expinfrav1.IAMNodegroupRolesReadyCondition,
    99  			expinfrav1.IAMNodegroupRolesReconciliationFailedReason,
   100  			clusterv1.ConditionSeverityError,
   101  			err.Error(),
   102  		)
   103  		return err
   104  	}
   105  	conditions.MarkTrue(s.scope.ManagedMachinePool, expinfrav1.IAMNodegroupRolesReadyCondition)
   106  
   107  	if err := s.reconcileNodegroup(); err != nil {
   108  		conditions.MarkFalse(
   109  			s.scope.ManagedMachinePool,
   110  			expinfrav1.EKSNodegroupReadyCondition,
   111  			expinfrav1.EKSNodegroupReconciliationFailedReason,
   112  			clusterv1.ConditionSeverityError,
   113  			err.Error(),
   114  		)
   115  		return err
   116  	}
   117  	conditions.MarkTrue(s.scope.ManagedMachinePool, expinfrav1.EKSNodegroupReadyCondition)
   118  
   119  	return nil
   120  }
   121  
   122  // ReconcilePoolDelete is the entrypoint for ManagedMachinePool deletion
   123  // reconciliation.
   124  func (s *NodegroupService) ReconcilePoolDelete() error {
   125  	s.scope.V(2).Info("Reconciling deletion of EKS nodegroup")
   126  
   127  	eksNodegroupName := s.scope.NodegroupName()
   128  
   129  	ng, err := s.describeNodegroup()
   130  	if err != nil {
   131  		if awserrors.IsNotFound(err) {
   132  			s.scope.V(4).Info("EKS nodegroup does not exist")
   133  			return nil
   134  		}
   135  		return errors.Wrap(err, "failed to describe EKS nodegroup")
   136  	}
   137  	if ng == nil {
   138  		return nil
   139  	}
   140  
   141  	if err := s.deleteNodegroupAndWait(); err != nil {
   142  		return errors.Wrap(err, "failed to delete nodegroup")
   143  	}
   144  
   145  	if err := s.deleteNodegroupIAMRole(); err != nil {
   146  		return errors.Wrap(err, "failed to delete nodegroup IAM role")
   147  	}
   148  
   149  	record.Eventf(s.scope.ManagedMachinePool, "SuccessfulDeleteEKSNodegroup", "Deleted EKS nodegroup %s", eksNodegroupName)
   150  
   151  	return nil
   152  }