From 53d4ae20a1529f656ad40e21ff8deefee5577853 Mon Sep 17 00:00:00 2001 From: Bob Haddleton Date: Fri, 29 Apr 2022 08:54:10 -0500 Subject: [PATCH 1/3] Cleanup Secret and Lease objects from tfstate Signed-off-by: Bob Haddleton --- internal/controller/workspace/workspace.go | 21 +++++++++++++++++-- .../controller/workspace/workspace_test.go | 10 +++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/internal/controller/workspace/workspace.go b/internal/controller/workspace/workspace.go index 518880a..57bd7d8 100644 --- a/internal/controller/workspace/workspace.go +++ b/internal/controller/workspace/workspace.go @@ -24,6 +24,7 @@ import ( "github.com/pkg/errors" "github.com/spf13/afero" + coordv1 "k8s.io/api/coordination/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/util/workqueue" @@ -238,7 +239,7 @@ func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.E type external struct { tf tfclient - kube client.Reader + kube client.Client } func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) { @@ -322,7 +323,23 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) error { return errors.Wrap(err, errOptions) } - return errors.Wrap(c.tf.Destroy(ctx, o...), errDestroy) + if err := c.tf.Destroy(ctx, o...); err != nil { + return errors.Wrap(err, errDestroy) + } + labels := map[string]string{"tfstate": "true", "tfstateWorkspace": cr.Name} + sl := &corev1.SecretList{} + _ = c.kube.List(ctx, sl, client.MatchingLabels(labels)) + for s := range sl.Items { + sec := sl.Items[s] + _ = c.kube.Delete(ctx, &sec) + } + ll := &coordv1.LeaseList{} + _ = c.kube.List(ctx, ll, client.MatchingLabels(labels)) + for l := range ll.Items { + ls := sl.Items[l] + _ = c.kube.Delete(ctx, &ls) + } + return nil } func (c *external) options(ctx context.Context, p v1alpha1.WorkspaceParameters) ([]terraform.Option, error) { diff --git a/internal/controller/workspace/workspace_test.go b/internal/controller/workspace/workspace_test.go index 6029038..f2d3c68 100644 --- a/internal/controller/workspace/workspace_test.go +++ b/internal/controller/workspace/workspace_test.go @@ -476,7 +476,7 @@ func TestObserve(t *testing.T) { type fields struct { tf tfclient - kube client.Reader + kube client.Client } type args struct { @@ -694,7 +694,7 @@ func TestCreate(t *testing.T) { type fields struct { tf tfclient - kube client.Reader + kube client.Client } type args struct { @@ -888,7 +888,7 @@ func TestDelete(t *testing.T) { type fields struct { tf tfclient - kube client.Reader + kube client.Client } type args struct { @@ -984,7 +984,9 @@ func TestDelete(t *testing.T) { MockDestroy: func(_ context.Context, _ ...terraform.Option) error { return nil }, }, kube: &test.MockClient{ - MockGet: test.NewMockGetFn(nil), + MockDelete: test.NewMockDeleteFn(nil), + MockGet: test.NewMockGetFn(nil), + MockList: test.NewMockListFn(nil), }, }, args: args{ From f87cf8b85272b52eccdccc19f56ba0fbfc2b655d Mon Sep 17 00:00:00 2001 From: Bob Haddleton Date: Sat, 30 Apr 2022 17:41:11 -0500 Subject: [PATCH 2/3] Fix type in delete of lease objects Signed-off-by: Bob Haddleton --- internal/controller/workspace/workspace.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/controller/workspace/workspace.go b/internal/controller/workspace/workspace.go index 57bd7d8..bc983d7 100644 --- a/internal/controller/workspace/workspace.go +++ b/internal/controller/workspace/workspace.go @@ -336,7 +336,7 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) error { ll := &coordv1.LeaseList{} _ = c.kube.List(ctx, ll, client.MatchingLabels(labels)) for l := range ll.Items { - ls := sl.Items[l] + ls := ll.Items[l] _ = c.kube.Delete(ctx, &ls) } return nil From 789dcf63d6af3e224fd8cb041e5243cf07403276 Mon Sep 17 00:00:00 2001 From: Bob Haddleton Date: Thu, 5 May 2022 08:39:30 -0500 Subject: [PATCH 3/3] Add error handling for kubernetes API calls Signed-off-by: Bob Haddleton --- internal/controller/workspace/workspace.go | 54 +++++++++++++--------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/internal/controller/workspace/workspace.go b/internal/controller/workspace/workspace.go index bc983d7..8c593ac 100644 --- a/internal/controller/workspace/workspace.go +++ b/internal/controller/workspace/workspace.go @@ -40,10 +40,11 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" + "github.com/hashicorp/go-getter" + "github.com/crossplane-contrib/provider-terraform/apis/v1alpha1" "github.com/crossplane-contrib/provider-terraform/internal/terraform" "github.com/crossplane-contrib/provider-terraform/internal/workdir" - getter "github.com/hashicorp/go-getter" ) const ( @@ -52,22 +53,25 @@ const ( errGetPC = "cannot get ProviderConfig" errGetCreds = "cannot get credentials" - errMkdir = "cannot make Terraform configuration directory" - errRemoteModule = "cannot get remote Terraform module" - errWriteCreds = "cannot write Terraform credentials" - errWriteGitCreds = "cannot write .git-credentials to /tmp dir" - errWriteConfig = "cannot write Terraform configuration " + tfConfig - errWriteMain = "cannot write Terraform configuration " + tfMain - errInit = "cannot initialize Terraform configuration" - errWorkspace = "cannot select Terraform workspace" - errResources = "cannot list Terraform resources" - errDiff = "cannot diff (i.e. plan) Terraform configuration" - errOutputs = "cannot list Terraform outputs" - errOptions = "cannot determine Terraform options" - errApply = "cannot apply Terraform configuration" - errDestroy = "cannot apply Terraform configuration" - errVarFile = "cannot get tfvars" - + errMkdir = "cannot make Terraform configuration directory" + errRemoteModule = "cannot get remote Terraform module" + errWriteCreds = "cannot write Terraform credentials" + errWriteGitCreds = "cannot write .git-credentials to /tmp dir" + errWriteConfig = "cannot write Terraform configuration " + tfConfig + errWriteMain = "cannot write Terraform configuration " + tfMain + errInit = "cannot initialize Terraform configuration" + errWorkspace = "cannot select Terraform workspace" + errResources = "cannot list Terraform resources" + errDiff = "cannot diff (i.e. plan) Terraform configuration" + errOutputs = "cannot list Terraform outputs" + errOptions = "cannot determine Terraform options" + errApply = "cannot apply Terraform configuration" + errDestroy = "cannot apply Terraform configuration" + errVarFile = "cannot get tfvars" + errListLeases = "cannot get list of Lease objects" + errListSecrets = "cannot get list of Secret objects" + errDeleteSecret = "cannot delete Secret for Workspace" + errDeleteLease = "cannot delete Least for Workspace" gitCredentialsFilename = ".git-credentials" ) @@ -328,16 +332,24 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) error { } labels := map[string]string{"tfstate": "true", "tfstateWorkspace": cr.Name} sl := &corev1.SecretList{} - _ = c.kube.List(ctx, sl, client.MatchingLabels(labels)) + if err := c.kube.List(ctx, sl, client.MatchingLabels(labels)); err != nil { + return errors.Wrap(err, errListSecrets) + } for s := range sl.Items { sec := sl.Items[s] - _ = c.kube.Delete(ctx, &sec) + if err := c.kube.Delete(ctx, &sec); err != nil { + return errors.Wrap(err, errDeleteSecret) + } } ll := &coordv1.LeaseList{} - _ = c.kube.List(ctx, ll, client.MatchingLabels(labels)) + if err := c.kube.List(ctx, ll, client.MatchingLabels(labels)); err != nil { + return errors.Wrap(err, errListLeases) + } for l := range ll.Items { ls := ll.Items[l] - _ = c.kube.Delete(ctx, &ls) + if err := c.kube.Delete(ctx, &ls); err != nil { + return errors.Wrap(err, errDeleteLease) + } } return nil }