-
Notifications
You must be signed in to change notification settings - Fork 9
/
k3d.go
211 lines (176 loc) · 6.18 KB
/
k3d.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package prepare
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/build/registry"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/parsing/devhost"
k3dp "namespacelabs.dev/foundation/internal/providers/k3d"
"namespacelabs.dev/foundation/internal/providers/nscloud/nsingress"
"namespacelabs.dev/foundation/internal/runtime/docker"
kubeclient "namespacelabs.dev/foundation/internal/runtime/kubernetes/client"
"namespacelabs.dev/foundation/internal/sdk/host"
"namespacelabs.dev/foundation/internal/sdk/k3d"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/schema/orchestration"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
)
const registryName = "k3d-ns-registry.nslocal.host"
func K3D(clusterName, ingressClass string) Stage {
return Stage{
Pre: func(ch chan *orchestration.Event) {
ch <- &orchestration.Event{
Category: "Local workstation",
ResourceId: "docker-checker",
ResourceLabel: "Validate Docker version",
}
ch <- &orchestration.Event{
Category: "Local workstation",
ResourceId: "k3s-cluster",
ResourceLabel: "Kubernetes cluster in Docker", // XXX remove soon.
}
},
Run: func(ctx context.Context, env cfg.Context, ch chan *orchestration.Event) (*schema.DevHost_ConfigureEnvironment, error) {
return PrepareK3d(ctx, clusterName, ingressClass, env, ch)
},
}
}
func PrepareK3d(ctx context.Context, clusterName, ingressClass string, env cfg.Context, ch chan *orchestration.Event) (*schema.DevHost_ConfigureEnvironment, error) {
return tasks.Return(ctx, tasks.Action("prepare.k3s").HumanReadablef("Preparing a local Kubernetes cluster (k3s running in Docker)"),
func(ctx context.Context) (*schema.DevHost_ConfigureEnvironment, error) {
dockerclient, err := docker.NewClient()
if err != nil {
return nil, err
}
if err := k3d.ValidateDocker(ctx, dockerclient); err != nil {
return nil, err
}
ch <- &orchestration.Event{
Category: "Local workstation",
ResourceId: "docker-checker",
Ready: orchestration.Event_READY,
Stage: orchestration.Event_DONE,
}
// download k3d
k3dbin, err := k3d.EnsureSDK(ctx, host.HostPlatform())
if err != nil {
return nil, err
}
k3dPrepare := &k3dPrepare{clusterName, k3dbin, dockerclient}
registryAddr, err := k3dPrepare.createOrRestartRegistry(ctx, registryName)
if err != nil {
return nil, err
}
if registryAddr == "" {
return nil, fnerrors.InternalError("failed address registration for %q", registryName)
}
r := &k3dp.Configuration{
RegistryContainerName: registryName,
ClusterName: clusterName,
}
hostEnv := kubeclient.NewLocalHostEnv("k3d-"+clusterName, env)
hostEnv.IngressClass = ingressClass
if hostEnv.IngressClass == "" {
hostEnv.IngressClass = nsingress.IngressClass().Name()
}
c, err := devhost.MakeConfiguration(r, hostEnv, ®istry.Provider{Provider: "k3d"})
if err != nil {
return nil, err
}
if err := k3dPrepare.createOrRestartCluster(ctx, clusterName, registryAddr); err != nil {
return nil, err
}
ch <- &orchestration.Event{
ResourceId: "k3s-cluster",
Ready: orchestration.Event_READY,
Stage: orchestration.Event_DONE,
}
return c, nil
})
}
type k3dPrepare struct {
clusterName string
k3dbin k3d.K3D
dockerclient docker.Client
}
func (p *k3dPrepare) createOrRestartRegistry(ctx context.Context, registryName string) (string, error) {
registryCtr, err := p.dockerclient.ContainerInspect(ctx, registryName)
if err != nil {
if !client.IsErrNotFound(err) {
return "", err
}
if err := p.k3dbin.CreateRegistry(ctx, registryName, 0); err != nil {
return "", err
}
registryCtr, err = p.dockerclient.ContainerInspect(ctx, registryName)
if err != nil {
return "", err
}
}
if !registryCtr.State.Running {
if err := p.dockerclient.ContainerStart(ctx, registryName, types.ContainerStartOptions{}); err != nil {
return "", fnerrors.InternalError("failed to restart registry %q: %w", registryName, err)
}
registryCtr, err = p.dockerclient.ContainerInspect(ctx, registryName)
if err != nil {
return "", fnerrors.InternalError("failed to inspect registry %q after a restart: %w", registryName, err)
}
}
const expectedPort = "5000/tcp"
registryPortBinding := findPort(registryCtr, expectedPort)
if len(registryPortBinding) == 0 {
return "", fnerrors.InternalError("failed to find expected port %q for registry %q", expectedPort, registryName)
}
registryPort := registryPortBinding[0].HostPort
registryAddr := fmt.Sprintf("%s:%s", registryName, registryPort)
return registryAddr, nil
}
func (p *k3dPrepare) createOrRestartCluster(ctx context.Context, clusterName string, registryAddr string) error {
clusters, err := p.k3dbin.ListClusters(ctx)
if err != nil {
return err
}
var ours *k3d.Cluster
for _, cl := range clusters {
if cl.Name == clusterName {
ours = &cl
}
}
if ours == nil {
// Create cluster.
if err := p.k3dbin.CreateCluster(ctx, clusterName, registryAddr, "rancher/k3s:v1.26.2-k3s1", true); err != nil {
return err
}
} else {
// Ensure that all of the nodes in the cluster are up and running.
var errs []error
for _, node := range ours.Nodes {
if !node.State.Running {
if err := p.k3dbin.StartNode(ctx, node.Name); err != nil {
errs = append(errs, err)
}
}
}
if err := multierr.New(errs...); err != nil {
return fnerrors.InternalError("failed to start node(s) for cluster %q: %w", clusterName, err)
}
}
if err := p.k3dbin.MergeConfiguration(ctx, clusterName); err != nil {
return err
}
return nil
}
func findPort(ctr types.ContainerJSON, port string) []nat.PortBinding {
if ctr.NetworkSettings == nil {
return nil
}
return ctr.NetworkSettings.Ports[nat.Port(port)]
}