This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocal_k8s_cluster.go
190 lines (153 loc) · 5.4 KB
/
local_k8s_cluster.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
package local_k8s_cluster
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"time"
"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/cmd"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
func CreateK8sCluster(nodeName string, kubeConfigPath string, k8sImage string) {
Expect(os.Setenv("KUBECONFIG", kubeConfigPath)).To(Succeed())
provider := cluster.NewProvider(
cluster.ProviderWithLogger(cmd.NewLogger()),
)
// Check if the cluster name already exists
n, err := provider.ListNodes(nodeName)
Expect(err).NotTo(HaveOccurred())
Expect(n).To(HaveLen(0), "node(s) already exist for a cluster with the name "+nodeName)
// create the cluster
err = provider.Create(
nodeName,
cluster.CreateWithRawConfig([]byte(`kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."registry:5000"]
endpoint = ["http://registry:5000"]
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
authorization-mode: "AlwaysAllow"
extraPortMappings:
- containerPort: 80
hostPort: 80
- containerPort: 139
hostPort: 139
- containerPort: 445
hostPort: 445
- containerPort: 443
hostPort: 443`)),
cluster.CreateWithNodeImage(k8sImage),
cluster.CreateWithRetain(true),
cluster.CreateWithWaitForReady(10*time.Minute),
cluster.CreateWithKubeconfigPath(kubeConfigPath),
cluster.CreateWithDisplayUsage(true),
cluster.CreateWithDisplaySalutation(true),
)
Expect(err).NotTo(HaveOccurred())
kubeContext := "kind-" + nodeName
Kubectl("--context", kubeContext, "--kubeconfig", kubeConfigPath)
ngninxYamlTempFile, err := ioutil.TempFile("/tmp", "nginx")
Expect(err).NotTo(HaveOccurred())
_, err = io.WriteString(ngninxYamlTempFile, NGNIX_YAML)
Expect(err).NotTo(HaveOccurred())
Expect(ngninxYamlTempFile.Close()).To(Succeed())
Kubectl("apply", "-f", ngninxYamlTempFile.Name())
Kubectl("patch", "deployments", "-n", "ingress-nginx", "nginx-ingress-controller", "-p", `{"spec":{"template":{"spec":{"containers":[{"name":"nginx-ingress-controller","ports":[{"containerPort":80,"hostPort":80},{"containerPort":443,"hostPort":443}]}],"nodeSelector":{"ingress-ready":"true"},"tolerations":[{"key":"node-role.kubernetes.io/master","operator":"Equal","effect":"NoSchedule"}]}}}}`)
runLocalDockerRegistry(err, nodeName)
}
func runLocalDockerRegistry(err error, nodeName string) {
registryBashTempFile, err := ioutil.TempFile("/tmp", "test")
Expect(err).NotTo(HaveOccurred())
Expect(os.Chmod(registryBashTempFile.Name(), os.ModePerm)).To(Succeed())
_, err = io.WriteString(registryBashTempFile, SPIN_UP_LOCAL_REGISTRY_BASH)
Expect(err).NotTo(HaveOccurred())
Expect(registryBashTempFile.Close()).To(Succeed())
runTestCommand("bash", "-c", registryBashTempFile.Name()+" "+nodeName+"-control-plane")
}
func DeleteK8sCluster(nodeName string, kubeConfigPath string) {
provider := cluster.NewProvider(
cluster.ProviderWithLogger(cmd.NewLogger()),
)
finished := make(chan interface{}, 1)
go func() {
provider.Delete(nodeName, kubeConfigPath)
close(finished)
}()
timeout := time.After(10 * time.Minute)
select {
case <-finished:
return
case <-timeout:
fmt.Fprint(GinkgoWriter, "Unable to stop the kind cluster. Skipping...")
return
}
}
func KubectlStdOut(cmd ...string) string {
stdout, _ := runTestCommand("kubectl", cmd...)
return stdout
}
func Kubectl(cmd ...string) string {
stdout, stderr := runTestCommand("kubectl", cmd...)
return stdout + stderr
}
func KubectlWithStringAsStdIn(cmd ...string) func(contents string) string {
return func(contents string) string {
tempYamlFile, err := ioutil.TempFile(os.TempDir(), "temp_kapply_yaml")
Expect(err).NotTo(HaveOccurred())
_, err = tempYamlFile.WriteString(contents)
Expect(err).NotTo(HaveOccurred())
Expect(tempYamlFile.Close()).NotTo(HaveOccurred())
cmd = append(cmd, tempYamlFile.Name())
stdout, stderr := runTestCommand("kubectl", cmd...)
return stdout + stderr
}
}
func KappWithStringAsStdIn(cmd ...string) func(contents string) string {
return func(contents string) string {
tempYamlFile, err := ioutil.TempFile(os.TempDir(), "temp_kapply_yaml")
Expect(err).NotTo(HaveOccurred())
_, err = tempYamlFile.WriteString(contents)
Expect(err).NotTo(HaveOccurred())
Expect(tempYamlFile.Close()).NotTo(HaveOccurred())
cmd = append(cmd, tempYamlFile.Name())
stdout, stderr := runTestCommand("kapp", cmd...)
return stdout + stderr
}
}
func Ytt(cmd ...string) string {
stdout, stderr := runTestCommand("ytt", cmd...)
return stdout + stderr
}
func YttStdout(cmd ...string) string {
stdout, _ := runTestCommand("ytt", cmd...)
return stdout
}
func Docker(cmd ...string) string {
stdout, stderr := runTestCommand("docker", cmd...)
return stdout + stderr
}
func KbldStdout(args ...string) string {
stdout, _ := runTestCommand("kbld", args...)
return stdout
}
func runTestCommand(name string, cmds ...string) (string, string) {
command := exec.Command(name, cmds...)
fmt.Println(fmt.Sprintf("Running %v", command.Args))
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(0), string(session.Out.Contents()))
return string(session.Out.Contents()), string(session.Err.Contents())
}