Skip to content
Open
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
4 changes: 3 additions & 1 deletion apis/bigtable/v1alpha1/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ go run . generate-types \
--resource BigtableBackup:Backup \
--resource BigtableCluster:Cluster \
--resource BigtableLogicalView:LogicalView \
--resource BigtableMaterializedView:MaterializedView
--resource BigtableMaterializedView:MaterializedView \
--resource BigtableSchemaBundle:SchemaBundle

go run . generate-mapper \
--multiversion \
--service google.bigtable.admin.v2 \
--api-version bigtable.cnrm.cloud.google.com/v1alpha1

Expand Down
112 changes: 112 additions & 0 deletions apis/bigtable/v1alpha1/schemabundle_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2026 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 v1alpha1

import (
"context"
"fmt"
"strings"

bigtablev1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/bigtable/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/apis/common"
"github.com/GoogleCloudPlatform/k8s-config-connector/apis/common/parent"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// SchemaBundleIdentity defines the resource reference to BigtableSchemaBundle, which "External" field
// holds the GCP identifier for the KRM object.
type SchemaBundleIdentity struct {
parent *bigtablev1beta1.TableIdentity
id string
}

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

func (i *SchemaBundleIdentity) ID() string {
return i.id
}

func (i *SchemaBundleIdentity) Parent() *bigtablev1beta1.TableIdentity {
return i.parent
}

// New builds a SchemaBundleIdentity from the Config Connector SchemaBundle object.
func NewSchemaBundleIdentity(ctx context.Context, reader client.Reader, obj *BigtableSchemaBundle) (*SchemaBundleIdentity, error) {

// Get Parent
tableRef, err := obj.Spec.TableRef.NormalizedExternal(ctx, reader, obj.GetNamespace())
if err != nil {
return nil, err
}
tableParent, tableID, err := bigtablev1beta1.ParseTableExternal(tableRef)
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 schema bundle name")
}

// Use approved External
externalRef := common.ValueOf(obj.Status.ExternalRef)
if externalRef != "" {
// Validate desired with actual
actualParent, actualResourceID, err := ParseSchemaBundleExternal(externalRef)
if err != nil {
return nil, err
}
if actualParent.Parent.Parent.ProjectID != tableParent.Parent.ProjectID {
return nil, fmt.Errorf("ProjectID changed, expect %s, got %s", actualParent.Parent.Parent.ProjectID, tableParent.Parent.ProjectID)
}
if actualParent.Parent.Id != tableParent.Id {
return nil, fmt.Errorf("InstanceID changed, expect %s, got %s", actualParent.Parent.Id, tableParent.Id)
}
if actualParent.Id != tableID {
return nil, fmt.Errorf("TableID changed, expect %s, got %s", actualParent.Id, tableID)
}
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)
}
}
return &SchemaBundleIdentity{
parent: &bigtablev1beta1.TableIdentity{
Parent: tableParent,
Id: tableID,
},
id: resourceID,
}, nil
}

func ParseSchemaBundleExternal(external string) (*bigtablev1beta1.TableIdentity, string, error) {
tokens := strings.Split(external, "/")
if len(tokens) != 8 || tokens[0] != "projects" || tokens[2] != "instances" || tokens[4] != "tables" || tokens[6] != "schemaBundles" {
return nil, "", fmt.Errorf("format of BigtableSchemaBundle external=%q was not known (use projects/{{projectID}}/instances/{{instanceID}}/tables/{{tableID}}/schemaBundles/{{schemaBundleID}})", external)
}
return &bigtablev1beta1.TableIdentity{
Parent: &bigtablev1beta1.InstanceIdentity{
Parent: &parent.ProjectParent{ProjectID: tokens[1]},
Id: tokens[3],
},
Id: tokens[5],
}, tokens[7], nil
}
83 changes: 83 additions & 0 deletions apis/bigtable/v1alpha1/schemabundle_reference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2026 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 v1alpha1

import (
"context"
"fmt"

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

var _ refsv1beta1.ExternalNormalizer = &SchemaBundleRef{}

// SchemaBundleRef defines the resource reference to BigtableSchemaBundle, which "External" field
// holds the GCP identifier for the KRM object.
type SchemaBundleRef struct {
// A reference to an externally managed BigtableSchemaBundle resource.
// Should be in the format "projects/{{projectID}}/instances/{{instanceID}}/tables/{{tableID}}/schemaBundles/{{schemaBundleID}}".
External string `json:"external,omitempty"`

// The name of a BigtableSchemaBundle resource.
Name string `json:"name,omitempty"`

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

// NormalizedExternal provision the "External" value for other resource that depends on BigtableSchemaBundle.
// If the "External" is given in the other resource's spec.BigtableSchemaBundleRef, the given value will be used.
// Otherwise, the "Name" and "Namespace" will be used to query the actual BigtableSchemaBundle object from the cluster.
func (r *SchemaBundleRef) NormalizedExternal(ctx context.Context, reader client.Reader, otherNamespace string) (string, error) {
if r.External != "" && r.Name != "" {
return "", fmt.Errorf("cannot specify both name and external on %s reference", BigtableSchemaBundleGVK.Kind)
}
// From given External
if r.External != "" {
if _, _, err := ParseSchemaBundleExternal(r.External); err != nil {
return "", err
}
return r.External, nil
}

// From the Config Connector object
if r.Namespace == "" {
r.Namespace = otherNamespace
}
key := types.NamespacedName{Name: r.Name, Namespace: r.Namespace}
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(BigtableSchemaBundleGVK)
if err := reader.Get(ctx, key, u); err != nil {
if apierrors.IsNotFound(err) {
return "", k8s.NewReferenceNotFoundError(u.GroupVersionKind(), key)
}
return "", fmt.Errorf("reading referenced %s %s: %w", BigtableSchemaBundleGVK, 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)
}
if actualExternalRef == "" {
return "", k8s.NewReferenceNotReadyError(u.GroupVersionKind(), key)
}
r.External = actualExternalRef
return r.External, nil
}
105 changes: 105 additions & 0 deletions apis/bigtable/v1alpha1/schemabundle_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2026 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 v1alpha1

import (
bigtablev1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/bigtable/v1beta1"
k8sv1alpha1 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/k8s/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var BigtableSchemaBundleGVK = GroupVersion.WithKind("BigtableSchemaBundle")

// BigtableSchemaBundleSpec defines the desired state of BigtableSchemaBundle
// +kcc:spec:proto=google.bigtable.admin.v2.SchemaBundle
type BigtableSchemaBundleSpec struct {
// The table to create the schema bundle in.
// +required
TableRef bigtablev1beta1.TableRef `json:"tableRef"`

// The BigtableSchemaBundle name. If not given, the metadata.name will be used.
ResourceID *string `json:"resourceID,omitempty"`

// Optional. The protobuf schema for the table.
// +optional
ProtoSchema *ProtoSchema `json:"protoSchema,omitempty"`
}

// ProtoSchema contains a protobuf-serialized FileDescriptorSet.
type ProtoSchema struct {
// Optional. The protobuf schema descriptor.
// The schema descriptor must be a file descriptor set, which can be generated using `protoc --descriptor_set_out=myschema.fd myschema.proto`.
// The file descriptor set must be base64 encoded.
// +optional
ProtoDescriptors []byte `json:"protoDescriptors,omitempty"`
}

// BigtableSchemaBundleStatus defines the config connector machine state of BigtableSchemaBundle
type BigtableSchemaBundleStatus struct {
/* Conditions represent the latest available observations of the
object's current state. */
Conditions []k8sv1alpha1.Condition `json:"conditions,omitempty"`

// ObservedGeneration is the generation of the resource that was most recently observed by the Config Connector controller. If this is equal to metadata.generation, then that means that the current reported status reflects the most recent desired state of the resource.
ObservedGeneration *int64 `json:"observedGeneration,omitempty"`

// A unique specifier for the BigtableSchemaBundle resource in GCP.
ExternalRef *string `json:"externalRef,omitempty"`

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

// BigtableSchemaBundleObservedState is the state of the BigtableSchemaBundle resource as most recently observed in GCP.
// +kcc:observedstate:proto=google.bigtable.admin.v2.SchemaBundle
type BigtableSchemaBundleObservedState struct {
// Optional. The etag for this schema bundle.
Etag *string `json:"etag,omitempty"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:categories=gcp,shortName=gcpbigtableschemabundle;gcpbigtableschemabundles
// +kubebuilder:subresource:status
// +kubebuilder:metadata:labels="cnrm.cloud.google.com/managed-by-kcc=true"
// +kubebuilder:metadata:labels="cnrm.cloud.google.com/system=true"
// +kubebuilder:metadata:annotations="cnrm.cloud.google.com/owner=fkc1e100/Frank Currie/fcurrie"
// +kubebuilder:printcolumn:name="Age",JSONPath=".metadata.creationTimestamp",type="date"
// +kubebuilder:printcolumn:name="Ready",JSONPath=".status.conditions[?(@.type=='Ready')].status",type="string",description="When 'True', the most recent reconcile of the resource succeeded"
// +kubebuilder:printcolumn:name="Status",JSONPath=".status.conditions[?(@.type=='Ready')].reason",type="string",description="The reason for the value in 'Ready'"
// +kubebuilder:printcolumn:name="Status Age",JSONPath=".status.conditions[?(@.type=='Ready')].lastTransitionTime",type="date",description="The last transition time for the value in 'Status'"

// BigtableSchemaBundle is the Schema for the BigtableSchemaBundle API
// +k8s:openapi-gen=true
type BigtableSchemaBundle struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// +required
Spec BigtableSchemaBundleSpec `json:"spec,omitempty"`
Status BigtableSchemaBundleStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// BigtableSchemaBundleList contains a list of BigtableSchemaBundle
type BigtableSchemaBundleList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []BigtableSchemaBundle `json:"items"`
}

func init() {
SchemeBuilder.Register(&BigtableSchemaBundle{}, &BigtableSchemaBundleList{})
}
2 changes: 2 additions & 0 deletions apis/bigtable/v1alpha1/types.generated.go

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

Loading
Loading