Skip to content

Commit

Permalink
fix: postgres cmd
Browse files Browse the repository at this point in the history
Get sqlinstance from app label
  • Loading branch information
erikvatt committed Feb 21, 2022
1 parent 5c7d685 commit 1c2128b
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions cmd/root/postgres/dbinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 1c2128b

Please sign in to comment.