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

Commit

Permalink
Merge pull request #103 from muvaf/will-call-you-back
Browse files Browse the repository at this point in the history
Callback for updating status with last operation error
  • Loading branch information
muvaf authored Oct 27, 2021
2 parents e956917 + 0b0886a commit a9595d6
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 252 deletions.
103 changes: 103 additions & 0 deletions pkg/controller/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2021 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"context"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrl "sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/crossplane-contrib/terrajet/pkg/resource"
"github.com/crossplane-contrib/terrajet/pkg/terraform"
)

const (
errGet = "cannot get resource"
)

// APISecretClient is a client for getting k8s secrets
type APISecretClient struct {
kube client.Client
}

// GetSecretData gets and returns data for the referenced secret
func (a *APISecretClient) GetSecretData(ctx context.Context, ref *xpv1.SecretReference) (map[string][]byte, error) {
secret := &v1.Secret{}
if err := a.kube.Get(ctx, types.NamespacedName{Namespace: ref.Namespace, Name: ref.Name}, secret); err != nil {
return nil, err
}
return secret.Data, nil
}

// GetSecretValue gets and returns value for key of the referenced secret
func (a *APISecretClient) GetSecretValue(ctx context.Context, sel xpv1.SecretKeySelector) ([]byte, error) {
d, err := a.GetSecretData(ctx, &sel.SecretReference)
if err != nil {
return nil, errors.Wrap(err, "cannot get secret data")
}
return d[sel.Key], err
}

// NewAPICallbacks returns a new APICallbacks.
func NewAPICallbacks(m ctrl.Manager, of xpresource.ManagedKind) *APICallbacks {
nt := func() resource.Terraformed {
return xpresource.MustCreateObject(schema.GroupVersionKind(of), m.GetScheme()).(resource.Terraformed)
}
return &APICallbacks{
kube: m.GetClient(),
newTerraformed: nt,
}
}

// APICallbacks providers callbacks that work on API resources.
type APICallbacks struct {
kube client.Client
newTerraformed func() resource.Terraformed
}

// Apply makes sure the error is saved in async operation condition.
func (ac *APICallbacks) Apply(name string) terraform.CallbackFn {
return func(err error, ctx context.Context) error {
nn := types.NamespacedName{Name: name}
tr := ac.newTerraformed()
if kErr := ac.kube.Get(ctx, nn, tr); kErr != nil {
return errors.Wrap(kErr, errGet)
}
tr.SetConditions(resource.AsyncOperationCondition(err))
return errors.Wrap(ac.kube.Status().Update(ctx, tr), errStatusUpdate)
}
}

// Destroy makes sure the error is saved in async operation condition.
func (ac *APICallbacks) Destroy(name string) terraform.CallbackFn {
return func(err error, ctx context.Context) error {
nn := types.NamespacedName{Name: name}
tr := ac.newTerraformed()
if kErr := ac.kube.Get(ctx, nn, tr); kErr != nil {
return errors.Wrap(kErr, errGet)
}
tr.SetConditions(resource.AsyncOperationCondition(err))
return errors.Wrap(ac.kube.Status().Update(ctx, tr), errStatusUpdate)
}
}
198 changes: 198 additions & 0 deletions pkg/controller/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/*
Copyright 2021 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"context"
"testing"

xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
xpfake "github.com/crossplane/crossplane-runtime/pkg/resource/fake"
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrl "sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/crossplane-contrib/terrajet/pkg/resource"
"github.com/crossplane-contrib/terrajet/pkg/resource/fake"
tjerrors "github.com/crossplane-contrib/terrajet/pkg/terraform/errors"
)

func TestAPICallbacks_Apply(t *testing.T) {
type args struct {
mgr ctrl.Manager
mg xpresource.ManagedKind
err error
}
type want struct {
err error
}
cases := map[string]struct {
reason string
args
want
}{
"ApplyOperationFailed": {
reason: "It should update the condition with error if async apply failed",
args: args{
mg: xpresource.ManagedKind(xpfake.GVK(&fake.Terraformed{})),
mgr: &xpfake.Manager{
Client: &test.MockClient{
MockGet: test.NewMockGetFn(nil),
MockStatusUpdate: func(_ context.Context, obj client.Object, _ ...client.UpdateOption) error {
got := obj.(resource.Terraformed).GetCondition(resource.TypeAsyncOperation)
if diff := cmp.Diff(resource.AsyncOperationCondition(tjerrors.NewApplyFailed(errBoom.Error())), got); diff != "" {
t.Errorf("\nApply(...): -want error, +got error:\n%s", diff)
}
return nil
},
},
Scheme: xpfake.SchemeWith(&fake.Terraformed{}),
},
err: tjerrors.NewApplyFailed(errBoom.Error()),
},
},
"ApplyOperationSucceeded": {
reason: "It should update the condition with success if the apply operation does not report error",
args: args{
mg: xpresource.ManagedKind(xpfake.GVK(&fake.Terraformed{})),
mgr: &xpfake.Manager{
Client: &test.MockClient{
MockGet: test.NewMockGetFn(nil),
MockStatusUpdate: func(_ context.Context, obj client.Object, _ ...client.UpdateOption) error {
got := obj.(resource.Terraformed).GetCondition(resource.TypeAsyncOperation)
if diff := cmp.Diff(resource.AsyncOperationCondition(nil), got); diff != "" {
t.Errorf("\nApply(...): -want error, +got error:\n%s", diff)
}
return nil
},
},
Scheme: xpfake.SchemeWith(&fake.Terraformed{}),
},
},
},
"CannotGet": {
reason: "It should return error if it cannot get the resource to update",
args: args{
mg: xpresource.ManagedKind(xpfake.GVK(&fake.Terraformed{})),
mgr: &xpfake.Manager{
Client: &test.MockClient{
MockGet: func(_ context.Context, _ client.ObjectKey, _ client.Object) error {
return errBoom
},
},
Scheme: xpfake.SchemeWith(&fake.Terraformed{}),
},
},
want: want{
err: errors.Wrap(errBoom, errGet),
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
e := NewAPICallbacks(tc.args.mgr, tc.args.mg)
err := e.Apply("name")(tc.args.err, context.TODO())
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nApply(...): -want error, +got error:\n%s", tc.reason, diff)
}
})
}
}

func TestAPICallbacks_Destroy(t *testing.T) {
type args struct {
mgr ctrl.Manager
mg xpresource.ManagedKind
err error
}
type want struct {
err error
}
cases := map[string]struct {
reason string
args
want
}{
"DestroyOperationFailed": {
reason: "It should update the condition with error if async destroy failed",
args: args{
mg: xpresource.ManagedKind(xpfake.GVK(&fake.Terraformed{})),
mgr: &xpfake.Manager{
Client: &test.MockClient{
MockGet: test.NewMockGetFn(nil),
MockStatusUpdate: func(_ context.Context, obj client.Object, _ ...client.UpdateOption) error {
got := obj.(resource.Terraformed).GetCondition(resource.TypeAsyncOperation)
if diff := cmp.Diff(resource.AsyncOperationCondition(tjerrors.NewDestroyFailed(errBoom.Error())), got); diff != "" {
t.Errorf("\nApply(...): -want error, +got error:\n%s", diff)
}
return nil
},
},
Scheme: xpfake.SchemeWith(&fake.Terraformed{}),
},
err: tjerrors.NewDestroyFailed(errBoom.Error()),
},
},
"DestroyOperationSucceeded": {
reason: "It should update the condition with success if the destroy operation does not report error",
args: args{
mg: xpresource.ManagedKind(xpfake.GVK(&fake.Terraformed{})),
mgr: &xpfake.Manager{
Client: &test.MockClient{
MockGet: test.NewMockGetFn(nil),
MockStatusUpdate: func(_ context.Context, obj client.Object, _ ...client.UpdateOption) error {
got := obj.(resource.Terraformed).GetCondition(resource.TypeAsyncOperation)
if diff := cmp.Diff(resource.AsyncOperationCondition(nil), got); diff != "" {
t.Errorf("\nApply(...): -want error, +got error:\n%s", diff)
}
return nil
},
},
Scheme: xpfake.SchemeWith(&fake.Terraformed{}),
},
},
},
"CannotGet": {
reason: "It should return error if it cannot get the resource to update",
args: args{
mg: xpresource.ManagedKind(xpfake.GVK(&fake.Terraformed{})),
mgr: &xpfake.Manager{
Client: &test.MockClient{
MockGet: func(_ context.Context, _ client.ObjectKey, _ client.Object) error {
return errBoom
},
},
Scheme: xpfake.SchemeWith(&fake.Terraformed{}),
},
},
want: want{
err: errors.Wrap(errBoom, errGet),
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
e := NewAPICallbacks(tc.args.mgr, tc.args.mg)
err := e.Destroy("name")(tc.args.err, context.TODO())
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nDestroy(...): -want error, +got error:\n%s", tc.reason, diff)
}
})
}
}
Loading

0 comments on commit a9595d6

Please sign in to comment.