/
dynamicbindcontroller.go
377 lines (314 loc) · 13.6 KB
/
dynamicbindcontroller.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package dynamicbindcontroller
import (
"context"
"fmt"
"math/rand"
"strings"
"time"
"github.com/golang/glog"
hfv1 "github.com/hobbyfarm/gargantua/pkg/apis/hobbyfarm.io/v1"
hfClientset "github.com/hobbyfarm/gargantua/pkg/client/clientset/versioned"
hfInformers "github.com/hobbyfarm/gargantua/pkg/client/informers/externalversions"
hfListers "github.com/hobbyfarm/gargantua/pkg/client/listers/hobbyfarm.io/v1"
"github.com/hobbyfarm/gargantua/pkg/controllers/scheduledevent"
"github.com/hobbyfarm/gargantua/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/retry"
"k8s.io/client-go/util/workqueue"
)
type DynamicBindController struct {
hfClientSet hfClientset.Interface
dynamicBindRequestWorkqueue workqueue.RateLimitingInterface
dynamicBindRequestLister hfListers.DynamicBindRequestLister
dynamicBindRequestsSynced cache.InformerSynced
ctx context.Context
}
func NewDynamicBindController(hfClientSet hfClientset.Interface, hfInformerFactory hfInformers.SharedInformerFactory, ctx context.Context) (*DynamicBindController, error) {
dynamicBindController := DynamicBindController{}
dynamicBindController.hfClientSet = hfClientSet
dynamicBindController.dynamicBindRequestsSynced = hfInformerFactory.Hobbyfarm().V1().DynamicBindRequests().Informer().HasSynced
dynamicBindController.dynamicBindRequestWorkqueue = workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "DynamicBindRequest")
dynamicBindRequestInformer := hfInformerFactory.Hobbyfarm().V1().DynamicBindRequests().Informer()
dynamicBindRequestInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
AddFunc: dynamicBindController.enqueueDynamicBindRequest,
UpdateFunc: func(old, new interface{}) {
dynamicBindController.enqueueDynamicBindRequest(new)
},
DeleteFunc: dynamicBindController.enqueueDynamicBindRequest,
}, time.Minute*30)
dynamicBindController.ctx = ctx
return &dynamicBindController, nil
}
func (d *DynamicBindController) enqueueDynamicBindRequest(obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
//utilruntime.HandleError(err)
return
}
glog.V(8).Infof("Enqueueing dynamic bind request %s", key)
d.dynamicBindRequestWorkqueue.AddRateLimited(key)
}
func (d *DynamicBindController) Run(stopCh <-chan struct{}) error {
defer d.dynamicBindRequestWorkqueue.ShutDown()
glog.V(4).Infof("Starting dynamic bind controller")
glog.Info("Waiting for informer caches to sync")
if ok := cache.WaitForCacheSync(stopCh, d.dynamicBindRequestsSynced); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}
glog.Info("Dynamic bind controller caches synced, starting controller loops")
go wait.Until(d.runDynamicBindWorker, time.Second, stopCh)
glog.Info("Started Dynamic Bind Controller worker")
<-stopCh
return nil
}
func (d *DynamicBindController) runDynamicBindWorker() {
for d.processNextDynamicBindRequest() {
}
}
func (d *DynamicBindController) processNextDynamicBindRequest() bool {
obj, shutdown := d.dynamicBindRequestWorkqueue.Get()
glog.V(8).Infof("processing dynamic bind request")
if shutdown {
return false
}
err := func() error {
defer d.dynamicBindRequestWorkqueue.Done(obj)
glog.V(4).Infof("processing dynamic bind request in dynamic bind controller: %v", obj)
_, objName, err := cache.SplitMetaNamespaceKey(obj.(string)) // this is actually not necessary because dynamic bind request's are not namespaced yet...
if err != nil {
glog.Errorf("error while splitting meta namespace key %v", err)
//e.vmWorkqueue.AddRateLimited(obj)
return nil
}
dynamicBindRequest, err := d.hfClientSet.HobbyfarmV1().DynamicBindRequests(util.GetReleaseNamespace()).Get(d.ctx, objName, metav1.GetOptions{})
if err != nil {
glog.Errorf("error while retrieving dynamic bind request %s, likely deleted %v", objName, err)
d.dynamicBindRequestWorkqueue.Forget(obj)
return nil
}
if dynamicBindRequest.Status.Fulfilled || dynamicBindRequest.Status.Expired {
d.dynamicBindRequestWorkqueue.Forget(obj)
return nil
}
err = d.reconcileDynamicBindRequest(dynamicBindRequest)
if err != nil {
d.dynamicBindRequestWorkqueue.AddRateLimited(obj)
glog.Error(err)
}
d.dynamicBindRequestWorkqueue.Forget(obj)
glog.V(4).Infof("dynamic bind request processed by dynamic bind controller %v", objName)
return nil
}()
if err != nil {
return true
}
return true
}
func (d *DynamicBindController) reconcileDynamicBindRequest(dynamicBindRequest *hfv1.DynamicBindRequest) error {
d.updateDynamicBindRequestStatus(dynamicBindRequest.Status.CurrentAttempts+1, false, false, "", make(map[string]string), dynamicBindRequest.Spec.Id)
vmClaim, err := d.hfClientSet.HobbyfarmV1().VirtualMachineClaims(util.GetReleaseNamespace()).Get(d.ctx, dynamicBindRequest.Spec.VirtualMachineClaim, metav1.GetOptions{})
if err != nil {
glog.Errorf("error retrieving corresponding virtual machine claim %s for dynamic bind request %s", dynamicBindRequest.Spec.VirtualMachineClaim, dynamicBindRequest.Spec.Id)
return err
}
var dbcSelector metav1.ListOptions
if vmClaim.Spec.RestrictedBind {
dbcSelector = metav1.ListOptions{
LabelSelector: fmt.Sprintf("restrictedbind=true,restrictedbindvalue=%s", vmClaim.Spec.RestrictedBindValue),
}
} else {
dbcSelector = metav1.ListOptions{
LabelSelector: "restrictedbind=false",
}
}
dynamicBindConfigurations, err := d.hfClientSet.HobbyfarmV1().DynamicBindConfigurations(util.GetReleaseNamespace()).List(d.ctx, dbcSelector)
if err != nil {
glog.Errorf("Error while retrieving dynamic bind configurations, %v", err)
return err
}
var chosenDynamicBindConfiguration *hfv1.DynamicBindConfiguration
var chosenEnvironment *hfv1.Environment
var dbcChosen bool
var provision bool
dbcChosen = false
provision = true
for _, dynamicBindConfiguration := range dynamicBindConfigurations.Items {
environment, err := d.hfClientSet.HobbyfarmV1().Environments(util.GetReleaseNamespace()).Get(d.ctx, dynamicBindConfiguration.Spec.Environment, metav1.GetOptions{})
if provisionMethod, ok := environment.Annotations["hobbyfarm.io/provisioner"]; ok {
if provisionMethod == "external" {
provision = false
}
}
if err != nil {
glog.Errorf("Error while retrieving environment %v", err)
return err
}
if !environment.Spec.BurstCapable {
glog.V(8).Infof("Environment %s is not burst capable. Ignoring.", environment.Name)
continue
}
suitable := true
needed := make(map[string]int)
for _, template := range vmClaim.Spec.VirtualMachines {
if template.VirtualMachineId == "" {
if val, ok := needed[template.Template]; ok {
needed[template.Template] = val + 1
} else {
needed[template.Template] = 1
}
}
}
if environment.Spec.CapacityMode == hfv1.CapacityModeRaw {
currentVMs, err := d.hfClientSet.HobbyfarmV1().VirtualMachines(util.GetReleaseNamespace()).List(d.ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("dynamic=true,environment=%s", environment.Name),
})
if err != nil {
glog.V(4).Infof("error retrieving current vm list, assuming environment empty")
}
availableCapacity := util.AvailableRawCapacity(d.hfClientSet, environment.Spec.BurstCapacity, currentVMs.Items, d.ctx)
availableVMCount := util.MaxVMCountsRaw(d.hfClientSet, needed, *availableCapacity, d.ctx)
if availableVMCount <= 0 {
suitable = false
}
} else {
for vmTemplate, vmsNeeded := range needed {
// first, let's see if the environment itself has capacity
currentVMs, err := d.hfClientSet.HobbyfarmV1().VirtualMachines(util.GetReleaseNamespace()).List(d.ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("dynamic=true,environment=%s,template=%s", environment.Name, vmTemplate),
})
if err != nil {
glog.V(4).Infof("error retrieving current vm list, assuming environment empty")
}
availableVMCount := environment.Spec.BurstCountCapacity[vmTemplate] - len(currentVMs.Items)
if availableVMCount < vmsNeeded {
// this environment does not suit us. move on.
suitable = false
break
}
currentVMs, err = d.hfClientSet.HobbyfarmV1().VirtualMachines(util.GetReleaseNamespace()).List(d.ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("dynamic=true,dynamicbindconfig=%s,environment=%s,template=%s", dynamicBindConfiguration.Spec.Id, environment.Name, vmTemplate),
})
if err != nil {
glog.V(4).Infof("error retrieving current vm list, assuming environment empty")
}
availableVMCount = dynamicBindConfiguration.Spec.BurstCountCapacity[vmTemplate] - len(currentVMs.Items)
if availableVMCount < vmsNeeded {
// this DBC has no more provisioning capacity
suitable = false
break
}
}
}
if suitable {
chosenDynamicBindConfiguration = &dynamicBindConfiguration
chosenEnvironment = environment
dbcChosen = true
break
}
}
if dbcChosen {
virtualMachines := make(map[string]string)
for vmClaimVMName, vmX := range vmClaim.Spec.VirtualMachines {
vmName := strings.Join([]string{chosenDynamicBindConfiguration.Spec.BaseName, fmt.Sprintf("%08x", rand.Uint32())}, "-")
vm := &hfv1.VirtualMachine{
ObjectMeta: metav1.ObjectMeta{
Name: vmName,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "hobbyfarm.io/v1",
Kind: "DynamicBindRequest",
Name: dynamicBindRequest.Name,
UID: dynamicBindRequest.UID,
},
},
Labels: map[string]string{
"dynamic": "true",
"dynamicbindrequest": dynamicBindRequest.Name,
"dynamicbindconfiguration": chosenDynamicBindConfiguration.Spec.Id,
"template": vmX.Template,
"environment": chosenDynamicBindConfiguration.Spec.Environment,
"bound": "true",
"ready": "false",
scheduledevent.ScheduledEventLabel: chosenDynamicBindConfiguration.ObjectMeta.Labels[scheduledevent.ScheduledEventLabel],
},
},
Spec: hfv1.VirtualMachineSpec{
Id: vmName,
VirtualMachineTemplateId: vmX.Template,
KeyPair: "",
VirtualMachineClaimId: dynamicBindRequest.Spec.VirtualMachineClaim,
UserId: vmClaim.Spec.UserId,
Provision: provision,
VirtualMachineSetId: "",
},
Status: hfv1.VirtualMachineStatus{
Status: hfv1.VmStatusRFP,
Allocated: true,
Tainted: false,
WsEndpoint: chosenEnvironment.Spec.WsEndpoint,
PublicIP: "",
PrivateIP: "",
EnvironmentId: chosenEnvironment.Name,
Hostname: "",
},
}
sshUser, exists := chosenEnvironment.Spec.TemplateMapping[vmX.Template]["ssh_username"]
if exists {
vm.Spec.SshUsername = sshUser
}
// extra label to indicate external provisioning so tfpcontroller ignores this request //
if provisionMethod, ok := chosenEnvironment.Annotations["hobbyfarm.io/provisioner"]; ok {
vm.ObjectMeta.Labels["hobbyfarm.io/provisioner"] = provisionMethod
vm.Spec.Provision = false
}
// label to indicate which vmtemplate being used for querying reasons
vm.ObjectMeta.Labels["hobbyfarm.io/vmtemplate"] = vm.Spec.VirtualMachineTemplateId
if chosenDynamicBindConfiguration.Spec.RestrictedBind {
vm.ObjectMeta.Labels["restrictedbind"] = "true"
vm.ObjectMeta.Labels["restrictedbindvalue"] = chosenDynamicBindConfiguration.Spec.RestrictedBindValue
} else {
vm.ObjectMeta.Labels["restrictedbind"] = "false"
}
vm, err := d.hfClientSet.HobbyfarmV1().VirtualMachines(util.GetReleaseNamespace()).Create(d.ctx, vm, metav1.CreateOptions{})
if err != nil {
glog.Error(err)
}
virtualMachines[vmClaimVMName] = vm.Name
}
d.updateDynamicBindRequestStatus(dynamicBindRequest.Spec.Attempts, false, true, chosenDynamicBindConfiguration.Spec.Id, virtualMachines, dynamicBindRequest.Spec.Id)
} else {
// check to see if we're above our attempt threshold
if dynamicBindRequest.Status.CurrentAttempts > dynamicBindRequest.Spec.Attempts {
d.updateDynamicBindRequestStatus(dynamicBindRequest.Status.CurrentAttempts, true, false, "", make(map[string]string), dynamicBindRequest.Spec.Id)
} else {
return fmt.Errorf("did not bind dynamic vms for dynamic bind request %s", dynamicBindRequest.Spec.Id)
}
}
return nil
}
func (d *DynamicBindController) updateDynamicBindRequestStatus(dynamicBindAttempts int, expired bool, fulfilled bool, dynamicBindConfigurationId string, virtualMachineIds map[string]string, dynamicBindRequestId string) error {
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
result, getErr := d.hfClientSet.HobbyfarmV1().DynamicBindRequests(util.GetReleaseNamespace()).Get(d.ctx, dynamicBindRequestId, metav1.GetOptions{})
if getErr != nil {
return fmt.Errorf("error retrieving latest version of DynamicBindRequest %s: %v", dynamicBindRequestId, getErr)
}
result.Status.CurrentAttempts = dynamicBindAttempts
result.Status.Expired = expired
result.Status.Fulfilled = fulfilled
result.Status.DynamicBindConfigurationId = dynamicBindConfigurationId
result.Status.VirtualMachineIds = virtualMachineIds
_, updateErr := d.hfClientSet.HobbyfarmV1().DynamicBindRequests(util.GetReleaseNamespace()).Update(d.ctx, result, metav1.UpdateOptions{})
if updateErr != nil {
return updateErr
}
glog.V(4).Infof("updated result for dynamic bind request")
return nil
})
if retryErr != nil {
return fmt.Errorf("error updating DynamicBindRequest: %s, %v", dynamicBindRequestId, retryErr)
}
return nil
}