forked from kubernetes-sigs/kind
/
derivedconfig.go
281 lines (237 loc) · 9.18 KB
/
derivedconfig.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
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package create
import (
"fmt"
"sort"
"sigs.k8s.io/kind/pkg/cluster/config"
"sigs.k8s.io/kind/pkg/util"
"github.com/pkg/errors"
)
// DerivedConfig contains config-like data computed from pkg/cluster/config.Config
// namely, it contains lists of NodeReplicas to be created based on the config
type DerivedConfig struct {
// allReplicas constains the list of node replicas defined in the `kind` Config
allReplicas ReplicaList
// controlPlanes contains the subset of node replicas with control-plane role
controlPlanes ReplicaList
// workers contains the subset of node replicas with worker role, if any
workers ReplicaList
// externalEtcd contains the node replica with external-etcd role, if defined
// TODO(fabriziopandini): eventually in future we would like to support
// external etcd clusters with more than one member
externalEtcd *NodeReplica
// externalLoadBalancer contains the node replica with external-load-balancer role, if defined
externalLoadBalancer *NodeReplica
}
// NodeReplica defines a `kind` config Node that is geneated by creating a replicas for a node
// This attribute exists only in the internal config version and is meant
// to simplify the usage of the config in the code base.
type NodeReplica struct {
// Node contains settings for the node in the `kind` Config.
// please note that the Replicas number is alway set to nil.
config.Node
// Name contains the unique name assigned to the node while generating the replica
Name string
}
// ReplicaList defines a list of NodeReplicas in the `kind` Config
// This attribute exists only in the internal config version and is meant
// to simplify the usage of the config in the code base.
type ReplicaList []*NodeReplica
// Validate validates that the configuration is possible to create
func (d *DerivedConfig) Validate() error {
errs := []error{}
// There should be at least one control plane
if d.BootStrapControlPlane() == nil {
errs = append(errs, fmt.Errorf("please add at least one node with role %q", config.ControlPlaneRole))
}
// There should be one load balancer if more than one control plane exists in the cluster
if len(d.ControlPlanes()) > 1 && d.ExternalLoadBalancer() == nil {
errs = append(errs, fmt.Errorf("please add a node with role %s because in the cluster there are more than one node with role %s", config.ExternalLoadBalancerRole, config.ControlPlaneRole))
}
// TODO(fabrizio pandini): this check is temporary / WIP
// kind v1alpha config fully supports multi nodes, but the cluster creation logic implemented in
// pkg/cluster/contex.go does it only partially (yet).
// As soon a external load-balancer and external etcd is implemented in pkg/cluster, this should go away
if d.ExternalLoadBalancer() != nil {
errs = append(errs, fmt.Errorf("multi node support is still a work in progress, currently external load balancer node is not supported"))
}
if d.SecondaryControlPlanes() != nil {
errs = append(errs, fmt.Errorf("multi node support is still a work in progress, currently only single control-plane node are supported"))
}
if d.ExternalEtcd() != nil {
errs = append(errs, fmt.Errorf("multi node support is still a work in progress, currently external etcd node is not supported"))
}
if len(errs) > 0 {
return util.NewErrors(errs)
}
return nil
}
// ProvisioningOrder returns the provisioning order for nodes, that
// should be defined according to the assigned NodeRole
func (n *NodeReplica) ProvisioningOrder() int {
switch n.Role {
// External dependencies should be provisioned first; we are defining an arbitrary
// precedence between etcd and load balancer in order to get predictable/repeatable results
case config.ExternalEtcdRole:
return 1
case config.ExternalLoadBalancerRole:
return 2
// Then control plane nodes
case config.ControlPlaneRole:
return 3
// Finally workers
case config.WorkerRole:
return 4
default:
return 99
}
}
// Len of the NodeList.
// It is required for making NodeList sortable.
func (t ReplicaList) Len() int {
return len(t)
}
// Less return the lower between two elements of the NodeList, where the
// lower element should be provisioned before the other.
// It is required for making NodeList sortable.
func (t ReplicaList) Less(i, j int) bool {
return t[i].ProvisioningOrder() < t[j].ProvisioningOrder() ||
// In case of same provisioning order, the name is used to get predictable/repeatable results
(t[i].ProvisioningOrder() == t[j].ProvisioningOrder() && t[i].Name < t[j].Name)
}
// Swap two elements of the NodeList.
// It is required for making NodeList sortable.
func (t ReplicaList) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}
// Derive populates DerivedConfig info starting
// from the current list on Nodes
func Derive(c *config.Config) (*DerivedConfig, error) {
d := &DerivedConfig{}
for _, n := range c.Nodes {
if err := d.Add(&n); err != nil {
return nil, err
}
}
return d, nil
}
// Add a Node to the `kind` cluster, generating requested node replicas
// and assigning a unique node name to each replica.
func (d *DerivedConfig) Add(node *config.Node) error {
// Creates the list of node replicas
expectedReplicas := 1
if node.Replicas != nil {
expectedReplicas = int(*node.Replicas)
}
replicas := ReplicaList{}
for i := 1; i <= expectedReplicas; i++ {
replica := &NodeReplica{
Node: *node.DeepCopy(),
}
replica.Replicas = nil // resetting the replicas number for each replica to default (1)
replicas = append(replicas, replica)
}
// adds replica to the config updating derivedConfig
for _, replica := range replicas {
// adds the replica to the list of nodes
d.allReplicas = append(d.allReplicas, replica)
// list of nodes with control plane role
if replica.IsControlPlane() {
// assign selected name for control plane node
replica.Name = "control-plane"
// stores the node in derivedConfig
d.controlPlanes = append(d.controlPlanes, replica)
}
// list of nodes with worker role
if replica.IsWorker() {
// assign selected name for worker node
replica.Name = "worker"
// stores the node in derivedConfig
d.workers = append(d.workers, replica)
}
// node with external etcd role
if replica.IsExternalEtcd() {
if d.externalEtcd != nil {
return errors.Errorf("invalid config. there are two nodes with role %q", config.ExternalEtcdRole)
}
// assign selected name for etcd node
replica.Name = "etcd"
// stores the node in derivedConfig
d.externalEtcd = replica
}
// node with external load balancer role
if replica.IsExternalLoadBalancer() {
if d.externalLoadBalancer != nil {
return errors.Errorf("invalid config. there are two nodes with role %q", config.ExternalLoadBalancerRole)
}
// assign selected name for load balancer node
replica.Name = "lb"
// stores the node in derivedConfig
d.externalLoadBalancer = replica
}
}
// if more than one control plane node exists, fixes names to get a progressive index
if len(d.controlPlanes) > 1 {
for i, n := range d.controlPlanes {
n.Name = fmt.Sprintf("%s%d", "control-plane", i+1)
}
}
// if more than one worker node exists, fixes names to get a progressive index
if len(d.workers) > 1 {
for i, n := range d.workers {
n.Name = fmt.Sprintf("%s%d", "worker", i+1)
}
}
// ensure the list of nodes is ordered.
// the ordering is key for getting a consistent and predictable behaviour
// when provisioning nodes and when executing actions on nodes
sort.Sort(d.allReplicas)
return nil
}
// AllReplicas returns all the node replicas defined in the `kind` Config.
func (d *DerivedConfig) AllReplicas() ReplicaList {
return d.allReplicas
}
// ControlPlanes returns all the nodes with control-plane role
func (d *DerivedConfig) ControlPlanes() ReplicaList {
return d.controlPlanes
}
// BootStrapControlPlane returns the first node with control-plane role
// This is the node where kubeadm init will be executed.
func (d *DerivedConfig) BootStrapControlPlane() *NodeReplica {
if len(d.controlPlanes) == 0 {
return nil
}
return d.controlPlanes[0]
}
// SecondaryControlPlanes returns all the nodes with control-plane role
// except the BootStrapControlPlane node, if any,
func (d *DerivedConfig) SecondaryControlPlanes() ReplicaList {
if len(d.controlPlanes) <= 1 {
return nil
}
return d.controlPlanes[1:]
}
// Workers returns all the nodes with Worker role, if any
func (d *DerivedConfig) Workers() ReplicaList {
return d.workers
}
// ExternalEtcd returns the node with external-etcd role, if defined
func (d *DerivedConfig) ExternalEtcd() *NodeReplica {
return d.externalEtcd
}
// ExternalLoadBalancer returns the node with external-load-balancer role, if defined
func (d *DerivedConfig) ExternalLoadBalancer() *NodeReplica {
return d.externalLoadBalancer
}