1
2
3
4
5
6
7
8
9
10
11
12
13
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
32
33 ro.Overrides = append(ro.Overrides, keepDatabaseVersionFieldOptionalWithDefault())
34
35
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