-
Notifications
You must be signed in to change notification settings - Fork 205
/
references.go
389 lines (357 loc) · 14 KB
/
references.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright 2022 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
//
// http://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 dcl
import (
"fmt"
"strings"
corekccv1alpha1 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/core/v1alpha1"
dclmetadata "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/metadata"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/schema/dclschemaloader"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/text"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/pathslice"
"github.com/nasa9084/go-openapi"
"k8s.io/apimachinery/pkg/runtime/schema"
)
var (
// The following variables represent the fields that are used to reference
// parent resources. DCL resources can support either one multi-type parent
// reference field or one of the single-type parent reference fields.
multiTypeParentReferenceField = "parent"
singleTypeParentReferenceFieldProject = "project"
singleTypeParentReferenceFieldFolder = "folder"
singleTypeParentReferenceFieldOrganization = "organization"
singleTypeParentReferenceFields = map[string]bool{
singleTypeParentReferenceFieldProject: true,
singleTypeParentReferenceFieldFolder: true,
singleTypeParentReferenceFieldOrganization: true,
}
// The following variables represent the different hierarchical reference
// configurations that can be had by DCL-based KCC resources.
hierarchicalReferenceProject = corekccv1alpha1.HierarchicalReference{
Key: "projectRef",
Type: corekccv1alpha1.HierarchicalReferenceTypeProject,
}
hierarchicalReferenceFolder = corekccv1alpha1.HierarchicalReference{
Key: "folderRef",
Type: corekccv1alpha1.HierarchicalReferenceTypeFolder,
}
hierarchicalReferenceOrganization = corekccv1alpha1.HierarchicalReference{
Key: "organizationRef",
Type: corekccv1alpha1.HierarchicalReferenceTypeOrganization,
}
hierarchicalReferenceBillingAccount = corekccv1alpha1.HierarchicalReference{
Key: "billingAccountRef",
Type: corekccv1alpha1.HierarchicalReferenceTypeBillingAccount,
}
)
/*
*
dclReferenceExtensionElem represents an element in a 'x-dcl-references' list.
```
x-dcl-references:
- field: name
parent: true
resource: SomeService/SomeResourceType
```
*
*/
type dclReferenceExtensionElem struct {
// Resource indicates the referenced resource type in the format: Service/ResourceKind, e.g. Compute/BackendBucket.
Resource string `json:"resource"`
// Field is the referenced resource's field from which to extract the desired value, e.g. "name", "selfLink"
Field string `json:"field"`
// Parent specifies whether the referenced resource is a parent. If the parent
// is successfully deleted, it's assumed that this resource may be deleted without any call to the
// underlying API.
Parent bool `json:"parent,omitempty"`
}
func GetReferenceTypeConfigs(schema *openapi.Schema, smLoader dclmetadata.ServiceMetadataLoader) ([]corekccv1alpha1.TypeConfig, error) {
refExtensionElems, err := getDCLReferenceExtensionElems(schema)
if err != nil {
return nil, err
}
res := make([]corekccv1alpha1.TypeConfig, 0)
for _, e := range refExtensionElems {
tc, err := toTypeConfig(e, smLoader)
if err != nil {
return nil, err
}
res = append(res, *tc)
}
return res, nil
}
// ToTypeConfig converts a 'x-dcl-references' element to a TypeConfig.
func ToTypeConfig(rawElem map[interface{}]interface{}, smLoader dclmetadata.ServiceMetadataLoader) (*corekccv1alpha1.TypeConfig, error) {
e, err := toDCLReferenceExtensionElem(rawElem)
if err != nil {
return nil, err
}
return toTypeConfig(e, smLoader)
}
func toTypeConfig(e dclReferenceExtensionElem, smLoader dclmetadata.ServiceMetadataLoader) (*corekccv1alpha1.TypeConfig, error) {
if err := validateDCLReferenceExtensionElem(e); err != nil {
return nil, err
}
tc := &corekccv1alpha1.TypeConfig{}
tc.TargetField = e.Field
tc.Parent = e.Parent
refGVK, err := getReferenceGVK(e.Resource, smLoader)
if err != nil {
return nil, fmt.Errorf("error resolving the GVK for referenced resource: %w", err)
}
tc.GVK = refGVK
tc.Key = text.LowercaseInitial(k8s.KindWithoutServicePrefix(tc.GVK)) + "Ref"
return tc, nil
}
func validateDCLReferenceExtensionElem(e dclReferenceExtensionElem) error {
if e.Resource == "" {
return fmt.Errorf("required 'resource' attribute is not specified in 'x-dcl-references' extension")
}
if e.Field == "" {
return fmt.Errorf("required 'field' attribute is not specified in 'x-dcl-references' extension")
}
return nil
}
func convertToStringInterfaceMap(in map[interface{}]interface{}) (map[string]interface{}, error) {
out := make(map[string]interface{})
for k, v := range in {
s, ok := k.(string)
if !ok {
return nil, fmt.Errorf("wrong type for the key: %T, expect to have string", k)
}
out[s] = v
}
return out, nil
}
func getReferenceGVK(resource string, smLoader dclmetadata.ServiceMetadataLoader) (schema.GroupVersionKind, error) {
// The 'resource' attribute of a 'x-dcl-references' element has the format
// "Service/DCLType" (e.g. Compute/BackendBucket)
components := strings.Split(resource, "/")
if len(components) != 2 {
return schema.GroupVersionKind{}, fmt.Errorf("invalid format for 'resource' attribute in 'x-dcl-references' extension: %v", resource)
}
service := components[0]
dclType := components[1]
sm, found := smLoader.GetServiceMetadata(service)
if !found {
return schema.GroupVersionKind{}, fmt.Errorf("ServiceMetadata for service %v is not found", service)
}
r, found := sm.GetResourceWithType(dclType)
if !found {
return schema.GroupVersionKind{}, fmt.Errorf("resource with DCL type %v not supported in service %v", dclType, service)
}
return dclmetadata.GVKForResource(sm, r), nil
}
func getDCLReferenceExtensionElems(schema *openapi.Schema) ([]dclReferenceExtensionElem, error) {
extension, ok := schema.Extension["x-dcl-references"]
if !ok {
return nil, fmt.Errorf("no 'x-dcl-references' extension found")
}
extensionAsList, ok := extension.([]interface{})
if !ok {
return nil, fmt.Errorf("wrong type for 'x-dcl-references' extension: %T, expect to have []interface{}", extension)
}
res := make([]dclReferenceExtensionElem, 0)
for _, elem := range extensionAsList {
elemAsMap, ok := elem.(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("wrong type for element in 'x-dcl-references' extension %T, expect to have map[interface{}]interface{}", elem)
}
e, err := toDCLReferenceExtensionElem(elemAsMap)
if err != nil {
return nil, err
}
res = append(res, e)
}
return res, nil
}
func toDCLReferenceExtensionElem(rawElem map[interface{}]interface{}) (dclReferenceExtensionElem, error) {
m, err := convertToStringInterfaceMap(rawElem)
if err != nil {
return dclReferenceExtensionElem{}, fmt.Errorf("error converting 'x-dcl-references' element to map[string]interface{}: %v", err)
}
e := dclReferenceExtensionElem{}
if err := util.Marshal(m, &e); err != nil {
return dclReferenceExtensionElem{}, fmt.Errorf("error marshalling 'x-dcl-references' element to struct: %v", err)
}
return e, nil
}
func GetHierarchicalReferencesForGVK(gvk schema.GroupVersionKind, smLoader dclmetadata.ServiceMetadataLoader, schemaLoader dclschemaloader.DCLSchemaLoader) ([]corekccv1alpha1.HierarchicalReference, error) {
r, found := smLoader.GetResourceWithGVK(gvk)
if !found {
return nil, fmt.Errorf("ServiceMetadata for resource with GroupVersionKind %v not found", gvk)
}
// TODO(b/186159460): Delete this if-block once all resources support
// hierarchical references.
if !r.SupportsHierarchicalReferences {
return nil, nil
}
stv, err := dclmetadata.ToServiceTypeVersion(gvk, smLoader)
if err != nil {
return nil, fmt.Errorf("error getting DCL ServiceTypeVersion for GroupVersionKind %v: %w", gvk, err)
}
dclSchema, err := schemaLoader.GetDCLSchema(stv)
if err != nil {
return nil, fmt.Errorf("error getting the DCL Schema for GroupVersionKind %v: %w", gvk, err)
}
hierarchicalRefs, err := GetHierarchicalReferenceConfigFromDCLSchema(dclSchema, smLoader)
if err != nil {
return nil, fmt.Errorf("error resolving the hierarchical reference config from DCL schema for GroupVersionKind %v: %w", gvk, err)
}
return hierarchicalRefs, nil
}
func GetHierarchicalReferenceConfigFromDCLSchema(schema *openapi.Schema, smLoader dclmetadata.ServiceMetadataLoader) ([]corekccv1alpha1.HierarchicalReference, error) {
// Resource supports multiple parent types
if SupportsMultipleParentTypes(schema) {
res, err := GetHierarchicalReferenceConfigForMultiParentResource(schema, smLoader)
if err != nil {
return nil, fmt.Errorf("error getting hierarchical reference config for resource that supports multiple parent types: %w", err)
}
return res, nil
}
// Resource supports one parent type or none at all
field, err := getSingleTypeParentReferenceField(schema)
if err != nil {
return nil, fmt.Errorf("error getting single-type parent reference field for resource: %w", err)
}
if field == "" {
// Resource doesn't support any parent reference fields
return nil, nil
}
switch field {
case singleTypeParentReferenceFieldProject:
return []corekccv1alpha1.HierarchicalReference{hierarchicalReferenceProject}, nil
case singleTypeParentReferenceFieldFolder:
return []corekccv1alpha1.HierarchicalReference{hierarchicalReferenceFolder}, nil
case singleTypeParentReferenceFieldOrganization:
return []corekccv1alpha1.HierarchicalReference{hierarchicalReferenceOrganization}, nil
default:
panic(fmt.Errorf("unrecognized single-type parent reference field: %v", field))
}
}
func GetHierarchicalReferenceConfigForMultiParentResource(schema *openapi.Schema, smLoader dclmetadata.ServiceMetadataLoader) ([]corekccv1alpha1.HierarchicalReference, error) {
if !SupportsMultipleParentTypes(schema) {
return nil, fmt.Errorf("resource does not support multiple parent types")
}
parentFieldSchema := schema.Properties["parent"]
tcs, err := GetReferenceTypeConfigs(parentFieldSchema, smLoader)
if err != nil {
return nil, fmt.Errorf("error getting reference type configs for DCL field 'parent': %w", err)
}
res := make([]corekccv1alpha1.HierarchicalReference, 0)
for _, tc := range tcs {
switch tc.GVK.Kind {
case "Project":
res = append(res, hierarchicalReferenceProject)
case "Folder":
res = append(res, hierarchicalReferenceFolder)
case "Organization":
res = append(res, hierarchicalReferenceOrganization)
case "BillingAccount":
res = append(res, hierarchicalReferenceBillingAccount)
default:
panic(fmt.Errorf("'parent' field references an unsupported resource kind: %v", tc.GVK.Kind))
}
}
return res, nil
}
func getSingleTypeParentReferenceField(schema *openapi.Schema) (string, error) {
if SupportsMultipleParentTypes(schema) {
return "", fmt.Errorf("resource supports multiple parent types, not one")
}
var res string
for f := range singleTypeParentReferenceFields {
_, ok := schema.Properties[f]
if !ok {
continue
}
if res != "" {
return "", fmt.Errorf("resource unexpectedly has more than one single-type parent reference field")
}
res = f
}
return res, nil
}
func ParentReferenceFields() []string {
return []string{
singleTypeParentReferenceFieldProject,
singleTypeParentReferenceFieldFolder,
singleTypeParentReferenceFieldOrganization,
multiTypeParentReferenceField,
}
}
func IsParentReferenceField(path []string) bool {
return IsSingleTypeParentReferenceField(path) ||
IsMultiTypeParentReferenceField(path)
}
func IsSingleTypeParentReferenceField(path []string) bool {
if len(path) > 1 {
return false
}
field := pathslice.Base(path)
_, ok := singleTypeParentReferenceFields[field]
return ok
}
func IsMultiTypeParentReferenceField(path []string) bool {
if len(path) > 1 {
return false
}
field := pathslice.Base(path)
return field == multiTypeParentReferenceField
}
func SupportsMultipleParentTypes(schema *openapi.Schema) bool {
_, ok := schema.Properties[multiTypeParentReferenceField]
return ok
}
// GetHierarchicalRefFromConfigForMultiParentResource gets the value and
// TypeConfig of the hierarchical reference in the KRM config, assuming that
// the config is for a multi-parent resource (i.e. supports more than one
// hierarchical reference). Returns nil if no hierarchical reference is found.
// Returns an error if multiple are found.
func GetHierarchicalRefFromConfigForMultiParentResource(config map[string]interface{}, schema *openapi.Schema,
smLoader dclmetadata.ServiceMetadataLoader) (interface{}, *corekccv1alpha1.TypeConfig, error) {
tcs, err := GetReferenceTypeConfigs(schema, smLoader)
if err != nil {
return nil, nil, fmt.Errorf("error getting reference type configs: %w", err)
}
var rawVal interface{}
var typeConfig corekccv1alpha1.TypeConfig
for _, tc := range tcs {
if v, ok := config[tc.Key]; ok && v != nil {
if rawVal != nil {
return nil, nil, fmt.Errorf("multiple hierarchical references found in config: %v and %v", typeConfig.Key, tc.Key)
}
rawVal = v
typeConfig = tc
}
}
if rawVal == nil {
return nil, nil, nil
}
return rawVal, &typeConfig, nil
}
// ParentPrefixForKind gets the parent prefix for the given kind (e.g.
// "Project" => "projects/"). This is used for setting/parsing values of
// multi-type parent reference fields in DCL which require a parent
// prefix to denote the parent type.
func ParentPrefixForKind(kind string) string {
switch kind {
case "Project", "Folder", "Organization", "BillingAccount":
return fmt.Sprintf("%vs/", text.LowercaseInitial(kind))
default:
panic(fmt.Errorf("tried to get parent prefix for kind %v which is not recognized as a hierarchical resource", kind))
}
}