Skip to content

Commit

Permalink
updated secret updation
Browse files Browse the repository at this point in the history
  • Loading branch information
jananivMS committed Apr 15, 2020
1 parent aaea1d9 commit 7e53ee4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
23 changes: 22 additions & 1 deletion pkg/resourcemanager/psql/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,28 @@ func (p *PSQLServerClient) GetServer(ctx context.Context, resourcegroup string,
return client.Get(ctx, resourcegroup, servername)
}

func (p *PSQLServerClient) AddServerCredsToSecrets(ctx context.Context, secretName string, data map[string][]byte, instance *azurev1alpha1.PostgreSQLServer, fullservername string) error {
func (p *PSQLServerClient) AddServerCredsToSecrets(ctx context.Context, secretName string, data map[string][]byte, instance *azurev1alpha1.PostgreSQLServer) error {
key := types.NamespacedName{
Name: secretName,
Namespace: instance.Namespace,
}

data["fullyQualifiedServerName"] = []byte(fullservername)

err := p.SecretClient.Upsert(ctx,
key,
data,
secrets.WithOwner(instance),
secrets.WithScheme(p.Scheme),
)
if err != nil {
return err
}

return nil
}

func (p *PSQLServerClient) UpdateSecretWithFullServerName(ctx context.Context, secretName string, data map[string][]byte, instance *azurev1alpha1.PostgreSQLServer, fullservername string) error {
key := types.NamespacedName{
Name: secretName,
Namespace: instance.Namespace,
Expand Down
15 changes: 9 additions & 6 deletions pkg/resourcemanager/psql/server/server_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func (p *PSQLServerClient) Ensure(ctx context.Context, obj runtime.Object, opts
return false, err
}

// Update secret with the fully qualified server name
err = p.AddServerCredsToSecrets(ctx, instance.Name, secret, instance)
if err != nil {
return false, err
}

// if an error occurs thats ok as it means that it doesn't exist yet
getServer, err := p.GetServer(ctx, instance.Spec.ResourceGroup, instance.Name)
if err == nil {
Expand All @@ -48,17 +54,14 @@ func (p *PSQLServerClient) Ensure(ctx context.Context, obj runtime.Object, opts
// succeeded! so end reconcilliation successfully
if getServer.UserVisibleState == "Ready" {

// Update secret with the fully qualified server name
err = p.AddServerCredsToSecrets(ctx, instance.Name, secret, instance, *getServer.FullyQualifiedDomainName)
if err != nil {
instance.Status.Message = "Could not save secrets"
return true, nil
}
// Update the secret with fully qualified server name. Ignore error as we have the admin creds which is critical.
p.UpdateSecretWithFullServerName(ctx, instance.Name, secret, instance, *getServer.FullyQualifiedDomainName)

instance.Status.Message = resourcemanager.SuccessMsg
instance.Status.ResourceId = *getServer.ID
instance.Status.Provisioned = true
instance.Status.Provisioning = false
instance.Status.State = string(getServer.UserVisibleState)
return true, nil
}

Expand Down

0 comments on commit 7e53ee4

Please sign in to comment.