Skip to content

Commit

Permalink
Merge pull request #2739 from 600lyy/mockgcp-bqcc-cover-all
Browse files Browse the repository at this point in the history
support bigqueryconnection cloudsql connection
  • Loading branch information
google-oss-prow[bot] authored Sep 21, 2024
2 parents a1ffee0 + a487086 commit 65a7c9c
Show file tree
Hide file tree
Showing 13 changed files with 2,280 additions and 15 deletions.
32 changes: 28 additions & 4 deletions apis/bigqueryconnection/v1alpha1/connection_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ type BigQueryConnectionConnectionSpec struct {
// User provided description.
Description *string `json:"description,omitempty"`

/* NOTYET
// Cloud SQL properties.
CloudSql *CloudSqlProperties `json:"cloudSql,omitempty"`
*/
CloudSQLSpec *CloudSqlPropertiesSpec `json:"cloudSql,omitempty"`

/* NOTYET
// Amazon Web Services (AWS) properties.
Expand Down Expand Up @@ -105,6 +103,8 @@ type BigQueryConnectionConnectionStatus struct {
type BigQueryConnectionConnectionObservedState struct {
CloudResource *CloudResourcePropertiesStatus `json:"cloudResource,omitempty"`

CloudSql *CloudSqlPropertiesStatus `json:"cloudSql,omitempty"`

// The display name for the connection.
FriendlyName *string `json:"friendlyName,omitempty"`

Expand Down Expand Up @@ -139,9 +139,33 @@ type BigQueryConnectionConnectionObservedState struct {

type CloudResourcePropertiesSpec struct{}

type CloudSqlPropertiesSpec struct {
// Reference to the Cloud SQL instance ID.
InstanceRef *refv1beta1.SQLInstanceRef `json:"instanceRef,omitempty"`

// Database name.
Database *string `json:"database,omitempty"`

// Type of the Cloud SQL database.
Type *string `json:"type,omitempty"`

// Cloud SQL credential.
Credential *CloudSqlCredential `json:"credential,omitempty"`
}

// +kcc:proto=google.cloud.bigquery.connection.v1.CloudSqlProperties
type CloudSqlPropertiesStatus struct {
// The account ID of the service used for the purpose of this connection.
//
// When the connection is used in the context of an operation in
// BigQuery, this service account will serve as the identity being used for
// connecting to the CloudSQL instance specified in this connection.
ServiceAccountID *string `json:"serviceAccountID,omitempty"`
}

// +kcc:proto=google.cloud.bigquery.connection.v1.CloudResourceProperties
type CloudResourcePropertiesStatus struct {
// Output only. The account ID of the service created for the purpose of this
// The account ID of the service created for the purpose of this
// connection.
//
// The service account does not have any permissions associated with it
Expand Down
65 changes: 65 additions & 0 deletions apis/bigqueryconnection/v1alpha1/zz_generated.deepcopy.go

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

97 changes: 97 additions & 0 deletions apis/refs/v1beta1/sqlinstanceref.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@

package v1beta1

import (
"context"
"fmt"
"strings"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type SQLInstanceRef struct {
/* The SQLInstance selfLink, when not managed by Config Connector. */
External string `json:"external,omitempty"`
Expand All @@ -22,3 +34,88 @@ type SQLInstanceRef struct {
/* The `namespace` field of a `SQLInstance` resource. */
Namespace string `json:"namespace,omitempty"`
}

type SQLInstance struct {
ProjectID string
Location string
SQLInstanceName string
}

func (s *SQLInstance) String() string {
return "projects/" + s.ProjectID + "locations/" + s.Location + "/instances/" + s.SQLInstanceName
}

func (s *SQLInstance) ConnectionName() string {
return s.ProjectID + ":" + s.Location + ":" + s.SQLInstanceName
}

func ResolveSQLInstanceRef(ctx context.Context, reader client.Reader, obj client.Object, ref *SQLInstanceRef) (*SQLInstance, error) {
if ref == nil {
return nil, nil
}

if ref.Name == "" && ref.External == "" {
return nil, fmt.Errorf("must specify either name or external on instanceRef")
}
if ref.External != "" && ref.Name != "" {
return nil, fmt.Errorf("cannot specify both spec.instanceRef.name and spec.instanceRef.external")
}

if ref.External != "" {
// External should be in the `projects/[projectID]/locations/[Location]/instances/[instanceName]` format.
tokens := strings.Split(ref.External, "/")
if len(tokens) == 6 && tokens[0] == "projects" && tokens[2] == "locations" && tokens[4] == "instances" {
return &SQLInstance{
ProjectID: tokens[1],
Location: tokens[3],
SQLInstanceName: tokens[5],
}, nil
}
return nil, fmt.Errorf("format of sqlinstance external=%q was not known (use projects/<projectId>/locations/[Location]/instances/<instanceName>)", ref.External)
}

key := types.NamespacedName{
Namespace: ref.Namespace,
Name: ref.Name,
}
if key.Namespace == "" {
key.Namespace = obj.GetNamespace()
}

sqlinstance := &unstructured.Unstructured{}
sqlinstance.SetGroupVersionKind(schema.GroupVersionKind{
Group: "sql.cnrm.cloud.google.com",
Version: "v1beta1",
Kind: "SQLInstance",
})
if err := reader.Get(ctx, key, sqlinstance); err != nil {
if apierrors.IsNotFound(err) {
return nil, fmt.Errorf("referenced SQLInstance %v not found", key)
}
return nil, fmt.Errorf("error reading referenced SQLInstance %v: %w", key, err)
}

resourceID, _, err := unstructured.NestedString(sqlinstance.Object, "spec", "resourceID")
if err != nil {
return nil, fmt.Errorf("reading spec.resourceID from SQLInstance %s/%s: %w", sqlinstance.GetNamespace(), sqlinstance.GetName(), err)
}
if resourceID == "" {
resourceID = sqlinstance.GetName()
}

location, _, err := unstructured.NestedString(sqlinstance.Object, "spec", "region")
if err != nil {
return nil, fmt.Errorf("reading spec.region from SQLInstance %s/%s: %w", sqlinstance.GetNamespace(), sqlinstance.GetName(), err)
}

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

return &SQLInstance{
ProjectID: projectID,
Location: location,
SQLInstanceName: resourceID,
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,54 @@ spec:
cloudResource:
description: Use Cloud Resource properties.
type: object
cloudSql:
description: Cloud SQL properties.
properties:
credential:
description: Cloud SQL credential.
properties:
password:
description: The password for the credential.
type: string
username:
description: The username for the credential.
type: string
type: object
database:
description: Database name.
type: string
instanceRef:
description: Reference to the Cloud SQL instance ID.
oneOf:
- not:
required:
- external
required:
- name
- not:
anyOf:
- required:
- name
- required:
- namespace
required:
- external
properties:
external:
description: The SQLInstance selfLink, when not managed by
Config Connector.
type: string
name:
description: The `name` field of a `SQLInstance` resource.
type: string
namespace:
description: The `namespace` field of a `SQLInstance` resource.
type: string
type: object
type:
description: Type of the Cloud SQL database.
type: string
type: object
description:
description: User provided description.
type: string
Expand Down Expand Up @@ -167,8 +215,8 @@ spec:
cloudResource:
properties:
serviceAccountID:
description: |-
Output only. The account ID of the service created for the purpose of this
description: |2-
The account ID of the service created for the purpose of this
connection.
The service account does not have any permissions associated with it
Expand All @@ -181,6 +229,17 @@ spec:
<service-1234>@gcp-sa-bigquery-cloudresource.iam.gserviceaccount.com
type: string
type: object
cloudSql:
properties:
serviceAccountID:
description: |-
The account ID of the service used for the purpose of this connection.
When the connection is used in the context of an operation in
BigQuery, this service account will serve as the identity being used for
connecting to the CloudSQL instance specified in this connection.
type: string
type: object
description:
description: The description for the connection.
type: string
Expand Down
Loading

0 comments on commit 65a7c9c

Please sign in to comment.