Skip to content

Commit

Permalink
Derive connection detail names from secret keys only when they're not…
Browse files Browse the repository at this point in the history
… explicitly set

I noticed while fixing this that I'd missed unit tests for most of this
file, so I've now added them.

Signed-off-by: Nic Cope <nicc@rk0n.org>
  • Loading branch information
negz committed Jan 20, 2023
1 parent add67cb commit 1a48024
Show file tree
Hide file tree
Showing 3 changed files with 331 additions and 218 deletions.
7 changes: 2 additions & 5 deletions internal/controller/apiextensions/composite/composition_pt.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@ const (
errRenderCR = "cannot render composite resource"
errSetControllerRef = "cannot set controller reference"

errFmtResourceName = "composed resource %q"
errFmtPatch = "cannot apply the patch at index %d"
errFmtConnDetailKey = "connection detail of type %q key is not set"
errFmtConnDetailVal = "connection detail of type %q value is not set"
errFmtConnDetailPath = "connection detail of type %q fromFieldPath is not set"
errFmtResourceName = "composed resource %q"
errFmtPatch = "cannot apply the patch at index %d"
)

// TODO(negz): Move P&T Composition logic into its own package?
Expand Down
18 changes: 16 additions & 2 deletions internal/controller/apiextensions/composite/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ import (
v1 "github.com/crossplane/crossplane/apis/apiextensions/v1"
)

// Error strings.
const (
errConnDetailName = "connection detail is missing name"

errFmtConnDetailKey = "connection detail of type %q key is not set"
errFmtConnDetailVal = "connection detail of type %q value is not set"
errFmtConnDetailPath = "connection detail of type %q fromFieldPath is not set"
)

// A ConnectionDetailsFetcherFn fetches the connection details of the supplied
// resource, if any.
type ConnectionDetailsFetcherFn func(ctx context.Context, o resource.ConnectionSecretOwner) (managed.ConnectionDetails, error)
Expand All @@ -53,7 +62,7 @@ func (fc ConnectionDetailsFetcherChain) FetchConnection(ctx context.Context, o r
for _, p := range fc {
conn, err := p.FetchConnection(ctx, o)
if err != nil {
return all, err
return nil, err
}
for k, v := range conn {
all[k] = v
Expand Down Expand Up @@ -187,9 +196,12 @@ func (fn ConnectionDetailsExtractorFn) ExtractConnection(cd resource.Composed, c
// ExtractConnectionDetails extracts XR connection details from the supplied
// composed resource. If no ExtractConfigs are supplied no connection details
// will be returned.
func ExtractConnectionDetails(cd resource.Composed, data managed.ConnectionDetails, cfg ...ConnectionDetailExtractConfig) (managed.ConnectionDetails, error) {
func ExtractConnectionDetails(cd resource.Composed, data managed.ConnectionDetails, cfg ...ConnectionDetailExtractConfig) (managed.ConnectionDetails, error) { //nolint:gocyclo // TODO(negz): Break extraction out from validation, like we do with readiness.
out := map[string][]byte{}
for _, cfg := range cfg {
if cfg.Name == "" {
return nil, errors.Errorf(errConnDetailName)
}
switch tp := cfg.Type; tp {
case ConnectionDetailTypeFromValue:
if cfg.Value == nil {
Expand Down Expand Up @@ -276,6 +288,7 @@ func ExtractConfigsFromTemplate(t *v1.ComposedTemplate) []ConnectionDetailExtrac

if t.ConnectionDetails[i].Name != nil {
out[i].Name = *t.ConnectionDetails[i].Name
continue
}

if out[i].Type == ConnectionDetailTypeFromConnectionSecretKey && out[i].FromConnectionSecretKey != nil {
Expand All @@ -302,6 +315,7 @@ func ExtractConfigsFromDesired(dr *iov1alpha1.DesiredResource) []ConnectionDetai

if dr.ConnectionDetails[i].Name != nil {
out[i].Name = *dr.ConnectionDetails[i].Name
continue
}

if out[i].Type == ConnectionDetailTypeFromConnectionSecretKey && out[i].FromConnectionSecretKey != nil {
Expand Down

0 comments on commit 1a48024

Please sign in to comment.