/
immutable_fields_validator.go
553 lines (502 loc) · 21.2 KB
/
immutable_fields_validator.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// 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 webhook
import (
"container/list"
"context"
"fmt"
"net/http"
"reflect"
"regexp"
"strings"
corekccv1alpha1 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/core/v1alpha1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl"
dclextension "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/extension"
dclcontainer "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/extension/container"
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/krmtotf"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/servicemapping/servicemappingloader"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/text"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/pathslice"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/typeutil"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/provider"
"github.com/nasa9084/go-openapi"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
var (
scheme = runtime.NewScheme()
codecs = serializer.NewCodecFactory(scheme)
ErrTFSchemaNotFound = fmt.Errorf("schema does not exist")
)
type immutableFieldsValidatorHandler struct {
smLoader *servicemappingloader.ServiceMappingLoader
tfResourceMap map[string]*schema.Resource
dclSchemaLoader dclschemaloader.DCLSchemaLoader
serviceMetadataLoader dclmetadata.ServiceMetadataLoader
}
var (
allowedResponse = admission.ValidationResponse(true, "admission controller passed")
)
func NewImmutableFieldsValidatorHandler(smLoader *servicemappingloader.ServiceMappingLoader, dclSchemaLoader dclschemaloader.DCLSchemaLoader, serviceMetadataLoader dclmetadata.ServiceMetadataLoader) HandlerFunc {
return func(mgr manager.Manager) admission.Handler {
return &immutableFieldsValidatorHandler{
smLoader: smLoader,
tfResourceMap: provider.ResourceMap(),
dclSchemaLoader: dclSchemaLoader,
serviceMetadataLoader: serviceMetadataLoader,
}
}
}
func (a *immutableFieldsValidatorHandler) Handle(_ context.Context, req admission.Request) admission.Response {
if regexp.MustCompile(ControllerManagerServiceAccountRegex).MatchString(req.AdmissionRequest.UserInfo.Username) {
return admission.ValidationResponse(true, "ignore non-user requests")
}
// decode the existing object and the updated object
deserializer := codecs.UniversalDeserializer()
obj := &unstructured.Unstructured{}
if _, _, err := deserializer.Decode(req.AdmissionRequest.Object.Raw, nil, obj); err != nil {
klog.Error(err)
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("error decoding object: %w", err))
}
oldObj := &unstructured.Unstructured{}
if _, _, err := deserializer.Decode(req.AdmissionRequest.OldObject.Raw, nil, oldObj); err != nil {
klog.Error(err)
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("error decoding old object: %w", err))
}
spec, ok := obj.Object["spec"].(map[string]interface{})
if obj.Object["spec"] != nil && !ok {
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("the type of spec field is not map[string]interface{}"))
}
oldSpec, ok := oldObj.Object["spec"].(map[string]interface{})
if oldObj.Object["spec"] != nil && !ok {
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("the type of spec field is not map[string]interface{}"))
}
if isIAMResource(oldObj) {
return validateImmutableFieldsForIAMResource(oldObj, oldSpec, spec)
}
if err := validateImmutableStateIntoSpecAnnotation(obj, oldObj); err != nil {
return admission.Errored(http.StatusForbidden, err)
}
if dclmetadata.IsDCLBasedResourceKind(obj.GroupVersionKind(), a.serviceMetadataLoader) {
return validateImmutableFieldsForDCLBasedResource(obj, oldObj, spec, oldSpec, a.dclSchemaLoader, a.serviceMetadataLoader)
}
return validateImmutableFieldsForTFBasedResource(obj, oldObj, spec, oldSpec, a.smLoader, a.tfResourceMap)
}
func validateImmutableStateIntoSpecAnnotation(obj, oldObj *unstructured.Unstructured) error {
val, found := k8s.GetAnnotation(k8s.StateIntoSpecAnnotation, obj)
prevVal, prevFound := k8s.GetAnnotation(k8s.StateIntoSpecAnnotation, oldObj)
if found != prevFound || val != prevVal {
return fmt.Errorf("annotation %v is immutable", k8s.StateIntoSpecAnnotation)
}
return nil
}
func validateImmutableFieldsForDCLBasedResource(obj, oldObj *unstructured.Unstructured, spec, oldSpec map[string]interface{}, dclSchemaLoader dclschemaloader.DCLSchemaLoader, serviceMetadataLoader dclmetadata.ServiceMetadataLoader) admission.Response {
gvk := obj.GroupVersionKind()
schema, err := dclschemaloader.GetDCLSchemaForGVK(gvk, serviceMetadataLoader, dclSchemaLoader)
if err != nil {
return admission.Errored(http.StatusInternalServerError,
fmt.Errorf("error getting the DCL Schema for GroupVersionKind %v: %w", gvk, err))
}
containers, err := dclcontainer.GetContainersForGVK(gvk, serviceMetadataLoader, dclSchemaLoader)
if err != nil {
return admission.Errored(http.StatusInternalServerError,
fmt.Errorf("error getting containers supported by GroupVersionKind %v: %w", gvk, err))
}
hierarchicalRefs, err := dcl.GetHierarchicalReferencesForGVK(gvk, serviceMetadataLoader, dclSchemaLoader)
if err != nil {
return admission.Errored(http.StatusInternalServerError,
fmt.Errorf("error getting hierarchical references supported by GroupVersionKind %v: %w", gvk, err))
}
if err := validateContainerAnnotationsForResource(gvk.Kind, obj.GetAnnotations(), oldObj.GetAnnotations(), containers, hierarchicalRefs); err != nil {
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("error validating container annotations: %w", err))
}
if isResourceIDModified(spec, oldSpec) {
return admission.Errored(http.StatusForbidden,
k8s.NewImmutableFieldsMutationError([]string{k8s.ResourceIDFieldPath}))
}
res, err := getChangesOnImmutableFields(spec, oldSpec, []string{"spec"}, []string{}, schema, hierarchicalRefs)
if err != nil {
return admission.Errored(http.StatusInternalServerError,
fmt.Errorf("unexpected error: %w", err))
}
if len(res) != 0 {
return admission.Errored(http.StatusForbidden,
k8s.NewImmutableFieldsMutationError(res))
}
return allowedResponse
}
func getChangesOnImmutableFields(spec, oldSpec map[string]interface{}, krmPath, dclPath []string, schema *openapi.Schema, hierarchicalRefs []corekccv1alpha1.HierarchicalReference) ([]string, error) {
if schema.Type != "object" {
return nil, fmt.Errorf("expect the schame type to be 'object', but got %v", schema.Type)
}
ret := make([]string, 0)
for f, s := range schema.Properties {
if s.ReadOnly {
continue
}
isImmutable, err := dclextension.IsImmutableField(s)
if err != nil {
return nil, fmt.Errorf("error determining if field %v is immutable", f)
}
if dclextension.IsReferenceField(s) {
if !isImmutable {
continue
}
// If parent reference field is immutable and resource supports
// multiple parent types, changes to either the hierarchical
// reference key or value should be rejected.
if dcl.IsMultiTypeParentReferenceField(append(dclPath, f)) {
for _, h := range hierarchicalRefs {
if !reflect.DeepEqual(oldSpec[h.Key], spec[h.Key]) {
krmPathToField := pathslice.ToString(append(krmPath, h.Key))
ret = append(ret, krmPathToField)
}
}
continue
}
refField, err := dclextension.GetReferenceFieldName(append(dclPath, f), s)
if err != nil {
return nil, err
}
if !reflect.DeepEqual(oldSpec[refField], spec[refField]) {
krmPathToField := pathslice.ToString(append(krmPath, refField))
ret = append(ret, krmPathToField)
}
continue
}
krmPathToField := pathslice.ToString(append(krmPath, f))
oldVal := oldSpec[f]
newVal := spec[f]
if oldVal == nil && newVal == nil {
continue
}
if isImmutable && (oldVal == nil || newVal == nil) {
ret = append(ret, krmPathToField)
continue
}
switch s.Type {
case "object":
var v1 map[string]interface{}
var v2 map[string]interface{}
if oldVal != nil {
v1 = oldVal.(map[string]interface{})
}
if newVal != nil {
v2 = newVal.(map[string]interface{})
}
// Field is a map of key-value pairs
if s.AdditionalProperties != nil {
// If map field is immutable, reject any mutations.
if isImmutable {
if !reflect.DeepEqual(v1, v2) {
ret = append(ret, krmPathToField)
}
continue
}
if typeutil.IsPrimitiveType(s.AdditionalProperties.Type) {
continue
}
// If map field is mutable, but its key-value pairs have
// non-primitive values (e.g. objects, arrays of objects), the
// values themselves may have immutable fields. For now, let
// such cases pass through the webhook, and let DCL or the GCP
// API handle them instead (see b/216381382).
continue
}
// Field is an object
nestedFields, err := getChangesOnImmutableFields(v1, v2, append(krmPath, f), append(dclPath, f), s, hierarchicalRefs)
if err != nil {
return nil, err
}
ret = append(ret, nestedFields...)
case "array":
if typeutil.IsPrimitiveType(s.Items.Type) {
if isImmutable && !reflect.DeepEqual(oldVal, newVal) {
ret = append(ret, krmPathToField)
}
continue
}
// Kubernetes considers all lists of objects to be atomic, and so all subsequent
// applies will currently wipe out defaulted immutable fields. Temporarily delegate validation to DCL.
case "string", "boolean", "number", "integer":
if isImmutable && !reflect.DeepEqual(oldSpec[f], spec[f]) {
ret = append(ret, krmPathToField)
}
default:
return nil, fmt.Errorf("unknown schema type %v", s.Type)
}
}
return ret, nil
}
func getQualifiedFieldName(prefix string, fieldName string) string {
qualifiedName := fieldName
if prefix != "" {
qualifiedName = prefix + "." + fieldName
}
return qualifiedName
}
func validateImmutableFieldsForTFBasedResource(obj, oldObj *unstructured.Unstructured, spec, oldSpec map[string]interface{}, smLoader *servicemappingloader.ServiceMappingLoader, tfResourceMap map[string]*schema.Resource) admission.Response {
rc, err := smLoader.GetResourceConfig(obj)
if err != nil {
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("couldn't get ResourceConfig for kind %v: %w", obj.GetKind(), err))
}
if err := validateContainerAnnotationsForResource(obj.GetKind(), obj.GetAnnotations(), oldObj.GetAnnotations(), rc.Containers, rc.HierarchicalReferences); err != nil {
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("error validating container annotations: %w", err))
}
r, ok := tfResourceMap[rc.Name]
if !ok {
return admission.Errored(http.StatusInternalServerError,
fmt.Errorf("unknown resource %v", rc.Name))
}
if findChangesOnImmutableResourceIDField(spec, oldSpec, rc) {
return admission.Errored(http.StatusForbidden,
k8s.NewImmutableFieldsMutationError([]string{k8s.ResourceIDFieldPath}))
}
if findChangesOnImmutableLocationField(spec, oldSpec, rc) {
return admission.Errored(http.StatusForbidden,
k8s.NewImmutableFieldsMutationError([]string{"spec.location"}))
}
fields := list.New()
compareAndFindChangesOnImmutableFields(spec, oldSpec, r.Schema, "", rc, getIgnoredFields(rc), fields)
if fields.Len() != 0 {
res := make([]string, 0)
for e := fields.Front(); e != nil; e = e.Next() {
res = append(res, constructCamelCasePath(e.Value.(string)))
}
return admission.Errored(http.StatusBadRequest,
k8s.NewImmutableFieldsMutationError(res))
}
return allowedResponse
}
func validateContainerAnnotationsForResource(kind string, annotations, oldAnnotations map[string]string, containers []corekccv1alpha1.Container, hierarchicalRefs []corekccv1alpha1.HierarchicalReference) error {
// TODO(b/193177782): Delete this if-block once all resources support
// hierarchical references.
if len(hierarchicalRefs) == 0 {
return validateContainerAnnotations(kind, annotations, oldAnnotations, containers)
}
return validateDeprecatedContainerAnnotations(annotations, oldAnnotations, containers, hierarchicalRefs)
}
func validateContainerAnnotations(kind string, annotations, oldAnnotations map[string]string, containers []corekccv1alpha1.Container) error {
for _, c := range containers {
a := k8s.GetAnnotationForContainerType(c.Type)
// No changes to the container annotation.
if oldAnnotations[a] == annotations[a] {
continue
}
// Reject changes to container annotations except for Projects and
// Folders which rely on container annotation updates to allow for
// migrations across different parent Folders and Organizations.
if kind != "Project" && kind != "Folder" {
return fmt.Errorf("cannot make changes to container annotation %v", a)
}
// Reject changes from one container annotation type to another.
for _, otherC := range containers {
if c == otherC {
continue
}
otherA := k8s.GetAnnotationForContainerType(otherC.Type)
_, ok := oldAnnotations[a]
_, otherOk := annotations[otherA]
if ok && otherOk {
return fmt.Errorf("cannot change from container annotation %v to container annotation %v", a, otherA)
}
}
}
return nil
}
func validateDeprecatedContainerAnnotations(annotations, oldAnnotations map[string]string, containers []corekccv1alpha1.Container, hierarchicalRefs []corekccv1alpha1.HierarchicalReference) error {
for _, c := range containers {
a := k8s.GetAnnotationForContainerType(c.Type)
// No changes to the container annotation.
if oldAnnotations[a] == annotations[a] {
continue
}
// Container annotation was removed. This is a change that we allow to
// give users the ability to "clean" their configurations now that
// container annotations have been deprecated for this resource.
if annotations[a] == "" {
continue
}
// Container annotation was either added or changed.
possibleFields := k8s.HierarchicalReferencesToFields(hierarchicalRefs)
return fmt.Errorf("cannot add/change container annotation %v as it is no longer supported by the resource; set one of [%v] instead", a, strings.Join(possibleFields, ", "))
}
return nil
}
func validateImmutableFieldsForIAMResource(oldObj *unstructured.Unstructured, oldSpec, newSpec map[string]interface{}) admission.Response {
if isIAMPolicy(oldObj) {
return handleIAMPolicy(oldSpec, newSpec)
}
if isIAMPartialPolicy(oldObj) {
return handleIAMPartialPolicy(oldSpec, newSpec)
}
if isIAMPolicyMember(oldObj) {
return handleIAMPolicyMember(oldSpec, newSpec)
}
if isIAMAuditConfig(oldObj) {
return handleIAMAuditConfig(oldSpec, newSpec)
}
return admission.ValidationResponse(false, fmt.Sprintf("unknown IAM resource type: %v", oldObj.GroupVersionKind()))
}
func handleIAMPolicy(oldSpec, newSpec map[string]interface{}) admission.Response {
if isIAMResourceReferenceModified(oldSpec, newSpec) {
msg := fmt.Sprintf("the IAMPolicy's spec.resourceRef is immutable")
return admission.ValidationResponse(false, msg)
}
return allowedResponse
}
func handleIAMPartialPolicy(oldSpec, newSpec map[string]interface{}) admission.Response {
if isIAMResourceReferenceModified(oldSpec, newSpec) {
msg := fmt.Sprintf("the IAMPartialPolicy's spec.resourceRef is immutable")
return admission.ValidationResponse(false, msg)
}
return allowedResponse
}
func handleIAMPolicyMember(oldSpec, newSpec map[string]interface{}) admission.Response {
if isIAMSpecModified(oldSpec, newSpec) {
msg := fmt.Sprintf("the IAMPolicyMember's spec is immutable")
return admission.ValidationResponse(false, msg)
}
return allowedResponse
}
func handleIAMAuditConfig(oldSpec, newSpec map[string]interface{}) admission.Response {
if isIAMResourceReferenceModified(oldSpec, newSpec) {
msg := fmt.Sprintf("the IAMAuditConfig's spec.resourceRef is immutable")
return admission.ValidationResponse(false, msg)
}
if isIAMAuditConfigServiceModified(oldSpec, newSpec) {
msg := fmt.Sprintf("the IAMAuditConfig's spec.service is immutable")
return admission.ValidationResponse(false, msg)
}
return allowedResponse
}
func findChangesOnImmutableResourceIDField(spec, oldSpec map[string]interface{}, rc *corekccv1alpha1.ResourceConfig) bool {
if rc.ResourceID.TargetField == "" {
return false
}
return isResourceIDModified(spec, oldSpec)
}
func isResourceIDModified(spec, oldSpec map[string]interface{}) bool {
return !reflect.DeepEqual(spec[k8s.ResourceIDFieldName], oldSpec[k8s.ResourceIDFieldName])
}
func findChangesOnImmutableLocationField(obj map[string]interface{}, oldObj map[string]interface{}, rc *corekccv1alpha1.ResourceConfig) bool {
if rc.Locationality == "" {
return false
}
// Location is immutable by default as it's part of the URL in underlying api.
return !reflect.DeepEqual(obj["location"], oldObj["location"])
}
// TODO: get rid of list.List by changing the function to return a []string recursively
func compareAndFindChangesOnImmutableFields(obj map[string]interface{}, oldObj map[string]interface{}, schemaMap map[string]*schema.Schema, prefix string, resourceConfig *corekccv1alpha1.ResourceConfig, ignoredFields map[string]bool, fields *list.List) {
for k, s := range schemaMap {
qualifiedName := getQualifiedFieldName(prefix, k)
if ignoredFields[qualifiedName] {
continue
}
if ok, refConfig := krmtotf.IsReferenceField(qualifiedName, resourceConfig); ok {
if !s.ForceNew {
continue
}
modified, refKey := isReferenceValRawModified(obj, oldObj, refConfig)
if modified {
refKey = getQualifiedFieldName(prefix, refKey)
fields.PushBack(refKey)
}
continue
}
camelCaseKey := text.SnakeCaseToLowerCamelCase(k)
v1 := obj[camelCaseKey]
v2 := oldObj[camelCaseKey]
if v1 == nil && v2 == nil {
continue
}
if (v1 == nil || v2 == nil) && s.ForceNew {
fields.PushBack(qualifiedName)
continue
}
switch s.Type {
// TODO: terraform schema doc says that TypeMap only support Elem to be a *Schema with a Type that is one of the primitives
// Is there any edge cases to handle?
case schema.TypeBool, schema.TypeFloat, schema.TypeString, schema.TypeInt, schema.TypeMap:
if s.ForceNew && !reflect.DeepEqual(v1, v2) {
fields.PushBack(qualifiedName)
}
case schema.TypeList, schema.TypeSet:
switch s.Elem.(type) {
case *schema.Schema:
// it's a list of primitives
if s.ForceNew && !reflect.DeepEqual(v1, v2) {
fields.PushBack(qualifiedName)
}
case *schema.Resource:
if s.MaxItems == 1 {
// A list with MaxItems == 1 is actually a nested object due to limitations with TF schemas.
tfObjSchemaMap := s.Elem.(*schema.Resource).Schema
var o1 map[string]interface{}
var o2 map[string]interface{}
if v1 != nil {
o1 = v1.(map[string]interface{})
}
if v2 != nil {
o2 = v2.(map[string]interface{})
}
compareAndFindChangesOnImmutableFields(o1, o2, tfObjSchemaMap, qualifiedName, resourceConfig, ignoredFields, fields)
} else { //nolint:revive
// TODO(kcc-eng): Kubernetes considers all lists of objects to be atomic, and so all subsequent
// applies will currently wipe out defaulted immutable fields. Temporarily delegate validation
// to the controller, which will determine via comparing the config with calculated fields in
// the live state to detect if a diff in immutable fields is present.
}
}
}
}
}
func isReferenceValRawModified(obj map[string]interface{}, oldObj map[string]interface{}, refConfig *corekccv1alpha1.ReferenceConfig) (bool, string) {
// currently we choose to treat switching between different reference approaches as modification, e.g. change from referencing to ComputeAddress to directly specifying ip address
referenceFieldKey := krmtotf.GetKeyForReferenceField(refConfig)
return !reflect.DeepEqual(obj[referenceFieldKey], oldObj[referenceFieldKey]), referenceFieldKey
}
func constructCamelCasePath(path string) string {
segs := make([]string, 0)
for _, f := range strings.Split(path, ".") {
segs = append(segs, text.SnakeCaseToLowerCamelCase(f))
}
return strings.Join(segs, ".")
}
func getIgnoredFields(rc *corekccv1alpha1.ResourceConfig) map[string]bool {
ignoredFields := make(map[string]bool)
for _, f := range rc.IgnoredFields {
ignoredFields[f] = true
}
// metadata mapping can be ignored because there are only two k8s metadata fields mapping to TF fields: name and label
// k8s object names are unique identifiers and labels are totally mutable by default
ignoredFields[rc.MetadataMapping.Name] = true
ignoredFields[rc.MetadataMapping.Labels] = true
return ignoredFields
}