...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package k8s
16
17 import (
18 "fmt"
19
20 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/crdgeneration/crdboilerplate"
21 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/text"
22
23 apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
24 )
25
26 var renameStatusFieldsWithReservedNamesExcludeList = map[string]bool{
27
28
29 "google_storage_default_object_access_control": true,
30 }
31
32 func RenameStatusFieldsWithReservedNames(status *apiextensions.JSONSchemaProps) (*apiextensions.JSONSchemaProps, error) {
33 statusCopy := status.DeepCopy()
34 for field := range ReservedStatusFieldNames() {
35 renamedField := RenameStatusFieldWithReservedName(field)
36
37
38
39
40
41
42
43
44 if _, ok := statusCopy.Properties[renamedField]; ok {
45 return nil, fmt.Errorf("status schema already has a field named "+
46 "'%v' which is the rename meant for fields that collide "+
47 "with the reserved name '%v'", renamedField, field)
48 }
49
50
51 if schema, ok := statusCopy.Properties[field]; ok {
52 statusCopy.Properties[renamedField] = schema
53 delete(statusCopy.Properties, field)
54 }
55 }
56
57 return statusCopy, nil
58 }
59
60 func RenameStatusFieldsWithReservedNamesIfResourceNotExcluded(tfResourceName string, status *apiextensions.JSONSchemaProps) (*apiextensions.JSONSchemaProps, error) {
61 if shouldSkip(tfResourceName) {
62 return status, nil
63 }
64 return RenameStatusFieldsWithReservedNames(status)
65 }
66
67 func ReservedStatusFieldNames() map[string]bool {
68 reservedFieldNames := make(map[string]bool)
69
70
71 crdSchema := crdboilerplate.GetOpenAPIV3SchemaSkeleton()
72 for fieldName := range crdSchema.Properties["status"].Properties {
73 reservedFieldNames[fieldName] = true
74 }
75
76
77 for _, fieldName := range ReservedStatusFieldNamesForFutureUse {
78 reservedFieldNames[fieldName] = true
79 }
80 return reservedFieldNames
81 }
82
83 func RenameStatusFieldWithReservedName(field string) string {
84 return "resource" + text.UppercaseInitial(field)
85 }
86
87 func RenameStatusFieldWithReservedNameIfResourceNotExcluded(tfResourceName, field string) string {
88 if shouldSkip(tfResourceName) {
89 return field
90 }
91 return RenameStatusFieldWithReservedName(field)
92 }
93
94
95 func shouldSkip(tfResourceName string) bool {
96 if _, found := renameStatusFieldsWithReservedNamesExcludeList[tfResourceName]; found {
97 return true
98 }
99 return false
100 }
101
View as plain text