-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
340 lines (269 loc) · 9.19 KB
/
magefile.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
// +build mage
// This is a magefile, and is a "makefile for go".
// See https://magefile.org/
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"text/template"
"github.com/carolynvs/magex/mgx"
"github.com/carolynvs/magex/pkg"
"github.com/carolynvs/magex/shx"
"github.com/magefile/mage/mg"
"github.com/pkg/errors"
)
// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
const (
// Version of KIND to install if not already present
kindVersion = "v0.10.0"
// Name of the KIND cluster used for testing
kindClusterName = "porter"
// Namespace where you can do manual testing
testNamespace = "test"
// Relative location of the KUBECONFIG for the test cluster
kubeconfig = "kind.config"
// Namespace of the porter operator
operatorNamespace = "porter-operator-system"
// Container name of the local registry
registryContainer = "registry"
)
// Build a command that stops the build on if the command fails
var must = shx.CommandBuilder{StopOnError: true}
// Ensure mage is installed.
func EnsureMage() error {
addGopathBinOnGithubActions()
return pkg.EnsureMage("v1.11.0")
}
// Add GOPATH/bin to the path on the GitHub Actions agent
// TODO: Add to magex
func addGopathBinOnGithubActions() error {
githubPath := os.Getenv("GITHUB_PATH")
if githubPath == "" {
return nil
}
log.Println("Adding GOPATH/bin to the PATH for the GitHub Actions Agent")
gopathBin := pkg.GetGopathBin()
return ioutil.WriteFile(githubPath, []byte(gopathBin), 0644)
}
func Generate() {
must.RunV("controller-gen", `object:headerFile="hack/boilerplate.go.txt"`, `paths="./..."`)
}
func Fmt() {
must.RunV("go", "fmt", "./...")
}
func Vet() {
must.RunV("go", "vet", "./...")
}
// Run all tests
func Test() {
mg.Deps(TestUnit)
}
// Run unit tests.
func TestUnit() {
must.RunV("go", "test", "./...", "-coverprofile", "coverage-unit.out")
}
// Ensure operator-sdk is installed.
func EnsureOperatorSDK() {
const version = "v1.3.0"
if runtime.GOOS == "windows" {
mgx.Must(errors.New("Sorry, OperatorSDK does not support Windows. In order to contribute to this repository, you will need to use WSL."))
}
url := "https://github.com/operator-framework/operator-sdk/releases/{{.VERSION}}/download/operator-sdk_{{.GOOS}}_{{.GOARCH}}"
mgx.Must(pkg.DownloadToGopathBin(url, "operator-sdk", version))
}
// Ensure that the test KIND cluster is up.
func EnsureCluster() {
mg.Deps(EnsureKubectl)
if !useCluster() {
CreateKindCluster()
}
configureCluster()
}
// get the config of the current kind cluster, if available
func getClusterConfig() (kubeconfig string, ok bool) {
contents, err := shx.OutputE("kind", "get", "kubeconfig", "--name", kindClusterName)
return contents, err == nil
}
// setup environment to use the current kind cluster, if available
func useCluster() bool {
contents, ok := getClusterConfig()
if ok {
log.Println("Reusing existing kind cluster")
userKubeConfig, _ := filepath.Abs(os.Getenv("KUBECONFIG"))
currentKubeConfig := filepath.Join(pwd(), kubeconfig)
if userKubeConfig != currentKubeConfig {
fmt.Printf("ATTENTION! You should set your KUBECONFIG to match the cluster used by this project\n\n\texport KUBECONFIG=%s\n\n", currentKubeConfig)
}
os.Setenv("KUBECONFIG", currentKubeConfig)
err := ioutil.WriteFile(kubeconfig, []byte(contents), 0644)
mgx.Must(errors.Wrapf(err, "error writing %s", kubeconfig))
setClusterNamespace(operatorNamespace)
return true
}
return false
}
func setClusterNamespace(name string) {
must.RunE("kubectl", "config", "set-context", "--current", "--namespace", name)
}
// Create a KIND cluster named porter.
func CreateKindCluster() {
mg.Deps(EnsureKind)
// Determine host ip to populate kind config api server details
// https://kind.sigs.k8s.io/docs/user/configuration/#api-server
addrs, err := net.InterfaceAddrs()
mgx.Must(errors.Wrap(err, "could not get a list of network interfaces"))
var ipAddress string
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
fmt.Println("Current IP address : ", ipnet.IP.String())
ipAddress = ipnet.IP.String()
break
}
}
}
os.Setenv("KUBECONFIG", filepath.Join(pwd(), kubeconfig))
kindCfg, err := ioutil.ReadFile("hack/kind.config.yaml")
mgx.Must(errors.Wrap(err, "error reading hack/kind.config.yaml"))
kindCfgTmpl, err := template.New("kind.config.yaml").Parse(string(kindCfg))
mgx.Must(errors.Wrap(err, "error parsing Kind config template hack/kind.config.yaml"))
var kindCfgContents bytes.Buffer
kindCfgData := struct {
Address string
}{
Address: ipAddress,
}
err = kindCfgTmpl.Execute(&kindCfgContents, kindCfgData)
err = ioutil.WriteFile("kind.config.yaml", kindCfgContents.Bytes(), 0644)
mgx.Must(errors.Wrap(err, "could not write kind config file"))
defer os.Remove("kind.config.yaml")
must.Run("kind", "create", "cluster", "--name", kindClusterName, "--config", "kind.config.yaml")
// Connect the kind and registry containers on the same network
must.Run("docker", "network", "connect", "kind", registryContainer)
// Document the local registry
kubectl("apply", "-f", "hack/local-registry.yaml").Run()
}
func configureCluster() {
mg.Deps(StartDockerRegistry)
setClusterNamespace(operatorNamespace)
must.RunV("flux", "install")
}
// Delete the KIND cluster named porter.
func DeleteKindCluster() {
mg.Deps(EnsureKind)
must.RunE("kind", "delete", "cluster", "--name", kindClusterName)
if isOnDockerNetwork(registryContainer, "kind") {
must.RunE("docker", "network", "disconnect", "kind", registryContainer)
}
}
func isOnDockerNetwork(container string, network string) bool {
networkId, _ := shx.OutputE("docker", "network", "inspect", network, "-f", "{{.Id}}")
networks, _ := shx.OutputE("docker", "inspect", container, "-f", "{{json .NetworkSettings.Networks}}")
return strings.Contains(networks, networkId)
}
// Ensure kind is installed.
func EnsureKind() {
if ok, _ := pkg.IsCommandAvailable("kind", ""); ok {
return
}
kindURL := "https://github.com/kubernetes-sigs/kind/releases/download/{{.VERSION}}/kind-{{.GOOS}}-{{.GOARCH}}"
mgx.Must(pkg.DownloadToGopathBin(kindURL, "kind", kindVersion))
}
// Ensure kubectl is installed.
func EnsureKubectl() {
if ok, _ := pkg.IsCommandAvailable("kubectl", ""); ok {
return
}
versionURL := "https://storage.googleapis.com/kubernetes-release/release/stable.txt"
versionResp, err := http.Get(versionURL)
mgx.Must(errors.Wrapf(err, "unable to determine the latest version of kubectl"))
if versionResp.StatusCode > 299 {
mgx.Must(errors.Errorf("GET %s (%s): %s", versionURL, versionResp.StatusCode, versionResp.Status))
}
defer versionResp.Body.Close()
kubectlVersion, err := ioutil.ReadAll(versionResp.Body)
mgx.Must(errors.Wrapf(err, "error reading response from %s", versionURL))
kindURL := "https://storage.googleapis.com/kubernetes-release/release/{{.VERSION}}/bin/{{.GOOS}}/{{.GOARCH}}/kubectl{{.EXT}}"
mgx.Must(pkg.DownloadToGopathBin(kindURL, "kubectl", string(kubectlVersion)))
}
// Run a makefile target
func makefile(args ...string) shx.PreparedCommand {
cmd := must.Command("make", args...)
cmd.Env("KUBECONFIG=" + os.Getenv("KUBECONFIG"))
return cmd
}
func kubectl(args ...string) shx.PreparedCommand {
kubeconfig := fmt.Sprintf("KUBECONFIG=%s", os.Getenv("KUBECONFIG"))
return must.Command("kubectl", args...).Env(kubeconfig)
}
func kustomize(args ...string) shx.PreparedCommand {
cmd := filepath.Join(pwd(), "bin/kustomize")
return must.Command(cmd, args...)
}
// Ensure yq is installed.
func EnsureYq() {
mgx.Must(pkg.EnsurePackage("github.com/mikefarah/yq/v4", "", ""))
}
// Ensure ginkgo is installed.
func EnsureGinkgo() {
mgx.Must(pkg.EnsurePackage("github.com/onsi/ginkgo/ginkgo", "", ""))
}
// Ensure kustomize is installed.
func EnsureKustomize() {
// TODO: implement installing from a URL that is tgz
makefile("kustomize").Run()
}
func EnsureFlux() {
// TODO
}
// Ensure controller-gen is installed.
func EnsureControllerGen() {
mgx.Must(pkg.EnsurePackage("sigs.k8s.io/controller-tools/cmd/controller-gen", "v0.4.1", "--version"))
}
// Ensure that a local docker registry is running.
func StartDockerRegistry() {
if isContainerRunning(registryContainer) {
return
}
StopDockerRegistry()
fmt.Println("Starting local docker registry")
must.RunE("docker", "run", "-d", "-p", "5000:5000", "--name", registryContainer, "registry:2")
}
// Stops the local docker registry.
func StopDockerRegistry() {
if containerExists(registryContainer) {
fmt.Println("Stopping local docker registry")
removeContainer(registryContainer)
}
}
func isContainerRunning(name string) bool {
out, _ := shx.OutputS("docker", "container", "inspect", "-f", "{{.State.Running}}", name)
running, _ := strconv.ParseBool(out)
return running
}
func containerExists(name string) bool {
err := shx.RunS("docker", "inspect", name)
return err == nil
}
func removeContainer(name string) {
stderr, err := shx.OutputE("docker", "rm", "-f", name)
// Gracefully handle the container already being gone
if err != nil && !strings.Contains(stderr, "No such container") {
mgx.Must(err)
}
}
func pwd() string {
wd, _ := os.Getwd()
return wd
}