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
/
machinesservice.go
263 lines (217 loc) · 6.68 KB
/
machinesservice.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
package cs
import (
"fmt"
"net/http"
"sync"
"github.com/caos/orbos/internal/tree"
"github.com/caos/orbos/mntr"
"github.com/caos/orbos/internal/helpers"
"github.com/cloudscale-ch/cloudscale-go-sdk"
"github.com/caos/orbos/internal/operator/orbiter/kinds/providers/ssh"
"github.com/pkg/errors"
"github.com/caos/orbos/internal/operator/orbiter/kinds/providers/core"
"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
)
func ListMachines(monitor mntr.Monitor, desiredTree *tree.Tree, orbID, providerID string) (map[string]infra.Machine, error) {
desired, err := parseDesired(desiredTree)
if err != nil {
return nil, errors.Wrap(err, "parsing desired state failed")
}
desiredTree.Parsed = desired
ctx, err := buildContext(monitor, &desired.Spec, orbID, providerID, true)
if err != nil {
return nil, err
}
return core.ListMachines(ctx.machinesService)
}
var _ core.MachinesService = (*machinesService)(nil)
type machinesService struct {
context *context
oneoff bool
key *SSHKey
cache struct {
instances map[string][]*machine
sync.Mutex
}
onCreate func(pool string, machine infra.Machine) error
}
func newMachinesService(context *context, oneoff bool) *machinesService {
return &machinesService{
context: context,
oneoff: oneoff,
}
}
func (m *machinesService) use(key *SSHKey) error {
if key == nil || key.Private == nil || key.Public == nil || key.Private.Value == "" || key.Public.Value == "" {
return errors.New("machines are not connectable. have you configured the orb by running orbctl configure?")
}
m.key = key
return nil
}
func (m *machinesService) Create(poolName string) (infra.Machine, error) {
desired, ok := m.context.desired.Pools[poolName]
if !ok {
return nil, fmt.Errorf("Pool %s is not configured", poolName)
}
name := newName()
monitor := machineMonitor(m.context.monitor, name, poolName)
monitor.Debug("Creating instance")
newServer, err := m.context.client.Servers.Create(m.context.ctx, &cloudscale.ServerRequest{
ZonalResourceRequest: cloudscale.ZonalResourceRequest{},
TaggedResourceRequest: cloudscale.TaggedResourceRequest{
Tags: map[string]string{
"orb": m.context.orbID,
"provider": m.context.providerID,
"pool": poolName,
},
},
Name: name,
Flavor: desired.Flavor,
Image: "centos-7",
Zone: desired.Zone,
VolumeSizeGB: desired.VolumeSizeGB,
Volumes: nil,
Interfaces: nil,
BulkVolumeSizeGB: 0,
SSHKeys: []string{m.context.desired.SSHKey.Public.Value},
Password: "",
UsePublicNetwork: boolPtr(m.oneoff || true), // Always use public Network
UsePrivateNetwork: boolPtr(true),
UseIPV6: boolPtr(false),
AntiAffinityWith: "",
ServerGroups: nil,
UserData: "",
})
if err != nil {
return nil, err
}
monitor.Info("Instance created")
infraMachine, err := m.toMachine(newServer, monitor, desired, poolName)
if err != nil {
return nil, err
}
if m.cache.instances != nil {
if _, ok := m.cache.instances[poolName]; !ok {
m.cache.instances[poolName] = make([]*machine, 0)
}
m.cache.instances[poolName] = append(m.cache.instances[poolName], infraMachine)
}
if err := m.onCreate(poolName, infraMachine); err != nil {
return nil, err
}
monitor.Info("Machine created")
return infraMachine, nil
}
func (m *machinesService) toMachine(server *cloudscale.Server, monitor mntr.Monitor, pool *Pool, poolName string) (*machine, error) {
internalIP, sshIP := createdIPs(server.Interfaces, m.oneoff || true /* always use public ip */)
sshMachine := ssh.NewMachine(monitor, "root", sshIP)
if err := sshMachine.UseKey([]byte(m.key.Private.Value)); err != nil {
return nil, err
}
infraMachine := newMachine(
server,
internalIP,
sshMachine,
m.removeMachineFunc(server.Tags["pool"], server.UUID),
m.context,
pool,
poolName,
)
return infraMachine, nil
}
func createdIPs(interfaces []cloudscale.Interface, oneoff bool) (string, string) {
var internalIP string
var sshIP string
for idx := range interfaces {
interf := interfaces[idx]
if internalIP != "" && sshIP != "" {
break
}
if interf.Type == "private" && len(interf.Addresses) > 0 {
internalIP = interf.Addresses[0].Address
if !oneoff {
sshIP = internalIP
break
}
}
if oneoff && interf.Type == "public" && len(interf.Addresses) > 0 {
sshIP = interf.Addresses[0].Address
continue
}
}
return internalIP, sshIP
}
func (m *machinesService) ListPools() ([]string, error) {
pools, err := m.machines()
if err != nil {
return nil, err
}
var poolNames []string
for poolName := range pools {
poolNames = append(poolNames, poolName)
}
return poolNames, nil
}
func (m *machinesService) List(poolName string) (infra.Machines, error) {
pools, err := m.machines()
if err != nil {
return nil, err
}
pool := pools[poolName]
machines := make([]infra.Machine, len(pool))
for idx := range pool {
machine := pool[idx]
machines[idx] = machine
}
return machines, nil
}
func (m *machinesService) machines() (map[string][]*machine, error) {
if m.cache.instances != nil {
return m.cache.instances, nil
}
// TODO: Doesn't work, all machines get destroyed that belong to the token
servers, err := m.context.client.Servers.List(m.context.ctx /**/, func(r *http.Request) {
params := r.URL.Query()
params["tag:orb"] = []string{m.context.orbID}
params["tag:provider"] = []string{m.context.providerID}
})
if err != nil {
return nil, err
}
m.cache.instances = make(map[string][]*machine)
for idx := range servers {
server := servers[idx]
pool := server.Tags["pool"]
machine, err := m.toMachine(&server, machineMonitor(m.context.monitor, server.Name, pool), m.context.desired.Pools[pool], pool)
if err != nil {
return nil, err
}
m.cache.instances[pool] = append(m.cache.instances[pool], machine)
}
return m.cache.instances, nil
}
func (m *machinesService) removeMachineFunc(pool, uuid string) func() error {
return func() error {
m.cache.Lock()
cleanMachines := make([]*machine, 0)
for idx := range m.cache.instances[pool] {
cachedMachine := m.cache.instances[pool][idx]
if cachedMachine.server.UUID != uuid {
cleanMachines = append(cleanMachines, cachedMachine)
}
}
m.cache.instances[pool] = cleanMachines
m.cache.Unlock()
return m.context.client.Servers.Delete(m.context.ctx, uuid)
}
}
func machineMonitor(monitor mntr.Monitor, name string, poolName string) mntr.Monitor {
return monitor.WithFields(map[string]interface{}{
"machine": name,
"pool": poolName,
})
}
func boolPtr(b bool) *bool { return &b }
func newName() string {
return "orbos-" + helpers.RandomStringRunes(6, []rune("abcdefghijklmnopqrstuvwxyz0123456789"))
}