-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
192 lines (172 loc) · 5.2 KB
/
utils.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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package crd
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
)
const (
stringType = "string"
intType = "int"
int32Type = "int32"
int64Type = "int64"
boolType = "bool"
byteType = "byte"
float32Type = "float32"
float64Type = "float64"
stringJSONType = "string"
integerJSONType = "integer"
booleanJSONType = "boolean"
floatJSONType = "float"
)
func isSimpleType(typeName string) bool {
return typeName == stringType || typeName == intType ||
typeName == int32Type || typeName == int64Type ||
typeName == boolType || typeName == byteType ||
typeName == float32Type || typeName == float64Type
}
// Converts the typeName simple type to json type
func jsonifyType(typeName string) string {
switch typeName {
case stringType:
return stringJSONType
case boolType:
return booleanJSONType
case intType, int32Type, int64Type:
return integerJSONType
case float32Type, float64Type:
return floatJSONType
case byteType:
return stringJSONType
}
fmt.Println("jsonifyType called with a complex type ", typeName)
panic("jsonifyType called with a complex type")
}
func mergeDefs(lhs v1beta1.JSONSchemaDefinitions, rhs v1beta1.JSONSchemaDefinitions) {
if lhs == nil || rhs == nil {
return
}
for key := range rhs {
_, ok := lhs[key]
if ok {
// change this to logger
fmt.Println("JSONSchemaProps ", key, " already present")
continue
}
lhs[key] = rhs[key]
}
}
func mergeExternalRefs(lhs ExternalReferences, rhs ExternalReferences) {
if lhs == nil || rhs == nil {
return
}
for key := range rhs {
_, ok := lhs[key]
if !ok {
lhs[key] = rhs[key]
} else {
lhs[key] = append(lhs[key], rhs[key]...)
}
}
}
func mergeCRDSpecs(lhs, rhs crdSpecByKind) {
if lhs == nil || rhs == nil {
return
}
for key := range rhs {
_, ok := lhs[key]
if ok {
// TODO: change this to use logger
fmt.Printf("CRD spec for kind %q already present", key)
continue
}
lhs[key] = rhs[key]
}
}
func mergeCRDVersions(lhs, rhs crdSpecByKind) error {
if lhs == nil || rhs == nil {
return nil
}
for gk := range rhs {
_, ok := lhs[gk]
if !ok {
lhs[gk] = rhs[gk]
continue
}
if len(lhs[gk].Group) == 0 {
lhs[gk].Group = rhs[gk].Group
} else if lhs[gk].Group != rhs[gk].Group {
return fmt.Errorf("group names %q and %q from different packages must match", lhs[gk].Group, rhs[gk].Group)
}
if len(lhs[gk].Scope) == 0 {
lhs[gk].Scope = rhs[gk].Scope
} else if lhs[gk].Scope != rhs[gk].Scope {
return fmt.Errorf("group names %q and %q from different packages must match", lhs[gk].Group, rhs[gk].Group)
}
if len(lhs[gk].Names.Kind) == 0 {
lhs[gk].Names.Kind = rhs[gk].Names.Kind
} else if lhs[gk].Names.Kind != rhs[gk].Names.Kind {
return fmt.Errorf("kind names %q and %q from different packages must match", lhs[gk].Names.Kind, rhs[gk].Names.Kind)
}
if len(lhs[gk].Names.Plural) == 0 {
lhs[gk].Names.Plural = rhs[gk].Names.Plural
} else if lhs[gk].Names.Plural != rhs[gk].Names.Plural {
return fmt.Errorf("plural resource names %q and %q from different packages must match", lhs[gk].Names.Plural, rhs[gk].Names.Plural)
}
if len(lhs[gk].Names.Singular) == 0 {
lhs[gk].Names.Singular = rhs[gk].Names.Singular
} else if lhs[gk].Names.Singular != rhs[gk].Names.Singular {
return fmt.Errorf("singular resource names %q and %q from different packages must match", lhs[gk].Names.Singular, rhs[gk].Names.Singular)
}
if len(lhs[gk].Names.ShortNames) == 0 {
lhs[gk].Names.ShortNames = rhs[gk].Names.ShortNames
} else if !reflect.DeepEqual(lhs[gk].Names.ShortNames, rhs[gk].Names.ShortNames) {
return fmt.Errorf("short names %s and %s from different packages must match", lhs[gk].Names.ShortNames, rhs[gk].Names.ShortNames)
}
lhs[gk].Versions = append(lhs[gk].Versions, rhs[gk].Versions...)
}
return nil
}
func debugPrint(obj interface{}) {
b, err3 := json.Marshal(obj)
if err3 != nil {
panic("Error")
}
fmt.Println(string(b))
}
// Gets the schema definition link of a resource
func getDefLink(resourceName string) *string {
ret := defPrefix + resourceName
return &ret
}
func getFullName(resourceName string, prefix string) string {
if prefix == "" {
return resourceName
}
prefix = strings.Replace(prefix, "/", ".", -1)
return prefix + "." + resourceName
}
func getPrefixedDefLink(resourceName string, prefix string) *string {
ret := defPrefix + getFullName(resourceName, prefix)
return &ret
}
// Gets the resource name from definitions url.
// Eg, returns 'TypeName' from '#/definitions/TypeName'
func getNameFromURL(url string) string {
slice := strings.Split(url, "/")
return slice[len(slice)-1]
}