-
Notifications
You must be signed in to change notification settings - Fork 41
/
cluster_test.go
347 lines (323 loc) · 10.9 KB
/
cluster_test.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package test
import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/gruntwork-io/terratest/modules/helm"
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
authv1 "k8s.io/api/authorization/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestTerraformAwsEksCluster(t *testing.T) {
t.Parallel()
environmentDir := "../examples/cluster/environment"
workingDir := "../examples/cluster"
// At the end of the test, run `terraform destroy` to clean up any resources that were created.
defer test_structure.RunTestStage(t, "cleanup_terraform", func() {
cleanupTerraform(t, workingDir)
})
test_structure.RunTestStage(t, "deploy_cluster", func() {
uniqueId := random.UniqueId()
clusterName := fmt.Sprintf("terraform-aws-eks-testing-%s", uniqueId)
deployTerraform(t, environmentDir, map[string]interface{}{})
deployTerraform(t, workingDir, map[string]interface{}{
"cluster_name": clusterName,
})
terraformOptions := test_structure.LoadTerraformOptions(t, workingDir)
clusterName = terraform.Output(t, terraformOptions, "cluster_name")
kubeconfig := writeKubeconfig(t, clusterName)
defer os.Remove(kubeconfig)
kubectlOptions := k8s.NewKubectlOptions("", kubeconfig, "default")
waitForCluster(t, kubectlOptions)
})
test_structure.RunTestStage(t, "install_karpenter", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, workingDir)
clusterName := terraform.Output(t, terraformOptions, "cluster_name")
sgName := terraform.Output(t, terraformOptions, "node_security_group_name")
kubeconfig := writeKubeconfig(t, clusterName)
defer os.Remove(kubeconfig)
installKarpenter(t, kubeconfig, clusterName, sgName)
})
test_structure.RunTestStage(t, "validate_cluster", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, workingDir)
kubeconfig := writeKubeconfig(t, terraform.Output(t, terraformOptions, "cluster_name"))
defer os.Remove(kubeconfig)
validateSecretsBehaviour(t, kubeconfig)
validateDNS(t, kubeconfig)
admin_kubeconfig := writeKubeconfig(t, terraform.Output(t, terraformOptions, "cluster_name"), terraform.Output(t, terraformOptions, "test_role_arn"))
defer os.Remove(admin_kubeconfig)
validateAdminRole(t, admin_kubeconfig)
validateKubeBench(t, kubeconfig)
validateStorage(t, kubeconfig)
})
}
func installKarpenter(t *testing.T, kubeconfig, clusterName, sgName string) {
kubectlOptions := k8s.NewKubectlOptions("", kubeconfig, "karpenter")
helmOptions := helm.Options{
KubectlOptions: kubectlOptions,
ExtraArgs: map[string][]string{
"upgrade": []string{"--create-namespace", "--version", "0.37.0", "--force"},
},
}
helm.Upgrade(t, &helmOptions, "oci://public.ecr.aws/karpenter/karpenter-crd", "karpenter-crd")
helmOptions = helm.Options{
SetValues: map[string]string{
"serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn": "arn:aws:iam::214219211678:role/Karpenter-" + clusterName,
"settings.clusterName": clusterName,
"settings.interruptionQueueName": "Karpenter-" + clusterName,
"controller.resources.requests.cpu": "1",
"controller.resources.requests.memory": "1Gi",
"controller.resources.limits.cpu": "1",
"controller.resources.limits.memory": "1Gi",
},
KubectlOptions: kubectlOptions,
ExtraArgs: map[string][]string{
"upgrade": []string{"--create-namespace", "--version", "0.37.0"},
},
}
helm.Upgrade(t, &helmOptions, "oci://public.ecr.aws/karpenter/karpenter", "karpenter")
WaitUntilPodsAvailable(t, kubectlOptions, metav1.ListOptions{LabelSelector: "app.kubernetes.io/name=karpenter"}, 2, 30, 6*time.Second)
provisionerManifest := fmt.Sprintf(KARPENTER_PROVISIONER, sgName, clusterName)
k8s.KubectlApplyFromString(t, kubectlOptions, provisionerManifest)
}
const KARPENTER_PROVISIONER = `---
apiVersion: karpenter.sh/v1beta1
kind: NodePool
metadata:
name: default
spec:
template:
spec:
nodeClassRef:
apiVersion: karpenter.k8s.aws/v1beta1
kind: EC2NodeClass
name: default
requirements:
- key: karpenter.k8s.aws/instance-family
operator: In
values: [t3]
- key: karpenter.sh/capacity-type
operator: In
values: ["spot"]
- key: karpenter.k8s.aws/instance-size
operator: In
values: [small, medium, large]
---
apiVersion: karpenter.k8s.aws/v1beta1
kind: EC2NodeClass
metadata:
name: default
spec:
amiFamily: Bottlerocket
subnetSelectorTerms:
- tags:
Name: terraform-aws-eks-test-environment-private*
securityGroupSelectorTerms:
- tags:
Name: %s
instanceProfile:
KarpenterNode-%s
`
func validateAdminRole(t *testing.T, kubeconfig string) {
kubectlOptions := k8s.NewKubectlOptions("", kubeconfig, "default")
k8s.CanIDo(t, kubectlOptions, authv1.ResourceAttributes{
Namespace: "*",
Verb: "*",
Group: "*",
Version: "*",
})
}
func validateSecretsBehaviour(t *testing.T, kubeconfig string) {
namespace := strings.ToLower(random.UniqueId())
kubectlOptions := k8s.NewKubectlOptions("", kubeconfig, namespace)
secretManifest := fmt.Sprintf(EXAMPLE_SECRET, namespace, namespace)
defer k8s.DeleteNamespace(t, kubectlOptions, namespace)
k8s.KubectlApplyFromString(t, kubectlOptions, secretManifest)
secret := k8s.GetSecret(t, kubectlOptions, "keys-to-the-kingdom")
password := secret.Data["password"]
assert.Equal(t, "Open Sesame", string(password))
}
const EXAMPLE_SECRET = `---
apiVersion: v1
kind: Namespace
metadata:
name: %s
---
apiVersion: v1
kind: Secret
metadata:
name: keys-to-the-kingdom
namespace: %s
type: Opaque
data:
password: T3BlbiBTZXNhbWU=
`
func validateDNS(t *testing.T, kubeconfig string) {
nameSuffix := strings.ToLower(random.UniqueId())
kubectlOptions := k8s.NewKubectlOptions("", kubeconfig, "default")
test := fmt.Sprintf(DNS_TEST_JOB, nameSuffix)
defer k8s.KubectlDeleteFromString(t, kubectlOptions, test)
k8s.KubectlApplyFromString(t, kubectlOptions, test)
WaitUntilPodsSucceeded(t, kubectlOptions, metav1.ListOptions{LabelSelector: "job-name=nslookup-" + nameSuffix}, 1, 30, 10*time.Second)
}
const DNS_TEST_JOB = `---
apiVersion: batch/v1
kind: Job
metadata:
name: nslookup-%s
namespace: default
spec:
template:
spec:
containers:
- name: dnsutils
image: gcr.io/kubernetes-e2e-test-images/dnsutils:1.3
command:
- nslookup
- kubernetes.default
imagePullPolicy: IfNotPresent
restartPolicy: Never
tolerations:
- key: CriticalAddonsOnly
operator: Exists
backoffLimit: 4
`
func validateStorage(t *testing.T, kubeconfig string) {
// Generate some example workload
namespace := strings.ToLower(random.UniqueId())
kubectlOptions := k8s.NewKubectlOptions("", kubeconfig, namespace)
workload := fmt.Sprintf(EXAMPLE_STORAGE_WORKLOAD, namespace, namespace, namespace)
defer k8s.DeleteNamespace(t, kubectlOptions, namespace)
k8s.KubectlApplyFromString(t, kubectlOptions, workload)
WaitUntilPodsSucceeded(t, kubectlOptions, metav1.ListOptions{LabelSelector: "app=storage-test-workload"}, 1, 30, 10*time.Second)
}
const EXAMPLE_STORAGE_WORKLOAD = `---
apiVersion: v1
kind: Namespace
metadata:
name: %s
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: ebs-claim
namespace: %s
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: batch/v1
kind: Job
metadata:
name: test-storage-workload
namespace: %s
spec:
template:
metadata:
labels:
app: storage-test-workload
spec:
restartPolicy: OnFailure
containers:
- name: app
image: alpine
command: ["/bin/sh"]
args: ["-c", "echo $(date -u) >> /data/out.txt && cat /data/out.txt"]
volumeMounts:
- name: persistent-storage
mountPath: /data
volumes:
- name: persistent-storage
persistentVolumeClaim:
claimName: ebs-claim
`
func validateKubeBench(t *testing.T, kubeconfig string) {
kubectlOptions := k8s.NewKubectlOptions("", kubeconfig, "kube-bench")
defer k8s.DeleteNamespace(t, kubectlOptions, "kube-bench")
k8s.KubectlApplyFromString(t, kubectlOptions, KUBEBENCH_MANIFEST)
WaitUntilPodsSucceeded(t, kubectlOptions, metav1.ListOptions{LabelSelector: "app=kube-bench"}, 1, 30, 5*time.Second)
output, err := k8s.RunKubectlAndGetOutputE(t, kubectlOptions, "logs", "-l", "app=kube-bench")
require.NoError(t, err)
resultWrapper := KubeBenchResult{}
err = json.Unmarshal([]byte(output), &resultWrapper)
require.NoError(t, err)
result := resultWrapper.Totals
if !assert.Equal(t, result.TotalFail, 0) {
fmt.Printf(`unexpected total_fail: %d`, result.TotalFail)
}
if !assert.Equal(t, result.TotalWarn, 0) {
fmt.Printf(`unexpected total_warn: %d`, result.TotalWarn)
}
}
type KubeBenchResult struct {
Totals KubeBenchResultTotals `json:"Totals"`
}
type KubeBenchResultTotals struct {
TotalPass int `json:"total_pass"`
TotalFail int `json:"total_fail"`
TotalWarn int `json:"total_warn"`
TotalInfo int `json:"total_info"`
}
//Skipped tests:
//3.2.8: --hostname-override is used by bottlerocket to have hostname match the dns name of the ec2 instance, this is appropriate and not a security issue
//3.2.9: eventRecordQPS is 50 by default, and can be overidden as required by users
//3.2.11: See https://github.com/bottlerocket-os/bottlerocket/issues/3506 - the test checks for the presence of RotateKubeletServerCertificate feature gate, but this is set by default since k8s 1.12 so is not needed
//3.3.1: Manual test: bottlerocket is a container-optimized OS so we pass this control
const KUBEBENCH_MANIFEST = `---
apiVersion: v1
kind: Namespace
metadata:
name: kube-bench
---
apiVersion: batch/v1
kind: Job
metadata:
name: kube-bench
namespace: kube-bench
spec:
template:
metadata:
labels:
app: kube-bench
spec:
hostPID: true
containers:
- name: kube-bench
image: aquasec/kube-bench:v0.8.0
command: ["kube-bench", "run", "--targets=node", "--benchmark", "eks-1.2.0", "--json", "--skip", "3.2.8,3.2.9,3.2.11,3.3.1"]
volumeMounts:
- name: var-lib-kubelet
mountPath: /var/lib/kubelet
readOnly: true
- name: etc-systemd
mountPath: /etc/systemd
readOnly: true
- name: etc-kubernetes
mountPath: /etc/kubernetes
readOnly: true
restartPolicy: Never
volumes:
- name: var-lib-kubelet
hostPath:
path: "/var/lib/kubelet"
- name: etc-systemd
hostPath:
path: "/etc/systemd"
- name: etc-kubernetes
hostPath:
path: "/etc/kubernetes"
tolerations:
- key: nvidia.com/gpu
operator: Exists
`