...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides/sql_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  
    20  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    21  
    22  	"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
    23  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    24  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    25  )
    26  
    27  func GetSQLInstanceResourceOverrides() ResourceOverrides {
    28  	ro := ResourceOverrides{
    29  		Kind: "SQLInstance",
    30  	}
    31  	// Keep the 'databaseVersion' field optional with default for backwards compatability.
    32  	// See b/206145549 for context.
    33  	ro.Overrides = append(ro.Overrides, keepDatabaseVersionFieldOptionalWithDefault())
    34  	// Preserve the legacy fields for first generation sql instances that have been removed from Terraform 4.x upgrade.
    35  	// See b/206145549 for context.
    36  	ro.Overrides = append(ro.Overrides, keepFirstGenerationFields())
    37  
    38  	ro.Overrides = append(ro.Overrides, copyInstanceTypeFieldToStatus())
    39  	return ro
    40  }
    41  
    42  func copyInstanceTypeFieldToStatus() ResourceOverride {
    43  	o := ResourceOverride{}
    44  	o.CRDDecorate = func(crd *apiextensions.CustomResourceDefinition) error {
    45  		schema := k8s.GetOpenAPIV3SchemaFromCRD(crd)
    46  		spec := schema.Properties["spec"]
    47  		status := schema.Properties["status"]
    48  		status.Properties["instanceType"] = spec.Properties["instanceType"]
    49  
    50  		return nil
    51  	}
    52  	o.PostActuationTransform = func(original, reconciled *k8s.Resource, tfState *terraform.InstanceState, dclState *unstructured.Unstructured) error {
    53  		if tfState != nil {
    54  			reconciled.Status["instanceType"] = tfState.Attributes["instance_type"]
    55  		}
    56  
    57  		return nil
    58  	}
    59  	return o
    60  }
    61  
    62  func keepDatabaseVersionFieldOptionalWithDefault() ResourceOverride {
    63  	o := ResourceOverride{}
    64  	o.CRDDecorate = func(crd *apiextensions.CustomResourceDefinition) error {
    65  		return KeepTopLevelFieldOptionalWithDefault(crd, "MYSQL_5_6", "databaseVersion")
    66  	}
    67  	return o
    68  }
    69  
    70  func keepFirstGenerationFields() ResourceOverride {
    71  	o := ResourceOverride{}
    72  	o.CRDDecorate = func(crd *apiextensions.CustomResourceDefinition) error {
    73  		schema := k8s.GetOpenAPIV3SchemaFromCRD(crd)
    74  		settings := schema.Properties["spec"].Properties["settings"]
    75  		settings.Properties["authorizedGaeApplications"] = apiextensions.JSONSchemaProps{
    76  			Description: "DEPRECATED. This property is only applicable to First Generation instances, and First Generation instances are now deprecated. see https://cloud.google.com/sql/docs/mysql/deprecation-notice for information on how to upgrade to Second Generation instances.\n" +
    77  				"Specifying this field has no-ops; it's recommended to remove this field from your configuration.",
    78  			Type: "array",
    79  			Items: &apiextensions.JSONSchemaPropsOrArray{
    80  				Schema: &apiextensions.JSONSchemaProps{
    81  					Type: "string",
    82  				},
    83  			},
    84  		}
    85  		settings.Properties["crashSafeReplication"] = apiextensions.JSONSchemaProps{
    86  			Description: "DEPRECATED. This property is only applicable to First Generation instances, and First Generation instances are now deprecated. see https://cloud.google.com/sql/docs/mysql/deprecation-notice for information on how to upgrade to Second Generation instances.\n" +
    87  				"Specifying this field has no-ops; it's recommended to remove this field from your configuration.",
    88  			Type: "boolean",
    89  		}
    90  		settings.Properties["replicationType"] = apiextensions.JSONSchemaProps{
    91  			Description: "DEPRECATED. This property is only applicable to First Generation instances, and First Generation instances are now deprecated. see https://cloud.google.com/sql/docs/mysql/deprecation-notice for information on how to upgrade to Second Generation instances.\n" +
    92  				"Specifying this field has no-ops; it's recommended to remove this field from your configuration.",
    93  			Type: "string",
    94  		}
    95  		return nil
    96  	}
    97  	o.PreActuationTransform = func(r *k8s.Resource) error {
    98  		if err := PruneNoOpsField(r, "settings", "authorizedGaeApplications"); err != nil {
    99  			return fmt.Errorf("error prunning no-ops field 'settings.authorizedGaeApplications' in pre-actuation transformation: %w", err)
   100  		}
   101  		if err := PruneNoOpsField(r, "settings", "crashSafeReplication"); err != nil {
   102  			return fmt.Errorf("error prunning no-ops field 'settings.crashSafeReplication' in pre-actuation transformation: %w", err)
   103  		}
   104  		if err := PruneNoOpsField(r, "settings", "replicationType"); err != nil {
   105  			return fmt.Errorf("error prunning no-ops field 'settings.replicationType' in pre-actuation transformation: %w", err)
   106  		}
   107  		return nil
   108  	}
   109  	o.PostActuationTransform = func(original, reconciled *k8s.Resource, tfState *terraform.InstanceState, dclState *unstructured.Unstructured) error {
   110  		if err := PreserveUserSpecifiedLegacyField(original, reconciled, "settings", "authorizedGaeApplications"); err != nil {
   111  			return fmt.Errorf("error preserving no-ops field 'settings.authorizedGaeApplications' in post-actuation transformation: %w", err)
   112  		}
   113  		if err := PreserveUserSpecifiedLegacyField(original, reconciled, "settings", "crashSafeReplication"); err != nil {
   114  			return fmt.Errorf("error preserving no-ops field 'settings.crashSafeReplication' in post-actuation transformation: %w", err)
   115  		}
   116  		if err := PreserveUserSpecifiedLegacyField(original, reconciled, "settings", "replicationType"); err != nil {
   117  			return fmt.Errorf("error preserving no-ops field 'settings.replicationType' in post-actuation transformation: %w", err)
   118  		}
   119  		return nil
   120  	}
   121  	return o
   122  }
   123  

View as plain text