...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package leasable
16
17 import (
18 "fmt"
19
20 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/core/v1alpha1"
21 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/extension"
22 tfresource "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/tf/resource"
23
24 tfschema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
25 "github.com/nasa9084/go-openapi"
26 )
27
28 func ResourceConfigSupportsLeasing(rc *v1alpha1.ResourceConfig, tfResourceMap map[string]*tfschema.Resource) (ok bool, err error) {
29
30
31
32 if rc.Kind == "DataflowJob" {
33 return false, nil
34 }
35 labelsField := rc.MetadataMapping.Labels
36 if labelsField == "" {
37 return false, nil
38 }
39 tfResource, ok := tfResourceMap[rc.Name]
40 if !ok {
41 return false, fmt.Errorf("unknown resource %v", rc.Name)
42 }
43 labelsFieldSchema, err := tfresource.GetTFSchemaForField(tfResource, labelsField)
44 if err != nil {
45 return false, fmt.Errorf("error getting schema for field '%v' of resource '%v': %v", labelsField, rc.Name, err)
46 }
47 labelsFieldIsMutable := !labelsFieldSchema.ForceNew
48 return labelsFieldIsMutable, nil
49 }
50
51 func DCLSchemaSupportsLeasing(schema *openapi.Schema) (bool, error) {
52 labelsField, s, found, err := extension.GetLabelsFieldSchema(schema)
53 if err != nil {
54 return false, fmt.Errorf("error getting schema for field %v of resource '%v': %w", labelsField, schema.Title, err)
55 }
56 if !found {
57 return false, nil
58 }
59 isImmutable, err := extension.IsImmutableField(s)
60 if err != nil {
61 return false, err
62 }
63 return !isImmutable, nil
64 }
65
View as plain text