Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spanner instance direct controller #3137

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions apis/spanner/v1beta1/instance_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 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 v1beta1

import (
"context"
"fmt"

"github.com/GoogleCloudPlatform/k8s-config-connector/apis/common"
refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type SpannerInstanceIdentity struct {
id string
parent *SpannerInstanceParent
}

func (i *SpannerInstanceIdentity) String() string {
return i.parent.String() + "/instances/" + i.id
}

func (r *SpannerInstanceIdentity) Parent() *SpannerInstanceParent {
return r.parent
}

func (r *SpannerInstanceIdentity) ID() string {
return r.id
}

type SpannerInstanceParent struct {
ProjectID string
}

func (p *SpannerInstanceParent) String() string {
return "projects/" + p.ProjectID
}

func NewSpannerInstanceIdentity(ctx context.Context, reader client.Reader, obj *SpannerInstance, u *unstructured.Unstructured) (*SpannerInstanceIdentity, error) {
// Get Parent
projectID, err := refsv1beta1.ResolveProjectID(ctx, reader, u)
if err != nil {
return nil, err
}
// Get desired ID
resourceID := common.ValueOf(obj.Spec.ResourceID)
if resourceID == "" {
resourceID = obj.GetName()
}
if resourceID == "" {
return nil, fmt.Errorf("cannot resolve resource ID")
}

// Use approved External
externalRef := common.ValueOf(obj.Status.ExternalRef)
if externalRef != "" {
actualIdentity, err := ParseSpannerInstanceExternal(externalRef)
if err != nil {
return nil, err
}
if actualIdentity.parent.ProjectID != projectID {
return nil, fmt.Errorf("spec.projectRef changed, expect %s, got %s", actualIdentity.parent.ProjectID, projectID)
}
if actualIdentity.id != resourceID {
return nil, fmt.Errorf("cannot reset `metadata.name` or `spec.resourceID` to %s, since it has already assigned to %s",
resourceID, actualIdentity.id)
}
}
return &SpannerInstanceIdentity{
parent: &SpannerInstanceParent{ProjectID: projectID},
id: resourceID,
}, nil
}

func (r *SpannerInstanceIdentity) SpannerInstanceConfigPrefix() string {
return fmt.Sprintf("projects/%s/instanceConfigs/", r.parent.ProjectID)
}
107 changes: 20 additions & 87 deletions apis/spanner/v1beta1/instance_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package v1beta1

import (
"context"
"errors"
"fmt"
"strings"

Expand All @@ -41,8 +42,6 @@ type SpannerInstanceRef struct {

// The namespace of a SpannerInstance resource.
Namespace string `json:"namespace,omitempty"`

parent *SpannerInstanceParent
}

// NormalizedExternal provision the "External" value for other resource that depends on SpannerInstance.
Expand All @@ -54,7 +53,7 @@ func (r *SpannerInstanceRef) NormalizedExternal(ctx context.Context, reader clie
}
// From given External
if r.External != "" {
if _, _, err := parseSpannerInstanceExternal(r.External); err != nil {
if _, err := ParseSpannerInstanceExternal(r.External); err != nil {
return "", err
}
return r.External, nil
Expand All @@ -74,9 +73,15 @@ func (r *SpannerInstanceRef) NormalizedExternal(ctx context.Context, reader clie
return "", fmt.Errorf("reading referenced %s %s: %w", SpannerInstanceGVK, key, err)
}
// Get external from status.externalRef. This is the most trustworthy place.
actualExternalRef, _, err := unstructured.NestedString(u.Object, "status", "externalRef")
if err != nil {
return "", fmt.Errorf("reading status.externalRef: %w", err)
actualExternalRef, _, err1 := unstructured.NestedString(u.Object, "status", "externalRef")
if err1 != nil {
err1 = fmt.Errorf("SecretManagerSecret `status.externalRef` not configured: %w", err1)
// Backward compatible to Terraform/DCL based resource, which does not have status.externalRef.
var err2 error
actualExternalRef, _, err2 = unstructured.NestedString(u.Object, "status", "name")
if err2 != nil {
return "", errors.Join(err1, err2)
}
}
if actualExternalRef == "" {
return "", k8s.NewReferenceNotReadyError(u.GroupVersionKind(), key)
Expand All @@ -85,93 +90,21 @@ func (r *SpannerInstanceRef) NormalizedExternal(ctx context.Context, reader clie
return r.External, nil
}

// New builds a SpannerInstanceRef from the Config Connector SpannerInstance object.
func NewSpannerInstanceRef(ctx context.Context, reader client.Reader, obj *SpannerInstance, u *unstructured.Unstructured) (*SpannerInstanceRef, error) {
id := &SpannerInstanceRef{}

projectID, err := refsv1beta1.ResolveProjectID(ctx, reader, u)
if err != nil {
return nil, err
}

id.parent = &SpannerInstanceParent{ProjectID: projectID}

// Get desired ID
resourceID := valueOf(obj.Spec.ResourceID)
if resourceID == "" {
resourceID = obj.GetName()
}
if resourceID == "" {
return nil, fmt.Errorf("cannot resolve resource ID")
}

// Use approved External
externalRef := valueOf(obj.Status.ExternalRef)
if externalRef == "" {
id.External = asSpannerInstanceExternal(id.parent, resourceID)
return id, nil
}

// Validate desired with actual
actualParent, actualResourceID, err := parseSpannerInstanceExternal(externalRef)
if err != nil {
return nil, err
}
if actualParent.ProjectID != projectID {
return nil, fmt.Errorf("spec.projectRef changed, expect %s, got %s", actualParent.ProjectID, projectID)
}
if actualResourceID != resourceID {
return nil, fmt.Errorf("cannot reset `metadata.name` or `spec.resourceID` to %s, since it has already assigned to %s",
resourceID, actualResourceID)
}
id.External = externalRef
id.parent = &SpannerInstanceParent{ProjectID: projectID}
return id, nil
}

func (r *SpannerInstanceRef) Parent() (*SpannerInstanceParent, error) {
if r.parent != nil {
return r.parent, nil
}
if r.External != "" {
parent, _, err := parseSpannerInstanceExternal(r.External)
if err != nil {
return nil, err
}
return parent, nil
}
return nil, fmt.Errorf("SpannerInstanceRef not initialized from `NewSpannerInstanceRef` or `NormalizedExternal`")
}

type SpannerInstanceParent struct {
ProjectID string
}

func (p *SpannerInstanceParent) String() string {
return "projects/" + p.ProjectID
}

func asSpannerInstanceExternal(parent *SpannerInstanceParent, resourceID string) (external string) {
return parent.String() + "/instances/" + resourceID
}

func parseSpannerInstanceExternal(external string) (parent *SpannerInstanceParent, resourceID string, err error) {
func ParseSpannerInstanceExternal(external string) (*SpannerInstanceIdentity, error) {
if external == "" {
return nil, fmt.Errorf("missing external value")
}
external = strings.TrimPrefix(external, "/")
tokens := strings.Split(external, "/")
if len(tokens) != 4 || tokens[0] != "projects" || tokens[2] != "instances" {
return nil, "", fmt.Errorf("format of SpannerInstance external=%q was not known (use projects/{{projectId}}/instances/{{instanceID}})", external)
}
parent = &SpannerInstanceParent{
ProjectID: tokens[1],
}
resourceID = tokens[3]
return parent, resourceID, nil
}

func valueOf[T any](t *T) T {
var zeroVal T
if t == nil {
return zeroVal
return nil, fmt.Errorf("format of SpannerInstance external=%q was not known (use projects/{{projectId}}/instances/{{instanceID}})", external)
}
return *t
return &SpannerInstanceIdentity{
parent: &SpannerInstanceParent{ProjectID: tokens[1]},
id: tokens[3],
}, nil
}
14 changes: 12 additions & 2 deletions apis/spanner/v1beta1/instance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ type SpannerInstanceSpec struct {
DisplayName string `json:"displayName"`

// +optional
NumNodes *int64 `json:"numNodes,omitempty"`
NumNodes *int32 `json:"numNodes,omitempty"`

// +optional
ProcessingUnits *int64 `json:"processingUnits,omitempty"`
ProcessingUnits *int32 `json:"processingUnits,omitempty"`

// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="ResourceID field is immutable"
// Immutable.
Expand All @@ -66,10 +66,20 @@ type SpannerInstanceStatus struct {
/* Instance status: 'CREATING' or 'READY'. */
// +optional
State *string `json:"state,omitempty"`

/* ObservedState is the state of the resource as most recently observed in GCP. */
// +optional
ObservedState *SpannerInstanceObservedState `json:"observedState,omitempty"`
}

// SpannerInstanceObservedState is the state of the SpannerInstance resource as most recently observed in GCP.
type SpannerInstanceObservedState struct {
/* NOTYET
// NumNodes and ProcessUnits is output fields with AutoScaler is set.
NumNodes *int32 `json:"numNodes,omitempty"`

ProcessingUnits *int32 `json:"processingUnits,omitempty"`
*/
}

// +genclient
Expand Down
34 changes: 27 additions & 7 deletions apis/spanner/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ spec:
in length.
type: string
numNodes:
format: int64
format: int32
type: integer
processingUnits:
format: int64
format: int32
anhdle-sso marked this conversation as resolved.
Show resolved Hide resolved
type: integer
resourceID:
description: Immutable. The SpannerInstance name. If not given, the
Expand Down Expand Up @@ -135,6 +135,10 @@ spec:
the resource.
format: int64
type: integer
observedState:
description: ObservedState is the state of the resource as most recently
observed in GCP.
type: object
state:
description: 'Instance status: ''CREATING'' or ''READY''.'
type: string
Expand Down
2 changes: 1 addition & 1 deletion dev/tasks/run-e2e
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if [[ -z "${KUBEBUILDER_ASSETS:-}" ]]; then
fi

if [[ -z "${KCC_USE_DIRECT_RECONCILERS:-}" ]]; then
KCC_USE_DIRECT_RECONCILERS=ComputeForwardingRule,GKEHubFeatureMembership
KCC_USE_DIRECT_RECONCILERS=ComputeForwardingRule,GKEHubFeatureMembership,SpannerInstance
fi
echo "Using direct controllers: $KCC_USE_DIRECT_RECONCILERS"
export KCC_USE_DIRECT_RECONCILERS
Expand Down
5 changes: 4 additions & 1 deletion dev/tools/controllerbuilder/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,12 @@ go run . generate-mapper \
# Spanner
go run main.go generate-types \
--service google.spanner.admin.instance.v1 \
--output-api $REPO_ROOT/apis \
--resource SpannerInstance:Instance \
--api-version "spanner.cnrm.cloud.google.com/v1beta1"

go run . generate-mapper \
--service google.spanner.admin.instance.v1 \
--api-version "spanner.cnrm.cloud.google.com/v1beta1" \

# Fix up formatting
${REPO_ROOT}/dev/tasks/fix-gofmt
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
cloud.google.com/go/secretmanager v1.14.2
cloud.google.com/go/securesourcemanager v1.1.1
cloud.google.com/go/security v1.18.2
cloud.google.com/go/spanner v1.73.0
cloud.google.com/go/workstations v1.1.1
contrib.go.opencensus.io/exporter/prometheus v0.1.0
github.com/GoogleCloudPlatform/declarative-resource-client-library v1.62.0
Expand Down
Loading
Loading