forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provisioner.go
896 lines (756 loc) · 26.5 KB
/
provisioner.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
package clusterprovisioner
import (
"context"
"fmt"
"reflect"
"sort"
"strings"
"time"
"github.com/mitchellh/mapstructure"
"github.com/rancher/kontainer-engine/drivers/rke"
"github.com/rancher/kontainer-engine/service"
"github.com/rancher/norman/controller"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/slice"
"github.com/rancher/norman/types/values"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/rancher/pkg/rkedialerfactory"
"github.com/rancher/rancher/pkg/settings"
"github.com/rancher/rke/services"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/flowcontrol"
)
const (
RKEDriverKey = "rancherKubernetesEngineConfig"
KontainerEngineUpdate = "provisioner.cattle.io/ke-driver-update"
)
type Provisioner struct {
ClusterController v3.ClusterController
Clusters v3.ClusterInterface
NodeLister v3.NodeLister
engineService *service.EngineService
backoff *flowcontrol.Backoff
KontainerDriverLister v3.KontainerDriverLister
DynamicSchemasLister v3.DynamicSchemaLister
Backups v3.EtcdBackupLister
}
func Register(ctx context.Context, management *config.ManagementContext) {
p := &Provisioner{
engineService: service.NewEngineService(NewPersistentStore(management.Core.Namespaces(""), management.Core)),
Clusters: management.Management.Clusters(""),
ClusterController: management.Management.Clusters("").Controller(),
NodeLister: management.Management.Nodes("").Controller().Lister(),
backoff: flowcontrol.NewBackOff(30*time.Second, 10*time.Minute),
KontainerDriverLister: management.Management.KontainerDrivers("").Controller().Lister(),
DynamicSchemasLister: management.Management.DynamicSchemas("").Controller().Lister(),
Backups: management.Management.EtcdBackups("").Controller().Lister(),
}
// Add handlers
p.Clusters.AddLifecycle(ctx, "cluster-provisioner-controller", p)
management.Management.Nodes("").AddHandler(ctx, "cluster-provisioner-controller", p.machineChanged)
local := &rkedialerfactory.RKEDialerFactory{
Factory: management.Dialer,
}
docker := &rkedialerfactory.RKEDialerFactory{
Factory: management.Dialer,
Docker: true,
}
driver := service.Drivers[service.RancherKubernetesEngineDriverName]
rkeDriver := driver.(*rke.Driver)
rkeDriver.DockerDialer = docker.Build
rkeDriver.LocalDialer = local.Build
rkeDriver.WrapTransportFactory = docker.WrapTransport
}
func (p *Provisioner) Remove(cluster *v3.Cluster) (runtime.Object, error) {
logrus.Infof("Deleting cluster [%s]", cluster.Name)
if skipProvisioning(cluster) ||
cluster.Status.Driver == "" {
return nil, nil
}
for i := 0; i < 4; i++ {
// cluster will be forcefully removed on last attempt
err := p.driverRemove(cluster, i == 3)
if err == nil {
break
}
if i == 3 {
return cluster, fmt.Errorf("failed to remove the cluster [%s]: %v", cluster.Name, err)
}
time.Sleep(1 * time.Second)
}
logrus.Infof("Deleted cluster [%s]", cluster.Name)
// cluster object will definitely have changed, reload
return p.Clusters.Get(cluster.Name, metav1.GetOptions{})
}
func (p *Provisioner) Updated(cluster *v3.Cluster) (runtime.Object, error) {
obj, err := v3.ClusterConditionUpdated.Do(cluster, func() (runtime.Object, error) {
anno, _ := cluster.Annotations[KontainerEngineUpdate]
if anno == "updated" {
// Cluster has already been updated proceed as usual
setVersion(cluster)
return p.update(cluster, false)
} else if strings.HasPrefix(anno, "updating/") {
// Check if it's been updating for more than 20 seconds, this lets
// the controller take over attempting to update the cluster
pieces := strings.Split(anno, "/")
t, err := time.Parse(time.RFC3339, pieces[1])
if err != nil || int(time.Since(t)/time.Second) > 20 {
cluster.Annotations[KontainerEngineUpdate] = "updated"
return p.Clusters.Update(cluster)
}
// Go routine is already running to update the cluster so wait
return nil, nil
}
// Set the annotation and kickoff the update
c, err := p.setKontainerEngineUpdate(cluster, "updating")
if err != nil {
return cluster, err
}
go p.waitForSchema(c)
return nil, nil
})
return obj.(*v3.Cluster), err
}
// waitForSchema waits for the driver and schema to be populated for the cluster
func (p *Provisioner) waitForSchema(cluster *v3.Cluster) {
var driver string
if cluster.Spec.GenericEngineConfig == nil {
if cluster.Spec.AmazonElasticContainerServiceConfig != nil {
driver = "amazonelasticcontainerservice"
}
if cluster.Spec.AzureKubernetesServiceConfig != nil {
driver = "azurekubernetesservice"
}
if cluster.Spec.GoogleKubernetesEngineConfig != nil {
driver = "googlekubernetesengine"
}
} else {
if d, ok := (*cluster.Spec.GenericEngineConfig)["driverName"]; ok {
driver = d.(string)
}
}
if driver != "" {
var schemaName string
backoff := wait.Backoff{
Duration: 2 * time.Second,
Factor: 1,
Jitter: 0,
Steps: 7,
}
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
driver, err := p.KontainerDriverLister.Get("", driver)
if err != nil {
if !apierrors.IsNotFound(err) {
return false, err
}
return false, nil
}
if driver.Spec.BuiltIn {
schemaName = driver.Status.DisplayName + "Config"
} else {
schemaName = driver.Status.DisplayName + "EngineConfig"
}
_, err = p.DynamicSchemasLister.Get("", strings.ToLower(schemaName))
if err != nil {
if !apierrors.IsNotFound(err) {
return false, err
}
return false, nil
}
return true, nil
})
if err != nil {
logrus.Warnf("[cluster-provisioner-controller] Failed to find driver %v and schema %v for cluster %v on upgrade: %v",
driver, schemaName, cluster.Name, err)
}
}
_, err := p.setKontainerEngineUpdate(cluster, "updated")
if err != nil {
logrus.Warnf("[cluster-provisioner-controller] Failed to set annotation on cluster %v on upgrade: %v", cluster.Name, err)
}
p.ClusterController.Enqueue(cluster.Namespace, cluster.Name)
}
func (p *Provisioner) setKontainerEngineUpdate(cluster *v3.Cluster, anno string) (*v3.Cluster, error) {
backoff := wait.Backoff{
Duration: 500 * time.Millisecond,
Factor: 1,
Jitter: 0,
Steps: 6,
}
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
newCluster, err := p.Clusters.Get(cluster.Name, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return false, err
}
return false, nil
}
if anno == "updating" {
// Add a timestamp for comparison since this anno was added
anno = anno + "/" + time.Now().Format(time.RFC3339)
}
newCluster.Annotations[KontainerEngineUpdate] = anno
newCluster, err = p.Clusters.Update(newCluster)
if err != nil {
if apierrors.IsConflict(err) {
return false, nil
}
return false, err
}
cluster = newCluster
return true, nil
})
if err != nil {
return cluster, fmt.Errorf("Failed to update cluster [%s]: %v", cluster.Name, err)
}
return cluster, nil
}
func setVersion(cluster *v3.Cluster) {
if cluster.Spec.RancherKubernetesEngineConfig != nil {
if cluster.Spec.RancherKubernetesEngineConfig.Version == "" {
//set version from the applied spec
if cluster.Status.AppliedSpec.RancherKubernetesEngineConfig != nil {
if cluster.Status.AppliedSpec.RancherKubernetesEngineConfig.Version != "" {
cluster.Spec.RancherKubernetesEngineConfig.Version = cluster.Status.AppliedSpec.RancherKubernetesEngineConfig.Version
} else {
cluster.Spec.RancherKubernetesEngineConfig.Version = settings.KubernetesVersion.Get()
}
}
}
} else if cluster.Spec.AmazonElasticContainerServiceConfig != nil {
if cluster.Status.Version != nil {
setConfigVersion := func(config *v3.MapStringInterface) {
v, found := values.GetValue(*config, "kubernetesVersion")
if !found || convert.ToString(v) == "" && cluster.Status.Version != nil && cluster.Status.Version.Major != "" && len(cluster.Status.Version.Minor) > 1 {
values.PutValue(*config, fmt.Sprintf("%s.%s", cluster.Status.Version.Major, cluster.Status.Version.Minor[:2]), "kubernetesVersion")
}
}
// during upgrade it is possible genericEngineConfig has not been set
if newConfig := cluster.Spec.AmazonElasticContainerServiceConfig; newConfig != nil {
setConfigVersion(newConfig)
}
if oldConfig := cluster.Status.AppliedSpec.AmazonElasticContainerServiceConfig; oldConfig != nil {
setConfigVersion(oldConfig)
}
}
}
}
func (p *Provisioner) update(cluster *v3.Cluster, create bool) (*v3.Cluster, error) {
cluster, err := p.reconcileCluster(cluster, create)
if err != nil {
return cluster, err
}
v3.ClusterConditionProvisioned.True(cluster)
v3.ClusterConditionProvisioned.Message(cluster, "")
v3.ClusterConditionProvisioned.Reason(cluster, "")
v3.ClusterConditionPending.True(cluster)
return cluster, nil
}
func (p *Provisioner) machineChanged(key string, machine *v3.Node) (runtime.Object, error) {
parts := strings.SplitN(key, "/", 2)
p.ClusterController.Enqueue("", parts[0])
return machine, nil
}
func (p *Provisioner) Create(cluster *v3.Cluster) (runtime.Object, error) {
var err error
// Initialize conditions, be careful to not continually update them
v3.ClusterConditionPending.CreateUnknownIfNotExists(cluster)
v3.ClusterConditionProvisioned.CreateUnknownIfNotExists(cluster)
if v3.ClusterConditionWaiting.GetStatus(cluster) == "" {
v3.ClusterConditionWaiting.Unknown(cluster)
}
if v3.ClusterConditionWaiting.GetMessage(cluster) == "" {
v3.ClusterConditionWaiting.Message(cluster, "Waiting for API to be available")
}
cluster, err = p.pending(cluster)
if err != nil {
return cluster, err
}
return p.provision(cluster)
}
func (p *Provisioner) provision(cluster *v3.Cluster) (*v3.Cluster, error) {
obj, err := v3.ClusterConditionProvisioned.Do(cluster, func() (runtime.Object, error) {
return p.update(cluster, true)
})
return obj.(*v3.Cluster), err
}
func (p *Provisioner) pending(cluster *v3.Cluster) (*v3.Cluster, error) {
if skipProvisioning(cluster) {
return cluster, nil
}
driver, err := p.validateDriver(cluster)
if err != nil {
return cluster, err
}
if driver == "" {
return cluster, &controller.ForgetError{
Err: fmt.Errorf("waiting for full cluster configuration"),
Reason: "Pending"}
}
if driver != cluster.Status.Driver {
cluster.Status.Driver = driver
if driver == v3.ClusterDriverRKE && cluster.Spec.RancherKubernetesEngineConfig == nil {
cluster.Spec.RancherKubernetesEngineConfig = &v3.RancherKubernetesEngineConfig{}
}
return p.Clusters.Update(cluster)
}
return cluster, nil
}
func (p *Provisioner) backoffFailure(cluster *v3.Cluster, spec *v3.ClusterSpec) (bool, time.Duration) {
if cluster.Status.FailedSpec == nil {
return false, 0
}
if !reflect.DeepEqual(cluster.Status.FailedSpec, spec) {
return false, 0
}
if p.backoff.IsInBackOffSinceUpdate(cluster.Name, time.Now()) {
go func() {
time.Sleep(p.backoff.Get(cluster.Name))
p.ClusterController.Enqueue("", cluster.Name)
}()
return true, p.backoff.Get(cluster.Name)
}
return false, 0
}
// reconcileCluster returns true if waiting or false if ready to provision
func (p *Provisioner) reconcileCluster(cluster *v3.Cluster, create bool) (*v3.Cluster, error) {
if skipProvisioning(cluster) {
return cluster, nil
}
var (
apiEndpoint, serviceAccountToken, caCert string
err error
)
if cluster.Name != "local" && !v3.ClusterConditionServiceAccountMigrated.IsTrue(cluster) &&
v3.ClusterConditionProvisioned.IsTrue(cluster) {
driverName, err := p.validateDriver(cluster)
if err != nil {
return nil, err
}
spec, _, err := p.getConfig(true, cluster.Spec, driverName, cluster.Name)
if err != nil {
return nil, err
}
serviceAccountToken, err = p.generateServiceAccount(cluster, *spec)
if err != nil {
return nil, err
}
cluster.Status.ServiceAccountToken = serviceAccountToken
v3.ClusterConditionServiceAccountMigrated.True(cluster)
// Update the cluster in k8s
cluster, err = p.Clusters.Update(cluster)
if err != nil {
return nil, err
}
err = p.removeLegacyServiceAccount(cluster, *spec)
if err != nil {
return nil, err
}
}
p.setGenericConfigs(cluster)
spec, err := p.getSpec(cluster)
if err != nil || spec == nil {
return cluster, err
}
if ok, delay := p.backoffFailure(cluster, spec); ok {
return cluster, &controller.ForgetError{Err: fmt.Errorf("backing off failure, delay: %v", delay)}
}
logrus.Infof("Provisioning cluster [%s]", cluster.Name)
if create {
logrus.Infof("Creating cluster [%s]", cluster.Name)
apiEndpoint, serviceAccountToken, caCert, err = p.driverCreate(cluster, *spec)
if err != nil && err.Error() == "cluster already exists" {
logrus.Infof("Create done, Updating cluster [%s]", cluster.Name)
apiEndpoint, serviceAccountToken, caCert, err = p.driverUpdate(cluster, *spec)
}
} else if spec.RancherKubernetesEngineConfig != nil && spec.RancherKubernetesEngineConfig.Restore.Restore {
logrus.Infof("Restoring cluster [%s] from backup", cluster.Name)
apiEndpoint, serviceAccountToken, caCert, err = p.restoreClusterBackup(cluster, *spec)
} else if spec.RancherKubernetesEngineConfig != nil && spec.RancherKubernetesEngineConfig.RotateCertificates != nil {
logrus.Infof("Rotating certificates for cluster [%s]", cluster.Name)
apiEndpoint, serviceAccountToken, caCert, err = p.driverUpdate(cluster, *spec)
} else {
logrus.Infof("Updating cluster [%s]", cluster.Name)
// Attempt to manually trigger updating, otherwise it will not be triggered until after exiting reconcile
v3.ClusterConditionUpdated.Unknown(cluster)
cluster, err = p.Clusters.Update(cluster)
if err != nil {
return cluster, fmt.Errorf("Failed to update cluster [%s]: %v", cluster.Name, err)
}
apiEndpoint, serviceAccountToken, caCert, err = p.driverUpdate(cluster, *spec)
}
// at this point we know the cluster has been modified in driverCreate/Update so reload
if newCluster, reloadErr := p.Clusters.Get(cluster.Name, metav1.GetOptions{}); reloadErr == nil {
cluster = newCluster
}
cluster, recordErr := p.recordFailure(cluster, *spec, err)
if recordErr != nil {
return cluster, recordErr
}
// for here out we want to always return the cluster, not just nil, so that the error can be properly
// recorded if needs be
if err != nil {
return cluster, err
}
err = p.removeLegacyServiceAccount(cluster, *spec)
if err != nil {
return nil, err
}
v3.ClusterConditionServiceAccountMigrated.True(cluster)
saved := false
for i := 0; i < 20; i++ {
cluster, err = p.Clusters.Get(cluster.Name, metav1.GetOptions{})
if err != nil {
return cluster, err
}
censoredSpec, err := p.censorGenericEngineConfig(*spec)
if err != nil {
return cluster, err
}
cluster.Status.AppliedSpec = censoredSpec
cluster.Status.APIEndpoint = apiEndpoint
cluster.Status.ServiceAccountToken = serviceAccountToken
cluster.Status.CACert = caCert
resetRkeConfigFlags(cluster)
if cluster, err = p.Clusters.Update(cluster); err == nil {
saved = true
break
} else {
logrus.Errorf("failed to update cluster [%s]: %v", cluster.Name, err)
time.Sleep(2)
}
}
if !saved {
return cluster, fmt.Errorf("failed to update cluster")
}
logrus.Infof("Provisioned cluster [%s]", cluster.Name)
return cluster, nil
}
func (p *Provisioner) setGenericConfigs(cluster *v3.Cluster) {
if cluster.Spec.GenericEngineConfig == nil || cluster.Status.AppliedSpec.GenericEngineConfig == nil {
setGenericConfig := func(spec *v3.ClusterSpec) {
if spec.GenericEngineConfig == nil {
if spec.AmazonElasticContainerServiceConfig != nil {
spec.GenericEngineConfig = spec.AmazonElasticContainerServiceConfig
(*spec.GenericEngineConfig)["driverName"] = "amazonelasticcontainerservice"
spec.AmazonElasticContainerServiceConfig = nil
}
if spec.AzureKubernetesServiceConfig != nil {
spec.GenericEngineConfig = spec.AzureKubernetesServiceConfig
(*spec.GenericEngineConfig)["driverName"] = "azurekubernetesservice"
spec.AzureKubernetesServiceConfig = nil
}
if spec.GoogleKubernetesEngineConfig != nil {
spec.GenericEngineConfig = spec.GoogleKubernetesEngineConfig
(*spec.GenericEngineConfig)["driverName"] = "googlekubernetesengine"
spec.GoogleKubernetesEngineConfig = nil
}
}
}
setGenericConfig(&cluster.Spec)
setGenericConfig(&cluster.Status.AppliedSpec)
}
}
func resetRkeConfigFlags(cluster *v3.Cluster) {
if cluster.Spec.RancherKubernetesEngineConfig != nil {
cluster.Spec.RancherKubernetesEngineConfig.RotateCertificates = nil
cluster.Spec.RancherKubernetesEngineConfig.Restore = v3.RestoreConfig{}
if cluster.Status.AppliedSpec.RancherKubernetesEngineConfig != nil {
cluster.Status.AppliedSpec.RancherKubernetesEngineConfig.RotateCertificates = nil
cluster.Status.AppliedSpec.RancherKubernetesEngineConfig.Restore = v3.RestoreConfig{}
}
}
}
func copyMap(toCopy v3.MapStringInterface) v3.MapStringInterface {
newMap := v3.MapStringInterface{}
for k, v := range toCopy {
newMap[k] = v
}
return newMap
}
func (p *Provisioner) censorGenericEngineConfig(input v3.ClusterSpec) (v3.ClusterSpec, error) {
if input.GenericEngineConfig == nil {
// nothing to do
return input, nil
}
config := copyMap(*input.GenericEngineConfig)
driverName, ok := config[DriverNameField].(string)
if !ok {
// can't figure out driver type so blank out the whole thing
logrus.Warnf("cluster %v has a generic engine config but no driver type field; can't hide password "+
"fields so removing the entire config", input.DisplayName)
input.GenericEngineConfig = nil
return input, nil
}
driver, err := p.KontainerDriverLister.Get("", driverName)
if err != nil {
return v3.ClusterSpec{}, err
}
var schemaName string
if driver.Spec.BuiltIn {
schemaName = driver.Status.DisplayName + "Config"
} else {
schemaName = driver.Status.DisplayName + "EngineConfig"
}
kontainerDriverSchema, err := p.DynamicSchemasLister.Get("", strings.ToLower(schemaName))
if err != nil {
return v3.ClusterSpec{}, fmt.Errorf("error getting dynamic schema %v", err)
}
for key := range config {
field := kontainerDriverSchema.Spec.ResourceFields[key]
if field.Type == "password" {
delete(config, key)
}
}
input.GenericEngineConfig = &config
return input, nil
}
func skipProvisioning(cluster *v3.Cluster) bool {
return cluster.Status.Driver == v3.ClusterDriverLocal || cluster.Status.Driver == v3.ClusterDriverImported
}
func (p *Provisioner) getConfig(reconcileRKE bool, spec v3.ClusterSpec, driverName, clusterName string) (*v3.ClusterSpec, interface{}, error) {
var v interface{}
if spec.GenericEngineConfig == nil {
if spec.RancherKubernetesEngineConfig != nil {
var err error
v, err = convert.EncodeToMap(spec.RancherKubernetesEngineConfig)
if err != nil {
return nil, nil, err
}
} else {
v = map[string]interface{}{}
}
} else {
v = *spec.GenericEngineConfig
}
if driverName == v3.ClusterDriverRKE && reconcileRKE {
nodes, err := p.reconcileRKENodes(clusterName)
if err != nil {
return nil, nil, err
}
systemImages, err := getSystemImages(spec)
if err != nil {
return nil, nil, err
}
copy := *spec.RancherKubernetesEngineConfig
spec.RancherKubernetesEngineConfig = ©
spec.RancherKubernetesEngineConfig.Nodes = nodes
spec.RancherKubernetesEngineConfig.SystemImages = *systemImages
data, _ := convert.EncodeToMap(spec)
v, _ = data[RKEDriverKey]
}
return &spec, v, nil
}
func (p *Provisioner) getDriver(cluster *v3.Cluster) (string, error) {
var driver *v3.KontainerDriver
var err error
if cluster.Spec.GenericEngineConfig != nil {
kontainerDriverName := (*cluster.Spec.GenericEngineConfig)["driverName"].(string)
driver, err = p.KontainerDriverLister.Get("", kontainerDriverName)
if err != nil {
return "", err
}
}
if cluster.Spec.RancherKubernetesEngineConfig != nil {
return v3.ClusterDriverRKE, nil
}
if driver == nil {
return "", nil
}
return driver.Status.DisplayName, nil
}
func (p *Provisioner) validateDriver(cluster *v3.Cluster) (string, error) {
oldDriver := cluster.Status.Driver
if oldDriver == v3.ClusterDriverImported {
return v3.ClusterDriverImported, nil
}
newDriver, err := p.getDriver(cluster)
if err != nil {
return "", err
}
if oldDriver == "" && newDriver == "" {
return newDriver, nil
}
if oldDriver == "" {
return newDriver, nil
}
if newDriver == "" {
return "", &controller.ForgetError{
Err: fmt.Errorf("waiting for nodes"),
Reason: "Pending",
}
}
if oldDriver != newDriver {
return newDriver, fmt.Errorf("driver change from %s to %s not allowed", oldDriver, newDriver)
}
return newDriver, nil
}
func getSystemImages(spec v3.ClusterSpec) (*v3.RKESystemImages, error) {
// fetch system images from settings
version := spec.RancherKubernetesEngineConfig.Version
systemImages, ok := v3.AllK8sVersions[version]
if !ok {
return nil, fmt.Errorf("failed to find system images for version %v", version)
}
privateRegistry := getPrivateRepo(spec.RancherKubernetesEngineConfig)
if privateRegistry == "" {
return &systemImages, nil
}
// prepend private repo
imagesMap, err := convert.EncodeToMap(systemImages)
if err != nil {
return nil, err
}
updatedMap := make(map[string]interface{})
for key, value := range imagesMap {
newValue := fmt.Sprintf("%s/%s", privateRegistry, value)
updatedMap[key] = newValue
}
if err := mapstructure.Decode(updatedMap, &systemImages); err != nil {
return nil, err
}
return &systemImages, nil
}
func getPrivateRepo(config *v3.RancherKubernetesEngineConfig) string {
if len(config.PrivateRegistries) > 0 {
return config.PrivateRegistries[0].URL
}
return settings.SystemDefaultRegistry.Get()
}
func (p *Provisioner) getSpec(cluster *v3.Cluster) (*v3.ClusterSpec, error) {
driverName, err := p.validateDriver(cluster)
if err != nil {
return nil, err
}
censoredOldSpec, err := p.censorGenericEngineConfig(cluster.Status.AppliedSpec)
if err != nil {
return nil, err
}
_, oldConfig, err := p.getConfig(false, censoredOldSpec, driverName, cluster.Name)
if err != nil {
return nil, err
}
censoredSpec, err := p.censorGenericEngineConfig(cluster.Spec)
if err != nil {
return nil, err
}
newSpec, newConfig, err := p.getConfig(true, censoredSpec, driverName, cluster.Name)
if err != nil {
return nil, err
}
// Version is the only parameter that can be updated for EKS, if they is equal we do not need to update
// TODO: Replace with logic that is more adaptable
if cluster.Spec.GenericEngineConfig != nil && (*cluster.Spec.GenericEngineConfig)["driverName"] == "amazonelasticcontainerservice" &&
cluster.Status.AppliedSpec.GenericEngineConfig != nil && (*cluster.Spec.GenericEngineConfig)["kubernetesVersion"] ==
(*cluster.Status.AppliedSpec.GenericEngineConfig)["kubernetesVersion"] {
return nil, nil
}
if reflect.DeepEqual(oldConfig, newConfig) {
return nil, nil
}
newSpec, _, err = p.getConfig(true, cluster.Spec, driverName, cluster.Name)
return newSpec, err
}
func (p *Provisioner) reconcileRKENodes(clusterName string) ([]v3.RKEConfigNode, error) {
machines, err := p.NodeLister.List(clusterName, labels.Everything())
if err != nil {
return nil, err
}
etcd := false
controlplane := false
var nodes []v3.RKEConfigNode
for _, machine := range machines {
if machine.DeletionTimestamp != nil {
continue
}
if v3.NodeConditionProvisioned.IsUnknown(machine) && (machine.Spec.Etcd || machine.Spec.ControlPlane) {
return nil, &controller.ForgetError{
Err: fmt.Errorf("waiting for %s to finish provisioning", machine.Spec.RequestedHostname),
Reason: "Provisioning",
}
}
if machine.Status.NodeConfig == nil {
continue
}
if len(machine.Status.NodeConfig.Role) == 0 {
continue
}
if !v3.NodeConditionProvisioned.IsTrue(machine) {
continue
}
if slice.ContainsString(machine.Status.NodeConfig.Role, services.ETCDRole) {
etcd = true
}
if slice.ContainsString(machine.Status.NodeConfig.Role, services.ControlRole) {
controlplane = true
}
node := *machine.Status.NodeConfig
if node.User == "" {
node.User = "root"
}
if node.Port == "" {
node.Port = "22"
}
if node.NodeName == "" {
node.NodeName = ref.FromStrings(machine.Namespace, machine.Name)
}
nodes = append(nodes, node)
}
if !etcd || !controlplane {
return nil, &controller.ForgetError{
Err: fmt.Errorf("waiting for etcd and controlplane nodes to be registered"),
Reason: "Provisioning",
}
}
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].NodeName < nodes[j].NodeName
})
return nodes, nil
}
func (p *Provisioner) recordFailure(cluster *v3.Cluster, spec v3.ClusterSpec, err error) (*v3.Cluster, error) {
if err == nil {
p.backoff.DeleteEntry(cluster.Name)
if cluster.Status.FailedSpec == nil {
return cluster, nil
}
cluster.Status.FailedSpec = nil
return p.Clusters.Update(cluster)
}
p.backoff.Next(cluster.Name, time.Now())
cluster.Status.FailedSpec = &spec
newCluster, _ := p.Clusters.Update(cluster)
// mask the error
return newCluster, nil
}
func (p *Provisioner) restoreClusterBackup(cluster *v3.Cluster, spec v3.ClusterSpec) (api string, token string, cert string, err error) {
snapshot := strings.Split(spec.RancherKubernetesEngineConfig.Restore.SnapshotName, ":")[1]
backup, err := p.Backups.Get(cluster.Name, snapshot)
if err != nil {
return "", "", "", err
}
if backup.Spec.ClusterID != cluster.Name {
return "", "", "", fmt.Errorf("snapshot [%s] is not a backup of cluster [%s]", backup.Name, cluster.Name)
}
api, token, cert, err = p.driverRestore(cluster, spec)
if err != nil {
return "", "", "", err
}
// checking if we have s3 config and that it's not inconsistent. This happens
// when restore is performed with invalid credentials and then the cluster is updated to fix it.
if spec.RancherKubernetesEngineConfig.Services.Etcd.BackupConfig.S3BackupConfig != nil {
s3Config := cluster.Spec.RancherKubernetesEngineConfig.Services.Etcd.BackupConfig.S3BackupConfig
appliedS3Conf := cluster.Status.AppliedSpec.RancherKubernetesEngineConfig.Services.Etcd.BackupConfig.S3BackupConfig
if !reflect.DeepEqual(s3Config, appliedS3Conf) {
logrus.Infof("updated spec during restore detected for cluster [%s], update is required", cluster.Name)
api, token, cert, err = p.driverUpdate(cluster, spec)
}
}
return api, token, cert, err
}