-
Notifications
You must be signed in to change notification settings - Fork 103
/
verify.go
102 lines (87 loc) · 2.72 KB
/
verify.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
package verify
import (
"fmt"
"strings"
"github.com/kudobuilder/kudo/pkg/kudoctl/packages"
"github.com/kudobuilder/kudo/pkg/kudoctl/packages/verifier/plan"
"github.com/kudobuilder/kudo/pkg/kudoctl/packages/verifier/task"
"github.com/kudobuilder/kudo/pkg/kudoctl/packages/verifier/template"
"github.com/kudobuilder/kudo/pkg/kudoctl/verifier"
"github.com/kudobuilder/kudo/pkg/version"
)
var verifiers = []packages.Verifier{
DuplicateVerifier{},
InvalidCharVerifier{";,"},
VersionVerifier{},
task.BuildVerifier{},
task.ReferenceVerifier{},
plan.ReferenceVerifier{},
template.ParametersVerifier{},
template.ReferenceVerifier{},
template.RenderVerifier{},
template.NamespaceVerifier{},
}
// PackageFiles verifies operator package files
func PackageFiles(pf *packages.Files) verifier.Result {
res := verifier.NewResult()
for _, vv := range verifiers {
res.Merge(vv.Verify(pf))
}
return res
}
// DuplicateVerifier provides verification that there are no duplicates disallowing casing (Kudo and kudo are duplicates)
type DuplicateVerifier struct{}
func (DuplicateVerifier) Verify(pf *packages.Files) verifier.Result {
res := verifier.NewResult()
names := map[string]bool{}
for _, param := range pf.Params.Parameters {
name := strings.ToLower(param.Name)
if names[name] {
res.AddParamError(param.Name, "has a duplicate")
}
names[name] = true
}
return res
}
type InvalidCharVerifier struct {
InvalidChars string
}
func (v InvalidCharVerifier) Verify(pf *packages.Files) verifier.Result {
res := verifier.NewResult()
for _, param := range pf.Params.Parameters {
name := strings.ToLower(param.Name)
for _, char := range name {
if strings.Contains(v.InvalidChars, strings.ToLower(string(char))) {
res.AddParamError(param.Name, fmt.Sprintf("contains invalid character %q", char))
}
}
}
return res
}
// VersionVerifier verifies the version in operator.yaml, kubernetesVersion, operatorVersion and kudoVersion
type VersionVerifier struct{}
func (VersionVerifier) Verify(pf *packages.Files) verifier.Result {
res := verifier.NewResult()
if pf.Operator == nil {
res.AddErrors("operator not defined.")
return res
}
verifySemVer(pf.Operator.OperatorVersion, "operatorVersion", &res, true)
verifySemVer(pf.Operator.KubernetesVersion, "kubernetesVersion", &res, true)
verifySemVer(pf.Operator.KUDOVersion, "kudoVersion", &res, false)
return res
}
func verifySemVer(ver string, name string, res *verifier.Result, required bool) {
v := strings.TrimSpace(ver)
if !required && v == "" {
return
}
if required && v == "" {
res.AddErrors(fmt.Sprintf("%q is required and must be semver", name))
return
}
_, err := version.New(ver)
if err != nil {
res.AddErrors(fmt.Sprintf("unable to parse %q: %v", name, err))
}
}