...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides/storage_bucket.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  	bucketPolicyOnlyFieldPath         = []string{"bucketPolicyOnly"}
    30  	uniformBucketLevelAccessFieldPath = []string{"uniformBucketLevelAccess"}
    31  )
    32  
    33  func GetStorageBucketResourceOverrides() ResourceOverrides {
    34  	ro := ResourceOverrides{
    35  		Kind: "StorageBucket",
    36  	}
    37  	// Preserve the legacy field 'bucketPolicyOnly' that has been removed from Terraform 4.x upgrade.
    38  	// See b/206156139 for context.
    39  	ro.Overrides = append(ro.Overrides, preserveBucketPolicyOnlyField())
    40  	// Keep the 'location' field optional and default it to 'US' for backwards compatability.
    41  	// See b/206156139 for context.
    42  	ro.Overrides = append(ro.Overrides, keepLocationFieldOptionalWithDefault())
    43  	return ro
    44  }
    45  
    46  func preserveBucketPolicyOnlyField() ResourceOverride {
    47  	o := ResourceOverride{}
    48  	o.CRDDecorate = func(crd *apiextensions.CustomResourceDefinition) error {
    49  		schema := k8s.GetOpenAPIV3SchemaFromCRD(crd)
    50  		spec := schema.Properties["spec"]
    51  		spec.Properties["bucketPolicyOnly"] = apiextensions.JSONSchemaProps{
    52  			Type: "boolean",
    53  			Description: "DEPRECATED. Please use the `uniformBucketLevelAccess` field as this field has been renamed by Google. The `uniformBucketLevelAccess` field will supersede this field.\n" +
    54  				"Enables Bucket PolicyOnly access to a bucket.",
    55  		}
    56  		schema.Properties["spec"] = spec
    57  		return nil
    58  	}
    59  	o.PreActuationTransform = func(r *k8s.Resource) error {
    60  		if err := FavorAuthoritativeFieldOverLegacyField(r, bucketPolicyOnlyFieldPath, uniformBucketLevelAccessFieldPath); err != nil {
    61  			return fmt.Errorf("error handling '%v' and '%v' fields in pre-actuation transformation: %w", strings.Join(bucketPolicyOnlyFieldPath, "."), strings.Join(uniformBucketLevelAccessFieldPath, "."), err)
    62  		}
    63  		return nil
    64  	}
    65  	o.PostActuationTransform = func(original, reconciled *k8s.Resource, tfState *terraform.InstanceState, dclState *unstructured.Unstructured) error {
    66  		if err := PreserveUserSpecifiedLegacyField(original, reconciled, bucketPolicyOnlyFieldPath...); err != nil {
    67  			return fmt.Errorf("error preserving '%v' in post-actuation transformation: %w", strings.Join(bucketPolicyOnlyFieldPath, "."), err)
    68  		}
    69  		if err := PruneDefaultedAuthoritativeFieldIfOnlyLegacyFieldSpecified(original, reconciled, bucketPolicyOnlyFieldPath, uniformBucketLevelAccessFieldPath); err != nil {
    70  			return fmt.Errorf("error conditionally pruning '%v' in post-actuation transformation: %w", strings.Join(uniformBucketLevelAccessFieldPath, "."), err)
    71  		}
    72  		return nil
    73  	}
    74  	return o
    75  }
    76  
    77  func keepLocationFieldOptionalWithDefault() ResourceOverride {
    78  	o := ResourceOverride{}
    79  	o.CRDDecorate = func(crd *apiextensions.CustomResourceDefinition) error {
    80  		return KeepTopLevelFieldOptionalWithDefault(crd, "US", "location")
    81  	}
    82  	return o
    83  }
    84  

View as plain text