/
k3s.go
334 lines (293 loc) · 8.11 KB
/
k3s.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
package k3s
import (
"bytes"
"context"
"fmt"
"html/template"
"io"
"golang.org/x/sync/errgroup"
"k8s.io/apimachinery/pkg/api/resource"
"github.com/google/go-containerregistry/pkg/name"
"github.com/chainguard-dev/terraform-provider-imagetest/internal/containers/provider"
"github.com/chainguard-dev/terraform-provider-imagetest/internal/harnesses/base"
"github.com/chainguard-dev/terraform-provider-imagetest/internal/log"
"github.com/chainguard-dev/terraform-provider-imagetest/internal/types"
"github.com/docker/docker/api/types/mount"
)
const (
K3sImageTag = "cgr.dev/chainguard/k3s:latest"
KubectlImageTag = "cgr.dev/chainguard/kubectl:latest-dev"
)
type k3s struct {
*base.Base
// opt are the options for the k3s harness
opt *Opt
// id is an identifier used to prepend to containers created by this harness
id string
// service is the provider that is running the service, which is k3s in a
// container
service provider.Provider
// sandbox is the provider where the user defined steps will execute. it is
// wired into the service
sandbox provider.Provider
}
func New(id string, cli *provider.DockerClient, opts ...Option) (types.Harness, error) {
harnessOptions := &Opt{
ImageRef: name.MustParseReference(K3sImageTag),
Cni: true,
MetricsServer: false,
Snapshotter: "overlayfs",
Traefik: false,
// Default to the bare minimum k3s needs to run properly
// https://docs.k3s.io/installation/requirements#hardware
Resources: provider.ContainerResourcesRequest{
MemoryRequest: resource.MustParse("2Gi"),
CpuRequest: resource.MustParse("1"),
},
Sandbox: provider.DockerRequest{
ContainerRequest: provider.ContainerRequest{
Ref: name.MustParseReference(KubectlImageTag),
Entrypoint: base.DefaultEntrypoint(),
Cmd: base.DefaultCmd(),
Env: map[string]string{
"KUBECONFIG": "/k3s-config/k3s.yaml",
"ENV": "/root/.ashrc",
},
Files: []provider.File{
{
Contents: bytes.NewBufferString("alias k=kubectl"),
Target: "/root/.ashrc",
Mode: 644,
},
},
User: "0:0",
// Default to something small just for "scheduling" purposes, the bulk of
// the work happens in the service container
Resources: provider.ContainerResourcesRequest{
MemoryRequest: resource.MustParse("250Mi"),
CpuRequest: resource.MustParse("100m"),
},
},
},
}
for _, o := range opts {
if err := o(harnessOptions); err != nil {
return nil, err
}
}
harnessOptions.Sandbox.Mounts = append(harnessOptions.Sandbox.Mounts, mount.Mount{
Type: mount.TypeVolume,
Source: harnessOptions.ContainerVolumeName,
Target: "/k3s-config",
})
k3s := &k3s{
Base: base.New(),
id: id,
opt: harnessOptions,
}
kcfg, err := k3s.genConfig()
if err != nil {
return nil, fmt.Errorf("creating k3s config: %w", err)
}
rcfg, err := k3s.genRegistries()
if err != nil {
return nil, fmt.Errorf("creating k3s registries config: %w", err)
}
service := provider.NewDocker(id, cli, provider.DockerRequest{
ContainerRequest: provider.ContainerRequest{
Ref: harnessOptions.ImageRef,
Cmd: []string{"server"},
Privileged: true,
Networks: harnessOptions.Networks,
Files: []provider.File{
{
Contents: kcfg,
Target: "/etc/rancher/k3s/config.yaml",
Mode: 0644,
},
{
Contents: rcfg,
Target: "/etc/rancher/k3s/registries.yaml",
Mode: 0644,
},
},
Resources: harnessOptions.Resources,
},
ManagedVolumes: []mount.Mount{
{
Type: mount.TypeVolume,
Source: harnessOptions.ContainerVolumeName,
Target: "/etc/rancher/k3s",
},
},
Mounts: []mount.Mount{
{
Type: mount.TypeTmpfs,
Target: "/run",
},
{
Type: mount.TypeTmpfs,
Target: "/tmp",
},
},
})
sandbox := provider.NewDocker(k3s.id+"-sandbox", cli, harnessOptions.Sandbox)
k3s.service = service
k3s.sandbox = sandbox
return k3s, nil
}
// Setup implements types.Harness.
func (h *k3s) Setup() types.StepFn {
return h.WithCreate(func(ctx context.Context) (context.Context, error) {
g, ctx := errgroup.WithContext(ctx)
// start the k3s service first since the sandbox depends on the network
// being created
if err := h.service.Start(ctx); err != nil {
return ctx, fmt.Errorf("starting k3s service: %w", err)
}
// Wait for the k3s service
g.Go(func() error {
// Block until k3s is ready. It is up to the caller to ensure the context
// is cancelled to stop waiting.
// TODO: Replace this with a better wait.Conditions, this will be ~50%
// faster if we just scan the startup logs looking for containerd's ready
// message
if _, err := h.service.Exec(ctx, provider.ExecConfig{
Command: `
while true; do
if k3s kubectl cluster-info; then
break
fi
sleep 0.5
done
`,
}); err != nil {
return fmt.Errorf("waiting for k3s cluster: %w", err)
}
// Move the kubeconfig into place, and give it the appropriate endpoint
if _, err := h.service.Exec(ctx, provider.ExecConfig{
Command: fmt.Sprintf(`
KUBECONFIG=/etc/rancher/k3s/k3s.yaml k3s kubectl config set-cluster default --server "https://%[1]s:6443" > /dev/null
`, h.id),
}); err != nil {
return fmt.Errorf("creating kubeconfig: %w", err)
}
return nil
})
g.Go(func() error {
// Start the sandbox
if err := h.sandbox.Start(ctx); err != nil {
return fmt.Errorf("starting sandbox: %w", err)
}
return nil
})
log.Info(ctx, "Waiting for k3s service to be ready")
if err := g.Wait(); err != nil {
return ctx, err
}
return ctx, nil
})
}
// Destroy implements types.Harness.
func (h *k3s) Destroy(ctx context.Context) error {
var errs []error
if err := h.sandbox.Teardown(ctx); err != nil {
errs = append(errs, fmt.Errorf("tearing down sandbox: %w", err))
}
if err := h.service.Teardown(ctx); err != nil {
errs = append(errs, fmt.Errorf("tearing down service: %w", err))
}
if len(errs) > 0 {
var err error
for _, e := range errs {
if err != nil {
err = fmt.Errorf("%w: %v", err, e)
} else {
err = e
}
}
return err
}
return nil
}
// StepFn implements types.Harness.
func (h *k3s) StepFn(config types.StepConfig) types.StepFn {
return func(ctx context.Context) (context.Context, error) {
log.Info(ctx, "stepping in k3s sandbox container", "command", config.Command)
r, err := h.sandbox.Exec(ctx, provider.ExecConfig{
Command: config.Command,
WorkingDir: config.WorkingDir,
})
if err != nil {
return ctx, err
}
out, err := io.ReadAll(r)
if err != nil {
return ctx, err
}
log.Info(ctx, "finished stepping in k3s sandbox container", "command", config.Command, "out", string(out))
return ctx, nil
}
}
func (h *k3s) genRegistries() (io.Reader, error) {
// who needs an an api when you have yaml and gotemplates!11!
cfgtmpl := `
mirrors:
{{- range $k, $v := .Mirrors }}
"{{ $k }}":
endpoint:
{{- range $v.Endpoints }}
- "{{ . }}"
{{- end }}
{{- end}}
configs:
{{- range $k, $v := .Registries }}
"{{ $k }}":
auth:
username: "{{ $v.Auth.Username }}"
password: "{{ $v.Auth.Password }}"
auth: "{{ $v.Auth.Auth }}"
{{- if and $v.Tls $v.Tls.CertFile $v.Tls.KeyFile $v.Tls.CaFile }}
tls:
cert_file: "{{ $v.Tls.CertFile }}"
key_file: "{{ $v.Tls.KeyFile }}"
ca_file: "{{ $v.Tls.CaFile }}"
{{- end }}
{{- end }}
`
tmpl, err := template.New("registry").Parse(cfgtmpl)
if err != nil {
return nil, err
}
buf := &bytes.Buffer{}
if err := tmpl.Execute(buf, h.opt); err != nil {
return nil, err
}
return buf, nil
}
func (h *k3s) genConfig() (io.Reader, error) {
// who needs an an api when you have yaml and gotemplates!11!
cfgtmpl := fmt.Sprintf(`
tls-san: "%[1]s"
disable:
{{- if not .Traefik }}
- traefik
{{- end }}
{{- if not .MetricsServer }}
- metrics-server
{{- end }}
{{- if not .Cni }}
flannel-backend: none
{{- end }}
snapshotter: "%[2]s"
`, h.id, h.opt.Snapshotter)
tmpl, err := template.New("config").Parse(cfgtmpl)
if err != nil {
return nil, err
}
buf := &bytes.Buffer{}
if err := tmpl.Execute(buf, h.opt); err != nil {
return nil, err
}
return buf, nil
}