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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
|
/*
* Copyright 2018 Intel Corporation, Inc
* Copyright © 2021 Samsung Electronics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/db"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/rb"
"github.com/ghodss/yaml"
pkgerrors "github.com/pkg/errors"
)
//ConfigStore contains the values that will be stored in the database
type configVersionDBContent struct {
ConfigNew Config `json:"config-new"`
ConfigPrev Config `json:"config-prev"`
Action string `json:"action"` // CRUD opration for this config
Resources []helm.KubernetesResource `json:"resources"`
}
//ConfigStore to Store the Config
type ConfigStore struct {
instanceID string
configName string
}
//ConfigVersionStore to Store the Versions of the Config
type ConfigVersionStore struct {
instanceID string
configName string
}
type configResourceList struct {
resourceTemplates []helm.KubernetesResourceTemplate
resources []helm.KubernetesResource
updatedResources chan []helm.KubernetesResource
profile rb.Profile
action string
}
type profileDataManager struct {
profileLockMap map[string]*sync.Mutex
resourceChannel map[string](chan configResourceList)
sync.Mutex
}
const (
storeName = "config"
tagCounter = "counter"
tagVersion = "configversion"
tagName = "configtag"
tagConfig = "configdata"
)
var profileData = profileDataManager{
profileLockMap: map[string]*sync.Mutex{},
resourceChannel: map[string]chan configResourceList{},
}
// Construct key for storing data
func constructKey(strs ...string) string {
var sb strings.Builder
sb.WriteString("onapk8s")
sb.WriteString("/")
sb.WriteString(storeName)
sb.WriteString("/")
for _, str := range strs {
sb.WriteString(str)
sb.WriteString("/")
}
return sb.String()
}
// Create an entry for the config in the database
func (c ConfigStore) createConfig(p Config) error {
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return pkgerrors.Wrap(err, "Retrieving model info")
}
cfgKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagConfig, p.ConfigName)
_, err = db.Etcd.Get(cfgKey)
if err == nil {
return pkgerrors.Wrap(err, "Config DB Entry Already exists")
}
configValue, err := db.Serialize(p)
if err != nil {
return pkgerrors.Wrap(err, "Serialize Config Value")
}
err = db.Etcd.Put(cfgKey, configValue)
if err != nil {
return pkgerrors.Wrap(err, "Config DB Entry")
}
return nil
}
// Update the config entry in the database. Updates with the new value
// Returns the previous value of the Config
func (c ConfigStore) updateConfig(p Config) (Config, error) {
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return Config{}, pkgerrors.Wrap(err, "Retrieving model info")
}
cfgKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagConfig, p.ConfigName)
value, err := db.Etcd.Get(cfgKey)
configPrev := Config{}
if err == nil {
// If updating Config after rollback then previous config may not exist
err = db.DeSerialize(string(value), &configPrev)
if err != nil {
return Config{}, pkgerrors.Wrap(err, "DeSerialize Config Value")
}
}
configValue, err := db.Serialize(p)
if err != nil {
return Config{}, pkgerrors.Wrap(err, "Serialize Config Value")
}
err = db.Etcd.Put(cfgKey, configValue)
if err != nil {
return Config{}, pkgerrors.Wrap(err, "Config DB Entry")
}
return configPrev, nil
}
// Read the config entry in the database
func (c ConfigStore) getConfig() (Config, error) {
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return Config{}, pkgerrors.Wrap(err, "Retrieving model info")
}
cfgKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagConfig, c.configName)
value, err := db.Etcd.Get(cfgKey)
if err != nil {
return Config{}, pkgerrors.Wrap(err, "Get Config DB Entry")
}
//value is a byte array
if value != nil {
cfg := Config{}
err = db.DeSerialize(string(value), &cfg)
if err != nil {
return Config{}, pkgerrors.Wrap(err, "Unmarshaling Config Value")
}
return cfg, nil
}
return Config{}, pkgerrors.Wrap(err, "Get Config DB Entry")
}
// Read the config entry in the database
func (c ConfigStore) getConfigList() ([]Config, error) {
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return []Config{}, pkgerrors.Wrap(err, "Retrieving model info")
}
cfgKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagConfig)
values, err := db.Etcd.GetAll(cfgKey)
if err != nil {
return []Config{}, pkgerrors.Wrap(err, "Get Config DB List")
}
//value is a byte array
if values != nil {
result := make([]Config, 0)
for _, value := range values {
cfg := Config{}
err = db.DeSerialize(string(value), &cfg)
if err != nil {
return []Config{}, pkgerrors.Wrap(err, "Unmarshaling Config Value")
}
result = append(result, cfg)
}
return result, nil
}
return []Config{}, pkgerrors.Wrap(err, "Get Config DB List")
}
// Delete the config entry in the database
func (c ConfigStore) deleteConfig() (Config, error) {
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return Config{}, pkgerrors.Wrap(err, "Retrieving model info")
}
cfgKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagConfig, c.configName)
value, err := db.Etcd.Get(cfgKey)
if err != nil {
return Config{}, pkgerrors.Wrap(err, "Config DB Entry Not found")
}
configPrev := Config{}
err = db.DeSerialize(string(value), &configPrev)
if err != nil {
return Config{}, pkgerrors.Wrap(err, "DeSerialize Config Value")
}
err = db.Etcd.Delete(cfgKey)
if err != nil {
return Config{}, pkgerrors.Wrap(err, "Config DB Entry")
}
return configPrev, nil
}
//Cleanup stored data in etcd before instance is being deleted
func (c ConfigVersionStore) cleanupIstanceTags(configName string) error {
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return pkgerrors.Wrap(err, "Retrieving model info")
}
versionKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagVersion, configName)
err = db.Etcd.DeletePrefix(versionKey)
if err != nil {
log.Printf("Deleting versions of instance failed: %s", err.Error())
}
counterKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagCounter, configName)
err = db.Etcd.DeletePrefix(counterKey)
if err != nil {
log.Printf("Deleting counters of instance failed: %s", err.Error())
}
nameKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagName, configName)
err = db.Etcd.DeletePrefix(nameKey)
if err != nil {
log.Printf("Deleting counters of instance failed: %s", err.Error())
}
return nil
}
// Create a version for the configuration. If previous config provided that is also stored
func (c ConfigVersionStore) createConfigVersion(configNew, configPrev Config, action string, resources []helm.KubernetesResource) (uint, error) {
configName := ""
if configNew.ConfigName != "" {
configName = configNew.ConfigName
} else {
configName = configPrev.ConfigName
}
version, err := c.incrementVersion(configName)
if err != nil {
return 0, pkgerrors.Wrap(err, "Get Next Version")
}
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return 0, pkgerrors.Wrap(err, "Retrieving model info")
}
versionKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagVersion, configName, strconv.Itoa(int(version)))
var cs configVersionDBContent
cs.Action = action
cs.ConfigNew = configNew
cs.ConfigPrev = configPrev
cs.Resources = resources //[]helm.KubernetesResource{}
configValue, err := db.Serialize(cs)
if err != nil {
return 0, pkgerrors.Wrap(err, "Serialize Config Value")
}
err = db.Etcd.Put(versionKey, configValue)
if err != nil {
return 0, pkgerrors.Wrap(err, "Create Config DB Entry")
}
return version, nil
}
// Delete current version of the configuration. Configuration always deleted from top
func (c ConfigVersionStore) deleteConfigVersion(configName string) error {
counter, err := c.getCurrentVersion(configName)
if err != nil {
return pkgerrors.Wrap(err, "Get Next Version")
}
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return pkgerrors.Wrap(err, "Retrieving model info")
}
versionKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagVersion, configName, strconv.Itoa(int(counter)))
err = db.Etcd.Delete(versionKey)
if err != nil {
return pkgerrors.Wrap(err, "Delete Config DB Entry")
}
err = c.decrementVersion(configName)
if err != nil {
return pkgerrors.Wrap(err, "Decrement Version")
}
return nil
}
// Read the specified version of the configuration and return its prev and current value.
// Also returns the action for the config version
func (c ConfigVersionStore) getConfigVersion(configName string, version uint) (Config, Config, string, []helm.KubernetesResource, error) {
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return Config{}, Config{}, "", []helm.KubernetesResource{}, pkgerrors.Wrap(err, "Retrieving model info")
}
versionKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagVersion, configName, strconv.Itoa(int(version)))
configBytes, err := db.Etcd.Get(versionKey)
if err != nil {
return Config{}, Config{}, "", []helm.KubernetesResource{}, pkgerrors.Wrap(err, "Get Config Version ")
}
if configBytes != nil {
pr := configVersionDBContent{}
err = db.DeSerialize(string(configBytes), &pr)
if err != nil {
return Config{}, Config{}, "", []helm.KubernetesResource{}, pkgerrors.Wrap(err, "DeSerialize Config Version")
}
return pr.ConfigNew, pr.ConfigPrev, pr.Action, pr.Resources, nil
}
return Config{}, Config{}, "", []helm.KubernetesResource{}, pkgerrors.Wrap(err, "Invalid data ")
}
// Get the counter for the version
func (c ConfigVersionStore) getCurrentVersion(configName string) (uint, error) {
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return 0, pkgerrors.Wrap(err, "Retrieving model info")
}
cfgKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagCounter, configName)
value, err := db.Etcd.Get(cfgKey)
if err != nil {
if strings.Contains(err.Error(), "Key doesn't exist") == true {
// Counter not started yet, 0 is invalid value
return 0, nil
} else {
return 0, pkgerrors.Wrap(err, "Get Current Version")
}
}
index, err := strconv.Atoi(string(value))
if err != nil {
return 0, pkgerrors.Wrap(err, "Invalid counter")
}
return uint(index), nil
}
// Update the counter for the version
func (c ConfigVersionStore) updateVersion(configName string, counter uint) error {
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return pkgerrors.Wrap(err, "Retrieving model info")
}
cfgKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagCounter, configName)
err = db.Etcd.Put(cfgKey, strconv.Itoa(int(counter)))
if err != nil {
return pkgerrors.Wrap(err, "Counter DB Entry")
}
return nil
}
// Increment the version counter
func (c ConfigVersionStore) incrementVersion(configName string) (uint, error) {
counter, err := c.getCurrentVersion(configName)
if err != nil {
return 0, pkgerrors.Wrap(err, "Get Next Counter Value")
}
//This is done while Profile lock is taken
counter++
err = c.updateVersion(configName, counter)
if err != nil {
return 0, pkgerrors.Wrap(err, "Store Next Counter Value")
}
return counter, nil
}
// Decrement the version counter
func (c ConfigVersionStore) decrementVersion(configName string) error {
counter, err := c.getCurrentVersion(configName)
if err != nil {
return pkgerrors.Wrap(err, "Get Next Counter Value")
}
//This is done while Profile lock is taken
counter--
err = c.updateVersion(configName, counter)
if err != nil {
return pkgerrors.Wrap(err, "Store Next Counter Value")
}
return nil
}
// Get tag version
func (c ConfigVersionStore) getTagVersion(configName, tagNameValue string) (string, error) {
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return "", pkgerrors.Wrap(err, "Retrieving model info")
}
tagKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagName, configName, tagNameValue)
value, err := db.Etcd.Get(tagKey)
if err != nil {
return "", pkgerrors.Wrap(err, "Config DB Entry Not found")
}
return string(value), nil
}
// Tag current version
func (c ConfigVersionStore) tagCurrentVersion(configName, tagNameValue string) error {
currentVersion, err := c.getCurrentVersion(configName)
if err != nil {
return pkgerrors.Wrap(err, "Get Current Config Version ")
}
rbName, rbVersion, profileName, _, err := resolveModelFromInstance(c.instanceID)
if err != nil {
return pkgerrors.Wrap(err, "Retrieving model info")
}
tagKey := constructKey(rbName, rbVersion, profileName, c.instanceID, tagName, configName, tagNameValue)
err = db.Etcd.Put(tagKey, strconv.Itoa(int(currentVersion)))
if err != nil {
return pkgerrors.Wrap(err, "TagIt store DB")
}
return nil
}
// Apply Config
func applyConfig(instanceID string, p Config, pChannel chan configResourceList, action string, resources []helm.KubernetesResource) ([]helm.KubernetesResource, error) {
rbName, rbVersion, profileName, releaseName, err := resolveModelFromInstance(instanceID)
if err != nil {
return []helm.KubernetesResource{}, pkgerrors.Wrap(err, "Retrieving model info")
}
// Get Template and Resolve the template with values
crl, err := resolve(rbName, rbVersion, profileName, p, releaseName)
if err != nil {
return []helm.KubernetesResource{}, pkgerrors.Wrap(err, "Resolve Config")
}
var updatedResources (chan []helm.KubernetesResource) = make(chan []helm.KubernetesResource)
crl.action = action
crl.resources = resources
crl.updatedResources = updatedResources
// Send the configResourceList to the channel. Using select for non-blocking channel
log.Printf("Before Sent to goroutine %v", crl.profile)
select {
case pChannel <- crl:
log.Printf("Message Sent to goroutine %v", crl.profile)
default:
}
var resultResources []helm.KubernetesResource = <-updatedResources
return resultResources, nil
}
// Per Profile Go routine to apply the configuration to Cloud Region
func scheduleResources(c chan configResourceList) {
// Keep thread running
log.Printf("[scheduleResources]: START thread")
for {
data := <-c
//TODO: ADD Check to see if Application running
ic := NewInstanceClient()
resp, err := ic.Find(data.profile.RBName, data.profile.RBVersion, data.profile.ProfileName, nil)
if (err != nil || len(resp) == 0) && data.action != "STOP" {
log.Println("Error finding a running instance. Retrying later...")
data.updatedResources <- []helm.KubernetesResource{}
continue
}
breakThread := false
switch {
case data.action == "POST":
log.Printf("[scheduleResources]: POST %v %v", data.profile, data.resourceTemplates)
var resources []helm.KubernetesResource
for _, inst := range resp {
k8sClient := KubernetesClient{}
err = k8sClient.Init(inst.Request.CloudRegion, inst.ID)
if err != nil {
log.Printf("Getting CloudRegion Information: %s", err.Error())
//Move onto the next cloud region
continue
}
//assuming - the resource is not exist already
resources, err = k8sClient.createResources(data.resourceTemplates, inst.Namespace)
errCreate := err
if err != nil {
// assuming - the err represent the resource is already exist, so going for update
resources, err = k8sClient.updateResources(data.resourceTemplates, inst.Namespace)
if err != nil {
log.Printf("Error Creating resources: %s", errCreate.Error())
log.Printf("Error Updating resources: %s", err.Error())
continue
}
}
}
data.updatedResources <- resources
case data.action == "PUT":
log.Printf("[scheduleResources]: PUT %v %v", data.profile, data.resourceTemplates)
var resources []helm.KubernetesResource
for _, inst := range resp {
k8sClient := KubernetesClient{}
err = k8sClient.Init(inst.Request.CloudRegion, inst.ID)
if err != nil {
log.Printf("Getting CloudRegion Information: %s", err.Error())
//Move onto the next cloud region
continue
}
resources, err = k8sClient.updateResources(data.resourceTemplates, inst.Namespace)
if err != nil {
log.Printf("Error Updating resources: %s", err.Error())
continue
}
}
data.updatedResources <- resources
case data.action == "DELETE":
log.Printf("[scheduleResources]: DELETE %v %v", data.profile, data.resources)
for _, inst := range resp {
k8sClient := KubernetesClient{}
err = k8sClient.Init(inst.Request.CloudRegion, inst.ID)
if err != nil {
log.Printf("Getting CloudRegion Information: %s", err.Error())
//Move onto the next cloud region
continue
}
err = k8sClient.deleteResources(helm.GetReverseK8sResources(data.resources), inst.Namespace)
if err != nil {
log.Printf("Error Deleting resources: %s", err.Error())
continue
}
}
data.updatedResources <- []helm.KubernetesResource{}
case data.action == "STOP":
breakThread = true
}
if breakThread {
break
}
}
log.Printf("[scheduleResources]: STOP thread")
}
//Resolve returns the path where the helm chart merged with
//configuration overrides resides.
var resolve = func(rbName, rbVersion, profileName string, p Config, releaseName string) (configResourceList, error) {
var resTemplates []helm.KubernetesResourceTemplate
profile, err := rb.NewProfileClient().Get(rbName, rbVersion, profileName)
if err != nil {
return configResourceList{}, pkgerrors.Wrap(err, "Reading Profile Data")
}
t, err := rb.NewConfigTemplateClient().Get(rbName, rbVersion, p.TemplateName)
if err != nil {
return configResourceList{}, pkgerrors.Wrap(err, "Getting Template")
}
if t.ChartName == "" {
return configResourceList{}, pkgerrors.New("Invalid template no Chart.yaml file found")
}
def, err := rb.NewConfigTemplateClient().Download(rbName, rbVersion, p.TemplateName)
if err != nil {
return configResourceList{}, pkgerrors.Wrap(err, "Downloading Template")
}
//Create a temp file in the system temp folder for values input
b, err := json.Marshal(p.Values)
if err != nil {
return configResourceList{}, pkgerrors.Wrap(err, "Error Marshalling config data")
}
data, err := yaml.JSONToYAML(b)
if err != nil {
return configResourceList{}, pkgerrors.Wrap(err, "JSON to YAML")
}
outputfile, err := ioutil.TempFile("", "helm-config-values-")
if err != nil {
return configResourceList{}, pkgerrors.Wrap(err, "Got error creating temp file")
}
_, err = outputfile.Write([]byte(data))
if err != nil {
return configResourceList{}, pkgerrors.Wrap(err, "Got error writting temp file")
}
defer outputfile.Close()
chartBasePath, err := rb.ExtractTarBall(bytes.NewBuffer(def))
if err != nil {
return configResourceList{}, pkgerrors.Wrap(err, "Extracting Template")
}
var finalReleaseName string
if releaseName == "" {
finalReleaseName = profile.ReleaseName
} else {
finalReleaseName = releaseName
}
helmClient := helm.NewTemplateClient(profile.KubernetesVersion,
profile.Namespace,
finalReleaseName)
chartPath := filepath.Join(chartBasePath, t.ChartName)
resTemplates, crdList, _, err := helmClient.GenerateKubernetesArtifacts(chartPath,
[]string{outputfile.Name()},
nil)
if err != nil {
return configResourceList{}, pkgerrors.Wrap(err, "Generate final k8s yaml")
}
for _, tmp := range resTemplates {
crdList = append(crdList, tmp)
}
crl := configResourceList{
resourceTemplates: crdList,
profile: profile,
}
return crl, nil
}
// Get the Mutex for the Profile
func getProfileData(key string) (*sync.Mutex, chan configResourceList) {
profileData.Lock()
defer profileData.Unlock()
_, ok := profileData.profileLockMap[key]
if !ok {
profileData.profileLockMap[key] = &sync.Mutex{}
}
_, ok = profileData.resourceChannel[key]
if !ok {
profileData.resourceChannel[key] = make(chan configResourceList)
go scheduleResources(profileData.resourceChannel[key])
time.Sleep(time.Second * 5)
}
return profileData.profileLockMap[key], profileData.resourceChannel[key]
}
func removeProfileData(key string) {
profileData.Lock()
defer profileData.Unlock()
_, ok := profileData.profileLockMap[key]
if ok {
delete(profileData.profileLockMap, key)
}
_, ok = profileData.resourceChannel[key]
if ok {
log.Printf("Stop config thread for %s", key)
crl := configResourceList{
action: "STOP",
}
profileData.resourceChannel[key] <- crl
delete(profileData.resourceChannel, key)
}
}
|