/
join_broker.go
562 lines (508 loc) · 19.4 KB
/
join_broker.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
Copyright 2021.
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 controllers
import (
"context"
"encoding/base64"
"fmt"
"regexp"
"strings"
"time"
submariner "github.com/submariner-io/submariner-operator/apis/submariner/v1alpha1"
operatorv1alpha1 "github.com/DanielXLee/cluster-fabric-operator/api/v1alpha1"
cmdVersion "github.com/DanielXLee/cluster-fabric-operator/controllers/checker"
"github.com/DanielXLee/cluster-fabric-operator/controllers/discovery/globalnet"
"github.com/DanielXLee/cluster-fabric-operator/controllers/discovery/network"
consts "github.com/DanielXLee/cluster-fabric-operator/controllers/ensures"
"github.com/DanielXLee/cluster-fabric-operator/controllers/ensures/broker"
"github.com/DanielXLee/cluster-fabric-operator/controllers/ensures/names"
"github.com/DanielXLee/cluster-fabric-operator/controllers/ensures/operator/servicediscoverycr"
"github.com/DanielXLee/cluster-fabric-operator/controllers/ensures/operator/submarinercr"
"github.com/DanielXLee/cluster-fabric-operator/controllers/ensures/operator/submarinerop"
"github.com/DanielXLee/cluster-fabric-operator/controllers/versions"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var clienttoken *v1.Secret
var nodeLabelBackoff wait.Backoff = wait.Backoff{
Steps: 10,
Duration: 1 * time.Second,
Factor: 1.2,
Jitter: 1,
}
func (r *FabricReconciler) JoinSubmarinerCluster(instance *operatorv1alpha1.Fabric, brokerInfo *broker.BrokerInfo) error {
joinConfig := instance.Spec.JoinConfig
if err := isValidCustomCoreDNSConfig(instance); err != nil {
klog.Errorf("Invalid Custom CoreDNS configuration: %v", err)
return err
}
if joinConfig.ClusterID == "" {
// r.Config
// rawConfig, err := r.Config.RawConfig()
// // This will be fatal later, no point in continuing
// utils.ExitOnError("Error connecting to the target cluster", err)
// clusterName := restconfig.ClusterNameFromContext(rawConfig, contextName)
// if clusterName != nil {
// clusterID = *clusterName
// }
klog.Errorf("Invalid ClusterID")
return fmt.Errorf("invalid ClusterID")
}
if valid, err := isValidClusterID(joinConfig.ClusterID); !valid {
klog.Errorf("Cluster ID invalid: %v", err)
return err
}
_, failedRequirements, err := cmdVersion.CheckRequirements(r.Config)
// We display failed requirements even if an error occurred
if len(failedRequirements) > 0 {
klog.Info("The target cluster fails to meet Submariner's requirements:")
for i := range failedRequirements {
klog.Infof("* %s", (failedRequirements)[i])
}
return fmt.Errorf("the target cluster fails to meet Submariner's requirements")
}
if err != nil {
klog.Errorf("Unable to check all requirements: %v", err)
return err
}
if brokerInfo.IsConnectivityEnabled() && joinConfig.LabelGateway {
if err := r.HandleNodeLabels(); err != nil {
klog.Errorf("Unable to set the gateway node up: %v", err)
return err
}
}
klog.Info("Discovering network details")
networkDetails, err := r.GetNetworkDetails()
if err != nil {
klog.Errorf("Error get network details: %v", err)
return err
}
serviceCIDR, serviceCIDRautoDetected, err := getServiceCIDR(joinConfig.ServiceCIDR, networkDetails)
if err != nil {
klog.Errorf("Error determining the service CIDR: %v", err)
return err
}
clusterCIDR, clusterCIDRautoDetected, err := getPodCIDR(joinConfig.ClusterCIDR, networkDetails)
if err != nil {
klog.Errorf("Error determining the pod CIDR: %v", err)
return err
}
brokerCluster, err := brokerInfo.GetBrokerAdministratorCluster()
if err != nil {
klog.Errorf("unable to get broker cluster client: %v", err)
return err
}
brokerNamespace := string(brokerInfo.ClientToken.Data["namespace"])
netconfig := globalnet.Config{
ClusterID: joinConfig.ClusterID,
ServiceCIDR: serviceCIDR,
ServiceCIDRAutoDetected: serviceCIDRautoDetected,
ClusterCIDR: clusterCIDR,
ClusterCIDRAutoDetected: clusterCIDRautoDetected,
GlobalnetCIDR: brokerInfo.GlobalnetCIDRRange,
GlobalnetClusterSize: brokerInfo.DefaultGlobalnetClusterSize,
}
if brokerInfo.IsGlobalnetEnabled() {
if err = r.AllocateAndUpdateGlobalCIDRConfigMap(brokerCluster.GetClient(), brokerCluster.GetAPIReader(), instance, brokerNamespace, &netconfig); err != nil {
klog.Errorf("Error Discovering multi cluster details: %v", err)
return err
}
}
klog.Info("Deploying the Submariner operator")
if err = submarinerop.Ensure(r.Client, r.Config, true); err != nil {
klog.Errorf("Error deploying the operator: %v", err)
return err
}
klog.Info("Creating SA for cluster")
clienttoken, err = broker.CreateSAForCluster(brokerCluster.GetClient(), brokerCluster.GetAPIReader(), joinConfig.ClusterID)
if err != nil {
klog.Errorf("Error creating SA for cluster: %v", err)
return err
}
if brokerInfo.IsConnectivityEnabled() {
klog.Info("Deploying Submariner")
submarinerSpec, err := populateSubmarinerSpec(instance, brokerInfo, netconfig)
if err != nil {
return err
}
if err = submarinercr.Ensure(r.Client, consts.SubmarinerOperatorNamespace, submarinerSpec); err != nil {
klog.Errorf("Submariner deployment failed: %v", err)
return err
}
klog.Info("Submariner is up and running")
} else if brokerInfo.IsServiceDiscoveryEnabled() {
klog.Info("Deploying service discovery only")
serviceDiscoverySpec, err := populateServiceDiscoverySpec(instance, brokerInfo)
if err != nil {
return err
}
if err = servicediscoverycr.Ensure(r.Client, consts.SubmarinerOperatorNamespace, serviceDiscoverySpec); err != nil {
klog.Errorf("Service discovery deployment failed: %v", err)
return err
}
klog.Info("Service discovery is up and running")
}
return nil
}
func (r *FabricReconciler) AllocateAndUpdateGlobalCIDRConfigMap(c client.Client, reader client.Reader, instance *operatorv1alpha1.Fabric, brokerNamespace string,
netconfig *globalnet.Config) error {
joinConfig := instance.Spec.JoinConfig
klog.Info("Discovering multi cluster details")
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
globalnetInfo, globalnetConfigMap, err := globalnet.GetGlobalNetworks(reader, brokerNamespace)
if err != nil {
klog.Errorf("error reading Global network details on Broker: %v", err)
return err
}
netconfig.GlobalnetCIDR, err = globalnet.ValidateGlobalnetConfiguration(globalnetInfo, *netconfig)
if err != nil {
klog.Errorf("error validating Globalnet configuration: %v", err)
return err
}
if globalnetInfo.GlobalnetEnabled {
netconfig.GlobalnetCIDR, err = globalnet.AssignGlobalnetIPs(globalnetInfo, *netconfig)
if err != nil {
klog.Errorf("error assigning Globalnet IPs: %v", err)
return err
}
if globalnetInfo.GlobalCidrInfo[joinConfig.ClusterID] == nil ||
globalnetInfo.GlobalCidrInfo[joinConfig.ClusterID].GlobalCIDRs[0] != netconfig.GlobalnetCIDR {
var newClusterInfo broker.ClusterInfo
newClusterInfo.ClusterID = joinConfig.ClusterID
newClusterInfo.GlobalCidr = []string{netconfig.GlobalnetCIDR}
return broker.UpdateGlobalnetConfigMap(c, brokerNamespace, globalnetConfigMap, newClusterInfo)
}
}
return err
})
return retryErr
}
func (r *FabricReconciler) GetNetworkDetails() (*network.ClusterNetwork, error) {
dynClient, err := dynamic.NewForConfig(r.Config)
if err != nil {
return nil, err
}
networkDetails, err := network.Discover(dynClient, r.Client, consts.SubmarinerOperatorNamespace)
if err != nil {
klog.Errorf("Error trying to discover network details: %v", err)
} else if networkDetails != nil {
networkDetails.Show()
}
return networkDetails, nil
}
func getPodCIDR(clusterCIDR string, nd *network.ClusterNetwork) (cidrType string, autodetected bool, err error) {
if clusterCIDR != "" {
if nd != nil && len(nd.PodCIDRs) > 0 && nd.PodCIDRs[0] != clusterCIDR {
klog.Warningf("Your provided cluster CIDR for the pods (%s) does not match discovered (%s)",
clusterCIDR, nd.PodCIDRs[0])
}
return clusterCIDR, false, nil
} else if nd != nil && len(nd.PodCIDRs) > 0 {
return nd.PodCIDRs[0], true, nil
}
return "", true, fmt.Errorf("not found invalidate cluster CIDR")
}
func getServiceCIDR(serviceCIDR string, nd *network.ClusterNetwork) (cidrType string, autodetected bool, err error) {
if serviceCIDR != "" {
if nd != nil && len(nd.ServiceCIDRs) > 0 && nd.ServiceCIDRs[0] != serviceCIDR {
klog.Warningf("Your provided service CIDR (%s) does not match discovered (%s)",
serviceCIDR, nd.ServiceCIDRs[0])
}
return serviceCIDR, false, nil
} else if nd != nil && len(nd.ServiceCIDRs) > 0 {
return nd.ServiceCIDRs[0], true, nil
}
return "", true, fmt.Errorf("not found invalidate service CIDR")
}
func isValidClusterID(clusterID string) (bool, error) {
// Make sure the clusterid is a valid DNS-1123 string
if match, _ := regexp.MatchString("^[a-z0-9][a-z0-9.-]*[a-z0-9]$", clusterID); !match {
return false, fmt.Errorf("cluster IDs must be valid DNS-1123 names, with only lowercase alphanumerics,\n"+
"'.' or '-' (and the first and last characters must be alphanumerics).\n"+
"%s doesn't meet these requirements", clusterID)
}
return true, nil
}
func populateSubmarinerSpec(instance *operatorv1alpha1.Fabric, brokerInfo *broker.BrokerInfo, netconfig globalnet.Config) (*submariner.SubmarinerSpec, error) {
joinConfig := instance.Spec.JoinConfig
brokerURL := brokerInfo.BrokerURL
if idx := strings.Index(brokerURL, "://"); idx >= 0 {
// Submariner doesn't work with a schema prefix
brokerURL = brokerURL[(idx + 3):]
}
// if our network discovery code was capable of discovering those CIDRs
// we don't need to explicitly set it in the operator
crServiceCIDR := ""
if !netconfig.ServiceCIDRAutoDetected {
crServiceCIDR = netconfig.ServiceCIDR
}
crClusterCIDR := ""
if !netconfig.ClusterCIDRAutoDetected {
crClusterCIDR = netconfig.ClusterCIDR
}
// customDomains := ""
if joinConfig.CustomDomains == nil && brokerInfo.CustomDomains != nil {
joinConfig.CustomDomains = *brokerInfo.CustomDomains
}
imageOverrides, err := getImageOverrides(instance)
if err != nil {
return nil, err
}
submarinerSpec := &submariner.SubmarinerSpec{
Repository: getImageRepo(instance),
Version: getImageVersion(instance),
CeIPSecNATTPort: joinConfig.NattPort,
CeIPSecIKEPort: joinConfig.IkePort,
CeIPSecDebug: joinConfig.IpsecDebug,
CeIPSecForceUDPEncaps: joinConfig.ForceUDPEncaps,
CeIPSecPreferredServer: joinConfig.PreferredServer,
CeIPSecPSK: base64.StdEncoding.EncodeToString(brokerInfo.IPSecPSK.Data["psk"]),
BrokerK8sCA: base64.StdEncoding.EncodeToString(brokerInfo.ClientToken.Data["ca.crt"]),
BrokerK8sRemoteNamespace: string(brokerInfo.ClientToken.Data["namespace"]),
BrokerK8sApiServerToken: string(clienttoken.Data["token"]),
BrokerK8sApiServer: brokerURL,
Broker: "k8s",
NatEnabled: joinConfig.NatTraversal,
Debug: joinConfig.SubmarinerDebug,
ClusterID: joinConfig.ClusterID,
ServiceCIDR: crServiceCIDR,
ClusterCIDR: crClusterCIDR,
Namespace: consts.SubmarinerOperatorNamespace,
CableDriver: joinConfig.CableDriver,
ServiceDiscoveryEnabled: brokerInfo.IsServiceDiscoveryEnabled(),
ImageOverrides: imageOverrides,
GlobalCIDR: brokerInfo.GlobalnetCIDRRange,
}
if netconfig.GlobalnetCIDR != "" {
submarinerSpec.GlobalCIDR = netconfig.GlobalnetCIDR
}
if joinConfig.CorednsCustomConfigMap != "" {
namespace, name := getCustomCoreDNSParams(instance)
submarinerSpec.CoreDNSCustomConfig = &submariner.CoreDNSCustomConfig{
ConfigMapName: name,
Namespace: namespace,
}
}
if brokerInfo.CustomDomains != nil && len(*brokerInfo.CustomDomains) > 0 {
submarinerSpec.CustomDomains = *brokerInfo.CustomDomains
}
return submarinerSpec, nil
}
func getImageVersion(instance *operatorv1alpha1.Fabric) string {
version := instance.Spec.JoinConfig.ImageVersion
if version == "" {
version = versions.DefaultSubmarinerOperatorVersion
}
return version
}
func getImageRepo(instance *operatorv1alpha1.Fabric) string {
repo := instance.Spec.JoinConfig.Repository
if repo == "" {
repo = versions.DefaultRepo
}
return repo
}
func removeSchemaPrefix(brokerURL string) string {
if idx := strings.Index(brokerURL, "://"); idx >= 0 {
// Submariner doesn't work with a schema prefix
brokerURL = brokerURL[(idx + 3):]
}
return brokerURL
}
func populateServiceDiscoverySpec(instance *operatorv1alpha1.Fabric, brokerInfo *broker.BrokerInfo) (*submariner.ServiceDiscoverySpec, error) {
brokerURL := removeSchemaPrefix(brokerInfo.BrokerURL)
joinConfig := instance.Spec.JoinConfig
var customDomains []string
if joinConfig.CustomDomains == nil && brokerInfo.CustomDomains != nil {
customDomains = *brokerInfo.CustomDomains
}
imageOverrides, err := getImageOverrides(instance)
if err != nil {
return nil, err
}
serviceDiscoverySpec := submariner.ServiceDiscoverySpec{
Repository: joinConfig.Repository,
Version: joinConfig.ImageVersion,
BrokerK8sCA: base64.StdEncoding.EncodeToString(brokerInfo.ClientToken.Data["ca.crt"]),
BrokerK8sRemoteNamespace: string(brokerInfo.ClientToken.Data["namespace"]),
BrokerK8sApiServerToken: string(clienttoken.Data["token"]),
BrokerK8sApiServer: brokerURL,
Debug: joinConfig.SubmarinerDebug,
ClusterID: joinConfig.ClusterID,
Namespace: consts.SubmarinerOperatorNamespace,
ImageOverrides: imageOverrides,
GlobalnetEnabled: brokerInfo.IsGlobalnetEnabled(),
}
if joinConfig.CorednsCustomConfigMap != "" {
namespace, name := getCustomCoreDNSParams(instance)
serviceDiscoverySpec.CoreDNSCustomConfig = &submariner.CoreDNSCustomConfig{
ConfigMapName: name,
Namespace: namespace,
}
}
if len(customDomains) > 0 {
serviceDiscoverySpec.CustomDomains = customDomains
}
return &serviceDiscoverySpec, nil
}
func getImageOverrides(instance *operatorv1alpha1.Fabric) (map[string]string, error) {
joinConfig := instance.Spec.JoinConfig
if len(joinConfig.ImageOverrideArr) > 0 {
imageOverrides := make(map[string]string)
for _, s := range joinConfig.ImageOverrideArr {
key := strings.Split(s, "=")[0]
if invalidImageName(key) {
klog.Errorf("Invalid image name %s provided. Please choose from %q", key, names.ValidImageNames)
return nil, fmt.Errorf("invalid image name %s provided. Please choose from %q", key, names.ValidImageNames)
}
value := strings.Split(s, "=")[1]
imageOverrides[key] = value
}
return imageOverrides, nil
}
return nil, nil
}
func invalidImageName(key string) bool {
for _, name := range names.ValidImageNames {
if key == name {
return false
}
}
return true
}
func isValidCustomCoreDNSConfig(instance *operatorv1alpha1.Fabric) error {
corednsCustomConfigMap := instance.Spec.JoinConfig.CorednsCustomConfigMap
if corednsCustomConfigMap != "" && strings.Count(corednsCustomConfigMap, "/") > 1 {
klog.Error("coredns-custom-configmap should be in <namespace>/<name> format, namespace is optional")
return fmt.Errorf("coredns-custom-configmap should be in <namespace>/<name> format, namespace is optional")
}
return nil
}
func getCustomCoreDNSParams(instance *operatorv1alpha1.Fabric) (namespace, name string) {
corednsCustomConfigMap := instance.Spec.JoinConfig.CorednsCustomConfigMap
if corednsCustomConfigMap != "" {
name = corednsCustomConfigMap
paramList := strings.Split(corednsCustomConfigMap, "/")
if len(paramList) > 1 {
namespace = paramList[0]
name = paramList[1]
}
}
return namespace, name
}
func (r *FabricReconciler) HandleNodeLabels() error {
const submarinerGatewayLabel = "submariner.io/gateway"
const trueLabel = "true"
selector, err := labels.Parse("submariner.io/gateway=true")
if err != nil {
return err
}
opts := &client.ListOptions{
LabelSelector: selector,
}
nodes := &v1.NodeList{}
if err := r.Client.List(context.TODO(), nodes, opts); err != nil {
return err
}
if len(nodes.Items) > 0 {
klog.Infof("* There are %d labeled nodes in the cluster:", len(nodes.Items))
for _, node := range nodes.Items {
klog.Infof(" - %s", node.GetName())
}
} else {
node, err := r.getWorkerNodeForGateway()
if err != nil {
return err
}
if node == nil {
klog.Info("* No worker node found to label as the gateway")
} else {
if err = r.addLabelsToNode(node.GetName(), map[string]string{submarinerGatewayLabel: trueLabel}); err != nil {
klog.Errorf("Error labeling the gateway node: %v", err)
return err
}
}
}
return nil
}
func (r *FabricReconciler) getWorkerNodeForGateway() (*v1.Node, error) {
// List the worker nodes and select one
workerNodes := &v1.NodeList{}
workerSelector, err := labels.Parse("node-role.kubernetes.io/worker")
if err != nil {
klog.Errorf("Parse node label failed: %v", err)
return nil, err
}
workerOpts := &client.ListOptions{
LabelSelector: workerSelector,
}
if err := r.Client.List(context.TODO(), workerNodes, workerOpts); err != nil {
klog.Errorf("List worker node failed: %v", err)
return nil, err
}
if len(workerNodes.Items) == 0 {
// In some deployments (like KIND), worker nodes are not explicitly labeled. So list non-master nodes.
workerSelector, err := labels.Parse("!node-role.kubernetes.io/master")
if err != nil {
klog.Errorf("Parse node label failed: %v", err)
return nil, err
}
workerOpts := &client.ListOptions{
LabelSelector: workerSelector,
}
if err := r.Client.List(context.TODO(), workerNodes, workerOpts); err != nil {
klog.Errorf("List non-master node failed: %v", err)
return nil, err
}
if len(workerNodes.Items) == 0 {
return nil, fmt.Errorf("not found any valid worker node for label")
}
}
// Return the first node
return &workerNodes.Items[0], nil
}
// this function was sourced from:
// https://github.com/kubernetes/kubernetes/blob/a3ccea9d8743f2ff82e41b6c2af6dc2c41dc7b10/test/utils/density_utils.go#L36
func (r *FabricReconciler) addLabelsToNode(nodeName string, labelsToAdd map[string]string) error {
var tokens = make([]string, 0, len(labelsToAdd))
for k, v := range labelsToAdd {
tokens = append(tokens, fmt.Sprintf("\"%s\":\"%s\"", k, v))
}
labelString := "{" + strings.Join(tokens, ",") + "}"
patch := []byte(fmt.Sprintf(`{"metadata":{"labels":%v}}`, labelString))
var lastErr error
err := wait.ExponentialBackoff(nodeLabelBackoff, func() (bool, error) {
node := &v1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName}}
lastErr := r.Client.Patch(context.TODO(), node, client.RawPatch(types.StrategicMergePatchType, patch))
if lastErr != nil {
if !errors.IsConflict(lastErr) {
return false, lastErr
}
return false, nil
} else {
return true, nil
}
})
if err == wait.ErrWaitTimeout {
return lastErr
}
return err
}