Skip to content

Commit

Permalink
change the format of cloudSQL.database
Browse files Browse the repository at this point in the history
  • Loading branch information
600lyy committed Nov 6, 2024
1 parent 28f9901 commit 65ee2e7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 26 deletions.
25 changes: 12 additions & 13 deletions apis/refs/v1beta1/sqldatabaseref.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package v1beta1
import (
"context"
"fmt"
"strings"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -41,8 +40,15 @@ type SQLDatabase struct {
DatabaseID string
}

func (s *SQLDatabase) Name() string {
return s.DatabaseID
}

func (s *SQLDatabase) String() string {
return "projects/" + s.ProjectID + "/instances/" + s.InstanceID + "/databases/" + s.DatabaseID
if s.ProjectID != "" && s.InstanceID != "" {
return "projects/" + s.ProjectID + "/instances/" + s.InstanceID + "/databases/" + s.DatabaseID
}
return s.DatabaseID
}

func ResolveSQLDatabaseRef(ctx context.Context, reader client.Reader, obj client.Object, ref *SQLDatabaseRef) (*SQLDatabase, error) {
Expand All @@ -58,17 +64,10 @@ func ResolveSQLDatabaseRef(ctx context.Context, reader client.Reader, obj client
}

if ref.External != "" {
// External must be in form `projects/<projectID>/instances/<instanceName>/databases/<databaseName>`.
// see https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1/databases/get
tokens := strings.Split(ref.External, "/")
if len(tokens) == 6 && tokens[0] == "projects" && tokens[2] == "instances" && tokens[4] == "databases" {
return &SQLDatabase{
ProjectID: tokens[1],
InstanceID: tokens[3],
DatabaseID: tokens[5],
}, nil
}
return nil, fmt.Errorf("format of SQLinstance external=%q was not known (use projects/<projectID>/instances/<instanceName>/databases/<databaseName>)", ref.External)
// External is the name of the sql database
return &SQLDatabase{
DatabaseID: ref.External,
}, nil
}

key := types.NamespacedName{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (a *Adapter) normalizeReference(ctx context.Context) error {
if err != nil {
return err
}
sql.DatabaseRef.External = database.String()
sql.DatabaseRef.External = database.Name()
}
if sql.Credential != nil {
if err := refsv1beta1secret.NormalizedSecret(ctx, sql.Credential.SecretRef, a.reader, a.namespace); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ X-Xss-Protection: 0
"instance": "sqlinstance-sample-${uniqueId}",
"kind": "sql#user",
"name": "root",
"passwordPolicy": {
"status": {}
},
"project": "${projectId}"
}
],
Expand Down Expand Up @@ -224,6 +227,7 @@ X-Xss-Protection: 0
"etag": "abcdef0123A=",
"gceZone": "us-central1-a",
"geminiConfig": {
"activeQueryEnabled": false,
"entitled": false,
"flagRecommenderEnabled": false,
"indexAdvisorEnabled": false
Expand All @@ -236,7 +240,7 @@ X-Xss-Protection: 0
}
],
"kind": "sql#instance",
"maintenanceVersion": "MYSQL_5_7_44.R20231105.01_03",
"maintenanceVersion": "MYSQL_5_7_44.R20241020.00_00",
"name": "sqlinstance-sample-${uniqueId}",
"project": "${projectId}",
"region": "us-central1",
Expand Down Expand Up @@ -365,6 +369,11 @@ X-Xss-Protection: 0
"displayName": "MySQL 8.0.37",
"majorVersion": "MYSQL_8_0",
"name": "MYSQL_8_0_37"
},
{
"displayName": "MySQL 8.0.39",
"majorVersion": "MYSQL_8_0",
"name": "MYSQL_8_0_39"
}
]
}
Expand Down Expand Up @@ -392,12 +401,11 @@ X-Xss-Protection: 0
"errors": [
{
"domain": "global",
"message": "database \"projects/${projectId}/instances/sqlinstance-sample-${uniqueId}/databases/sqldatabase-sample-${uniqueId}\" not found",
"message": "Not Found.",
"reason": "notFound"
}
],
"message": "database \"projects/${projectId}/instances/sqlinstance-sample-${uniqueId}/databases/sqldatabase-sample-${uniqueId}\" not found",
"status": "NOT_FOUND"
"message": "Not Found"
}
}

Expand Down Expand Up @@ -541,6 +549,7 @@ X-Xss-Protection: 0
"etag": "abcdef0123A=",
"gceZone": "us-central1-a",
"geminiConfig": {
"activeQueryEnabled": false,
"entitled": false,
"flagRecommenderEnabled": false,
"indexAdvisorEnabled": false
Expand All @@ -553,7 +562,7 @@ X-Xss-Protection: 0
}
],
"kind": "sql#instance",
"maintenanceVersion": "MYSQL_5_7_44.R20231105.01_03",
"maintenanceVersion": "MYSQL_5_7_44.R20241020.00_00",
"name": "sqlinstance-sample-${uniqueId}",
"project": "${projectId}",
"region": "us-central1",
Expand Down Expand Up @@ -682,6 +691,11 @@ X-Xss-Protection: 0
"displayName": "MySQL 8.0.37",
"majorVersion": "MYSQL_8_0",
"name": "MYSQL_8_0_37"
},
{
"displayName": "MySQL 8.0.39",
"majorVersion": "MYSQL_8_0",
"name": "MYSQL_8_0_39"
}
]
}
Expand Down Expand Up @@ -779,6 +793,9 @@ X-Xss-Protection: 0
"kind": "sql#user",
"name": "sqluser-${uniqueId}",
"password": "cGFzc3dvcmQ=",
"passwordPolicy": {
"status": {}
},
"project": "${projectId}"
}
],
Expand Down Expand Up @@ -810,6 +827,7 @@ X-Xss-Protection: 0
"etag": "abcdef0123A=",
"gceZone": "us-central1-a",
"geminiConfig": {
"activeQueryEnabled": false,
"entitled": false,
"flagRecommenderEnabled": false,
"indexAdvisorEnabled": false
Expand All @@ -822,7 +840,7 @@ X-Xss-Protection: 0
}
],
"kind": "sql#instance",
"maintenanceVersion": "MYSQL_5_7_44.R20231105.01_03",
"maintenanceVersion": "MYSQL_5_7_44.R20241020.00_00",
"name": "sqlinstance-sample-${uniqueId}",
"project": "${projectId}",
"region": "us-central1",
Expand Down Expand Up @@ -951,6 +969,11 @@ X-Xss-Protection: 0
"displayName": "MySQL 8.0.37",
"majorVersion": "MYSQL_8_0",
"name": "MYSQL_8_0_37"
},
{
"displayName": "MySQL 8.0.39",
"majorVersion": "MYSQL_8_0",
"name": "MYSQL_8_0_39"
}
]
}
Expand Down Expand Up @@ -980,6 +1003,9 @@ X-Xss-Protection: 0
"kind": "sql#user",
"name": "sqluser-${uniqueId}",
"password": "cGFzc3dvcmQ=",
"passwordPolicy": {
"status": {}
},
"project": "${projectId}"
}
],
Expand Down Expand Up @@ -1011,6 +1037,7 @@ X-Xss-Protection: 0
"etag": "abcdef0123A=",
"gceZone": "us-central1-a",
"geminiConfig": {
"activeQueryEnabled": false,
"entitled": false,
"flagRecommenderEnabled": false,
"indexAdvisorEnabled": false
Expand All @@ -1023,7 +1050,7 @@ X-Xss-Protection: 0
}
],
"kind": "sql#instance",
"maintenanceVersion": "MYSQL_5_7_44.R20231105.01_03",
"maintenanceVersion": "MYSQL_5_7_44.R20241020.00_00",
"name": "sqlinstance-sample-${uniqueId}",
"project": "${projectId}",
"region": "us-central1",
Expand Down Expand Up @@ -1152,6 +1179,11 @@ X-Xss-Protection: 0
"displayName": "MySQL 8.0.37",
"majorVersion": "MYSQL_8_0",
"name": "MYSQL_8_0_37"
},
{
"displayName": "MySQL 8.0.39",
"majorVersion": "MYSQL_8_0",
"name": "MYSQL_8_0_39"
}
]
}
Expand All @@ -1169,7 +1201,7 @@ x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1
"password": "cGFzc3dvcmQ=",
"username": "sqluser-${uniqueId}"
},
"database": "projects/${projectId}/instances/sqlinstance-sample-${uniqueId}/databases/sqldatabase-sample-${uniqueId}",
"database": "sqldatabase-sample-${uniqueId}",
"instanceId": "${projectId}:us-central1:sqlinstance-sample-${uniqueId}",
"type": 2
}
Expand All @@ -1188,7 +1220,7 @@ X-Xss-Protection: 0

{
"cloudSql": {
"database": "projects/${projectId}/instances/sqlinstance-sample-${uniqueId}/databases/sqldatabase-sample-${uniqueId}",
"database": "sqldatabase-sample-${uniqueId}",
"instanceId": "${projectId}:us-central1:sqlinstance-sample-${uniqueId}",
"serviceAccountId": "service-${projectNumber}@gcp-sa-bigqueryconnection.iam.gserviceaccount.com",
"type": 2
Expand Down Expand Up @@ -1219,7 +1251,7 @@ X-Xss-Protection: 0

{
"cloudSql": {
"database": "projects/${projectId}/instances/sqlinstance-sample-${uniqueId}/databases/sqldatabase-sample-${uniqueId}",
"database": "sqldatabase-sample-${uniqueId}",
"instanceId": "${projectId}:us-central1:sqlinstance-sample-${uniqueId}",
"serviceAccountId": "service-${projectNumber}@gcp-sa-bigqueryconnection.iam.gserviceaccount.com",
"type": 2
Expand Down Expand Up @@ -1275,6 +1307,9 @@ X-Xss-Protection: 0
"kind": "sql#user",
"name": "sqluser-${uniqueId}",
"password": "cGFzc3dvcmQ=",
"passwordPolicy": {
"status": {}
},
"project": "${projectId}"
}
],
Expand Down Expand Up @@ -1306,6 +1341,7 @@ X-Xss-Protection: 0
"etag": "abcdef0123A=",
"gceZone": "us-central1-a",
"geminiConfig": {
"activeQueryEnabled": false,
"entitled": false,
"flagRecommenderEnabled": false,
"indexAdvisorEnabled": false
Expand All @@ -1318,7 +1354,7 @@ X-Xss-Protection: 0
}
],
"kind": "sql#instance",
"maintenanceVersion": "MYSQL_5_7_44.R20231105.01_03",
"maintenanceVersion": "MYSQL_5_7_44.R20241020.00_00",
"name": "sqlinstance-sample-${uniqueId}",
"project": "${projectId}",
"region": "us-central1",
Expand Down Expand Up @@ -1447,6 +1483,11 @@ X-Xss-Protection: 0
"displayName": "MySQL 8.0.37",
"majorVersion": "MYSQL_8_0",
"name": "MYSQL_8_0_37"
},
{
"displayName": "MySQL 8.0.39",
"majorVersion": "MYSQL_8_0",
"name": "MYSQL_8_0_39"
}
]
}
Expand Down Expand Up @@ -1625,6 +1666,7 @@ X-Xss-Protection: 0
"etag": "abcdef0123A=",
"gceZone": "us-central1-a",
"geminiConfig": {
"activeQueryEnabled": false,
"entitled": false,
"flagRecommenderEnabled": false,
"indexAdvisorEnabled": false
Expand All @@ -1637,7 +1679,7 @@ X-Xss-Protection: 0
}
],
"kind": "sql#instance",
"maintenanceVersion": "MYSQL_5_7_44.R20231105.01_03",
"maintenanceVersion": "MYSQL_5_7_44.R20241020.00_00",
"name": "sqlinstance-sample-${uniqueId}",
"project": "${projectId}",
"region": "us-central1",
Expand Down Expand Up @@ -1766,6 +1808,11 @@ X-Xss-Protection: 0
"displayName": "MySQL 8.0.37",
"majorVersion": "MYSQL_8_0",
"name": "MYSQL_8_0_37"
},
{
"displayName": "MySQL 8.0.39",
"majorVersion": "MYSQL_8_0",
"name": "MYSQL_8_0_39"
}
]
}
Expand Down

0 comments on commit 65ee2e7

Please sign in to comment.