-
Notifications
You must be signed in to change notification settings - Fork 62
/
cluster.go
273 lines (236 loc) · 7.23 KB
/
cluster.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
package kind
import (
"context"
"embed"
"errors"
"fmt"
"io/fs"
"os"
"strconv"
"strings"
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/log"
kindv1alpha4 "sigs.k8s.io/kind/pkg/apis/config/v1alpha4"
"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/cluster/nodes"
kindexec "sigs.k8s.io/kind/pkg/exec"
"sigs.k8s.io/yaml"
)
const (
ingressNginxNodeLabelKey = "ingress-ready"
ingressNginxNodeLabelValue = "true"
)
var (
setupLog = log.Log.WithName("setup")
)
type Cluster struct {
provider IProvider
name string
kubeVersion string
kubeConfigPath string
kindConfigPath string
extraPortsMapping string
cfg v1alpha1.BuildCustomizationSpec
}
type PortMapping struct {
HostPort string
ContainerPort string
}
type IProvider interface {
List() ([]string, error)
ListNodes(string) ([]nodes.Node, error)
CollectLogs(string, string) error
Delete(string, string) error
Create(string, ...cluster.CreateOption) error
ExportKubeConfig(string, string, bool) error
}
type TemplateConfig struct {
v1alpha1.BuildCustomizationSpec
KubernetesVersion string
ExtraPortsMapping []PortMapping
}
//go:embed resources/*
var configFS embed.FS
func (c *Cluster) getConfig() ([]byte, error) {
var rawConfigTempl []byte
var err error
if c.kindConfigPath != "" {
rawConfigTempl, err = os.ReadFile(c.kindConfigPath)
} else {
rawConfigTempl, err = fs.ReadFile(configFS, "resources/kind.yaml.tmpl")
}
if err != nil {
return nil, fmt.Errorf("reading kind config: %w", err)
}
var portMappingPairs []PortMapping
if len(c.extraPortsMapping) > 0 {
// Split pairs of ports "11=1111","22=2222",etc
pairs := strings.Split(c.extraPortsMapping, ",")
// Create a slice to store PortMapping pairs.
portMappingPairs = make([]PortMapping, len(pairs))
// Parse each pair into PortPair objects.
for i, pair := range pairs {
parts := strings.Split(pair, ":")
if len(parts) == 2 {
portMappingPairs[i] = PortMapping{parts[0], parts[1]}
}
}
}
var retBuff []byte
if retBuff, err = util.ApplyTemplate(rawConfigTempl, TemplateConfig{
BuildCustomizationSpec: c.cfg,
KubernetesVersion: c.kubeVersion,
ExtraPortsMapping: portMappingPairs,
}); err != nil {
return nil, err
}
if c.kindConfigPath != "" {
parsedCluster, err := c.ensureCorrectConfig(retBuff)
if err != nil {
return nil, fmt.Errorf("ensuring custom kind config is correct: %w", err)
}
out, err := yaml.Marshal(parsedCluster)
if err != nil {
return nil, fmt.Errorf("marshaling custom kind cluster config: %w", err)
}
return out, nil
}
return retBuff, nil
}
func NewCluster(name, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping string, cfg v1alpha1.BuildCustomizationSpec, cliLogger logr.Logger) (*Cluster, error) {
detectOpt, err := util.DetectKindNodeProvider()
if err != nil {
return nil, err
}
provider := cluster.NewProvider(cluster.ProviderWithLogger(KindLoggerFromLogr(&cliLogger)), detectOpt)
return &Cluster{
provider: provider,
name: name,
kindConfigPath: kindConfigPath,
kubeVersion: kubeVersion,
kubeConfigPath: kubeConfigPath,
extraPortsMapping: extraPortsMapping,
cfg: cfg,
}, nil
}
func (c *Cluster) Exists() (bool, error) {
providerClusters, err := c.provider.List()
if err != nil {
return false, err
}
for _, pc := range providerClusters {
if pc == c.name {
return true, nil
}
}
return false, nil
}
func (c *Cluster) Reconcile(ctx context.Context, recreate bool) error {
clusterExitsts, err := c.Exists()
if err != nil {
return err
}
if clusterExitsts {
if recreate {
setupLog.Info("Existing cluster found. Deleting.", "cluster", c.name)
err := c.provider.Delete(c.name, "")
if err != nil {
return fmt.Errorf("deleting cluster %w", err)
}
} else {
setupLog.Info("Cluster already exists", "cluster", c.name)
return nil
}
}
rawConfig, err := c.getConfig()
if err != nil {
return err
}
fmt.Print("########################### Our kind config ############################\n")
fmt.Printf("%s", rawConfig)
fmt.Print("\n######################### config end ############################\n")
setupLog.Info("Creating kind cluster", "cluster", c.name)
if err = c.provider.Create(
c.name,
cluster.CreateWithRawConfig(rawConfig),
); err != nil {
t := &kindexec.RunError{}
if errors.As(err, &t) {
return fmt.Errorf("%w: %s", err, t.Output)
}
return err
}
setupLog.Info("Done creating cluster", "cluster", c.name)
return nil
}
func (c *Cluster) ExportKubeConfig(name string, internal bool) error {
return c.provider.ExportKubeConfig(name, c.kubeConfigPath, internal)
}
func (c *Cluster) ensureCorrectConfig(in []byte) (kindv1alpha4.Cluster, error) {
// see pkg/kind/resources/kind.yaml.tmpl and pkg/controllers/localbuild/resources/nginx/k8s/ingress-nginx.yaml
// defines which container port we should be looking for.
containerPort := "443"
if c.cfg.Protocol == "http" {
containerPort = "80"
}
parsedCluster := kindv1alpha4.Cluster{}
err := yaml.Unmarshal(in, &parsedCluster)
if err != nil {
return kindv1alpha4.Cluster{}, fmt.Errorf("parsing kind config: %w", err)
}
// the port and ingress-nginx label must be on the same node to ensure nginx runs on the node with the right port.
appendNecessaryPort := true
appendIngressNodeLabel := true
// pick the first node for the ingress-nginx if we need to configure node port.
nodePosition := 0
if parsedCluster.Nodes == nil || len(parsedCluster.Nodes) == 0 {
return kindv1alpha4.Cluster{}, fmt.Errorf("provided kind config does not have the node field defined")
}
nodes:
for i := range parsedCluster.Nodes {
node := parsedCluster.Nodes[i]
for _, pm := range node.ExtraPortMappings {
if strconv.Itoa(int(pm.HostPort)) == c.cfg.Port {
appendNecessaryPort = false
nodePosition = i
if node.Labels != nil {
v, ok := node.Labels[ingressNginxNodeLabelKey]
if ok && v == ingressNginxNodeLabelValue {
appendIngressNodeLabel = false
}
}
break nodes
}
}
if node.Labels != nil {
v, ok := node.Labels[ingressNginxNodeLabelKey]
if ok && v == ingressNginxNodeLabelValue {
appendIngressNodeLabel = false
nodePosition = i
break nodes
}
}
}
if appendNecessaryPort {
hp, err := strconv.Atoi(c.cfg.Port)
if err != nil {
return kindv1alpha4.Cluster{}, fmt.Errorf("converting port, %s, to int: %w", c.cfg.Port, err)
}
// either "80" or "443". No need to check for err
cp, _ := strconv.Atoi(containerPort)
if parsedCluster.Nodes[nodePosition].ExtraPortMappings == nil {
parsedCluster.Nodes[nodePosition].ExtraPortMappings = make([]kindv1alpha4.PortMapping, 0, 1)
}
parsedCluster.Nodes[nodePosition].ExtraPortMappings =
append(parsedCluster.Nodes[nodePosition].ExtraPortMappings, kindv1alpha4.PortMapping{ContainerPort: int32(cp), HostPort: int32(hp), Protocol: "TCP"})
}
if appendIngressNodeLabel {
if parsedCluster.Nodes[nodePosition].Labels == nil {
parsedCluster.Nodes[nodePosition].Labels = make(map[string]string)
}
parsedCluster.Nodes[nodePosition].Labels[ingressNginxNodeLabelKey] = ingressNginxNodeLabelValue
}
return parsedCluster, nil
}