/
transform.go
304 lines (257 loc) · 9.76 KB
/
transform.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
/*
Copyright © 2020 The k3d Author(s)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package config
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/docker/go-connections/nat"
cliutil "github.com/rancher/k3d/v4/cmd/util" // TODO: move parseapiport to pkg
conf "github.com/rancher/k3d/v4/pkg/config/v1alpha1"
"github.com/rancher/k3d/v4/pkg/runtimes"
k3d "github.com/rancher/k3d/v4/pkg/types"
"github.com/rancher/k3d/v4/pkg/types/k3s"
"github.com/rancher/k3d/v4/pkg/util"
"github.com/rancher/k3d/v4/version"
"gopkg.in/yaml.v2"
log "github.com/sirupsen/logrus"
)
// TransformSimpleToClusterConfig transforms a simple configuration to a full-fledged cluster configuration
func TransformSimpleToClusterConfig(ctx context.Context, runtime runtimes.Runtime, simpleConfig conf.SimpleConfig) (*conf.ClusterConfig, error) {
// set default cluster name
if simpleConfig.Name == "" {
simpleConfig.Name = k3d.DefaultClusterName
}
// fetch latest image
if simpleConfig.Image == "latest" {
simpleConfig.Image = version.GetK3sVersion(true)
}
clusterNetwork := k3d.ClusterNetwork{}
if simpleConfig.Network != "" {
clusterNetwork.Name = simpleConfig.Network
clusterNetwork.External = true
}
// -> API
if simpleConfig.ExposeAPI.Host == "" {
simpleConfig.ExposeAPI.Host = k3d.DefaultAPIHost
}
if simpleConfig.ExposeAPI.HostIP == "" {
simpleConfig.ExposeAPI.HostIP = k3d.DefaultAPIHost
}
kubeAPIExposureOpts := &k3d.ExposureOpts{
Host: simpleConfig.ExposeAPI.Host,
}
kubeAPIExposureOpts.Port = k3d.DefaultAPIPort
kubeAPIExposureOpts.Binding = nat.PortBinding{
HostIP: simpleConfig.ExposeAPI.HostIP,
HostPort: simpleConfig.ExposeAPI.HostPort,
}
// FILL CLUSTER CONFIG
newCluster := k3d.Cluster{
Name: simpleConfig.Name,
Network: clusterNetwork,
Token: simpleConfig.ClusterToken,
KubeAPI: kubeAPIExposureOpts,
}
// -> NODES
newCluster.Nodes = []*k3d.Node{}
if !simpleConfig.Options.K3dOptions.DisableLoadbalancer {
newCluster.ServerLoadBalancer = &k3d.Node{
Role: k3d.LoadBalancerRole,
}
}
/*************
* Add Nodes *
*************/
for i := 0; i < simpleConfig.Servers; i++ {
serverNode := k3d.Node{
Role: k3d.ServerRole,
Image: simpleConfig.Image,
Args: simpleConfig.Options.K3sOptions.ExtraServerArgs,
ServerOpts: k3d.ServerOpts{},
}
// first server node will be init node if we have more than one server specified but no external datastore
if i == 0 && simpleConfig.Servers > 1 {
serverNode.ServerOpts.IsInit = true
newCluster.InitNode = &serverNode
}
newCluster.Nodes = append(newCluster.Nodes, &serverNode)
}
for i := 0; i < simpleConfig.Agents; i++ {
agentNode := k3d.Node{
Role: k3d.AgentRole,
Image: simpleConfig.Image,
Args: simpleConfig.Options.K3sOptions.ExtraAgentArgs,
}
newCluster.Nodes = append(newCluster.Nodes, &agentNode)
}
/****************************
* Extra Node Configuration *
****************************/
// -> VOLUMES
nodeCount := simpleConfig.Servers + simpleConfig.Agents
nodeList := newCluster.Nodes
if !simpleConfig.Options.K3dOptions.DisableLoadbalancer {
nodeCount++
nodeList = append(nodeList, newCluster.ServerLoadBalancer)
}
for _, volumeWithNodeFilters := range simpleConfig.Volumes {
nodes, err := util.FilterNodes(newCluster.Nodes, volumeWithNodeFilters.NodeFilters)
if err != nil {
return nil, err
}
for _, node := range nodes {
node.Volumes = append(node.Volumes, volumeWithNodeFilters.Volume)
}
}
// -> PORTS
for _, portWithNodeFilters := range simpleConfig.Ports {
if len(portWithNodeFilters.NodeFilters) == 0 && nodeCount > 1 {
return nil, fmt.Errorf("Portmapping '%s' lacks a node filter, but there's more than one node", portWithNodeFilters.Port)
}
nodes, err := util.FilterNodes(nodeList, portWithNodeFilters.NodeFilters)
if err != nil {
return nil, err
}
for _, node := range nodes {
portmappings, err := nat.ParsePortSpec(portWithNodeFilters.Port)
if err != nil {
return nil, fmt.Errorf("Failed to parse port spec '%s': %+v", portWithNodeFilters.Port, err)
}
if node.Ports == nil {
node.Ports = nat.PortMap{}
}
for _, pm := range portmappings {
if _, exists := node.Ports[pm.Port]; exists {
node.Ports[pm.Port] = append(node.Ports[pm.Port], pm.Binding)
} else {
node.Ports[pm.Port] = []nat.PortBinding{pm.Binding}
}
}
}
}
// -> LABELS
for _, labelWithNodeFilters := range simpleConfig.Labels {
if len(labelWithNodeFilters.NodeFilters) == 0 && nodeCount > 1 {
return nil, fmt.Errorf("Labelmapping '%s' lacks a node filter, but there's more than one node", labelWithNodeFilters.Label)
}
nodes, err := util.FilterNodes(nodeList, labelWithNodeFilters.NodeFilters)
if err != nil {
return nil, err
}
for _, node := range nodes {
if node.Labels == nil {
node.Labels = make(map[string]string) // ensure that the map is initialized
}
k, v := util.SplitLabelKeyValue(labelWithNodeFilters.Label)
node.Labels[k] = v
}
}
// -> ENV
for _, envVarWithNodeFilters := range simpleConfig.Env {
if len(envVarWithNodeFilters.NodeFilters) == 0 && nodeCount > 1 {
return nil, fmt.Errorf("EnvVarMapping '%s' lacks a node filter, but there's more than one node", envVarWithNodeFilters.EnvVar)
}
nodes, err := util.FilterNodes(nodeList, envVarWithNodeFilters.NodeFilters)
if err != nil {
return nil, err
}
for _, node := range nodes {
node.Env = append(node.Env, envVarWithNodeFilters.EnvVar)
}
}
/**************************
* Cluster Create Options *
**************************/
clusterCreateOpts := k3d.ClusterCreateOpts{
DisableImageVolume: simpleConfig.Options.K3dOptions.DisableImageVolume,
WaitForServer: simpleConfig.Options.K3dOptions.Wait,
Timeout: simpleConfig.Options.K3dOptions.Timeout,
DisableLoadBalancer: simpleConfig.Options.K3dOptions.DisableLoadbalancer,
K3sServerArgs: simpleConfig.Options.K3sOptions.ExtraServerArgs,
K3sAgentArgs: simpleConfig.Options.K3sOptions.ExtraAgentArgs,
GlobalLabels: map[string]string{}, // empty init
GlobalEnv: []string{}, // empty init
}
// ensure, that we have the default object labels
for k, v := range k3d.DefaultObjectLabels {
clusterCreateOpts.GlobalLabels[k] = v
}
/*
* Registries
*/
if simpleConfig.Registries.Create {
regPort, err := cliutil.ParsePortExposureSpec("random", k3d.DefaultRegistryPort)
if err != nil {
return nil, fmt.Errorf("Failed to get port for registry: %+v", err)
}
clusterCreateOpts.Registries.Create = &k3d.Registry{
ClusterRef: newCluster.Name,
Host: fmt.Sprintf("%s-%s-registry", k3d.DefaultObjectNamePrefix, newCluster.Name),
Image: fmt.Sprintf("%s:%s", k3d.DefaultRegistryImageRepo, k3d.DefaultRegistryImageTag),
ExposureOpts: *regPort,
}
}
for _, usereg := range simpleConfig.Registries.Use {
reg, err := util.ParseRegistryRef(usereg)
if err != nil {
return nil, fmt.Errorf("Failed to parse use-registry string '%s': %+v", usereg, err)
}
log.Tracef("Parsed registry reference: %+v", reg)
clusterCreateOpts.Registries.Use = append(clusterCreateOpts.Registries.Use, reg)
}
if simpleConfig.Registries.Config != "" {
var k3sRegistry *k3s.Registry
if strings.Contains(simpleConfig.Registries.Config, "\n") { // CASE 1: embedded registries.yaml (multiline string)
log.Debugf("Found multiline registries config embedded in SimpleConfig:\n%s", simpleConfig.Registries.Config)
if err := yaml.Unmarshal([]byte(simpleConfig.Registries.Config), &k3sRegistry); err != nil {
return nil, fmt.Errorf("Failed to read embedded registries config: %+v", err)
}
} else { // CASE 2: registries.yaml file referenced by path (single line)
registryConfigFile, err := os.Open(simpleConfig.Registries.Config)
if err != nil {
return nil, fmt.Errorf("Failed to open registry config file at %s: %+v", simpleConfig.Registries.Config, err)
}
configBytes, err := ioutil.ReadAll(registryConfigFile)
if err != nil {
return nil, fmt.Errorf("Failed to read registry config file at %s: %+v", registryConfigFile.Name(), err)
}
if err := yaml.Unmarshal(configBytes, &k3sRegistry); err != nil {
return nil, fmt.Errorf("Failed to read registry configuration: %+v", err)
}
}
log.Tracef("Registry: read config from input:\n%+v", k3sRegistry)
clusterCreateOpts.Registries.Config = k3sRegistry
}
/**********************
* Kubeconfig Options *
**********************/
// Currently, the kubeconfig options for the cluster config are the same as for the simple config
/******************************
* Create Full Cluster Config *
******************************/
clusterConfig := &conf.ClusterConfig{
Cluster: newCluster,
ClusterCreateOpts: clusterCreateOpts,
KubeconfigOpts: simpleConfig.Options.KubeconfigOptions,
}
return clusterConfig, nil
}