This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
adapt.go
141 lines (119 loc) · 3.75 KB
/
adapt.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
package kubernetes
import (
"github.com/caos/orbos/internal/tree"
core "k8s.io/api/core/v1"
"github.com/pkg/errors"
"github.com/caos/orbos/internal/operator/common"
"github.com/caos/orbos/internal/operator/orbiter"
"github.com/caos/orbos/mntr"
)
var deployErrors int
func AdaptFunc(
clusterID string,
oneoff bool,
deployOrbiter bool,
destroyProviders func() (map[string]interface{}, error),
whitelist func(whitelist []*orbiter.CIDR)) orbiter.AdaptFunc {
return func(monitor mntr.Monitor, finishedChan chan struct{}, desiredTree *tree.Tree, currentTree *tree.Tree) (queryFunc orbiter.QueryFunc, destroyFunc orbiter.DestroyFunc, configureFunc orbiter.ConfigureFunc, migrate bool, err error) {
defer func() {
err = errors.Wrapf(err, "building %s failed", desiredTree.Common.Kind)
}()
if desiredTree.Common.Version != "v0" {
migrate = true
}
desiredKind, err := parseDesiredV0(desiredTree)
if err != nil {
return nil, nil, nil, migrate, errors.Wrap(err, "parsing desired state failed")
}
desiredTree.Parsed = desiredKind
if desiredKind.Spec.ControlPlane.Taints == nil {
taints := Taints([]Taint{{
Key: "node-role.kubernetes.io/master",
Effect: core.TaintEffectNoSchedule,
}})
desiredKind.Spec.ControlPlane.Taints = &taints
migrate = true
}
for _, workers := range desiredKind.Spec.Workers {
if workers.Taints == nil {
taints := Taints(make([]Taint, 0))
workers.Taints = &taints
migrate = true
}
}
if err := desiredKind.validate(); err != nil {
return nil, nil, nil, migrate, err
}
if desiredKind.Spec.Verbose && !monitor.IsVerbose() {
monitor = monitor.Verbose()
}
whitelist([]*orbiter.CIDR{&desiredKind.Spec.Networking.PodCidr})
var kc *string
if desiredKind.Spec.Kubeconfig != nil && desiredKind.Spec.Kubeconfig.Value != "" {
kc = &desiredKind.Spec.Kubeconfig.Value
}
k8sClient := NewK8sClient(monitor, kc)
if k8sClient.Available() && deployOrbiter {
if err := EnsureCommonArtifacts(monitor, k8sClient); err != nil {
deployErrors++
monitor.WithFields(map[string]interface{}{
"count": deployErrors,
"error": err.Error(),
}).Info("Applying Common failed, awaiting next iteration")
}
if deployErrors > 50 {
panic(err)
}
if err := EnsureOrbiterArtifacts(monitor, k8sClient, desiredKind.Spec.Versions.Orbiter); err != nil {
deployErrors++
monitor.WithFields(map[string]interface{}{
"count": deployErrors,
"error": err.Error(),
}).Info("Deploying Orbiter failed, awaiting next iteration")
} else {
if oneoff {
monitor.Info("Deployed Orbiter takes over control")
finishedChan <- struct{}{}
}
deployErrors = 0
}
if deployErrors > 50 {
panic(err)
}
}
current := &CurrentCluster{}
currentTree.Parsed = &Current{
Common: tree.Common{
Kind: "orbiter.caos.ch/KubernetesCluster",
Version: "v0",
},
Current: current,
}
return func(nodeAgentsCurrent *common.CurrentNodeAgents, nodeAgentsDesired *common.DesiredNodeAgents, providers map[string]interface{}) (orbiter.EnsureFunc, error) {
ensureFunc, err := query(
monitor,
clusterID,
desiredKind,
current,
providers,
nodeAgentsCurrent,
nodeAgentsDesired,
k8sClient,
oneoff)
return ensureFunc, errors.Wrapf(err, "querying %s failed", desiredKind.Common.Kind)
}, func() error {
defer func() {
err = errors.Wrapf(err, "destroying %s failed", desiredKind.Common.Kind)
}()
providers, err := destroyProviders()
if err != nil {
return err
}
desiredKind.Spec.Kubeconfig = nil
destroyFunc := func() error {
return destroy(providers, k8sClient)
}
return orbiter.DestroyFuncGoroutine(destroyFunc)
}, orbiter.NoopConfigure, migrate, nil
}
}