...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides/compute_instance.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  	networkInterfacePath  = []string{"networkInterface"}
    30  	networkIpFieldPath    = []string{"networkIp"}
    31  	networkIpRefFieldPath = []string{"networkIpRef"}
    32  	supportedKinds        = []string{"ComputeAddress"}
    33  )
    34  
    35  func GetComputeInstanceResourceOverrides() ResourceOverrides {
    36  	ro := ResourceOverrides{
    37  		Kind: "ComputeInstance",
    38  	}
    39  	ro.Overrides = append(ro.Overrides, addNetworkIpRefField())
    40  	return ro
    41  }
    42  
    43  func addNetworkIpRefField() ResourceOverride {
    44  	o := ResourceOverride{}
    45  	o.CRDDecorate = func(crd *apiextensions.CustomResourceDefinition) error {
    46  		if err := PreserveMutuallyExclusiveNonReferenceField(crd, networkInterfacePath, networkIpRefFieldPath[0], networkIpFieldPath[0]); err != nil {
    47  			return err
    48  		}
    49  		if err := EnsureReferenceFieldIsMultiKind(crd, networkInterfacePath, networkIpRefFieldPath[0], supportedKinds); err != nil {
    50  			return err
    51  		}
    52  
    53  		return nil
    54  	}
    55  	o.PreActuationTransform = func(r *k8s.Resource) error {
    56  		if err := FavorReferenceFieldOverNonReferenceFieldUnderSlice(r, networkInterfacePath, networkIpFieldPath, networkIpRefFieldPath); err != nil {
    57  			return fmt.Errorf("error handling '%v' and '%v' fields in pre-actuation transformation: %w", strings.Join(networkIpFieldPath, "."), strings.Join(networkIpRefFieldPath, "."), err)
    58  		}
    59  		return nil
    60  	}
    61  	o.PostActuationTransform = func(original, reconciled *k8s.Resource, tfState *terraform.InstanceState, dclState *unstructured.Unstructured) error {
    62  		if err := PreserveUserSpecifiedLegacyFieldUnderSlice(original, reconciled, networkInterfacePath, networkIpFieldPath); err != nil {
    63  			return fmt.Errorf("error preserving '%v' in post-actuation transformation: %w", strings.Join(networkIpFieldPath, "."), err)
    64  		}
    65  		if err := PruneDefaultedAuthoritativeFieldIfOnlyLegacyFieldSpecifiedUnderSlice(original, reconciled, networkInterfacePath, networkIpFieldPath, networkIpRefFieldPath); err != nil {
    66  			return fmt.Errorf("error conditionally pruning '%v' in post-actuation transformation: %w", strings.Join(networkIpRefFieldPath, "."), err)
    67  		}
    68  		return nil
    69  	}
    70  	return o
    71  }
    72  

View as plain text