-
Notifications
You must be signed in to change notification settings - Fork 246
/
property.go
286 lines (261 loc) · 8.45 KB
/
property.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package property
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/operator-framework/api/pkg/operators/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Property struct {
Type string `json:"type"`
Value json.RawMessage `json:"value"`
}
func (p Property) Validate() error {
if len(p.Type) == 0 {
return errors.New("type must be set")
}
if len(p.Value) == 0 {
return errors.New("value must be set")
}
var raw json.RawMessage
if err := json.Unmarshal(p.Value, &raw); err != nil {
return fmt.Errorf("value is not valid json: %v", err)
}
return nil
}
func (p Property) String() string {
return fmt.Sprintf("type: %q, value: %q", p.Type, p.Value)
}
type Package struct {
PackageName string `json:"packageName"`
Version string `json:"version"`
}
// NOTICE: The Channel properties are for internal use only.
//
// DO NOT use it for any public-facing functionalities.
// This API is in alpha stage and it is subject to change.
type Channel struct {
ChannelName string `json:"channelName"`
//Priority string `json:"priority"`
Priority int `json:"priority"`
}
type PackageRequired struct {
PackageName string `json:"packageName"`
VersionRange string `json:"versionRange"`
}
type GVK struct {
Group string `json:"group"`
Kind string `json:"kind"`
Version string `json:"version"`
}
type GVKRequired struct {
Group string `json:"group"`
Kind string `json:"kind"`
Version string `json:"version"`
}
type BundleObject struct {
Data []byte `json:"data"`
}
type CSVMetadata struct {
Annotations map[string]string `json:"annotations,omitempty"`
APIServiceDefinitions v1alpha1.APIServiceDefinitions `json:"apiServiceDefinitions,omitempty"`
CustomResourceDefinitions v1alpha1.CustomResourceDefinitions `json:"crdDescriptions,omitempty"`
Description string `json:"description,omitempty"`
DisplayName string `json:"displayName,omitempty"`
InstallModes []v1alpha1.InstallMode `json:"installModes,omitempty"`
Keywords []string `json:"keywords,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Links []v1alpha1.AppLink `json:"links,omitempty"`
Maintainers []v1alpha1.Maintainer `json:"maintainers,omitempty"`
Maturity string `json:"maturity,omitempty"`
MinKubeVersion string `json:"minKubeVersion,omitempty"`
NativeAPIs []metav1.GroupVersionKind `json:"nativeAPIs,omitempty"`
Provider v1alpha1.AppLink `json:"provider,omitempty"`
}
type Properties struct {
Packages []Package `hash:"set"`
PackagesRequired []PackageRequired `hash:"set"`
GVKs []GVK `hash:"set"`
GVKsRequired []GVKRequired `hash:"set"`
BundleObjects []BundleObject `hash:"set"`
Channels []Channel `hash:"set"`
CSVMetadatas []CSVMetadata `hash:"set"`
Others []Property `hash:"set"`
}
const (
TypePackage = "olm.package"
TypePackageRequired = "olm.package.required"
TypeGVK = "olm.gvk"
TypeGVKRequired = "olm.gvk.required"
TypeBundleObject = "olm.bundle.object"
TypeCSVMetadata = "olm.csv.metadata"
TypeConstraint = "olm.constraint"
TypeChannel = "olm.channel"
)
func Parse(in []Property) (*Properties, error) {
var out Properties
for i, prop := range in {
switch prop.Type {
case TypePackage:
var p Package
if err := json.Unmarshal(prop.Value, &p); err != nil {
return nil, ParseError{Idx: i, Typ: prop.Type, Err: err}
}
out.Packages = append(out.Packages, p)
case TypePackageRequired:
var p PackageRequired
if err := json.Unmarshal(prop.Value, &p); err != nil {
return nil, ParseError{Idx: i, Typ: prop.Type, Err: err}
}
out.PackagesRequired = append(out.PackagesRequired, p)
case TypeGVK:
var p GVK
if err := json.Unmarshal(prop.Value, &p); err != nil {
return nil, ParseError{Idx: i, Typ: prop.Type, Err: err}
}
out.GVKs = append(out.GVKs, p)
case TypeGVKRequired:
var p GVKRequired
if err := json.Unmarshal(prop.Value, &p); err != nil {
return nil, ParseError{Idx: i, Typ: prop.Type, Err: err}
}
out.GVKsRequired = append(out.GVKsRequired, p)
case TypeBundleObject:
var p BundleObject
if err := json.Unmarshal(prop.Value, &p); err != nil {
return nil, ParseError{Idx: i, Typ: prop.Type, Err: err}
}
out.BundleObjects = append(out.BundleObjects, p)
case TypeCSVMetadata:
var p CSVMetadata
if err := json.Unmarshal(prop.Value, &p); err != nil {
return nil, ParseError{Idx: i, Typ: prop.Type, Err: err}
}
out.CSVMetadatas = append(out.CSVMetadatas, p)
// NOTICE: The Channel properties are for internal use only.
// DO NOT use it for any public-facing functionalities.
// This API is in alpha stage and it is subject to change.
case TypeChannel:
var p Channel
if err := json.Unmarshal(prop.Value, &p); err != nil {
return nil, ParseError{Idx: i, Typ: prop.Type, Err: err}
}
out.Channels = append(out.Channels, p)
default:
var p json.RawMessage
if err := json.Unmarshal(prop.Value, &p); err != nil {
return nil, ParseError{Idx: i, Typ: prop.Type, Err: err}
}
out.Others = append(out.Others, prop)
}
}
return &out, nil
}
func Deduplicate(in []Property) []Property {
type key struct {
typ string
value string
}
props := map[key]Property{}
var out []Property
for _, p := range in {
k := key{p.Type, string(p.Value)}
if _, ok := props[k]; ok {
continue
}
props[k] = p
out = append(out, p)
}
return out
}
func Build(p interface{}) (*Property, error) {
var (
typ string
val interface{}
)
if prop, ok := p.(*Property); ok {
typ = prop.Type
val = prop.Value
} else {
t := reflect.TypeOf(p)
if t.Kind() != reflect.Ptr {
return nil, errors.New("input must be a pointer to a type")
}
typ, ok = scheme[t]
if !ok {
return nil, fmt.Errorf("%s not a known property type registered with the scheme", t)
}
val = p
}
d, err := jsonMarshal(val)
if err != nil {
return nil, err
}
return &Property{
Type: typ,
Value: d,
}, nil
}
func MustBuild(p interface{}) Property {
prop, err := Build(p)
if err != nil {
panic(err)
}
return *prop
}
func jsonMarshal(p interface{}) ([]byte, error) {
buf := &bytes.Buffer{}
dec := json.NewEncoder(buf)
dec.SetEscapeHTML(false)
err := dec.Encode(p)
if err != nil {
return nil, err
}
out := &bytes.Buffer{}
if err := json.Compact(out, buf.Bytes()); err != nil {
return nil, err
}
return out.Bytes(), nil
}
func MustBuildPackage(name, version string) Property {
return MustBuild(&Package{PackageName: name, Version: version})
}
func MustBuildPackageRequired(name, versionRange string) Property {
return MustBuild(&PackageRequired{name, versionRange})
}
func MustBuildGVK(group, version, kind string) Property {
return MustBuild(&GVK{group, kind, version})
}
func MustBuildGVKRequired(group, version, kind string) Property {
return MustBuild(&GVKRequired{group, kind, version})
}
func MustBuildBundleObject(data []byte) Property {
return MustBuild(&BundleObject{Data: data})
}
func MustBuildCSVMetadata(csv v1alpha1.ClusterServiceVersion) Property {
return MustBuild(&CSVMetadata{
Annotations: csv.GetAnnotations(),
APIServiceDefinitions: csv.Spec.APIServiceDefinitions,
CustomResourceDefinitions: csv.Spec.CustomResourceDefinitions,
Description: csv.Spec.Description,
DisplayName: csv.Spec.DisplayName,
InstallModes: csv.Spec.InstallModes,
Keywords: csv.Spec.Keywords,
Labels: csv.GetLabels(),
Links: csv.Spec.Links,
Maintainers: csv.Spec.Maintainers,
Maturity: csv.Spec.Maturity,
MinKubeVersion: csv.Spec.MinKubeVersion,
NativeAPIs: csv.Spec.NativeAPIs,
Provider: csv.Spec.Provider,
})
}
// NOTICE: The Channel properties are for internal use only.
//
// DO NOT use it for any public-facing functionalities.
// This API is in alpha stage and it is subject to change.
func MustBuildChannelPriority(name string, priority int) Property {
return MustBuild(&Channel{ChannelName: name, Priority: priority})
}