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
130 lines (108 loc) · 3.37 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
package kubernetes
import (
"github.com/caos/orbos/internal/orb"
"github.com/caos/orbos/internal/tree"
"os"
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(
orb *orb.Orb,
orbiterCommit string,
id string,
oneoff bool,
deployOrbiterAndBoom bool,
destroyProviders func() (map[string]interface{}, error),
whitelist func(whitelist []*orbiter.CIDR)) orbiter.AdaptFunc {
return func(monitor mntr.Monitor, desiredTree *tree.Tree, currentTree *tree.Tree) (queryFunc orbiter.QueryFunc, destroyFunc orbiter.DestroyFunc, 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, orb.Masterkey)
if err != nil {
return 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
}
if err := desiredKind.validate(); err != nil {
return nil, nil, migrate, err
}
initializeNecessarySecrets(desiredKind, orb.Masterkey)
if desiredKind.Spec.Verbose && !monitor.IsVerbose() {
monitor = monitor.Verbose()
}
whitelist([]*orbiter.CIDR{&desiredKind.Spec.Networking.PodCidr})
var kc *string
if desiredKind.Spec.Kubeconfig.Value != "" {
kc = &desiredKind.Spec.Kubeconfig.Value
}
k8sClient := NewK8sClient(monitor, kc)
if k8sClient.Available() && deployOrbiterAndBoom {
if err := ensureArtifacts(monitor, k8sClient, orb, desiredKind.Spec.Versions.Orbiter, desiredKind.Spec.Versions.Boom); err != nil {
deployErrors++
monitor.WithFields(map[string]interface{}{
"count": deployErrors,
"error": err.Error(),
}).Info("Deploying Orbiter failed, awaiting next iteration")
if deployErrors > 50 {
panic(err)
}
} else {
if oneoff {
monitor.Info("Deployed Orbiter takes over control")
os.Exit(0)
}
deployErrors = 0
}
}
current := &CurrentCluster{}
currentTree.Parsed = &Current{
Common: tree.Common{
Kind: "orbiter.caos.ch/KubernetesCluster",
Version: "v0",
},
Current: current,
}
return func(nodeAgentsCurrent map[string]*common.NodeAgentCurrent, nodeAgentsDesired map[string]*common.NodeAgentSpec, providers map[string]interface{}) (orbiter.EnsureFunc, error) {
ensureFunc, err := query(monitor,
desiredKind,
current,
providers,
nodeAgentsCurrent,
nodeAgentsDesired,
k8sClient,
orb.URL,
orb.Repokey,
orbiterCommit,
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(monitor, providers, k8sClient)
}
return orbiter.DestroyFuncGoroutine(destroyFunc)
}, migrate, nil
}
}