...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides/container_cluster.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    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  	// Preserve the legacy field 'workloadIdentityConfig.identityNamespace' that has been removed from Terraform 4.x upgrade.
    38  	// See b/206133327 for context.
    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