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
113 lines (96 loc) · 3.73 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
package static
import (
"github.com/caos/orbos/internal/operator/orbiter/kinds/loadbalancers"
"github.com/caos/orbos/internal/operator/orbiter/kinds/loadbalancers/dynamic"
"github.com/caos/orbos/internal/operator/orbiter/kinds/providers/core"
"github.com/caos/orbos/internal/orb"
"github.com/caos/orbos/internal/secret"
"github.com/caos/orbos/internal/ssh"
"github.com/caos/orbos/internal/tree"
"github.com/pkg/errors"
"github.com/caos/orbos/internal/operator/common"
"github.com/caos/orbos/internal/operator/orbiter"
"github.com/caos/orbos/mntr"
)
func AdaptFunc(id string, whitelist dynamic.WhiteListFunc, orbiterCommit, repoURL, repoKey string) 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)
}()
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.Verbose && !monitor.IsVerbose() {
monitor = monitor.Verbose()
}
if err := desiredKind.validate(); err != nil {
return nil, nil, nil, migrate, err
}
lbCurrent := &tree.Tree{}
var lbQuery orbiter.QueryFunc
lbQuery, lbDestroy, lbConfigure, migrateLocal, err := loadbalancers.GetQueryAndDestroyFunc(monitor, whitelist, desiredKind.Loadbalancing, lbCurrent, finishedChan)
if err != nil {
return nil, nil, nil, migrate, err
}
if migrateLocal {
migrate = true
}
current := &Current{
Common: &tree.Common{
Kind: "orbiter.caos.ch/StaticProvider",
Version: "v0",
},
}
currentTree.Parsed = current
svc := NewMachinesService(monitor, desiredKind, id)
return func(nodeAgentsCurrent *common.CurrentNodeAgents, nodeAgentsDesired *common.DesiredNodeAgents, _ map[string]interface{}) (ensureFunc orbiter.EnsureFunc, err error) {
defer func() {
err = errors.Wrapf(err, "querying %s failed", desiredKind.Common.Kind)
}()
lbQueryFunc := func() (orbiter.EnsureFunc, error) {
return lbQuery(nodeAgentsCurrent, nodeAgentsDesired, nil)
}
if _, err := orbiter.QueryFuncGoroutine(lbQueryFunc); err != nil {
return nil, err
}
if err := svc.updateKeys(); err != nil {
return nil, err
}
queryFunc := func() (orbiter.EnsureFunc, error) {
_, iterateNA := core.NodeAgentFuncs(monitor, repoURL, repoKey)
return query(desiredKind, current, nodeAgentsDesired, nodeAgentsCurrent, lbCurrent.Parsed, monitor, svc, iterateNA, orbiterCommit)
}
return orbiter.QueryFuncGoroutine(queryFunc)
}, func() error {
if err := lbDestroy(); err != nil {
return err
}
if err := svc.updateKeys(); err != nil {
return err
}
return destroy(svc, desiredKind, current)
}, func(orb orb.Orb) error {
if err := lbConfigure(orb); err != nil {
return err
}
initKeys := desiredKind.Spec.Keys == nil
if initKeys ||
desiredKind.Spec.Keys.MaintenanceKeyPrivate == nil || desiredKind.Spec.Keys.MaintenanceKeyPrivate.Value == "" ||
desiredKind.Spec.Keys.MaintenanceKeyPublic == nil || desiredKind.Spec.Keys.MaintenanceKeyPublic.Value == "" {
priv, pub, err := ssh.Generate()
if err != nil {
return err
}
if initKeys {
desiredKind.Spec.Keys = &Keys{}
}
desiredKind.Spec.Keys.MaintenanceKeyPrivate = &secret.Secret{Value: priv}
desiredKind.Spec.Keys.MaintenanceKeyPublic = &secret.Secret{Value: pub}
return nil
}
return core.ConfigureNodeAgents(svc, monitor, orb)
}, migrate, nil
}
}