forked from kiali/kiali
/
istio_validation.go
108 lines (91 loc) · 2.79 KB
/
istio_validation.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
package models
import (
"encoding/json"
)
// NamespaceValidations represents a set of IstioValidations grouped by namespace
type NamespaceValidations map[string]IstioValidations
// IstioValidationKey is the key value composed of an Istio ObjectType and Name.
type IstioValidationKey struct {
ObjectType string
Name string
}
// IstioValidations represents a set of IstioValidation grouped by IstioValidationKey.
type IstioValidations map[IstioValidationKey]*IstioValidation
// IstioValidation represents a list of checks associated to an Istio object.
// swagger:model
type IstioValidation struct {
// Name of the object itself
// required: true
// example: reviews
Name string `json:"name"`
// Type of the object
// required: true
// example: virtualservice
ObjectType string `json:"objectType"`
// Represents validity of the object: in case of warning, validity remains as true
// required: true
// example: false
Valid bool `json:"valid"`
// Array of checks. It might be empty.
Checks []*IstioCheck `json:"checks"`
}
// IstioCheck represents an individual check.
// swagger:model
type IstioCheck struct {
// Description of the check
// required: true
// example: Weight sum should be 100
Message string `json:"message"`
// Indicates the level of importance: error or warning
// required: true
// example: error
Severity string `json:"severity"`
// String that describes where in the yaml file is the check located
// example: spec/http[0]/route
Path string `json:"path"`
}
var ObjectTypeSingular = map[string]string{
"gateways": "gateway",
"virtualservices": "virtualservice",
"destinationrules": "destinationrule",
"serviceentries": "serviceentry",
"rules": "rule",
"quotaspecs": "quotaspec",
"quotaspecbindings": "quotaspecbinding",
}
func BuildCheck(message, severity, path string) IstioCheck {
return IstioCheck{Message: message, Severity: severity, Path: path}
}
func (iv IstioValidations) FilterByKey(objectType, name string) IstioValidations {
fiv := IstioValidations{}
for k, v := range iv {
if k.Name == name && k.ObjectType == objectType {
fiv[k] = v
}
}
return fiv
}
func (iv IstioValidations) MergeValidations(validations IstioValidations) IstioValidations {
for key, validation := range validations {
v, ok := iv[key]
if !ok {
iv[key] = validation
} else {
v.Checks = append(v.Checks, validation.Checks...)
v.Valid = v.Valid && validation.Valid
}
}
return iv
}
// MarshalJSON implements the json.Marshaler interface.
func (iv IstioValidations) MarshalJSON() ([]byte, error) {
out := make(map[string]map[string]*IstioValidation)
for k, v := range iv {
_, ok := out[k.ObjectType]
if !ok {
out[k.ObjectType] = make(map[string]*IstioValidation)
}
out[k.ObjectType][k.Name] = v
}
return json.Marshal(out)
}