Skip to content

Commit

Permalink
Merge pull request #82 from vshn/bugfix-iamkey-conn-details
Browse files Browse the repository at this point in the history
bugfix: return error when iam key or secret are not present
  • Loading branch information
susana-garcia authored Jan 24, 2023
2 parents be0a840 + 567bf03 commit 8ec08f8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
4 changes: 2 additions & 2 deletions operator/iamkeycontroller/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

type IAMKeyConnector struct {
type connector struct {
Kube client.Client
Recorder event.Recorder
}

// Connect implements managed.ExternalConnecter.
func (c *IAMKeyConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
ctx = pipeline.MutableContext(ctx)
log := ctrl.LoggerFrom(ctx)
log.V(1).Info("Connecting resource")
Expand Down
12 changes: 10 additions & 2 deletions operator/iamkeycontroller/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ func (p *IAMKeyPipeline) Create(ctx context.Context, mg resource.Managed) (manag
if err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, "cannot create IAM Key")
}
connDetails, err := toConnectionDetails(pctx.iamExoscaleKey)
if err != nil {
return managed.ExternalCreation{}, fmt.Errorf("cannot parse connection details: %w", err)
}

return managed.ExternalCreation{ConnectionDetails: toConnectionDetails(pctx.iamExoscaleKey)}, nil
return managed.ExternalCreation{ConnectionDetails: connDetails}, nil
}

// createIAMKey creates a new IAMKey in the project associated with the API Key and Secret.
Expand Down Expand Up @@ -114,7 +118,11 @@ func (p *IAMKeyPipeline) createCredentialsSecret(ctx *pipelineContext) error {
if secret.Data == nil {
secret.Data = map[string][]byte{}
}
for k, v := range toConnectionDetails(ctx.iamExoscaleKey) {
connDetails, err := toConnectionDetails(ctx.iamExoscaleKey)
if err != nil {
return fmt.Errorf("cannot parse connection details: %w", err)
}
for k, v := range connDetails {
secret.Data[k] = v
}
secret.Immutable = pointer.Bool(true)
Expand Down
15 changes: 12 additions & 3 deletions operator/iamkeycontroller/observe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package iamkeycontroller
import (
"context"
"fmt"
"net/url"

pipeline "github.com/ccremer/go-command-pipeline"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/errors"
Expand All @@ -13,7 +15,6 @@ import (
"github.com/vshn/provider-exoscale/operator/pipelineutil"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"net/url"
controllerruntime "sigs.k8s.io/controller-runtime"
)

Expand Down Expand Up @@ -48,14 +49,22 @@ func (p *IAMKeyPipeline) Observe(ctx context.Context, mg resource.Managed) (mana
).RunWithContext(pctx)

if result != nil {
return managed.ExternalObservation{ResourceExists: true, ResourceUpToDate: false, ConnectionDetails: toConnectionDetails(pctx.iamExoscaleKey)}, nil
connDetails, err := toConnectionDetails(pctx.iamExoscaleKey)
if err != nil {
return managed.ExternalObservation{}, fmt.Errorf("cannot parse connection details: %w", err)
}
return managed.ExternalObservation{ResourceExists: true, ResourceUpToDate: false, ConnectionDetails: connDetails}, nil
}

iamKey.Status.AtProvider.KeyName = *pctx.iamExoscaleKey.Name
iamKey.Status.AtProvider.ServicesSpec.SOS.Buckets = getBuckets(*pctx.iamExoscaleKey.Resources)

iamKey.SetConditions(xpv1.Available())
return managed.ExternalObservation{ResourceExists: true, ResourceUpToDate: true, ConnectionDetails: toConnectionDetails(pctx.iamExoscaleKey)}, nil
connDetails, err := toConnectionDetails(pctx.iamExoscaleKey)
if err != nil {
return managed.ExternalObservation{}, fmt.Errorf("cannot parse connection details: %w", err)
}
return managed.ExternalObservation{ResourceExists: true, ResourceUpToDate: true, ConnectionDetails: connDetails}, nil
}

// getIAMKey fetches an existing IAM key from the project associated with the API Key and Secret.
Expand Down
12 changes: 10 additions & 2 deletions operator/iamkeycontroller/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package iamkeycontroller

import (
"context"
"errors"

exoscalesdk "github.com/exoscale/egoscale/v2"
exoscalev1 "github.com/vshn/provider-exoscale/apis/exoscale/v1"

Expand Down Expand Up @@ -61,11 +63,17 @@ func NewPipeline(client client.Client, recorder event.Recorder, exoscaleClient *
}
}

func toConnectionDetails(iamKey *exoscalesdk.IAMAccessKey) managed.ConnectionDetails {
func toConnectionDetails(iamKey *exoscalesdk.IAMAccessKey) (managed.ConnectionDetails, error) {
if iamKey.Key == nil {
return nil, errors.New("iamKey key not found in connection details")
}
if iamKey.Secret == nil {
return nil, errors.New("iamKey secret not found in connection details")
}
return map[string][]byte{
exoscalev1.AccessKeyIDName: []byte(*iamKey.Key),
exoscalev1.SecretAccessKeyName: []byte(*iamKey.Secret),
}
}, nil
}

func fromManaged(mg resource.Managed) *exoscalev1.IAMKey {
Expand Down
2 changes: 1 addition & 1 deletion operator/iamkeycontroller/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func SetupController(mgr ctrl.Manager) error {

r := managed.NewReconciler(mgr,
resource.ManagedKind(exoscalev1.IAMKeyGroupVersionKind),
managed.WithExternalConnecter(&IAMKeyConnector{
managed.WithExternalConnecter(&connector{
Kube: mgr.GetClient(),
Recorder: recorder,
}),
Expand Down

0 comments on commit 8ec08f8

Please sign in to comment.