From 1c2128be36fc0fb6d14de44a23d7f73e2a3a7441 Mon Sep 17 00:00:00 2001 From: Erik Vattekar Date: Mon, 21 Feb 2022 16:13:17 +0100 Subject: [PATCH] fix: postgres cmd Get sqlinstance from app label --- cmd/root/postgres/dbinfo.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/cmd/root/postgres/dbinfo.go b/cmd/root/postgres/dbinfo.go index b50471b8..a36a24c7 100644 --- a/cmd/root/postgres/dbinfo.go +++ b/cmd/root/postgres/dbinfo.go @@ -88,17 +88,27 @@ func (i *DBInfo) DBConnection(ctx context.Context) (*ConnectionInfo, error) { } func (i *DBInfo) fetchDBInstance(ctx context.Context) error { - app, err := i.dynamicClient.Resource(schema.GroupVersionResource{ + sqlInstances, err := i.dynamicClient.Resource(schema.GroupVersionResource{ Group: "sql.cnrm.cloud.google.com", Version: "v1beta1", Resource: "sqlinstances", - }).Namespace(i.namespace).Get(ctx, i.appName, v1.GetOptions{}) + }).Namespace(i.namespace).List(ctx, v1.ListOptions{ + LabelSelector: "app=" + i.appName, + }) if err != nil { - return fmt.Errorf("fetchDBInstance: can't find sqlinstance %q in %q: %w", i.appName, i.namespace, err) + return fmt.Errorf("fetchDBInstance: error looking for sqlinstance %q in %q: %w", i.appName, i.namespace, err) } - i.connectionName = app.Object["status"].(map[string]interface{})["connectionName"].(string) - i.projectID = app.GetAnnotations()["cnrm.cloud.google.com/project-id"] + if len(sqlInstances.Items) == 0 { + return fmt.Errorf("fetchDBInstance: no sqlinstance found for app %q in %q", i.appName, i.namespace) + } else if len(sqlInstances.Items) > 1 { + return fmt.Errorf("fetchDBInstance: multiple sqlinstances found for app %q in %q", i.appName, i.namespace) + } + + sqlInstance := sqlInstances.Items[0] + + i.connectionName = sqlInstance.Object["status"].(map[string]interface{})["connectionName"].(string) + i.projectID = sqlInstance.GetAnnotations()["cnrm.cloud.google.com/project-id"] return nil }