/
result.go
187 lines (162 loc) · 4.95 KB
/
result.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 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package framework
import (
"fmt"
"sort"
"strings"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
// Severity indicates the severity of the Result
type Severity string
const (
// Error indicates the result is an error. Will cause the function to exit non-0.
Error Severity = "error"
// Warning indicates the result is a warning
Warning Severity = "warning"
// Info indicates the result is an informative message
Info Severity = "info"
)
// ResultItem defines a validation result
type Result struct {
// Message is a human readable message. This field is required.
Message string `yaml:"message,omitempty" json:"message,omitempty"`
// Severity is the severity of this result
Severity Severity `yaml:"severity,omitempty" json:"severity,omitempty"`
// ResourceRef is a reference to a resource.
// Required fields: apiVersion, kind, name.
ResourceRef *yaml.ResourceIdentifier `yaml:"resourceRef,omitempty" json:"resourceRef,omitempty"`
// Field is a reference to the field in a resource this result refers to
Field *Field `yaml:"field,omitempty" json:"field,omitempty"`
// File references a file containing the resource this result refers to
File *File `yaml:"file,omitempty" json:"file,omitempty"`
// Tags is an unstructured key value map stored with a result that may be set
// by external tools to store and retrieve arbitrary metadata
Tags map[string]string `yaml:"tags,omitempty" json:"tags,omitempty"`
}
// String provides a human-readable message for the result item
func (i Result) String() string {
identifier := i.ResourceRef
var idStringList []string
if identifier != nil {
if identifier.APIVersion != "" {
idStringList = append(idStringList, identifier.APIVersion)
}
if identifier.Kind != "" {
idStringList = append(idStringList, identifier.Kind)
}
if identifier.Namespace != "" {
idStringList = append(idStringList, identifier.Namespace)
}
if identifier.Name != "" {
idStringList = append(idStringList, identifier.Name)
}
}
formatString := "[%s]"
severity := i.Severity
// We default Severity to Info when converting a result to a message.
if i.Severity == "" {
severity = Info
}
list := []interface{}{severity}
if len(idStringList) > 0 {
formatString += " %s"
list = append(list, strings.Join(idStringList, "/"))
}
if i.Field != nil {
formatString += " %s"
list = append(list, i.Field.Path)
}
formatString += ": %s"
list = append(list, i.Message)
return fmt.Sprintf(formatString, list...)
}
// File references a file containing a resource
type File struct {
// Path is relative path to the file containing the resource.
// This field is required.
Path string `yaml:"path,omitempty" json:"path,omitempty"`
// Index is the index into the file containing the resource
// (i.e. if there are multiple resources in a single file)
Index int `yaml:"index,omitempty" json:"index,omitempty"`
}
// Field references a field in a resource
type Field struct {
// Path is the field path. This field is required.
Path string `yaml:"path,omitempty" json:"path,omitempty"`
// CurrentValue is the current field value
CurrentValue interface{} `yaml:"currentValue,omitempty" json:"currentValue,omitempty"`
// ProposedValue is the proposed value of the field to fix an issue.
ProposedValue interface{} `yaml:"proposedValue,omitempty" json:"proposedValue,omitempty"`
}
type Results []*Result
// Error enables Results to be returned as an error
func (e Results) Error() string {
var msgs []string
for _, i := range e {
msgs = append(msgs, i.String())
}
return strings.Join(msgs, "\n\n")
}
// ExitCode provides the exit code based on the result's severity
func (e Results) ExitCode() int {
for _, i := range e {
if i.Severity == Error {
return 1
}
}
return 0
}
// Sort performs an in place stable sort of Results
func (e Results) Sort() {
sort.SliceStable(e, func(i, j int) bool {
if fileLess(e, i, j) != 0 {
return fileLess(e, i, j) < 0
}
if severityLess(e, i, j) != 0 {
return severityLess(e, i, j) < 0
}
return resultToString(*e[i]) < resultToString(*e[j])
})
}
func severityLess(items Results, i, j int) int {
severityToNumber := map[Severity]int{
Error: 0,
Warning: 1,
Info: 2,
}
severityLevelI, found := severityToNumber[items[i].Severity]
if !found {
severityLevelI = 3
}
severityLevelJ, found := severityToNumber[items[j].Severity]
if !found {
severityLevelJ = 3
}
return severityLevelI - severityLevelJ
}
func fileLess(items Results, i, j int) int {
var fileI, fileJ File
if items[i].File == nil {
fileI = File{}
} else {
fileI = *items[i].File
}
if items[j].File == nil {
fileJ = File{}
} else {
fileJ = *items[j].File
}
if fileI.Path != fileJ.Path {
if fileI.Path < fileJ.Path {
return -1
} else {
return 1
}
}
return fileI.Index - fileJ.Index
}
func resultToString(item Result) string {
return fmt.Sprintf("resource-ref:%s,field:%s,message:%s",
item.ResourceRef, item.Field, item.Message)
}