Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Get connection details in controller
Browse files Browse the repository at this point in the history
Signed-off-by: Hasan Turken <[email protected]>
  • Loading branch information
turkenh committed Oct 6, 2021
1 parent 890de46 commit e920c43
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
15 changes: 12 additions & 3 deletions pkg/controller/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ func (e *external) Observe(ctx context.Context, mg xpresource.Managed) (managed.
return managed.ExternalObservation{}, errors.Wrap(err, "cannot late initialize parameters")
}

conn, err := tr.GetConnectionDetails(attr)
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, "cannot get connection details")
}

// During creation (i.e. apply), Terraform already waits until resource is
// ready. So, I believe it would be safe to assume it is available if create
// step completed (i.e. resource exists).
Expand All @@ -170,11 +175,11 @@ func (e *external) Observe(ctx context.Context, mg xpresource.Managed) (managed.
return managed.ExternalObservation{}, errors.Wrap(err, errPlan)
}

// TODO(muvaf): Handle connection details.
return managed.ExternalObservation{
ResourceExists: true,
ResourceUpToDate: plan.UpToDate,
ResourceLateInitialized: lateInitedAnn || lateInitedParams,
ConnectionDetails: conn,
}, nil
}

Expand All @@ -194,11 +199,15 @@ func (e *external) Create(ctx context.Context, mg xpresource.Managed) (managed.E
if err := json.JSParser.Unmarshal(res.State.GetAttributes(), &attr); err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, "cannot unmarshal state attributes")
}
// TODO(muvaf): Handle connection details.

conn, err := tr.GetConnectionDetails(attr)
if err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, "cannot get connection details")
}

// NOTE(muvaf): Only spec and metadata changes are saved after Create call.
_, err = lateInitializeAnnotations(tr, attr, string(res.State.GetPrivateRaw()))
return managed.ExternalCreation{}, errors.Wrap(err, "cannot late initialize annotations")
return managed.ExternalCreation{ConnectionDetails: conn}, errors.Wrap(err, "cannot late initialize annotations")
}

func (e *external) Update(ctx context.Context, mg xpresource.Managed) (managed.ExternalUpdate, error) {
Expand Down
11 changes: 10 additions & 1 deletion pkg/resource/sensitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package resource
import (
"context"

kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"

v1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
Expand Down Expand Up @@ -113,12 +114,20 @@ func GetSensitiveParameters(ctx context.Context, client SecretClient, from runti
// GetSensitiveObservation will return sensitive information as terraform state
// attributes by reading them from connection details.
func GetSensitiveObservation(ctx context.Context, client SecretClient, from *v1.SecretReference, into map[string]interface{}) error {
if from == nil {
// No secret reference set
return nil
}
conn, err := client.GetSecretData(ctx, from)
if kerrors.IsNotFound(err) {
// Secret not available/created yet
return nil
}
if err != nil {
return errors.Wrapf(err, "cannot get connection secret")
}
paveTF := fieldpath.Pave(into)

paveTF := fieldpath.Pave(into)
for k, v := range conn {
if err = paveTF.SetString(k, string(v)); err != nil {
return errors.Wrapf(err, "cannot set sensitive string in tf attributes for key %q", k)
Expand Down

0 comments on commit e920c43

Please sign in to comment.