Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into apigee-envgroup-2
Browse files Browse the repository at this point in the history
  • Loading branch information
Camila-B committed Dec 18, 2024
2 parents fa3d6c7 + 4dffbd6 commit 2e6f323
Show file tree
Hide file tree
Showing 420 changed files with 40,250 additions and 276,322 deletions.
6 changes: 3 additions & 3 deletions apis/bigquery/v1beta1/dataset_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var _ refsv1beta1.ExternalNormalizer = &BigQueryDatasetRef{}
// holds the GCP identifier for the KRM object.
type BigQueryDatasetRef 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}}/locations/{{location}}/datasets/{{datasetID}}".
External string `json:"external,omitempty"`

// The name of a BigQueryDataset resource.
Expand Down Expand Up @@ -90,7 +90,7 @@ func NewBigQueryDatasetRef(ctx context.Context, reader client.Reader, obj *BigQu
id := &BigQueryDatasetRef{}

// Get Parent
projectRef, err := refsv1beta1.ResolveProject(ctx, reader, obj, obj.Spec.ProjectRef)
projectRef, err := refsv1beta1.ResolveProject(ctx, reader, obj.GetNamespace(), obj.Spec.ProjectRef)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func ParseBigQueryDatasetExternal(external string) (parent *BigQueryDatasetParen
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)
return nil, "", fmt.Errorf("format of BigQueryDataset external=%q was not known (use projects/{{projectID}}/datasets/{{datasetID}})", external)
}
parent = &BigQueryDatasetParent{
ProjectID: tokens[1],
Expand Down
6 changes: 3 additions & 3 deletions apis/bigqueryanalyticshub/v1alpha1/dataexchange_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var _ refsv1beta1.ExternalNormalizer = &BigQueryAnalyticsHubDataExchangeRef{}
// holds the GCP identifier for the KRM object.
type BigQueryAnalyticsHubDataExchangeRef struct {
// A reference to an externally managed BigQueryAnalyticsHubDataExchange resource.
// Should be in the format "projects/<projectID>/locations/<location>/dataexchanges/<dataexchangeID>".
// Should be in the format "projects/{{projectID}}/locations/{{location}}/dataexchanges/{{dataexchangeID}}".
External string `json:"external,omitempty"`

// The name of a BigQueryAnalyticsHubDataExchange resource.
Expand Down Expand Up @@ -90,7 +90,7 @@ func NewBigQueryAnalyticsHubDataExchangeRef(ctx context.Context, reader client.R
id := &BigQueryAnalyticsHubDataExchangeRef{}

// Get Parent
projectRef, err := refsv1beta1.ResolveProject(ctx, reader, obj, obj.Spec.ProjectRef)
projectRef, err := refsv1beta1.ResolveProject(ctx, reader, obj.GetNamespace(), obj.Spec.ProjectRef)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func parseBigQueryAnalyticsHubDataExchangeExternal(external string) (parent *Big
external = strings.TrimPrefix(external, "/")
tokens := strings.Split(external, "/")
if len(tokens) != 6 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "dataexchange" {
return nil, "", fmt.Errorf("format of BigQueryAnalyticsHubDataExchange external=%q was not known (use projects/<projectId>/locations/<location>/dataexchanges/<dataexchangeID>)", external)
return nil, "", fmt.Errorf("format of BigQueryAnalyticsHubDataExchange external=%q was not known (use projects/{{projectID}}/locations/{{location}}/dataexchanges/{{dataexchangeID}})", external)
}
parent = &BigQueryAnalyticsHubDataExchangeParent{
ProjectID: tokens[1],
Expand Down
6 changes: 3 additions & 3 deletions apis/bigqueryanalyticshub/v1alpha1/listing_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var _ refsv1beta1.ExternalNormalizer = &BigQueryAnalyticsHubListingRef{}
// holds the GCP identifier for the KRM object.
type BigQueryAnalyticsHubListingRef struct {
// A reference to an externally managed BigQueryAnalyticsHubListing resource.
// Should be in the format "projects/<projectID>/locations/<location>/listings/<listingID>".
// Should be in the format "projects/{{projectID}}/locations/{{location}}/listings/{{listingID}}".
External string `json:"external,omitempty"`

// The name of a BigQueryAnalyticsHubListing resource.
Expand Down Expand Up @@ -91,7 +91,7 @@ func NewBigQueryAnalyticsHubListingRef(ctx context.Context, reader client.Reader
id := &BigQueryAnalyticsHubListingRef{}

// Get Parent
projectRef, err := refsv1beta1.ResolveProject(ctx, reader, obj, obj.Spec.ProjectRef)
projectRef, err := refsv1beta1.ResolveProject(ctx, reader, obj.GetNamespace(), obj.Spec.ProjectRef)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func parseBigQueryAnalyticsHubListingExternal(external string) (parent *BigQuery
external = strings.TrimPrefix(external, "/")
tokens := strings.Split(external, "/")
if len(tokens) != 8 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "dataExchanges" || tokens[6] != "listings" {
return nil, "", fmt.Errorf("format of BigQueryAnalyticsHubListing external=%q was not known (use projects/<projectID>/locations/<location>/dataExchanges/<dataExchangeID>/listings/<listingID>)", external)
return nil, "", fmt.Errorf("format of BigQueryAnalyticsHubListing external=%q was not known (use projects/{{projectID}}/locations/{{location}}/dataExchanges/{{dataExchangeID}}/listings/{{listingID}})", external)
}
parent = &BigQueryAnalyticsHubListingParent{
ProjectID: tokens[1],
Expand Down
7 changes: 2 additions & 5 deletions apis/bigqueryanalyticshub/v1alpha1/listing_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ type SelectedResource struct {
// Format:
// `projects/{projectId}/datasets/{datasetId}/tables/{tableId}`
// Example:"projects/test_project/datasets/test_dataset/tables/test_table"
TableRef *refv1beta1.BigQueryTableRef `json:"table,omitempty"`
TableRef *refv1beta1.BigQueryTableRef `json:"tableRef,omitempty"`
}

type BigQueryDatasetSource struct {
// +required
// Resource name of the dataset source for this listing.
// e.g. `projects/myproject/datasets/123`
Dataset *refv1beta1.BigQueryDatasetRef `json:"datasetRef,omitempty"`
DatasetRef *refv1beta1.BigQueryDatasetRef `json:"datasetRef,omitempty"`

// Optional. Resources in this dataset that are selectively shared.
// If this field is empty, then the entire dataset (all resources) are
Expand Down Expand Up @@ -127,9 +127,6 @@ type BigQueryAnalyticsHubListingSpec struct {
// // increases the size of the data by 33-36%) when using JSON on the wire.
// Icon []byte `json:"icon,omitempty"`

// Not yet
// Source *Listing_Source `json:"source,omitempty"`

/* Immutable. The name of the location this data exchange. */
// +required
Location string `json:"location"`
Expand Down
4 changes: 2 additions & 2 deletions apis/bigqueryanalyticshub/v1alpha1/zz_generated.deepcopy.go

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

4 changes: 2 additions & 2 deletions apis/bigqueryanalyticshub/v1beta1/dataexchange_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var _ refsv1beta1.ExternalNormalizer = &BigQueryAnalyticsHubDataExchangeRef{}
// holds the GCP identifier for the KRM object.
type BigQueryAnalyticsHubDataExchangeRef struct {
// A reference to an externally managed BigQueryAnalyticsHubDataExchange resource.
// Should be in the format "projects/<projectID>/locations/<location>/dataexchanges/<dataexchangeID>".
// Should be in the format "projects/{{projectID}}/locations/{{ocation}}/dataexchanges/{{dataexchangeID}}".
External string `json:"external,omitempty"`

// The name of a BigQueryAnalyticsHubDataExchange resource.
Expand Down Expand Up @@ -87,7 +87,7 @@ func ParseDataExchangeIdentity(external string) (identity *DataExchangeIdentity,
external = strings.TrimPrefix(external, "/")
tokens := strings.Split(external, "/")
if len(tokens) != 6 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "dataExchanges" {
return nil, fmt.Errorf("format of BigQueryAnalyticsHubDataExchange external=%q was not known (use projects/<projectId>/locations/<location>/dataExchanges/<dataExchangeID>)", external)
return nil, fmt.Errorf("format of BigQueryAnalyticsHubDataExchange external=%q was not known (use projects/{{projectId}}/locations/{{location}}/dataExchanges/{{dataExchangeID}})", external)
}

return &DataExchangeIdentity{
Expand Down
214 changes: 214 additions & 0 deletions apis/bigqueryanalyticshub/v1beta1/listing_reference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
// 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"

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 = &BigQueryAnalyticsHubListingRef{}

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

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

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

parent *BigQueryAnalyticsHubListingParent
}

// NormalizedExternal provision the "External" value for other resource that depends on BigQueryAnalyticsHubListing.
// If the "External" is given in the other resource's spec.BigQueryAnalyticsHubListingRef, the given value will be used.
// Otherwise, the "Name" and "Namespace" will be used to query the actual BigQueryAnalyticsHubListing object from the cluster.
func (r *BigQueryAnalyticsHubListingRef) 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", BigQueryAnalyticsHubListingGVK.Kind)
}
// From given External
if r.External != "" {
if _, _, err := parseBigQueryAnalyticsHubListingExternal(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(BigQueryAnalyticsHubListingGVK)
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", BigQueryAnalyticsHubListingGVK, 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 "", fmt.Errorf("BigQueryAnalyticsHubListing is not ready yet")
}
r.External = actualExternalRef
return r.External, nil
}

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

// 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")
}
location := obj.Spec.Location
if location == "" {
return nil, fmt.Errorf("location cannot be empty")
}

if obj.Spec.DataExchangeRef == nil {
return nil, fmt.Errorf("spec.dataExchangeRef cannot be empty")
}
dataExchangeExternal, err := obj.Spec.DataExchangeRef.NormalizedExternal(ctx, reader, obj.Namespace)
if err != nil {
return nil, fmt.Errorf("cannot normalize dataExchangeRef for listing ref: %w", err)
}

dataExchangeID, err := ParseDataExchangeIdentity(dataExchangeExternal)
if err != nil {
return nil, fmt.Errorf("cannot parse dataExchangeRef for listing ref: %w", err)
}

deID := dataExchangeID.ID()
id.parent = &BigQueryAnalyticsHubListingParent{ProjectID: projectID, Location: location, DataExchangeID: deID}

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

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

// Validate desired with actual
actualParent, actualResourceID, err := parseBigQueryAnalyticsHubListingExternal(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 actualParent.Location != location {
return nil, fmt.Errorf("spec.location changed, expect %s, got %s", actualParent.Location, location)
}
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)
}
if actualParent.DataExchangeID != deID {
return nil, fmt.Errorf("spec.dataExchangeRef changed, expect %s, got %s", actualParent.DataExchangeID, deID)
}

id.External = externalRef
id.parent = &BigQueryAnalyticsHubListingParent{ProjectID: projectID, Location: location, DataExchangeID: deID}
return id, nil
}

func (r *BigQueryAnalyticsHubListingRef) Parent() (*BigQueryAnalyticsHubListingParent, error) {
if r.parent != nil {
return r.parent, nil
}
if r.External != "" {
parent, _, err := parseBigQueryAnalyticsHubListingExternal(r.External)
if err != nil {
return nil, err
}
return parent, nil
}
return nil, fmt.Errorf("BigQueryAnalyticsHubListingRef not initialized from `NewBigQueryAnalyticsHubListingRef` or `NormalizedExternal`")
}

type BigQueryAnalyticsHubListingParent struct {
ProjectID string
Location string
DataExchangeID string
}

func (p *BigQueryAnalyticsHubListingParent) String() string {
return "projects/" + p.ProjectID + "/locations/" + p.Location + "/dataExchanges/" + p.DataExchangeID
}

func asBigQueryAnalyticsHubListingExternal(parent *BigQueryAnalyticsHubListingParent, resourceID string) (external string) {
return parent.String() + "/listings/" + resourceID
}

func parseBigQueryAnalyticsHubListingExternal(external string) (parent *BigQueryAnalyticsHubListingParent, resourceID string, err error) {
external = strings.TrimPrefix(external, "/")
tokens := strings.Split(external, "/")
if len(tokens) != 8 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "dataExchanges" || tokens[6] != "listings" {
return nil, "", fmt.Errorf("format of BigQueryAnalyticsHubListing external=%q was not known (use projects/<projectID>/locations/<location>/dataExchanges/<dataExchangeID>/listings/<listingID>)", external)
}
parent = &BigQueryAnalyticsHubListingParent{
ProjectID: tokens[1],
Location: tokens[3],
DataExchangeID: tokens[5],
}
resourceID = tokens[7]
return parent, resourceID, nil
}

func valueOf[T any](t *T) T {
var zeroVal T
if t == nil {
return zeroVal
}
return *t
}
Loading

0 comments on commit 2e6f323

Please sign in to comment.