...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides/compute_backendservice.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  	oauth2ClientIdPath    = []string{"iap", "oauth2ClientId"}
    30  	oauth2ClientIdRefPath = []string{"iap", "oauth2ClientIdRef"}
    31  )
    32  
    33  func GetComputeBackendServiceResourceOverrides() ResourceOverrides {
    34  	ro := ResourceOverrides{
    35  		Kind: "ComputeBackendService",
    36  	}
    37  	// Preserve the legacy non-reference field 'iap.oauth2ClientId' after it is changed to
    38  	// a reference field, 'iap.oauth2ClientIdRef'.
    39  	ro.Overrides = append(ro.Overrides, keepOauth2ClientIdField())
    40  	return ro
    41  }
    42  
    43  func keepOauth2ClientIdField() ResourceOverride {
    44  	o := ResourceOverride{}
    45  	o.CRDDecorate = func(crd *apiextensions.CustomResourceDefinition) error {
    46  		if err := PreserveMutuallyExclusiveNonReferenceField(crd, []string{"iap"}, oauth2ClientIdRefPath[1], oauth2ClientIdPath[1]); err != nil {
    47  			return err
    48  		}
    49  		return nil
    50  	}
    51  	o.PreActuationTransform = func(r *k8s.Resource) error {
    52  		if err := FavorAuthoritativeFieldOverLegacyField(r, oauth2ClientIdPath, oauth2ClientIdRefPath); err != nil {
    53  			return fmt.Errorf("error handling '%v' and '%v' fields in pre-actuation transformation: %w", strings.Join(oauth2ClientIdPath, "."), strings.Join(oauth2ClientIdRefPath, "."), err)
    54  		}
    55  		return nil
    56  	}
    57  	o.PostActuationTransform = func(original, reconciled *k8s.Resource, tfState *terraform.InstanceState, dclState *unstructured.Unstructured) error {
    58  		if err := PreserveUserSpecifiedLegacyField(original, reconciled, oauth2ClientIdPath...); err != nil {
    59  			return fmt.Errorf("error preserving '%v' in post-actuation transformation: %w", strings.Join(oauth2ClientIdPath, "."), err)
    60  		}
    61  		if err := PruneDefaultedAuthoritativeFieldIfOnlyLegacyFieldSpecified(original, reconciled, oauth2ClientIdPath, oauth2ClientIdRefPath); err != nil {
    62  			return fmt.Errorf("error conditionally pruning '%v' in post-actuation transformation: %w", strings.Join(oauth2ClientIdRefPath, "."), err)
    63  		}
    64  		return nil
    65  	}
    66  	return o
    67  }
    68  

View as plain text