-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubebuilder.go
168 lines (136 loc) · 4.41 KB
/
kubebuilder.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
package mage
import (
"context"
"fmt"
"path/filepath"
"github.com/dosquad/mage/helper"
"github.com/magefile/mage/mg"
"github.com/na4ma4/go-permbits"
"github.com/princjef/mageutil/shellcmd"
)
// Kubebuilder namespace is defined to group Kubebuilder functions.
type Kubebuilder mg.Namespace
// Manifests Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
func (Kubebuilder) Manifests(_ context.Context) error {
_ = helper.BinKubeControllerGen().Ensure()
return helper.BinKubeControllerGen().Command(
`rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases`,
).Run()
}
// Generate Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
func (Kubebuilder) Generate(_ context.Context) error {
_ = helper.BinKubeControllerGen().Ensure()
return helper.BinKubeControllerGen().Command(`object:headerFile="hack/boilerplate.go.txt" paths="./..."`).Run()
}
func kustomizeBuildCommand(cmd, filename string) (string, error) {
out, err := helper.Command(string(helper.BinKustomize().Command(cmd)))
if err != nil {
return "", err
}
k8sPath := helper.MustGetArtifactPath("k8s")
helper.MustMakeDir(
k8sPath,
permbits.MustString("u=rwx,go=rx"),
)
return filepath.Join(k8sPath, filename), helper.FileWrite(
out,
filepath.Join(k8sPath, filename),
)
}
// Install CRDs into the K8s cluster specified in ~/.kube/config.
func (Kubebuilder) Install(ctx context.Context) error {
mg.CtxDeps(ctx, Kubebuilder.Manifests)
var crdManifest string
{
var err error
if crdManifest, err = kustomizeBuildCommand("build config/crd", "crd.yaml"); err != nil {
return err
}
}
if err := shellcmd.Command(fmt.Sprintf(`kubectl apply -f "%s"`, crdManifest)).Run(); err != nil {
return err
}
return nil
}
// Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
// Call with ignore-not-found=true to ignore resource not found errors during deletion.
func (Kubebuilder) Uninstall(ctx context.Context) error {
mg.CtxDeps(ctx, Kubebuilder.Manifests)
var crdManifest string
{
var err error
if crdManifest, err = kustomizeBuildCommand("build config/crd", "crd.yaml"); err != nil {
return err
}
}
if err := shellcmd.Command(
fmt.Sprintf(`kubectl delete --ignore-not-found=true -f "%s"`, crdManifest),
).Run(); err != nil {
return err
}
return nil
}
// .PHONY: deploy
// deploy: manifests $(KUSTOMIZE) ## Deploy controller to the K8s cluster specified in ~/.kube/config.
// cd config/manager && ../../$(KUSTOMIZE) edit set image controller=${IMG}
// $(KUSTOMIZE) build config/default | kubectl apply -f -
// Deploy controller to the K8s cluster specified in ~/.kube/config.
func (Kubebuilder) Deploy(ctx context.Context) error {
mg.CtxDeps(ctx, Kubebuilder.Manifests)
_ = helper.BinKustomize()
var dcfg *helper.DockerConfig
{
var err error
dcfg, err = helper.DockerLoadConfig()
helper.PanicIfError(err, "unable to load docker config")
}
{
err := shellcmd.Command(
fmt.Sprintf(`cd %s; %s edit set image controller=%s`,
helper.MustGetGitTopLevel("config", "manager"),
helper.MustGetArtifactPath("bin", "kustomize"),
dcfg.GetImageRef(),
),
).Run()
if err != nil {
return err
}
}
var deployManifest string
{
var err error
if deployManifest, err = kustomizeBuildCommand("build config/default", "deploy.yaml"); err != nil {
return err
}
}
if err := shellcmd.Command(fmt.Sprintf(`kubectl apply -f "%s"`, deployManifest)).Run(); err != nil {
return err
}
return nil
}
// Undeploy Undeploy controller from the K8s cluster specified in ~/.kube/config.
// Call with ignore-not-found=true to ignore resource not found errors during deletion.
func (Kubebuilder) Undeploy(ctx context.Context) error {
mg.CtxDeps(ctx, Kubebuilder.Manifests)
var deployManifest string
{
var err error
if deployManifest, err = kustomizeBuildCommand("build config/default", "deploy.yaml"); err != nil {
return err
}
}
if err := shellcmd.Command(
fmt.Sprintf(`kubectl delete --ignore-not-found=true -f "%s"`, deployManifest),
).Run(); err != nil {
return err
}
return nil
}
// Run a controller from your host.
func (Kubebuilder) Run(ctx context.Context) error {
mg.CtxDeps(ctx, Kubebuilder.Manifests)
mg.CtxDeps(ctx, Kubebuilder.Generate)
mg.CtxDeps(ctx, Golang.Fmt)
mg.CtxDeps(ctx, Golang.Vet)
return shellcmd.Command("go run ./cmd/main.go --config=artifacts/data/config.yml -zap-devel").Run()
}