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
120 lines (100 loc) · 3.96 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
package cs
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(providerID, orbID string, whitelist dynamic.WhiteListFunc, orbiterCommit, repoURL, repoKey string, oneoff bool) 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, secrets map[string]*secret.Secret, err error) {
defer func() {
err = errors.Wrapf(err, "building %s failed", desiredTree.Common.Kind)
}()
desiredKind, err := parseDesired(desiredTree)
if err != nil {
return nil, nil, nil, migrate, nil, errors.Wrap(err, "parsing desired state failed")
}
desiredTree.Parsed = desiredKind
secrets = make(map[string]*secret.Secret, 0)
secret.AppendSecrets("", secrets, getSecretsMap(desiredKind))
if desiredKind.Spec.RebootRequired == nil {
desiredKind.Spec.RebootRequired = make([]string, 0)
migrate = true
}
if desiredKind.Spec.Verbose && !monitor.IsVerbose() {
monitor = monitor.Verbose()
}
if err := desiredKind.validate(); err != nil {
return nil, nil, nil, migrate, nil, err
}
lbCurrent := &tree.Tree{}
var lbQuery orbiter.QueryFunc
lbQuery, lbDestroy, lbConfigure, migrateLocal, lbSecrets, err := loadbalancers.GetQueryAndDestroyFunc(monitor, whitelist, desiredKind.Loadbalancing, lbCurrent, finishedChan)
if err != nil {
return nil, nil, nil, migrate, nil, err
}
if migrateLocal {
migrate = true
}
secret.AppendSecrets("", secrets, lbSecrets)
ctx, err := buildContext(monitor, &desiredKind.Spec, orbID, providerID, oneoff)
if err != nil {
return nil, nil, nil, migrate, nil, err
}
current := &Current{
Common: &tree.Common{
Kind: "orbiter.caos.ch/CloudScaleProvider",
Version: "v0",
},
}
currentTree.Parsed = current
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)
}()
if err := ctx.machinesService.use(desiredKind.Spec.SSHKey); err != nil {
return nil, err
}
if _, err := lbQuery(nodeAgentsCurrent, nodeAgentsDesired, nil); err != nil {
return nil, err
}
_, naFuncs := core.NodeAgentFuncs(monitor, repoURL, repoKey)
return query(&desiredKind.Spec, current, lbCurrent.Parsed, ctx, nodeAgentsCurrent, nodeAgentsDesired, naFuncs, orbiterCommit)
}, func() error {
if err := lbDestroy(); err != nil {
return err
}
if err := ctx.machinesService.use(desiredKind.Spec.SSHKey); err != nil {
return err
}
return destroy(ctx, current)
}, func(orb orb.Orb) error {
if err := lbConfigure(orb); err != nil {
return err
}
if desiredKind.Spec.SSHKey == nil ||
desiredKind.Spec.SSHKey.Private == nil || desiredKind.Spec.SSHKey.Private.Value == "" ||
desiredKind.Spec.SSHKey.Public == nil || desiredKind.Spec.SSHKey.Public.Value == "" {
priv, pub, err := ssh.Generate()
if err != nil {
return err
}
desiredKind.Spec.SSHKey = &SSHKey{
Private: &secret.Secret{Value: priv},
Public: &secret.Secret{Value: pub},
}
}
if err := ctx.machinesService.use(desiredKind.Spec.SSHKey); err != nil {
panic(err)
}
return core.ConfigureNodeAgents(ctx.machinesService, ctx.monitor, orb)
}, migrate, secrets, nil
}
}