...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides/vpc_access_connector.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  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    25  )
    26  
    27  var (
    28  	projectRefPath       = []string{"projectRef", "external"}
    29  	subnetProjectRefPath = []string{"subnet", "projectRef", "external"}
    30  )
    31  
    32  func GetVPCAccessConnectorResourceOverrides() ResourceOverrides {
    33  	ro := ResourceOverrides{
    34  		Kind: "VPCAccessConnector",
    35  	}
    36  	ro.Overrides = append(ro.Overrides, handleProjectsPrefixInProjectRefExternalFields())
    37  	return ro
    38  }
    39  
    40  // For backward compatibility on 'projectRef.external' field after migrating from DCL-based to TF-based.
    41  // DCL-based implementation (old) uses format "projects/${PROJECT_ID?}".
    42  // TF-based implementation (new) uses format "${PROJECT_ID?}"
    43  // Assuming user still uses "projects/${PROJECT_ID?}", we need to trim the "projects" prefix before TF actuation,
    44  // and add back the prefix after TF actuation to preserve the user specified value.
    45  func handleProjectsPrefixInProjectRefExternalFields() ResourceOverride {
    46  	o := ResourceOverride{}
    47  	o.PreActuationTransform = func(r *k8s.Resource) error {
    48  		return removePrefixFromProjectRefExternalFields(r, "pre-actuation")
    49  	}
    50  	o.PostActuationTransform = func(original, reconciled *k8s.Resource, tfState *terraform.InstanceState, dclState *unstructured.Unstructured) error {
    51  		for _, p := range [][]string{projectRefPath, subnetProjectRefPath} {
    52  			if err := PreserveUserSpecifiedLegacyField(original, reconciled, p...); err != nil {
    53  				return fmt.Errorf("error preserving field %v in post-actuation transformation: %v", strings.Join(p, "."), err)
    54  			}
    55  		}
    56  		return nil
    57  	}
    58  	// After status update, the http response is decoded to the original resource object, reverting the prefix removal.
    59  	// So we need to remove the prefix again to ensure the prefix is removed.
    60  	o.PostUpdateStatusTransform = func(r *k8s.Resource) error {
    61  		return removePrefixFromProjectRefExternalFields(r, "post-status update")
    62  	}
    63  	return o
    64  }
    65  
    66  func removePrefixFromProjectRefExternalFields(r *k8s.Resource, stage string) error {
    67  	for _, p := range [][]string{projectRefPath, subnetProjectRefPath} {
    68  		if err := RemovePrefixFromStringFieldInSpec(r, "projects/", p...); err != nil {
    69  			return fmt.Errorf("error removing 'projects/' prefix from field %v in %s transformation: %v", strings.Join(p, "."), stage, err)
    70  		}
    71  	}
    72  	return nil
    73  }
    74  

View as plain text