-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.go
187 lines (154 loc) · 6.67 KB
/
start.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
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
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 analysis
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/armory-io/kayentactl/internal/options"
"github.com/armory-io/kayentactl/internal/canaryConfig"
"github.com/armory-io/kayentactl/internal/report"
"github.com/armory-io/kayentactl/internal/analysis"
"github.com/armory-io/kayentactl/pkg/kayenta"
"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// TODO: get rid of these package global variables. it was easier to port existing code by using them.
var (
scope, configLocation, control, experiment, startTimeIso, endTimeIso, thresholds, metricsAccount, storageAccount string
controlOffset, lifetimeDuration, analysisInterval, checkInterval, timeout time.Duration
noWait bool
)
// processThresholds takes a string in the format of marginal=?,pass=? and creates
// a kayenta.Threshold using the values. if the string is malformed or cannot be
// processed, the defaults are used
func processThresholds(t string, defaultMarginal, defaultPass string) kayenta.Threshold {
parts := strings.Split(t, ",")
m := map[string]string{}
for _, part := range parts {
s := strings.Split(part, "=")
if len(s) == 2 {
m[s[0]] = s[1]
}
}
marginal := defaultMarginal
if v, ok := m["marginal"]; ok && v != "" {
marginal = v
}
pass := defaultPass
if v, ok := m["pass"]; ok && v != "" {
pass = v
}
return kayenta.Threshold{Marginal: marginal, Pass: pass}
}
// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
globals, _ := options.Globals(cmd)
if !globals.NoColor {
fmt.Printf("%v\n", color.HiMagentaString(report.AsciiKayenta))
}
kc := kayenta.NewDefaultClient(kayenta.ClientBaseURL(globals.KayentaURL))
log.Debugf("Fetching canary config from: %s", color.BlueString(configLocation))
canaryConfig, err := canaryConfig.GetCanaryConfig(configLocation)
if err != nil {
log.Fatalf("failed to fetch and parse canary config: %s", err.Error())
}
// if the control and experiment are the same, users
// can provide a single scope option for both
if (control == "" && experiment == "") && scope != "" {
control, experiment = scope, scope
}
executionRequest, err := analysis.BuildExecutionRequest(analysis.ExecutionRequestContext{
ControlScope: control,
ExperimentScope: experiment,
StartTimeIso: startTimeIso,
EndTimeIso: endTimeIso,
ControlOffset: controlOffset,
AnalysisIntervalMins: analysisInterval,
LifetimeDurationMins: lifetimeDuration,
Thresholds: processThresholds(thresholds, "50", "90"),
})
if err != nil {
log.Fatalf("unable to create execution request: %s", err.Error())
}
input := kayenta.StandaloneCanaryAnalysisInput{
ExecutionRequest: *executionRequest,
CanaryConfig: *canaryConfig,
MetricsAccountName: metricsAccount,
StorageAccountName: storageAccount,
}
// start standalone canary
log.Debugf("Analysis Execution starting with kayenta host: %v", color.BlueString(globals.KayentaURL))
output, err := kc.StartStandaloneCanaryAnalysis(input)
if err != nil {
log.Fatalf("error starting canary analysis: %s", err.Error())
}
analysisID := output.CanaryAnalysisExecutionID
log.Info(fmt.Sprintf("Analysis Execution ID: %s", color.GreenString(analysisID)))
// if the no-wait flag is set, we exit early. this enables users
// to implement their own wait login within scripts
if noWait {
return
}
// poll until standalone canary is complete
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ticker := time.NewTicker(checkInterval)
progressPrinter := analysis.NewDefaultGraphicalProgressPrinter()
progressPrinter.Start()
if err := analysis.WaitForComplete(ctx, analysisID, kc, ticker, progressPrinter.PrintProgress); err != nil {
log.Fatalf(err.Error())
}
progressPrinter.Stop()
// generate some kind of report
result, err := kc.GetStandaloneCanaryAnalysis(analysisID)
if err != nil {
log.Fatalf("Failed to get analysis result: %s", err.Error())
}
if err := report.Report(result, "pretty", os.Stdout); err != nil {
log.Fatalf("error generating analysis report: %s", err.Error())
}
fmt.Println(analysis.TableStatus(result))
exitCode := 1
if result.IsSuccessful() {
exitCode = 0
}
os.Exit(exitCode)
},
}
func init() {
analysisCmd.AddCommand(startCmd)
flags := startCmd.Flags()
flags.StringVar(&configLocation, "canary-config", "canary.json", "location of canary configuration")
flags.StringVarP(&scope, "scope", "s", "", "name of the scope to use")
flags.StringVarP(&control, "control", "c", "", "application to use as the experiment control (i.e. baseline)")
flags.StringVarP(&experiment, "experiment", "e", "", "application to use as the experiment (i.e. canary)")
flags.StringVar(&startTimeIso, "start-time-iso", "", "start time for the analysis in ISO format. Ex: 2020-12-20T14:49:31.647Z")
flags.StringVar(&endTimeIso, "end-time-iso", "", "end time for the analysis in ISO format. Ex: 2020-12-20T15:49:31.647Z")
flags.StringVar(&thresholds, "thresholds", "marginal=50,pass=90", "comma-delimeted threshold levels")
flags.StringVar(&metricsAccount, "metrics-account", "", "metrics account name")
flags.StringVar(&storageAccount, "storage-account", "", "storage account name")
flags.DurationVar(&analysisInterval, "analysis-interval", 1*time.Minute, "Minutes between each analysis. Default is once per minute")
flags.DurationVar(&lifetimeDuration, "lifetime-duration", time.Minute*5, "Total duration time for the analysis")
flags.DurationVar(&checkInterval, "interval", time.Second*5, "polling interval")
flags.DurationVar(&timeout, "timeout", time.Hour, "timeout")
flags.DurationVar(&controlOffset, "control-offset", time.Hour, "The control offset to compare against the experiment, by default is your new deployment")
flags.BoolVar(&noWait, "no-wait", false, "don't wait for canary execution to complete before exiting")
}