Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#2993 from acpana/acpana/listin…
Browse files Browse the repository at this point in the history
…g-ctrl

feat: listing direct ctrl
  • Loading branch information
google-oss-prow[bot] authored Nov 26, 2024
2 parents cf8e50c + 4556cde commit 28df5a4
Show file tree
Hide file tree
Showing 24 changed files with 1,774 additions and 318 deletions.
179 changes: 179 additions & 0 deletions apis/bigqueryanalyticshub/v1alpha1/dataexchange_reference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// 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 v1alpha1

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

// BigQueryAnalyticsHubDataExchangeRef defines the resource reference to BigQueryAnalyticsHubDataExchange, which "External" field
// 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>".
External string `json:"external,omitempty"`

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

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

parent *BigQueryAnalyticsHubDataExchangeParent
}

// NormalizedExternal provision the "External" value for other resource that depends on BigQueryAnalyticsHubDataExchange.
// If the "External" is given in the other resource's spec.BigQueryAnalyticsHubDataExchangeRef, the given value will be used.
// Otherwise, the "Name" and "Namespace" will be used to query the actual BigQueryAnalyticsHubDataExchange object from the cluster.
func (r *BigQueryAnalyticsHubDataExchangeRef) 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", BigQueryAnalyticsHubDataExchangeGVK.Kind)
}
// From given External
if r.External != "" {
if _, _, err := parseBigQueryAnalyticsHubDataExchangeExternal(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(BigQueryAnalyticsHubDataExchangeGVK)
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", BigQueryAnalyticsHubDataExchangeGVK, 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
}

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

// Get Parent
projectRef, err := refsv1beta1.ResolveProject(ctx, reader, obj, 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
id.parent = &BigQueryAnalyticsHubDataExchangeParent{ProjectID: projectID, Location: location}

// 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 = asBigQueryAnalyticsHubDataExchangeExternal(id.parent, resourceID)
return id, nil
}

// Validate desired with actual
actualParent, actualResourceID, err := parseBigQueryAnalyticsHubDataExchangeExternal(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)
}
id.External = externalRef
id.parent = &BigQueryAnalyticsHubDataExchangeParent{ProjectID: projectID, Location: location}
return id, nil
}

func (r *BigQueryAnalyticsHubDataExchangeRef) Parent() (*BigQueryAnalyticsHubDataExchangeParent, error) {
if r.parent != nil {
return r.parent, nil
}
if r.External != "" {
parent, _, err := parseBigQueryAnalyticsHubDataExchangeExternal(r.External)
if err != nil {
return nil, err
}
return parent, nil
}
return nil, fmt.Errorf("BigQueryAnalyticsHubDataExchangeRef not initialized from `NewBigQueryAnalyticsHubDataExchangeRef` or `NormalizedExternal`")
}

type BigQueryAnalyticsHubDataExchangeParent struct {
ProjectID string
Location string
}

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

func asBigQueryAnalyticsHubDataExchangeExternal(parent *BigQueryAnalyticsHubDataExchangeParent, resourceID string) (external string) {
return parent.String() + "/dataexchanges/" + resourceID
}

func parseBigQueryAnalyticsHubDataExchangeExternal(external string) (parent *BigQueryAnalyticsHubDataExchangeParent, resourceID string, err error) {
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)
}
parent = &BigQueryAnalyticsHubDataExchangeParent{
ProjectID: tokens[1],
Location: tokens[3],
}
resourceID = tokens[5]
return parent, resourceID, nil
}
38 changes: 29 additions & 9 deletions apis/bigqueryanalyticshub/v1alpha1/listing_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"strings"

v1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/bigqueryanalyticshub/v1beta1"
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"
Expand Down Expand Up @@ -99,7 +100,24 @@ func NewBigQueryAnalyticsHubListingRef(ctx context.Context, reader client.Reader
return nil, fmt.Errorf("cannot resolve project")
}
location := obj.Spec.Location
id.parent = &BigQueryAnalyticsHubListingParent{ProjectID: projectID, Location: 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 := v1beta1.ParseDataExchangeIdentity(dataExchangeExternal)
if err != nil {
return nil, fmt.Errorf("cannot parse dataExchangeRef for listing ref: %w", err)
}

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

// Get desired ID
resourceID := valueOf(obj.Spec.ResourceID)
Expand Down Expand Up @@ -152,12 +170,13 @@ func (r *BigQueryAnalyticsHubListingRef) Parent() (*BigQueryAnalyticsHubListingP
}

type BigQueryAnalyticsHubListingParent struct {
ProjectID string
Location string
ProjectID string
Location string
DataExchangeID string
}

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

func asBigQueryAnalyticsHubListingExternal(parent *BigQueryAnalyticsHubListingParent, resourceID string) (external string) {
Expand All @@ -167,14 +186,15 @@ func asBigQueryAnalyticsHubListingExternal(parent *BigQueryAnalyticsHubListingPa
func parseBigQueryAnalyticsHubListingExternal(external string) (parent *BigQueryAnalyticsHubListingParent, resourceID string, err error) {
external = strings.TrimPrefix(external, "/")
tokens := strings.Split(external, "/")
if len(tokens) != 6 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "listing" {
return nil, "", fmt.Errorf("format of BigQueryAnalyticsHubListing external=%q was not known (use projects/<projectId>/locations/<location>/listings/<listingID>)", 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],
ProjectID: tokens[1],
Location: tokens[3],
DataExchangeID: tokens[5],
}
resourceID = tokens[5]
resourceID = tokens[7]
return parent, resourceID, nil
}

Expand Down
34 changes: 30 additions & 4 deletions apis/bigqueryanalyticshub/v1alpha1/listing_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@
package v1alpha1

import (
v1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/bigqueryanalyticshub/v1beta1"

refv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/k8s/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var BigQueryAnalyticsHubListingGVK = GroupVersion.WithKind("BigQueryAnalyticsHubDataExchangeListing")
var BigQueryAnalyticsHubListingGVK = GroupVersion.WithKind("BigQueryAnalyticsHubListing")

// +kcc:proto=google.cloud.bigquery.analyticshub.v1.Listing.BigQueryDatasetSource.SelectedResource
type SelectedResource struct {
// Optional. A reference to a BigQueryTable.
// Format:
// `projects/{projectId}/datasets/{datasetId}/tables/{tableId}`
// Example:"projects/test_project/datasets/test_dataset/tables/test_table"
TableRef *refv1beta1.BigQueryTableRef `json:"table,omitempty"`
}

type BigQueryDatasetSource struct {
// +required
Expand All @@ -31,12 +42,27 @@ type BigQueryDatasetSource struct {
// Optional. Resources in this dataset that are selectively shared.
// If this field is empty, then the entire dataset (all resources) are
// shared. This field is only valid for data clean room exchanges.
SelectedResources []Listing_BigQueryDatasetSource_SelectedResource `json:"selectedResources,omitempty"`
SelectedResources []SelectedResource `json:"selectedResources,omitempty"`

// Optional. If set, restricted export policy will be propagated and
// enforced on the linked dataset.
RestrictedExportPolicy *Listing_BigQueryDatasetSource_RestrictedExportPolicy `json:"restrictedExportPolicy,omitempty"`
RestrictedExportPolicy *RestrictedExportPolicy `json:"restrictedExportPolicy,omitempty"`
}

// +kcc:proto=google.cloud.bigquery.analyticshub.v1.Listing.BigQueryDatasetSource.RestrictedExportPolicy
type RestrictedExportPolicy struct {
// Optional. If true, enable restricted export.
Enabled *bool `json:"enabled,omitempty"`

// Optional. If true, restrict direct table access (read
// api/tabledata.list) on linked table.
RestrictDirectTableAccess *bool `json:"restrictDirectTableAccess,omitempty"`

// Optional. If true, restrict export of query result derived from
// restricted linked dataset table.
RestrictQueryResult *bool `json:"restrictQueryResult,omitempty"`
}

type Source struct {
// One of the following fields must be set.
BigQueryDatasetSource *BigQueryDatasetSource `json:"bigQueryDatasetSource,omitempty"`
Expand Down Expand Up @@ -112,7 +138,7 @@ type BigQueryAnalyticsHubListingSpec struct {
ProjectRef *refv1beta1.ProjectRef `json:"projectRef"`

// +required
DataExchangeRef *refv1beta1.DataExchangeRef `json:"dataExchangeRef"`
DataExchangeRef *v1beta1.BigQueryAnalyticsHubDataExchangeRef `json:"dataExchangeRef"`

// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="ResourceID field is immutable"
// Immutable.
Expand Down
27 changes: 2 additions & 25 deletions apis/bigqueryanalyticshub/v1alpha1/types.generated.go

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

Loading

0 comments on commit 28df5a4

Please sign in to comment.