-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
897 lines (825 loc) · 29.5 KB
/
utils.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
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
package main
import (
"context"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/argoproj/argo-rollouts/utils/defaults"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
const DefaultsErrorTopicsJson = `{
"errorTopics": [
{
"string": "OnOutOfMemoryError",
"topic": "critical",
"type": "default"
},
{
"string": "StackOverflowError",
"topic": "critical",
"type": "default"
},
{
"string": "ClassNotFoundException",
"topic": "critical",
"type": "default"
},
{
"string": "FileNotFoundException",
"topic": "critical",
"type": "default"
},
{
"string": "ArrayIndexOutOfBounds",
"topic": "critical",
"type": "default"
},
{
"string": "NullPointerException",
"topic": "critical",
"type": "default"
},
{
"string": "StringIndexOutOfBoundsException",
"topic": "critical",
"type": "default"
},
{
"string": "FATAL",
"topic": "critical",
"type": "default"
},
{
"string": "SEVERE",
"topic": "critical",
"type": "default"
},
{
"string": "NoClassDefFoundError",
"topic": "error",
"type": "default"
},
{
"string": "NoSuchMethodFoundError",
"topic": "error",
"type": "default"
},
{
"string": "NumberFormatException",
"topic": "error",
"type": "default"
},
{
"string": "IllegalArgumentException",
"topic": "error",
"type": "default"
},
{
"string": "ParseException",
"topic": "error",
"type": "default"
},
{
"string": "SQLException",
"topic": "error",
"type": "default"
},
{
"string": "ArithmeticException",
"topic": "error",
"type": "default"
},
{
"string": "status=404",
"topic": "error",
"type": "default"
},
{
"string": "status=500",
"topic": "error",
"type": "default"
},
{
"string": "EXCEPTION",
"topic": "error",
"type": "default"
},
{
"string": "ERROR",
"topic": "error",
"type": "default"
},
{
"string": "WARN",
"topic": "warn",
"type": "default"
}
]
}`
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func logNon0CodeExit(exitcode ExitCode) {
log.Infof("exiting the pod with status code %d", exitcode)
os.Exit(int(exitcode))
}
func getJobNameFromPod(p *Clients, podName string) (string, error) {
ns := defaults.Namespace()
ctx := context.TODO()
pod, err := p.kubeclientset.CoreV1().Pods(ns).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
return "", err
}
podOwner := pod.OwnerReferences[0]
if podOwner.Kind != "Job" {
return "", errors.New("the owner of the pod is not a job")
}
return podOwner.Name, nil
}
func checkPatchabilityReturnResources(c *Clients) (ResourceNames, error) {
podName, ok := os.LookupEnv("MY_POD_NAME")
if !ok {
return ResourceNames{}, errors.New("analysisTemplate validation error: environment variable MY_POD_NAME is not set")
}
jobName, err := getJobNameFromPod(c, podName)
if err != nil {
return ResourceNames{}, err
}
log.Println("jobname earlier ", jobName)
_, err = c.kubeclientset.BatchV1().Jobs(defaults.Namespace()).Patch(context.TODO(), jobName, types.StrategicMergePatchType, []byte(`{}`), metav1.PatchOptions{}, "status")
if err != nil {
log.Error("cannot patch to Job")
return ResourceNames{}, err
}
resourceNames := ResourceNames{
podName: podName,
jobName: jobName,
}
return resourceNames, nil
}
func isJSON(s string) bool {
var j map[string]interface{}
if err := json.Unmarshal([]byte(s), &j); err != nil {
return false
}
return true
}
func roundFloat(val float64, precision uint) float64 {
ratio := math.Pow(10, float64(precision))
return math.Round(val*ratio) / ratio
}
func makeRequest(client http.Client, requestType string, url string, body string, user string) ([]byte, string, string, error) {
reqBody := strings.NewReader(body)
req, _ := http.NewRequest(
requestType,
url,
reqBody,
)
req.Header.Set("x-spinnaker-user", user)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return []byte{}, "", "", err
}
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
return []byte{}, "", "", err
}
var urlScore string
var urlToken string
if strings.Contains(url, "registerCanary") {
urlScore = res.Header.Get("Location")
urlToken = res.Header.Get("x-opsmx-report-token")
}
return data, urlScore, urlToken, err
}
func (metric *OPSMXMetric) checkISDUrl(c *Clients, opsmxIsdUrl string) error {
resp, err := c.client.Get(opsmxIsdUrl)
if err != nil && metric.OpsmxIsdUrl != "" && !strings.Contains(err.Error(), "timeout") {
errorMsg := fmt.Sprintf("provider config map validation error: incorrect opsmxIsdUrl: %v", opsmxIsdUrl)
return errors.New(errorMsg)
} else if err != nil && metric.OpsmxIsdUrl == "" && !strings.Contains(err.Error(), "timeout") {
errorMsg := fmt.Sprintf("opsmx profile secret validation error: incorrect opsmxIsdUrl: %v", opsmxIsdUrl)
return errors.New(errorMsg)
} else if err != nil {
return errors.New(err.Error())
} else if resp.StatusCode != 200 {
return errors.New(resp.Status)
}
return nil
}
// Check few conditions pre-analysis
func (metric *OPSMXMetric) basicChecks() error {
if metric.LifetimeMinutes == 0 && metric.EndTime == "" {
return errors.New("provider config map validation error: provide either lifetimeMinutes or end time")
}
if metric.CanaryStartTime != metric.BaselineStartTime && metric.LifetimeMinutes == 0 {
return errors.New("provider config map validation error: both canaryStartTime and baselineStartTime should be kept same while using endTime argument for analysis")
}
if metric.LifetimeMinutes != 0 && metric.LifetimeMinutes < 3 {
return errors.New("provider config map validation error: lifetimeMinutes cannot be less than 3 minutes")
}
if metric.IntervalTime != 0 && metric.IntervalTime < 3 {
return errors.New("provider config map validation error: intervalTime cannot be less than 3 minutes")
}
if metric.LookBackType != "" && metric.IntervalTime == 0 {
return errors.New("provider config map validation error: intervalTime should be given along with lookBackType to perform interval analysis")
}
return nil
}
// Return epoch values of the specific time provided along with lifetimeMinutes for the Run
func (metric *OPSMXMetric) getTimeVariables() error {
var canaryStartTime string
var baselineStartTime string
tm := time.Now()
if metric.CanaryStartTime == "" {
canaryStartTime = fmt.Sprintf("%d", tm.UnixNano()/int64(time.Millisecond))
} else {
tsStart, err := time.Parse(time.RFC3339, metric.CanaryStartTime)
if err != nil {
errorMsg := fmt.Sprintf("provider config map validation error: Error in parsing canaryStartTime: %v", err)
return errors.New(errorMsg)
}
canaryStartTime = fmt.Sprintf("%d", tsStart.UnixNano()/int64(time.Millisecond))
}
if metric.BaselineStartTime == "" {
baselineStartTime = fmt.Sprintf("%d", tm.UnixNano()/int64(time.Millisecond))
} else {
tsStart, err := time.Parse(time.RFC3339, metric.BaselineStartTime)
if err != nil {
errorMsg := fmt.Sprintf("provider config map validation error: Error in parsing baselineStartTime: %v", err)
return errors.New(errorMsg)
}
baselineStartTime = fmt.Sprintf("%d", tsStart.UnixNano()/int64(time.Millisecond))
}
//If lifetimeMinutes not given calculate using endTime
if metric.LifetimeMinutes == 0 {
tsEnd, err := time.Parse(time.RFC3339, metric.EndTime)
if err != nil {
errorMsg := fmt.Sprintf("provider config map validation error: Error in parsing endTime: %v", err)
return errors.New(errorMsg)
}
if metric.CanaryStartTime != "" && metric.CanaryStartTime > metric.EndTime {
err := errors.New("provider config map validation error: canaryStartTime cannot be greater than endTime")
return err
}
tsStart := tm
if metric.CanaryStartTime != "" {
tsStart, _ = time.Parse(time.RFC3339, metric.CanaryStartTime)
}
tsDifference := tsEnd.Sub(tsStart)
min, _ := time.ParseDuration(tsDifference.String())
metric.LifetimeMinutes = int(roundFloat(min.Minutes(), 0))
}
metric.BaselineStartTime = baselineStartTime
metric.CanaryStartTime = canaryStartTime
return nil
}
func getAnalysisTemplateData(basePath string) (OPSMXMetric, error) {
path := filepath.Join(basePath, "provider/providerConfig")
data, err := os.ReadFile(path)
if err != nil {
errorMsg := fmt.Sprintf("provider config map validation error: %v\n Action Required: Provider config map has to be mounted on '/etc/config/provider' in AnalysisTemplate and must carry data element 'providerConfig'", err)
err = errors.New(errorMsg)
return OPSMXMetric{}, err
}
var opsmx OPSMXMetric
if err := yaml.Unmarshal(data, &opsmx); err != nil {
errorMsg := fmt.Sprintf("provider config map validation error: %v", err)
err = errors.New(errorMsg)
return OPSMXMetric{}, err
}
if opsmx.Application == "" {
opsmx.Application, err = getScopeValues("{{env.APP_NAME}}")
if err != nil {
log.Warn("provider config map validation warning: unset environment variable APPName and missing application parameter in the provider config map.")
log.Info("attempting to retrieve App Name via labels of provider ConfigMap")
}
}
return opsmx, nil
}
func getProviderConfigNameFromJob(c *Clients, r ResourceNames) (string, error) {
jobValue, err := c.kubeclientset.BatchV1().Jobs(defaults.Namespace()).Get(context.TODO(), r.jobName, metav1.GetOptions{})
if err != nil {
return "", err
}
var analysisTemplateName string
for i := range jobValue.Spec.Template.Spec.Volumes {
if jobValue.Spec.Template.Spec.Volumes[i].ConfigMap.LocalObjectReference.Name != "" {
analysisTemplateName = jobValue.Spec.Template.Spec.Volumes[i].ConfigMap.LocalObjectReference.Name
break
}
}
analysisTemplate, err := c.kubeclientset.CoreV1().ConfigMaps(defaults.Namespace()).Get(context.TODO(), analysisTemplateName, metav1.GetOptions{})
if err != nil {
return "", err
}
return analysisTemplate.ObjectMeta.Labels["argocd.argoproj.io/instance"], nil
}
func generateSHA1(s string) string {
h := sha1.New()
h.Write([]byte(s))
sha1_hash := hex.EncodeToString(h.Sum(nil))
return sha1_hash
}
func isExists(list []string, item string) bool {
for _, v := range list {
if item == v {
return true
}
}
return false
}
func getTemplateDataYaml(templateFileData []byte, template string, templateType string, ScopeVariables string) ([]byte, error) {
if templateType == "LOG" {
var logdata LogTemplateYaml
if err := yaml.Unmarshal([]byte(templateFileData), &logdata); err != nil {
errorMessage := fmt.Sprintf("gitops '%s' template config map validation error: %v", template, err)
return nil, errors.New(errorMessage)
}
logdata.TemplateName = template
logdata.FilterKey = ScopeVariables
if len(logdata.Tags) >= 1 {
logdata.TagEnabled = true
}
var defaults LogTemplateYaml
err := json.Unmarshal([]byte(DefaultsErrorTopicsJson), &defaults)
if err != nil {
return nil, err
}
var defaultErrorString []string
defaultErrorStringMapType := make(map[string]string)
for _, items := range defaults.ErrorTopics {
defaultErrorStringMapType[items.ErrorStrings] = items.Topic
defaultErrorString = append(defaultErrorString, items.ErrorStrings)
}
var errorStringsAvailable []string
for i, items := range logdata.ErrorTopics {
errorStringsAvailable = append(errorStringsAvailable, items.ErrorStrings)
if isExists(defaultErrorString, items.ErrorStrings) {
if items.Topic == defaultErrorStringMapType[items.ErrorStrings] {
logdata.ErrorTopics[i].Type = "default"
} else {
logdata.ErrorTopics[i].Type = "custom"
}
}
}
if !logdata.DisableDefaultsErrorTopics {
log.Info("loading defaults tags for log template")
for _, items := range defaults.ErrorTopics {
if !isExists(errorStringsAvailable, items.ErrorStrings) {
logdata.ErrorTopics = append(logdata.ErrorTopics, items)
}
}
}
if logdata.ErrorTopics == nil {
logdata.ErrorTopics = make([]errorTopics, 0)
}
log.Info("processed template and converting to json", logdata)
return json.Marshal(logdata)
}
metricStruct, err := processYamlMetrics(templateFileData, template, ScopeVariables)
if err != nil {
return nil, err
}
return json.Marshal(metricStruct)
}
func getTemplateData(client http.Client, secretData map[string]string, template string, templateType string, basePath string, ScopeVariables string) (string, error) {
log.Info("processing gitops template", template)
var templateData string
templatePath := filepath.Join(basePath, "templates/")
path := filepath.Join(templatePath, template)
templateFileData, err := os.ReadFile(path)
if err != nil {
errorMsg := fmt.Sprintf("gitops '%s' template config map validation error: %v\n Action Required: Template has to be mounted on '/etc/config/templates' in AnalysisTemplate and must carry data element '%s'", template, err, template)
err = errors.New(errorMsg)
return "", err
}
log.Info("checking if json or yaml for template ", template)
if !isJSON(string(templateFileData)) {
log.Info("template not recognized in json format")
templateFileData, err = getTemplateDataYaml(templateFileData, template, templateType, ScopeVariables)
log.Info("json for template ", template, string(templateFileData))
if err != nil {
return "", err
}
} else {
checktemplateName := gjson.Get(string(templateFileData), "templateName")
if checktemplateName.String() == "" {
errmessage := fmt.Sprintf("gitops '%s' template config map validation error: template name not provided inside json", template)
return "", errors.New(errmessage)
}
if template != checktemplateName.String() {
errmessage := fmt.Sprintf("gitops '%s' template config map validation error: Mismatch between templateName and data.%s key", template, template)
return "", errors.New(errmessage)
}
}
sha1Code := generateSHA1(string(templateFileData))
tempLink := fmt.Sprintf(templateApi, sha1Code, templateType, template)
s := []string{secretData["opsmxIsdUrl"], tempLink}
templateUrl := strings.Join(s, "")
log.Debug("sending a GET request to gitops API")
data, _, _, err := makeRequest(client, "GET", templateUrl, "", secretData["user"])
if err != nil {
return "", err
}
var templateVerification bool
err = json.Unmarshal(data, &templateVerification)
if err != nil {
errorMessage := fmt.Sprintf("analysis Error: Expected bool response from gitops verifyTemplate response Error: %v. Action: Check endpoint given in secret/providerConfig.", err)
return "", errors.New(errorMessage)
}
templateData = sha1Code
var templateCheckSave map[string]interface{}
if !templateVerification {
log.Debug("sending a POST request to gitops API")
data, _, _, err = makeRequest(client, "POST", templateUrl, string(templateFileData), secretData["user"])
if err != nil {
return "", err
}
err = json.Unmarshal(data, &templateCheckSave)
if err != nil {
return "", err
}
log.Debugf("the value of templateCheckSave var is %v", templateCheckSave)
var errorss string
if templateCheckSave["errorMessage"] != nil && templateCheckSave["errorMessage"] != "" {
errorss = fmt.Sprintf("%v", templateCheckSave["errorMessage"])
} else {
errorss = fmt.Sprintf("%v", templateCheckSave["error"])
}
errorss = strings.Replace(strings.Replace(errorss, "[", "", -1), "]", "", -1)
if templateCheckSave["status"] != "CREATED" {
err = fmt.Errorf("gitops '%s' template config map validation error: %s", template, errorss)
return "", err
}
}
return templateData, nil
}
func (metric *OPSMXMetric) getDataSecret(basePath string) (map[string]string, error) {
secretData := map[string]string{}
userPath := filepath.Join(basePath, "secrets/user")
secretUser, err := os.ReadFile(userPath)
if err != nil {
err = errors.New("opsmx profile secret validation error: `user` key not present in the secret file\n Action Required: secret file has to be mounted on '/etc/config/secrets' in AnalysisTemplate and must carry data element 'user'")
return nil, err
}
opsmxIsdUrlPath := filepath.Join(basePath, "secrets/opsmxIsdUrl")
opsmxIsdUrl, err := os.ReadFile(opsmxIsdUrlPath)
if err != nil {
err = errors.New("opsmx profile secret validation error: `opsmxIsdUrl` key not present in the secret file\n Action Required: secret file has to be mounted on '/etc/config/secrets' in AnalysisTemplate and must carry data element 'opsmxIsdUrl'")
return nil, err
}
sourceNamePath := filepath.Join(basePath, "secrets/sourceName")
secretsourcename, err := os.ReadFile(sourceNamePath)
if err != nil {
err = errors.New("opsmx profile secret validation error: `sourceName` key not present in the secret file\n Action Required: secret file has to be mounted on '/etc/config/secrets' in AnalysisTemplate and must carry data element 'sourceName'")
return nil, err
}
cdIntegrationPath := filepath.Join(basePath, "secrets/cdIntegration")
secretcdintegration, err := os.ReadFile(cdIntegrationPath)
if err != nil {
err = errors.New("opsmx profile secret validation error: `cdIntegration` key not present in the secret file\n Action Required: secret file has to be mounted on '/etc/config/secrets' in AnalysisTemplate and must carry data element 'cdIntegration'")
return nil, err
}
agentNamePath := filepath.Join(basePath, "secrets/agentName")
secretagentname, err := os.ReadFile(agentNamePath)
if err != nil && string(secretcdintegration) == "true" {
err = errors.New("opsmx profile secret validation error: `agentName` key not present in the secret file\n Action Required: secret file has to be mounted on '/etc/config/secrets' in AnalysisTemplate and must carry data element 'agentName' for 'cdIntegration' as 'true'")
return nil, err
}
opsmxIsdURL := metric.OpsmxIsdUrl
if opsmxIsdURL == "" {
opsmxIsdURL = string(opsmxIsdUrl)
}
secretData["opsmxIsdUrl"] = opsmxIsdURL
user := metric.User
if user == "" {
user = string(secretUser)
}
secretData["user"] = user
var cdIntegration string
if string(secretcdintegration) == "true" {
cdIntegration = cdIntegrationArgoCD
} else if string(secretcdintegration) == "false" {
cdIntegration = cdIntegrationArgoRollouts
} else {
err := errors.New("opsmx profile secret validation error: cdIntegration should be either true or false")
return nil, err
}
secretData["cdIntegration"] = cdIntegration
secretData["sourceName"] = string(secretsourcename)
secretData["agentName"] = string(secretagentname)
return secretData, nil
}
func getScopeValues(scope string) (string, error) {
splitScope := strings.Split(scope, ",")
for i, items := range splitScope {
if strings.Contains(items, "{{env.") {
extrctVal := strings.Split(items, "{{env.")
extractkey := strings.Split(extrctVal[1], "}}")
podName, ok := os.LookupEnv(extractkey[0])
if !ok {
err := fmt.Sprintf("analysisTemplate validation error: environment variable %s not set", extractkey[0])
return "", errors.New(err)
}
old := fmt.Sprintf("{{env.%s}}", extractkey[0])
testresult := strings.Replace(items, old, podName, 1)
splitScope[i] = testresult
}
}
scopeValue := strings.Join(splitScope, ",")
return scopeValue, nil
}
func (metric *OPSMXMetric) generatePayload(c *Clients, secretData map[string]string, basePath string) (string, error) {
var intervalTime string
if metric.IntervalTime != 0 {
intervalTime = fmt.Sprintf("%d", metric.IntervalTime)
}
var opsmxdelay string
if metric.Delay != 0 {
opsmxdelay = fmt.Sprintf("%d", metric.Delay)
}
var services []string
//Generate the payload
payload := jobPayload{
Application: metric.Application,
SourceName: secretData["sourceName"],
SourceType: secretData["cdIntegration"],
AgentName: secretData["agentName"],
CanaryConfig: canaryConfig{
LifetimeMinutes: fmt.Sprintf("%d", metric.LifetimeMinutes),
LookBackType: metric.LookBackType,
IntervalTime: intervalTime,
Delays: opsmxdelay,
CanaryHealthCheckHandler: canaryHealthCheckHandler{
MinimumCanaryResultScore: fmt.Sprintf("%d", metric.Pass),
},
CanarySuccessCriteria: canarySuccessCriteria{
CanaryResultScore: fmt.Sprintf("%d", metric.Pass),
},
},
CanaryDeployments: []canaryDeployments{},
}
if metric.Services != nil || len(metric.Services) != 0 {
deployment := canaryDeployments{
BaselineStartTimeMs: metric.BaselineStartTime,
CanaryStartTimeMs: metric.CanaryStartTime,
Baseline: &logMetric{
Log: map[string]map[string]string{},
Metric: map[string]map[string]string{},
},
Canary: &logMetric{
Log: map[string]map[string]string{},
Metric: map[string]map[string]string{},
},
}
for i, item := range metric.Services {
valid := false
serviceName := fmt.Sprintf("service%d", i+1)
if item.ServiceName != "" {
serviceName = item.ServiceName
}
if isExists(services, serviceName) {
errorMsg := fmt.Sprintf("provider config map validation error: serviceName '%s' mentioned in provider Config exists more than once", serviceName)
return "", errors.New(errorMsg)
}
services = append(services, serviceName)
gateName := fmt.Sprintf("gate%d", i+1)
if item.LogScopeVariables == "" && item.BaselineLogScope != "" || item.LogScopeVariables == "" && item.CanaryLogScope != "" {
errorMsg := fmt.Sprintf("provider config map validation error: missing log Scope placeholder for the provided baseline/canary of service '%s'", serviceName)
err := errors.New(errorMsg)
if err != nil {
return "", err
}
}
//For Log Analysis is to be added in analysis-run
if item.LogScopeVariables != "" {
//Check if no baseline or canary
if item.BaselineLogScope != "" && item.CanaryLogScope == "" {
errorMsg := fmt.Sprintf("provider config map validation error: missing canary for log analysis of service '%s'", serviceName)
err := errors.New(errorMsg)
if err != nil {
return "", err
}
}
//Check if the number of placeholders provided dont match
if len(strings.Split(item.LogScopeVariables, ",")) != len(strings.Split(item.BaselineLogScope, ",")) || len(strings.Split(item.LogScopeVariables, ",")) != len(strings.Split(item.CanaryLogScope, ",")) {
errorMsg := fmt.Sprintf("provider config map validation error: mismatch in number of log scope variables and baseline/canary log scope of service '%s'", serviceName)
err := errors.New(errorMsg)
if err != nil {
return "", err
}
}
if item.LogTemplateName == "" && metric.GlobalLogTemplate == "" {
errorMsg := fmt.Sprintf("provider config map validation error: provide either a service specific log template or global log template for service '%s'", serviceName)
err := errors.New(errorMsg)
if err != nil {
return "", err
}
}
baslineLogScope, errors := getScopeValues(item.BaselineLogScope)
if errors != nil {
return "", errors
}
//Add mandatory field for baseline
deployment.Baseline.Log[serviceName] = map[string]string{
item.LogScopeVariables: baslineLogScope,
"serviceGate": gateName,
}
canaryLogScope, errors := getScopeValues(item.CanaryLogScope)
if errors != nil {
return "", errors
}
//Add mandatory field for canary
deployment.Canary.Log[serviceName] = map[string]string{
item.LogScopeVariables: canaryLogScope,
"serviceGate": gateName,
}
var tempName string
tempName = item.LogTemplateName
if item.LogTemplateName == "" {
tempName = metric.GlobalLogTemplate
}
//Add service specific templateName
deployment.Baseline.Log[serviceName]["template"] = tempName
deployment.Canary.Log[serviceName]["template"] = tempName
var templateData string
var err error
if metric.GitOPS && item.LogTemplateVersion == "" {
templateData, err = getTemplateData(c.client, secretData, tempName, "LOG", basePath, item.LogScopeVariables)
if err != nil {
return "", err
}
}
if metric.GitOPS && templateData != "" && item.LogTemplateVersion == "" {
deployment.Baseline.Log[serviceName]["templateSha1"] = templateData
deployment.Canary.Log[serviceName]["templateSha1"] = templateData
}
//Add non-mandatory field of Templateversion if provided
if item.LogTemplateVersion != "" {
deployment.Baseline.Log[serviceName]["templateVersion"] = item.LogTemplateVersion
deployment.Canary.Log[serviceName]["templateVersion"] = item.LogTemplateVersion
}
valid = true
}
if item.MetricScopeVariables == "" && item.BaselineMetricScope != "" || item.MetricScopeVariables == "" && item.CanaryMetricScope != "" {
errorMsg := fmt.Sprintf("provider config map validation error: missing metric Scope placeholder for the provided baseline/canary of service '%s'", serviceName)
err := errors.New(errorMsg)
if err != nil {
return "", err
}
}
//For metric analysis is to be added in analysis-run
if item.MetricScopeVariables != "" {
//Check if no baseline or canary
if item.BaselineMetricScope == "" || item.CanaryMetricScope == "" {
errorMsg := fmt.Sprintf("provider config map validation error: missing baseline/canary for metric analysis of service '%s'", serviceName)
err := errors.New(errorMsg)
if err != nil {
return "", err
}
}
//Check if the number of placeholders provided dont match
if len(strings.Split(item.MetricScopeVariables, ",")) != len(strings.Split(item.BaselineMetricScope, ",")) || len(strings.Split(item.MetricScopeVariables, ",")) != len(strings.Split(item.CanaryMetricScope, ",")) {
errorMsg := fmt.Sprintf("provider config map validation error: mismatch in number of metric scope variables and baseline/canary metric scope of service '%s'", serviceName)
err := errors.New(errorMsg)
if err != nil {
return "", err
}
}
if item.MetricTemplateName == "" && metric.GlobalMetricTemplate == "" {
errorMsg := fmt.Sprintf("provider config map validation error: provide either a service specific metric template or global metric template for service: %s", serviceName)
err := errors.New(errorMsg)
if err != nil {
return "", err
}
}
baselineMetricScope, errors := getScopeValues(item.BaselineMetricScope)
if errors != nil {
return "", errors
}
//Add mandatory field for baseline
deployment.Baseline.Metric[serviceName] = map[string]string{
item.MetricScopeVariables: baselineMetricScope,
"serviceGate": gateName,
}
canaryMetricScope, errors := getScopeValues(item.CanaryMetricScope)
if errors != nil {
return "", errors
}
//Add mandatory field for canary
deployment.Canary.Metric[serviceName] = map[string]string{
item.MetricScopeVariables: canaryMetricScope,
"serviceGate": gateName,
}
var tempName string
tempName = item.MetricTemplateName
if item.MetricTemplateName == "" {
tempName = metric.GlobalMetricTemplate
}
//Add templateName
deployment.Baseline.Metric[serviceName]["template"] = tempName
deployment.Canary.Metric[serviceName]["template"] = tempName
var templateData string
var err error
if metric.GitOPS && item.MetricTemplateVersion == "" {
templateData, err = getTemplateData(c.client, secretData, tempName, "METRIC", basePath, item.MetricScopeVariables)
if err != nil {
return "", err
}
}
if metric.GitOPS && templateData != "" && item.MetricTemplateVersion == "" {
deployment.Baseline.Metric[serviceName]["templateSha1"] = templateData
deployment.Canary.Metric[serviceName]["templateSha1"] = templateData
}
//Add non-mandatory field of Template Version if provided
if item.MetricTemplateVersion != "" {
deployment.Baseline.Metric[serviceName]["templateVersion"] = item.MetricTemplateVersion
deployment.Canary.Metric[serviceName]["templateVersion"] = item.MetricTemplateVersion
}
valid = true
}
//Check if no logs or metrics were provided
if !valid {
err := errors.New("provider config map validation error: at least one of log or metric context must be provided")
if err != nil {
return "", err
}
}
}
payload.CanaryDeployments = append(payload.CanaryDeployments, deployment)
} else {
//Check if no services were provided
err := errors.New("provider config map validation error: no services provided")
return "", err
}
buffer, err := json.Marshal(payload)
if err != nil {
return "", err
}
return string(buffer), err
}
// Evaluate canaryScore and accordingly set the AnalysisPhase
func evaluateResult(score int, pass int) string {
if score >= pass {
return "Successful"
}
return "Failed"
}
// Extract the canaryScore and evaluateResult
func (metric *OPSMXMetric) processResume(data []byte) (string, string, error) {
var (
canaryScore string
result map[string]interface{}
finalScore map[string]interface{}
)
err := json.Unmarshal(data, &result)
if err != nil {
errorMessage := fmt.Sprintf("analysis Error: Error in post processing canary Response. Error: %v", err)
return "", "", errors.New(errorMessage)
}
jsonBytes, _ := json.MarshalIndent(result["canaryResult"], "", " ")
err = json.Unmarshal(jsonBytes, &finalScore)
if err != nil {
return "", "", err
}
if finalScore["overallScore"] == nil {
canaryScore = "0"
} else {
canaryScore = fmt.Sprintf("%v", finalScore["overallScore"])
}
var score int
// var err error
if strings.Contains(canaryScore, ".") {
floatScore, err := strconv.ParseFloat(canaryScore, 64)
if err != nil {
return "", "", err
}
score = int(roundFloat(floatScore, 0))
} else {
score, err = strconv.Atoi(canaryScore)
if err != nil {
return "", "", err
}
}
Phase := evaluateResult(score, int(metric.Pass))
return Phase, fmt.Sprintf("%v", score), nil
}