-
Notifications
You must be signed in to change notification settings - Fork 104
/
types.go
175 lines (147 loc) · 5.71 KB
/
types.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
package packages
import (
"fmt"
kudoapi "github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
)
const (
APIVersion = "kudo.dev/v1beta1"
)
// Resolver will try to resolve a given package name to either local tarball, folder, remote url or
// an operator in the remote repository.
type Resolver interface {
Resolve(name string, appVersion string, operatorVersion string) (*PackageScope, error)
}
// PackageScope type provides operator resources together with a "scope-aware" dependency resolver.
// For example, a dependency like `./child-operator` in an local directory needs the host system to be resolved, but when
// found in a tarball, it needs the contents of the tarball because the dependency is not allowed to "escape" into the
// host system.
type PackageScope struct {
Resources *Resources
DependenciesResolver Resolver
}
// Resources is collection of CRDs that are used when installing operator
// during installation, package format is converted to this structure
type Resources struct {
Operator *kudoapi.Operator
OperatorVersion *kudoapi.OperatorVersion
Instance *kudoapi.Instance
}
func (p *Resources) OperatorName() string {
if p == nil || p.Operator == nil {
return ""
}
return p.Operator.Name
}
func (p *Resources) OperatorVersionString() string {
if p == nil || p.OperatorVersion == nil {
return ""
}
return p.OperatorVersion.Spec.Version
}
func (p *Resources) AppVersionString() string {
if p == nil || p.OperatorVersion == nil {
return ""
}
return p.OperatorVersion.Spec.AppVersion
}
// Modified kudoapi.Parameter that allows for defaults provided as YAML.
type Parameter struct {
DisplayName string `json:"displayName,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Required *bool `json:"required,omitempty"`
Default interface{} `json:"default,omitempty"`
Trigger string `json:"trigger,omitempty"`
Type kudoapi.ParameterType `json:"type,omitempty"`
Immutable *bool `json:"immutable,omitempty"`
Enum *[]interface{} `json:"enum,omitempty"`
// The following fields are descriptive only and are not used in the OperatorVersion. They are only used on the
// package level and are not converted to the CRDs, as they are only used during installation of an operator and
// are not necessary server-side.
Group string `json:"group,omitempty"`
Advanced *bool `json:"advanced,omitempty"`
Hint string `json:"hint,omitempty"`
}
func (p Parameter) IsImmutable() bool {
return p.Immutable != nil && *p.Immutable
}
func (p Parameter) IsRequired() bool {
return p.Required != nil && *p.Required
}
func (p Parameter) IsAdvanced() bool {
return p.Advanced != nil && *p.Advanced
}
func (p Parameter) IsEnum() bool {
return p.Enum != nil
}
func (p *Parameter) HasDefault() bool {
return p.Default != nil
}
func (p *Parameter) ValidateDefault() error {
if err := kudoapi.ValidateParameterValueForType(p.Type, p.Default); err != nil {
return fmt.Errorf("parameter \"%s\" has an invalid default value: %v", p.Name, err)
}
if p.IsEnum() {
for _, eValue := range p.EnumValues() {
if p.Default == eValue {
return nil
}
}
return fmt.Errorf("parameter \"%s\" has an invalid default value: value is %q, but only allowed values are %v", p.Name, p.Default, p.EnumValues())
}
return nil
}
func (p *Parameter) EnumValues() []interface{} {
if p.IsEnum() {
return *p.Enum
}
return []interface{}{}
}
type Parameters []Parameter
// Len returns the number of params.
// This is needed to allow sorting of params.
func (p Parameters) Len() int { return len(p) }
// Swap swaps the position of two items in the params slice.
// This is needed to allow sorting of params.
func (p Parameters) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Less returns true if the name of a param a is less than the name of param b.
// This is needed to allow sorting of params.
func (p Parameters) Less(x, y int) bool {
return p[x].Name < p[y].Name
}
type Groups []Group
type Group struct {
Name string `json:"name,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Description string `json:"description,omitempty"`
Priority int `json:"prio,omitempty"`
}
// Templates is a map of file names and stringified files in the template folder of an operator
type Templates map[string]string
// Files represents the raw operator package format the way it is found in the tgz packages
type Files struct {
Templates Templates
Operator *OperatorFile
Params *ParamsFile
}
// ParamsFile is a representation of the package params.yaml
type ParamsFile struct {
APIVersion string `json:"apiVersion,omitempty"`
Groups Groups `json:"groups,omitempty"`
Parameters Parameters `json:"parameters"`
}
// OperatorFile is a representation of the package operator.yaml
type OperatorFile struct {
APIVersion string `json:"apiVersion,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
OperatorVersion string `json:"operatorVersion"`
AppVersion string `json:"appVersion,omitempty"`
KUDOVersion string `json:"kudoVersion,omitempty"`
KubernetesVersion string `json:"kubernetesVersion,omitempty"`
Maintainers []*kudoapi.Maintainer `json:"maintainers,omitempty"`
URL string `json:"url,omitempty"`
Tasks []kudoapi.Task `json:"tasks"`
Plans map[string]kudoapi.Plan `json:"plans"`
NamespaceManifest string `json:"namespaceManifest,omitempty"`
}