-
Notifications
You must be signed in to change notification settings - Fork 9
/
k3d.go
173 lines (144 loc) · 5.04 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
// 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/compute"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/parsing/devhost"
k3dp "namespacelabs.dev/foundation/internal/providers/k3d"
"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/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
)
const registryName = "k3d-ns-registry.nslocal.host"
func PrepareK3d(clusterName string, env cfg.Context) compute.Computable[*schema.DevHost_ConfigureEnvironment] {
return compute.Map(
tasks.Action("prepare.k3d").HumanReadablef("Prepare the local k3d environment"),
compute.Inputs().Str("clusterName", clusterName).Indigestible("env", env),
compute.Output{NotCacheable: true},
func(ctx context.Context, _ compute.Resolved) (*schema.DevHost_ConfigureEnvironment, error) {
// download k3d
k3dbin, err := k3d.EnsureSDK(ctx, host.HostPlatform())
if err != nil {
return nil, err
}
dockerclient, err := docker.NewClient()
if err != nil {
return nil, err
}
if err := k3d.ValidateDocker(ctx, dockerclient); 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)
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
}
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.22.9-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)]
}