...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package resourceoverrides
16
17 import (
18 "fmt"
19 "strings"
20
21 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
22
23 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
24 apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
25 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26 )
27
28 var (
29 identityNamespaceFieldPath = []string{"workloadIdentityConfig", "identityNamespace"}
30 workloadPoolFieldPath = []string{"workloadIdentityConfig", "workloadPool"}
31 )
32
33 func GetContainerClusterResourceOverrides() ResourceOverrides {
34 ro := ResourceOverrides{
35 Kind: "ContainerCluster",
36 }
37
38
39 ro.Overrides = append(ro.Overrides, keepIdentityNamespaceField())
40 return ro
41 }
42
43 func keepIdentityNamespaceField() ResourceOverride {
44 o := ResourceOverride{}
45 o.CRDDecorate = func(crd *apiextensions.CustomResourceDefinition) error {
46 schema := k8s.GetOpenAPIV3SchemaFromCRD(crd)
47 workloadIdentityConfig := schema.Properties["spec"].Properties["workloadIdentityConfig"]
48 workloadIdentityConfig.Properties["identityNamespace"] = apiextensions.JSONSchemaProps{
49 Description: "DEPRECATED. This field will be removed in a future major release as it has been deprecated in the API. Use `workloadPool` instead; `workloadPool` field will supersede this field.\n" +
50 "Enables workload identity.",
51 Type: "string",
52 }
53 return nil
54 }
55 o.PreActuationTransform = func(r *k8s.Resource) error {
56 if err := FavorAuthoritativeFieldOverLegacyField(r, identityNamespaceFieldPath, workloadPoolFieldPath); err != nil {
57 return fmt.Errorf("error handling '%v' and '%v' fields in pre-actuation transformation: %w", strings.Join(identityNamespaceFieldPath, "."), strings.Join(workloadPoolFieldPath, "."), err)
58 }
59 return nil
60 }
61 o.PostActuationTransform = func(original, reconciled *k8s.Resource, tfState *terraform.InstanceState, dclState *unstructured.Unstructured) error {
62 if err := PreserveUserSpecifiedLegacyField(original, reconciled, identityNamespaceFieldPath...); err != nil {
63 return fmt.Errorf("error preserving '%v' in post-actuation transformation: %w", strings.Join(identityNamespaceFieldPath, "."), err)
64 }
65 if err := PruneDefaultedAuthoritativeFieldIfOnlyLegacyFieldSpecified(original, reconciled, workloadPoolFieldPath, workloadPoolFieldPath); err != nil {
66 return fmt.Errorf("error conditionally pruning '%v' in post-actuation transformation: %w", strings.Join(workloadPoolFieldPath, "."), err)
67 }
68 return nil
69 }
70 return o
71 }
72
View as plain text