This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 139
/
eks.go
143 lines (122 loc) · 4.27 KB
/
eks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package aws
import (
"fmt"
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/eks"
versionChecker "github.com/hashicorp/go-version"
kfapis "github.com/kubeflow/kfctl/v3/pkg/apis"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"os/exec"
"path/filepath"
)
type Cluster struct {
name string
apiServerEndpoint string
oidcIssuerUrl string
clusterArn string
roleArn string
kubernetesVersion string
}
// getEksCluster obtain detail info of an eks cluster
func (aws *Aws) getEksCluster(clusterName string) (*Cluster, error) {
input := &eks.DescribeClusterInput{
Name: awssdk.String(clusterName),
}
result, err := aws.eksClient.DescribeCluster(input)
if err != nil {
return nil, err
}
cluster := &Cluster{
name: awssdk.StringValue(result.Cluster.Name),
apiServerEndpoint: awssdk.StringValue(result.Cluster.Endpoint),
oidcIssuerUrl: awssdk.StringValue(result.Cluster.Identity.Oidc.Issuer),
clusterArn: awssdk.StringValue(result.Cluster.Arn),
roleArn: awssdk.StringValue(result.Cluster.RoleArn),
kubernetesVersion: awssdk.StringValue(result.Cluster.Version),
}
return cluster, nil
}
// IsEksCluster checks if an AWS cluster is EKS cluster.
func (aws *Aws) IsEksCluster(clusterName string) (bool, error) {
input := &eks.DescribeClusterInput{
Name: awssdk.String(clusterName),
}
exist := true
if _, err := aws.eksClient.DescribeCluster(input); err != nil {
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() != eks.ErrCodeResourceNotFoundException {
return false, err
}
exist = false
}
}
return exist, nil
}
// createEKSCluster creates a new EKS cluster if want kfctl to manage cluster
// @Deprecated. In order to simplify workflow, user should always brings their own cluster and install kubeflow on top of it.
// We still leave this option here and probably remove codes in future version
func (aws *Aws) createEKSCluster() error {
awsPluginSpec, err := aws.GetPluginSpec()
if err != nil {
return err
}
if awsPluginSpec.GetManagedCluster() {
log.Infoln("Start to create eks cluster. Please wait for 10-15 mins...")
clusterConfigFile := filepath.Join(aws.kfDef.Spec.AppDir, KUBEFLOW_AWS_INFRA_DIR, CLUSTER_CONFIG_FILE)
output, err := exec.Command("eksctl", "create", "cluster", "--config-file="+clusterConfigFile).Output()
if err != nil {
return &kfapis.KfError{
Code: int(kfapis.INVALID_ARGUMENT),
Message: fmt.Sprintf("Call 'eksctl create cluster --config-file=%s' with errors: %v", clusterConfigFile, string(output)),
}
}
log.Infoln(string(output))
nodeGroupIamRoles, getRoleError := aws.getWorkerNodeGroupRoles(aws.kfDef.Name)
if getRoleError != nil {
return errors.WithStack(getRoleError)
}
aws.roles = nodeGroupIamRoles
} else {
log.Infof("You already have cluster setup. Skip creating new eks cluster. ")
}
return nil
}
// deleteEKSCluster deletes eks cluster if current cluster is a managed cluster
func (aws *Aws) deleteEKSCluster() error {
awsPluginSpec, err := aws.GetPluginSpec()
if err != nil {
return err
}
// Delete cluster if it's a managed cluster created by kfctl
if awsPluginSpec.GetManagedCluster() {
log.Infoln("Start to delete eks cluster. Please wait for 5 mins...")
clusterConfigFile := filepath.Join(aws.kfDef.Spec.AppDir, KUBEFLOW_AWS_INFRA_DIR, CLUSTER_CONFIG_FILE)
output, err := exec.Command("eksctl", "delete", "cluster", "--config-file="+clusterConfigFile).Output()
log.Infoln("Please go to aws console to check CloudFormation status and double make sure your cluster has been shutdown.")
if err != nil {
return &kfapis.KfError{
Code: int(kfapis.INVALID_ARGUMENT),
Message: fmt.Sprintf("could not call 'eksctl delete cluster --config-file=%s': %v", clusterConfigFile, string(output)),
}
}
log.Infoln(string(output))
}
return nil
}
// isEksctlVersionLessThan compare two version and return true if v1 is less than v2
func isEksctlVersionLessThan(v1, v2 string) (bool, error) {
version1, err := versionChecker.NewVersion(v1)
if err != nil {
return false, err
}
version2, err := versionChecker.NewVersion(v2)
if err != nil {
return false, err
}
if version1.LessThan(version2) {
return true, nil
}
return false, nil
}