forked from kiali/kiali
/
iter8.go
372 lines (331 loc) · 12 KB
/
iter8.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
package kubernetes
import (
"fmt"
"gopkg.in/yaml.v2"
core_v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
var iter8typeMeta = meta_v1.TypeMeta{
Kind: PluralType[iter8experiments],
APIVersion: ApiIter8Version,
}
// Linked with https://github.com/iter8-tools/iter8-controller/blob/master/pkg/apis/iter8/v1alpha1/experiment_types.go
type Iter8ExperimentSpec struct {
TargetService struct {
ApiVersion string `json:"apiVersion"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Baseline string `json:"baseline"`
Candidate string `json:"candidate"`
} `json:"targetService"`
TrafficControl struct {
Strategy string `json:"strategy,omitempty"`
MaxTrafficPercentage float64 `json:"maxTrafficPercentage,omitempty"`
TrafficStepSize float64 `json:"trafficStepSize,omitempty"`
Interval string `json:"interval,omitempty"`
MaxIterations int `json:"maxIterations,omitempty"`
OnSuccess string `json:"onSuccess,omitempty"`
Confidence float64 `json:"confidence,omitempty"`
} `json:"trafficControl,omitempty"`
Analysis struct {
AnalyticsService string `json:"analyticsService,omitempty"`
GrafanaEndpoint string `json:"grafanaEndpoint,omitempty"`
SuccessCriteria []struct {
MetricName string `json:"metricName,omitempty"`
ToleranceType string `json:"toleranceType,omitempty"`
Tolerance float64 `json:"tolerance,omitempty"`
SampleSize int `json:"sampleSize,omitempty"`
MinMax struct {
Min float64 `json:"min,omitempty"`
Max float64 `json:"max,omitempty"`
} `json:"min_max,omitempty"`
StopOnFailure bool `json:"stopOnFailure,omitempty"`
} `json:"successCriteria,omitempty"`
Reward *struct {
MetricName string `json:"metricName,omitempty"`
MinMax string `json:"min_max,omitempty"`
} `json:"reward,omitempty"`
} `json:"analysis,omitempty"`
Assessment string `json:"assessment,omitempty"`
Cleanup string `json:"cleanup,omitempty"`
RoutingReference *struct {
ApiVersion string `json:"apiVersion,omitempty"`
Kind string `json:"kind,omitempty"`
Name string `json:"name,omitempty"`
} `json:"routingReference,omitempty"`
}
type Iter8ExperimentStatus struct {
Conditions []struct {
LastTransitionTime string `json:"lastTransitionTime"`
Message string `json:"message"`
Reason string `json:"reason"`
Status string `json:"status"`
Type string `json:"type"`
} `json:"conditions"`
CreateTimeStamp int64 `json:"createTimestamp"`
StartTimeStamp int64 `json:"startTimestamp"`
EndTimestamp int64 `json:"endTimestamp"`
LastIncrementTime string `json:"lastIncrementTime"`
CurrentIteration int `json:"currentIteration"`
AnalysisState map[string]interface{} `json:"analysisState"`
GrafanaURL string `json:"grafanaURL"`
Assestment struct {
Conclusions []string `json:"conclusions"`
AllSuccessCriteriaMet bool `json:"all_success_criteria_met,omitempty"`
AbortExperiment bool `json:"abort_experiment,omitempty"`
SuccessCriteriaStatus []struct {
// Name of the metric to which the criterion applies
// example: iter8_latency
MetricName string `json:"metric_name"`
// Assessment of this success criteria in plain English
Conclusions []string `json:"conclusions"`
// Indicates whether or not the success criterion for the corresponding metric has been met
SuccessCriterionMet bool `json:"success_criterion_met"`
// Indicates whether or not the experiment must be aborted on the basis of the criterion for this metric
AbortExperiment bool `json:"abort_experiment"`
} `json:"success_criteria,omitempty"`
} `json:"assessment"`
TrafficSplitPercentage struct {
Baseline int `json:"baseline"`
Candidate int `json:"candidate"`
} `json:"trafficSplitPercentage"`
Phase string `json:"phase"`
Message string `json:"message"`
}
type Iter8ExperimentMetrics map[string]struct {
AbsentValue string `json:"absent_value,omitempty"`
IsCounter bool `json:"is_counter,omitempty"`
QueryTemplate string `json:"query_template,omitempty"`
SampleSizeTemplate string `json:"sample_size_template,omitempty"`
}
// Iter8Experiment is a dynamic object to map Iter8 Experiments
type Iter8Experiment interface {
runtime.Object
GetSpec() Iter8ExperimentSpec
SetSpec(Iter8ExperimentSpec)
GetStatus() Iter8ExperimentStatus
SetStatus(Iter8ExperimentStatus)
GetMetrics() Iter8ExperimentMetrics
SetMetrics(Iter8ExperimentMetrics)
GetTypeMeta() meta_v1.TypeMeta
SetTypeMeta(meta_v1.TypeMeta)
GetObjectMeta() meta_v1.ObjectMeta
SetObjectMeta(meta_v1.ObjectMeta)
DeepCopyIter8Object() Iter8Experiment
}
type Iter8ExperimentList interface {
runtime.Object
GetItems() []Iter8Experiment
}
type Iter8ExperimentObject struct {
meta_v1.TypeMeta `json:",inline"`
meta_v1.ObjectMeta `json:"metadata"`
Spec Iter8ExperimentSpec `json:"spec"`
Status Iter8ExperimentStatus `json:"status"`
Metrics Iter8ExperimentMetrics `json:"metrics"`
}
type Iter8ExperimentObjectList struct {
meta_v1.TypeMeta `json:",inline"`
meta_v1.ListMeta `json:"metadata"`
Items []Iter8ExperimentObject `json:"items"`
}
// GetSpec from a wrapper
func (in *Iter8ExperimentObject) GetSpec() Iter8ExperimentSpec {
return in.Spec
}
// SetSpec for a wrapper
func (in *Iter8ExperimentObject) SetSpec(spec Iter8ExperimentSpec) {
in.Spec = spec
}
func (in *Iter8ExperimentObject) GetStatus() Iter8ExperimentStatus {
return in.Status
}
// SetStatus for a wrapper
func (in *Iter8ExperimentObject) SetStatus(status Iter8ExperimentStatus) {
in.Status = status
}
func (in *Iter8ExperimentObject) GetMetrics() Iter8ExperimentMetrics {
return in.Metrics
}
// SetSpec for a wrapper
func (in *Iter8ExperimentObject) SetMetrics(metrics Iter8ExperimentMetrics) {
in.Metrics = metrics
}
// GetTypeMeta from a wrapper
func (in *Iter8ExperimentObject) GetTypeMeta() meta_v1.TypeMeta {
return in.TypeMeta
}
// SetObjectMeta for a wrapper
func (in *Iter8ExperimentObject) SetTypeMeta(typemeta meta_v1.TypeMeta) {
in.TypeMeta = typemeta
}
// GetObjectMeta from a wrapper
func (in *Iter8ExperimentObject) GetObjectMeta() meta_v1.ObjectMeta {
return in.ObjectMeta
}
// SetObjectMeta for a wrapper
func (in *Iter8ExperimentObject) SetObjectMeta(metadata meta_v1.ObjectMeta) {
in.ObjectMeta = metadata
}
// GetItems from a wrapper
func (in *Iter8ExperimentObjectList) GetItems() []Iter8Experiment {
out := make([]Iter8Experiment, len(in.Items))
for i := range in.Items {
out[i] = &in.Items[i]
}
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Iter8ExperimentObject) DeepCopyInto(out *Iter8ExperimentObject) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
out.Spec = in.Spec
out.Status = in.Status
out.Metrics = in.Metrics
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GenericIstioObject.
func (in *Iter8ExperimentObject) DeepCopy() *Iter8ExperimentObject {
if in == nil {
return nil
}
out := new(Iter8ExperimentObject)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *Iter8ExperimentObject) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
func (in *Iter8ExperimentObject) DeepCopyIter8Object() Iter8Experiment {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Iter8ExperimentObjectList) DeepCopyInto(out *Iter8ExperimentObjectList) {
*out = *in
out.TypeMeta = in.TypeMeta
out.ListMeta = in.ListMeta
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]Iter8ExperimentObject, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GenericIstioObjectList.
func (in *Iter8ExperimentObjectList) DeepCopy() *Iter8ExperimentObjectList {
if in == nil {
return nil
}
out := new(Iter8ExperimentObjectList)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *Iter8ExperimentObjectList) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// Metric structure of cm/iter8_metric
type Iter8AnalyticMetric struct {
Name string `yaml:"name"`
IsCounter bool `yaml:"is_counter"`
AbsentValue string `yaml:"absent_value"`
SampleSizeTemplate string `yaml:"sample_size_query_template"`
}
type Iter8ClientInterface interface {
CreateIter8Experiment(namespace string, json string) (Iter8Experiment, error)
DeleteIter8Experiment(namespace string, name string) error
GetIter8Experiment(namespace string, name string) (Iter8Experiment, error)
GetIter8Experiments(namespace string) ([]Iter8Experiment, error)
IsIter8Api() bool
Iter8ConfigMap() ([]string, error)
}
func (in *IstioClient) IsIter8Api() bool {
if in.isIter8Api == nil {
isIter8Api := false
_, err := in.k8s.RESTClient().Get().AbsPath("/apis/iter8.tools").Do().Raw()
if err == nil {
isIter8Api = true
}
in.isIter8Api = &isIter8Api
}
return *in.isIter8Api
}
func (in *IstioClient) Iter8ConfigMap() ([]string, error) {
mnames := make([]string, 0)
var result = &core_v1.ConfigMap{}
err := in.k8s.CoreV1().RESTClient().Get().Namespace("iter8").Resource("configmaps").
Name(iter8configMap).Do().Into(result)
if err == nil {
metrics := []Iter8AnalyticMetric{}
err = yaml.Unmarshal([]byte(result.Data["metrics"]), &metrics)
if err == nil {
for _, m := range metrics {
mnames = append(mnames, m.Name)
}
}
}
return mnames, err
}
func (in *IstioClient) CreateIter8Experiment(namespace string, json string) (Iter8Experiment, error) {
var result runtime.Object
var err error
byteJson := []byte(json)
result, err = in.iter8Api.Post().Namespace(namespace).Resource(iter8experiments).Body(byteJson).Do().Get()
if err != nil {
return nil, err
}
iter8ExperimentObject, ok := result.(*Iter8ExperimentObject)
if !ok {
return nil, fmt.Errorf("%s doesn't return a Iter8 Experiment object", namespace)
}
i8 := iter8ExperimentObject.DeepCopyIter8Object()
i8.SetTypeMeta(iter8typeMeta)
return i8, nil
}
func (in *IstioClient) GetIter8Experiment(namespace string, name string) (Iter8Experiment, error) {
result, err := in.iter8Api.Get().Namespace(namespace).Resource(iter8experiments).SubResource(name).Do().Get()
if err != nil {
return nil, err
}
iter8ExperimentObject, ok := result.(*Iter8ExperimentObject)
if !ok {
return nil, fmt.Errorf("%s/%s doesn't return a Iter8 Experiment object", namespace, name)
}
i8 := iter8ExperimentObject.DeepCopyIter8Object()
i8.SetTypeMeta(iter8typeMeta)
return i8, nil
}
func (in *IstioClient) GetIter8Experiments(namespace string) ([]Iter8Experiment, error) {
result, err := in.iter8Api.Get().Namespace(namespace).Resource(iter8experiments).Do().Get()
if err != nil {
return nil, err
}
iter8ExperimentList, ok := result.(*Iter8ExperimentObjectList)
if !ok {
return nil, fmt.Errorf("%s doesn't return a Iter8 Experiment list", namespace)
}
iter8Experiments := make([]Iter8Experiment, 0)
for _, iter8Experiment := range iter8ExperimentList.GetItems() {
i8 := iter8Experiment.DeepCopyIter8Object()
i8.SetTypeMeta(iter8typeMeta)
iter8Experiments = append(iter8Experiments, i8)
}
return iter8Experiments, nil
}
func (in *IstioClient) DeleteIter8Experiment(namespace string, name string) error {
var err error
_, err = in.iter8Api.Delete().Namespace(namespace).Resource(iter8experiments).Name(name).Do().Get()
return err
}