Skip to content

Commit

Permalink
Add overrides implementation to keep legacy instanceType along with i…
Browse files Browse the repository at this point in the history
…nstanceTypeRef
  • Loading branch information
199201shubhamsahu committed Mar 7, 2024
1 parent 8dea56c commit 0a1e230
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ spec:
metadata:
type: object
spec:
oneOf:
- required:
- instanceType
- required:
- instanceTypeRef
properties:
annotations:
additionalProperties:
Expand Down Expand Up @@ -120,6 +125,11 @@ spec:
instance, an error will be thrown. If this is absent for a ZONAL
instance, instance is created in a random zone with available capacity.
type: string
instanceType:
description: DEPRECATED. Although this field is still available, there
is limited support. We recommend that you use `spec.instanceTypeRef`
instead.
type: string
instanceTypeRef:
oneOf:
- not:
Expand Down Expand Up @@ -171,7 +181,6 @@ spec:
type: string
required:
- clusterRef
- instanceTypeRef
type: object
status:
properties:
Expand Down
64 changes: 64 additions & 0 deletions pkg/resourceoverrides/alloydb_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package resourceoverrides

import (
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"

"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

var (
instanceTypeField = []string{"instanceType"}
instanceTypeRefField = []string{"instanceTypeRef"}
)

func GetAlloyDBInstanceResourceOverrides() ResourceOverrides {
ro := ResourceOverrides{
Kind: "AlloyDBInstance",
}
// Preserve the legacy non-reference field 'instanceType' after it is changed to
// a reference field, 'instanceTypeRef'.
ro.Overrides = append(ro.Overrides, keepInstanceTypeField())
return ro
}

func keepInstanceTypeField() ResourceOverride {
o := ResourceOverride{}
o.CRDDecorate = func(crd *apiextensions.CustomResourceDefinition) error {
return PreserveMutuallyExclusiveNonReferenceField(crd, nil, instanceTypeRefField[0], instanceTypeField[0])
}
o.PreActuationTransform = func(r *k8s.Resource) error {
if err := FavorAuthoritativeFieldOverLegacyField(r, instanceTypeField, instanceTypeRefField); err != nil {
return fmt.Errorf("error handling '%v' and '%v' fields in pre-actuation transformation: %w", strings.Join(instanceTypeField, "."), strings.Join(instanceTypeRefField, "."), err)
}
return nil
}
o.PostActuationTransform = func(original, reconciled *k8s.Resource, tfState *terraform.InstanceState, dclState *unstructured.Unstructured) error {
if err := PreserveUserSpecifiedLegacyField(original, reconciled, instanceTypeField...); err != nil {
return fmt.Errorf("error preserving '%v' in post-actuation transformation: %w", strings.Join(instanceTypeField, "."), err)
}
if err := PruneDefaultedAuthoritativeFieldIfOnlyLegacyFieldSpecified(original, reconciled, instanceTypeField, instanceTypeRefField); err != nil {
return fmt.Errorf("error conditionally pruning '%v' in post-actuation transformation: %w", strings.Join(instanceTypeRefField, "."), err)
}
return nil
}
return o
}
1 change: 1 addition & 0 deletions pkg/resourceoverrides/overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func init() {
Handler.Register(GetVPCAccessConnectorResourceOverrides())
Handler.Register(GetRedisInstanceResourceOverrides())
Handler.Register(GetRunServiceResourceOverrides())
Handler.Register(GetAlloyDBInstanceResourceOverrides())

// IAM
Handler.Register(GetIAMCustomRoleResourceOverrides())
Expand Down

0 comments on commit 0a1e230

Please sign in to comment.