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

refactor: new new new ref style #3403

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
113 changes: 113 additions & 0 deletions apis/bigquery/v1beta1/dataset_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// 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"
"strings"

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

// DatasetIdentity defines the resource reference to BigQueryDataset, which "External" field
// holds the GCP identifier for the KRM object.
type DatasetIdentity struct {
parent *DatasetParent
id string
}

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

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

func (i *DatasetIdentity) Parent() *DatasetParent {
return i.parent
}

type DatasetParent struct {
ProjectID string
}

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

// New builds a DatasetIdentity from the Config Connector Dataset object.
func NewDatasetIdentity(ctx context.Context, reader client.Reader, obj *BigQueryDataset) (*DatasetIdentity, error) {

// Get Parent
projectRef, err := refsv1beta1.ResolveProject(ctx, reader, obj.GetNamespace(), obj.Spec.ProjectRef)
if err != nil {
return nil, err
}
projectID := projectRef.ProjectID
if projectID == "" {
return nil, fmt.Errorf("cannot resolve project")
}

// 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 != "" {
// Validate desired with actual
actualParent, actualResourceID, err := ParseDatasetExternal(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)
}
}
return &DatasetIdentity{
parent: &DatasetParent{
ProjectID: projectID,
},
id: resourceID,
}, nil
}

func ParseDatasetExternal(external string) (parent *DatasetParent, resourceID string, err error) {
tokens := strings.Split(external, "/")

if len(tokens) != 4 || tokens[0] != "projects" || tokens[2] != "datasets" {
return nil, "", fmt.Errorf("format of BigQueryDataset external=%q was not known (use projects/<projectId>/datasets/<datasetID>)", external)
}
parent = &DatasetParent{
ProjectID: tokens[1],
}

resourceID = tokens[3]

return parent, resourceID, nil
}
111 changes: 6 additions & 105 deletions apis/bigquery/v1beta1/dataset_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package v1beta1
import (
"context"
"fmt"
"strings"

refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
Expand All @@ -27,34 +26,32 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ refsv1beta1.ExternalNormalizer = &BigQueryDatasetRef{}
var _ refsv1beta1.ExternalNormalizer = &DatasetRef{}

// BigQueryDatasetRef defines the resource reference to BigQueryDataset, which "External" field
// DatasetRef defines the resource reference to BigQueryDataset, which "External" field
// holds the GCP identifier for the KRM object.
type BigQueryDatasetRef struct {
type DatasetRef struct {
// A reference to an externally managed BigQueryDataset resource.
// Should be in the format "projects/{{projectID}}/locations/{{location}}/datasets/{{datasetID}}".
// Should be in the format "projects/<projectID>/datasets/<datasetID>".
External string `json:"external,omitempty"`

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

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

parent *BigQueryDatasetParent
}

// NormalizedExternal provision the "External" value for other resource that depends on BigQueryDataset.
// If the "External" is given in the other resource's spec.BigQueryDatasetRef, the given value will be used.
// Otherwise, the "Name" and "Namespace" will be used to query the actual BigQueryDataset object from the cluster.
func (r *BigQueryDatasetRef) NormalizedExternal(ctx context.Context, reader client.Reader, otherNamespace string) (string, error) {
func (r *DatasetRef) 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", BigQueryDatasetGVK.Kind)
}
// From given External
if r.External != "" {
if _, _, err := ParseBigQueryDatasetExternal(r.External); err != nil {
if _, _, err := ParseDatasetExternal(r.External); err != nil {
return "", err
}
return r.External, nil
Expand Down Expand Up @@ -84,99 +81,3 @@ func (r *BigQueryDatasetRef) NormalizedExternal(ctx context.Context, reader clie
r.External = actualExternalRef
return r.External, nil
}

// New builds a BigQueryDatasetRef from the Config Connector BigQueryDataset object.
func NewBigQueryDatasetRef(ctx context.Context, reader client.Reader, obj *BigQueryDataset) (*BigQueryDatasetRef, error) {
id := &BigQueryDatasetRef{}

// Get Parent
projectRef, err := refsv1beta1.ResolveProject(ctx, reader, obj.GetNamespace(), obj.Spec.ProjectRef)
if err != nil {
return nil, err
}
projectID := projectRef.ProjectID
if projectID == "" {
return nil, fmt.Errorf("cannot resolve project")
}
id.parent = &BigQueryDatasetParent{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 = asBigQueryDatasetExternal(id.parent, resourceID)
return id, nil
}

// Validate desired with actual
actualParent, actualResourceID, err := ParseBigQueryDatasetExternal(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 = &BigQueryDatasetParent{ProjectID: projectID}
return id, nil
}

func (r *BigQueryDatasetRef) Parent() (*BigQueryDatasetParent, error) {
if r.parent != nil {
return r.parent, nil
}
if r.External != "" {
parent, _, err := ParseBigQueryDatasetExternal(r.External)
if err != nil {
return nil, err
}
return parent, nil
}
return nil, fmt.Errorf("BigQueryDatasetRef not initialized from `NewBigQueryDatasetRef` or `NormalizedExternal`")
}

type BigQueryDatasetParent struct {
ProjectID string
}

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

func asBigQueryDatasetExternal(parent *BigQueryDatasetParent, resourceID string) (external string) {
// Link Reference https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/get
return parent.String() + "/datasets/" + resourceID
}

func ParseBigQueryDatasetExternal(external string) (parent *BigQueryDatasetParent, resourceID string, err error) {
external = strings.TrimPrefix(external, "/")
tokens := strings.Split(external, "/")
if len(tokens) != 4 || tokens[0] != "projects" || tokens[2] != "datasets" {
return nil, "", fmt.Errorf("format of BigQueryDataset external=%q was not known (use projects/{{projectID}}/datasets/{{datasetID}})", external)
}
parent = &BigQueryDatasetParent{
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 *t
}
85 changes: 50 additions & 35 deletions apis/bigquery/v1beta1/zz_generated.deepcopy.go

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

Loading
Loading