github.com/jenkins-x/jx/v2@v2.1.155/pkg/cluster/eks/clusterclient.go (about) 1 package eks 2 3 import ( 4 "fmt" 5 6 "github.com/jenkins-x/jx-logging/pkg/log" 7 8 "github.com/pkg/errors" 9 10 awsAPI "github.com/aws/aws-sdk-go/aws" 11 "github.com/jenkins-x/jx/v2/pkg/cloud/amazon" 12 "github.com/jenkins-x/jx/v2/pkg/cluster" 13 ) 14 15 // awsClusterClient that will provide functions to interact with EKS through the AWS API or the AWS CLI 16 type awsClusterClient struct { 17 amazon.Provider 18 } 19 20 // NewAWSClusterClient will return an AWSClusterClient 21 func NewAWSClusterClient() (cluster.Client, error) { 22 provider, err := amazon.NewProvider("", "") 23 if err != nil { 24 return nil, errors.Wrap(err, "error obtaining a cluster provider for AWS") 25 } 26 return &awsClusterClient{ 27 provider, 28 }, nil 29 } 30 31 // List will return a slice of every EKS cluster existing in the configured region 32 func (a awsClusterClient) List() ([]*cluster.Cluster, error) { 33 return a.EKS().ListClusters() 34 } 35 36 // ListFilter lists the clusters with the matching label filters 37 func (a awsClusterClient) ListFilter(tags map[string]string) ([]*cluster.Cluster, error) { 38 return cluster.ListFilter(a, tags) 39 } 40 41 // Connect connects to the given cluster - returning an error if the connection cannot be made 42 func (a awsClusterClient) Connect(cluster *cluster.Cluster) error { 43 return a.AWSCli().ConnectToClusterWithAWSCLI(cluster.Name) 44 } 45 46 // String returns a text representation of the client 47 func (a awsClusterClient) String() string { 48 return fmt.Sprintf("EKS Cluster client") 49 } 50 51 // SetClusterLabels adds labels to the given cluster 52 func (a awsClusterClient) SetClusterLabels(cluster *cluster.Cluster, clusterTags map[string]string) error { 53 // AWS works with Tags, should be equal 54 return a.EKS().AddTagsToCluster(cluster.Name, awsAPI.StringMap(clusterTags)) 55 } 56 57 // Get looks up a given cluster by name returning nil if its not found 58 func (a awsClusterClient) Get(clusterName string) (*cluster.Cluster, error) { 59 describedCluster, _, err := a.EKS().DescribeCluster(clusterName) 60 return describedCluster, err 61 } 62 63 // Delete should delete the given cluster using eksctl and delete any created EBS volumes to avoid extra charges 64 func (a awsClusterClient) Delete(cluster *cluster.Cluster) error { 65 log.Logger().Infof("Attempting to delete cluster %s", cluster.Name) 66 err := a.EKSCtl().DeleteCluster(cluster) 67 if err != nil { 68 return errors.Wrapf(err, "error deleting cluster %s", cluster.Name) 69 } 70 // clean up the left over EBS volumes 71 err = a.EC2().DeleteVolumesForCluster(cluster) 72 if err != nil { 73 return errors.Wrapf(err, "error deleting EC2 Volume for cluster %s", cluster.Name) 74 } 75 76 return nil 77 }