-
Notifications
You must be signed in to change notification settings - Fork 79
/
ng.go
200 lines (181 loc) · 5.49 KB
/
ng.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Package ng implements EKS worker nodes with a custom AMI.
package ng
import (
"errors"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/aws/aws-k8s-tester/eksconfig"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go/service/eks/eksiface"
"github.com/aws/aws-sdk-go/service/iam/iamiface"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
)
// Config defines Node Group configuration.
type Config struct {
Logger *zap.Logger
Stopc chan struct{}
Sig chan os.Signal
EKSConfig *eksconfig.Config
K8SClient k8sClientSetGetter
IAMAPI iamiface.IAMAPI
CFNAPI cloudformationiface.CloudFormationAPI
EC2API ec2iface.EC2API
ASGAPI autoscalingiface.AutoScalingAPI
EKSAPI eksiface.EKSAPI
}
type k8sClientSetGetter interface {
KubernetesClientSet() *clientset.Clientset
}
// Tester implements EKS "Node Group" for "kubetest2" Deployer.
// ref. https://github.com/kubernetes/test-infra/blob/master/kubetest2/pkg/types/types.go
// ref. https://docs.aws.amazon.com/eks/latest/userguide/create-managed-node-group.html
// ref. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eks-nodegroup.html
type Tester interface {
// Create creates EKS "Node Group", and waits for completion.
Create() error
// Delete deletes all EKS "Node Group" resources.
Delete() error
// FetchLogs fetches logs from all worker nodes.
FetchLogs() error
// DownloadClusterLogs dumps all logs to artifact directory.
// Let default kubetest log dumper handle all artifact uploads.
// See https://github.com/kubernetes/test-infra/pull/9811/files#r225776067.
DownloadClusterLogs(artifactDir string) error
}
// New creates a new Job tester.
func New(cfg Config) (Tester, error) {
return &tester{
cfg: cfg,
logsMu: new(sync.RWMutex),
}, nil
}
type tester struct {
cfg Config
logsMu *sync.RWMutex
failedOnce bool
}
func (ts *tester) Create() (err error) {
if !ts.cfg.EKSConfig.IsEnabledAddOnNodeGroups() {
return nil
}
if ts.cfg.EKSConfig.AddOnNodeGroups.Created {
ts.cfg.Logger.Info("ManagedNodeGroup is already created; skipping creation")
return nil
}
if len(ts.cfg.EKSConfig.Parameters.PublicSubnetIDs) == 0 {
return errors.New("empty EKSConfig.Parameters.PublicSubnetIDs")
}
defer func() {
ts.cfg.EKSConfig.AddOnNodeGroups.Created = true
ts.cfg.EKSConfig.Sync()
}()
if err = ts.createRole(); err != nil {
return err
}
if err = ts.createSG(); err != nil {
return err
}
if err = ts.createASGs(); err != nil {
return err
}
if err = ts.createConfigMap(); err != nil {
return err
}
if err = ts.waitForNodes(); err != nil {
return err
}
return ts.cfg.EKSConfig.Sync()
}
func (ts *tester) waitForNodes() error {
waitDur := 5 * time.Minute
var items []v1.Node
ts.cfg.Logger.Info("checking nodes via client-go")
for _, mv := range ts.cfg.EKSConfig.AddOnNodeGroups.ASGs {
retryStart, threshold := time.Now(), 3*time.Minute
for time.Now().Sub(retryStart) < waitDur {
select {
case <-ts.cfg.Stopc:
return errors.New("checking nodes aborted")
case <-ts.cfg.Sig:
return errors.New("checking nodes aborted")
case <-time.After(5 * time.Second):
}
nodes, err := ts.cfg.K8SClient.KubernetesClientSet().CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
ts.cfg.Logger.Error("get nodes failed", zap.Error(err))
continue
}
items = nodes.Items
readies := int64(0)
for _, node := range items {
for _, cond := range node.Status.Conditions {
if cond.Type != v1.NodeReady {
continue
}
ts.cfg.Logger.Info("node info",
zap.String("name", node.GetName()),
zap.String("type", fmt.Sprintf("%s", cond.Type)),
zap.String("status", fmt.Sprintf("%s", cond.Status)),
)
if cond.Status == v1.ConditionTrue {
readies++
}
}
}
ts.cfg.Logger.Info("nodes",
zap.Int64("current-ready-nodes", readies),
zap.Int64("desired-ready-nodes", mv.ASGDesiredCapacity),
)
if readies >= mv.ASGDesiredCapacity {
break
}
took := time.Now().Sub(retryStart)
if took > threshold {
fmt.Printf("\n\nkubectl (%q, %q)\n\n", ts.cfg.EKSConfig.ConfigPath, ts.cfg.EKSConfig.KubectlCommand())
fmt.Println(ts.cfg.EKSConfig.KubectlCommands())
fmt.Printf("\n\nSSH (%q, %q)\n\n", ts.cfg.EKSConfig.ConfigPath, ts.cfg.EKSConfig.KubectlCommand())
fmt.Println(ts.cfg.EKSConfig.SSHCommands())
}
}
}
println()
for _, v := range items {
fmt.Printf("'Node' %q (using client-go): %+v\n", v.GetName(), v.Status.Addresses)
}
println()
return ts.cfg.EKSConfig.Sync()
}
func (ts *tester) Delete() error {
if !ts.cfg.EKSConfig.IsEnabledAddOnNodeGroups() {
return nil
}
if !ts.cfg.EKSConfig.AddOnNodeGroups.Created {
ts.cfg.Logger.Info("ManagedNodeGroup is not created; skipping deletion")
return nil
}
var errs []string
if err := ts.deleteASGs(); err != nil {
ts.cfg.Logger.Warn("failed to delete ASGs", zap.Error(err))
errs = append(errs, err.Error())
}
if err := ts.deleteSG(); err != nil {
ts.cfg.Logger.Warn("failed to delete SG", zap.Error(err))
errs = append(errs, err.Error())
}
if err := ts.deleteRole(); err != nil {
ts.cfg.Logger.Warn("failed to delete role", zap.Error(err))
errs = append(errs, err.Error())
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, ", "))
}
return ts.cfg.EKSConfig.Sync()
}