-
Notifications
You must be signed in to change notification settings - Fork 51
/
profile.go
163 lines (138 loc) · 4.81 KB
/
profile.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
package profiles
import (
"fmt"
"regexp"
"strings"
"golang.org/x/mod/semver"
"gopkg.in/yaml.v3"
"github.com/redhat-certification/chart-verifier/internal/chartverifier/checks"
"github.com/redhat-certification/chart-verifier/internal/chartverifier/utils"
"github.com/redhat-certification/chart-verifier/internal/profileconfig"
apiChecks "github.com/redhat-certification/chart-verifier/pkg/chartverifier/checks"
)
type (
Annotation string
VendorType string
VendorVersion string
)
const (
DigestAnnotation Annotation = "Digest"
OCPVersionAnnotation Annotation = "OCPVersion"
TestedOCPVersionAnnotation Annotation = "TestedOpenShiftVersion"
LastCertifiedTimestampAnnotation Annotation = "LastCertifiedTimestamp"
SupportedOCPVersionsAnnotation Annotation = "SupportedOpenShiftVersions"
VendorTypeConfigName string = "profile.vendortype"
VersionConfigName string = "profile.version"
VendorTypeDefault VendorType = "default"
VendorTypeNotSpecified VendorType = "vendorTypeNotSpecified"
)
var profileMap map[VendorType][]*Profile
func init() {
profileMap = make(map[VendorType][]*Profile)
getProfiles()
// add default profile to the map if a default profile was not found.
if _, ok := profileMap[VendorTypeDefault]; !ok {
profileMap[VendorTypeDefault] = profileMap[DefaultProfile]
}
}
type Profile struct {
Apiversion string `json:"apiversion" yaml:"apiversion"`
Kind string `json:"kind" yaml:"kind"`
Name string `json:"name" yaml:"name"`
Vendor VendorType `json:"vendorType" yaml:"vendorType"`
Version string `json:"version" yaml:"version"`
Annotations []Annotation `json:"annotations" yaml:"annotations"`
Checks []*Check `json:"checks" yaml:"checks"`
}
type Check struct {
Name string `json:"name" yaml:"name"`
Type apiChecks.CheckType `json:"type" yaml:"type"`
}
type FilteredRegistry map[apiChecks.CheckName]checks.Check
var profileInUse *Profile
func Get() *Profile {
if profileInUse == nil {
return getDefaultProfile("No profile set for get")
}
return profileInUse
}
func New(values map[string]interface{}) *Profile {
profileVendorType := VendorTypeDefault
var profileVersion string
if values != nil {
if vendorType, ok := values[VendorTypeConfigName]; ok {
profileVendorType = VendorType(strings.ToLower(fmt.Sprintf("%v", vendorType)))
}
if version, ok := values[VersionConfigName]; ok {
versionString := (fmt.Sprintf("%v", version))
if semver.IsValid(versionString) {
profileVersion = versionString
}
}
}
profileInUse = getDefaultProfile(fmt.Sprintf("profile %s not found", profileVendorType))
if vendorProfiles, ok := profileMap[profileVendorType]; ok {
if len(vendorProfiles) > 0 {
profileInUse = vendorProfiles[0]
if len(vendorProfiles) > 1 {
for _, vendorProfile := range vendorProfiles {
if len(profileVersion) > 0 {
if semver.Compare(semver.MajorMinor(vendorProfile.Version), semver.MajorMinor(profileVersion)) == 0 {
profileInUse = vendorProfile
break
}
}
if semver.Compare(semver.MajorMinor(vendorProfile.Version), semver.MajorMinor(profileInUse.Version)) > 0 {
profileInUse = vendorProfile
}
}
}
}
}
utils.LogInfo(fmt.Sprintf("Profile in use: %s %s", profileInUse.Vendor, profileInUse.Version))
return profileInUse
}
// Get all profiles in the profiles directory, and any subdirectories, and add each to the profile map
func getProfiles() {
profileFiles, err := profileconfig.GetProfiles()
if err != nil {
return
}
for _, profileFile := range profileFiles {
if strings.HasSuffix(profileFile.Name, ".yaml") {
profileRead, err := readProfile(profileFile.Data)
if err == nil {
// If version is not valid set to a default version
if !semver.IsValid(profileRead.Version) {
profileRead.Version = DefaultProfileVersion
}
if len(profileRead.Vendor) == 0 {
profileRead.Vendor = VendorTypeNotSpecified
}
profileMap[profileRead.Vendor] = append(profileMap[profileRead.Vendor], profileRead)
profileRead.Name = strings.Split(profileFile.Name, ".yaml")[0]
}
}
}
}
func (profile *Profile) FilterChecks(registry checks.DefaultRegistry) FilteredRegistry {
filteredChecks := make(map[apiChecks.CheckName]checks.Check)
for _, check := range profile.Checks {
splitter := regexp.MustCompile(`/`)
splitCheck := splitter.Split(check.Name, -1)
checkIndex := checks.CheckID{Name: apiChecks.CheckName(splitCheck[1]), Version: splitCheck[0]}
if newCheck, ok := registry[checkIndex]; ok {
newCheck.Type = check.Type
filteredChecks[checkIndex.Name] = newCheck
}
}
return filteredChecks
}
func readProfile(profileBytes []byte) (*Profile, error) {
profile := &Profile{}
err := yaml.Unmarshal(profileBytes, profile)
if err != nil {
return nil, err
}
return profile, nil
}