Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Configuration update issue #267

Merged
merged 3 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions controllers/configuration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ func (r *ConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
return ctrl.Result{RequeueAfter: 3 * time.Second}, errors.Wrap(err, "continue reconciling to destroy cloud resource")
}

configuration, err := tfcfg.Get(ctx, r.Client, req.NamespacedName)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if controllerutil.ContainsFinalizer(&configuration, configurationFinalizer) {
controllerutil.RemoveFinalizer(&configuration, configurationFinalizer)
if err := r.Update(ctx, &configuration); err != nil {
Expand Down Expand Up @@ -324,6 +329,9 @@ func (r *ConfigurationReconciler) terraformDestroy(ctx context.Context, namespac
}

// When the deletion Job process succeeded, clean up work is starting.
if err := k8sClient.Get(ctx, client.ObjectKey{Name: meta.DestroyJobName, Namespace: meta.Namespace}, &destroyJob); err != nil {
return err
}
if destroyJob.Status.Succeeded == int32(1) || deleteConfigurationDirectly {
// 1. delete Terraform input Configuration ConfigMap
if err := meta.deleteConfigMap(ctx, k8sClient); err != nil {
Expand Down
50 changes: 16 additions & 34 deletions controllers/configuration_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,23 @@ func TestConfigurationReconcile(t *testing.T) {
HCL: "c",
},
}
configuration2.Spec.ProviderReference = &crossplane.Reference{
configuration3.Spec.ProviderReference = &crossplane.Reference{
Name: "default",
Namespace: "default",
}

destroyJob3 := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "a-destroy",
Namespace: req.Namespace,
},
Status: batchv1.JobStatus{
Succeeded: int32(1),
},
}

r3 := &ConfigurationReconciler{}
r3.Client = fake.NewClientBuilder().WithScheme(s).WithObjects(secret, provider, configuration3).Build()
r3.Client = fake.NewClientBuilder().WithScheme(s).WithObjects(secret, provider, configuration3, destroyJob3).Build()

type args struct {
req reconcile.Request
Expand Down Expand Up @@ -598,35 +609,6 @@ func TestTerraformDestroy(t *testing.T) {
k8sClient2 := fake.NewClientBuilder().WithScheme(s).WithObjects(provider1, configuration).Build()
r2.Client = k8sClient2

//r3 := &ConfigurationReconciler{}
//provider1.Status.State = types.ProviderIsReady
//job3 := &batchv1.Job{
// ObjectMeta: metav1.ObjectMeta{
// Name: "a",
// Namespace: "default",
// },
// Status: batchv1.JobStatus{
// Succeeded: int32(1),
// },
//}
//configuration3 := &v1beta1.Configuration{
// ObjectMeta: metav1.ObjectMeta{
// Namespace: "default",
// Name: "b",
// },
//}
//configuration3.Spec.WriteConnectionSecretToReference = &crossplane.SecretReference{
// Name: "b",
// Namespace: "default",
//}
//k8sClient3 := fake.NewClientBuilder().WithScheme(s).WithObjects(provider1, job3, configuration3).Build()
//r3.Client = k8sClient3
//meta3 := &TFConfigurationMeta{
// DestroyJobName: "a",
// Namespace: "b",
// DeleteResource: true,
//}

r4 := &ConfigurationReconciler{}
provider1.Status.State = types.ProviderIsReady
job4 := &batchv1.Job{
Expand Down Expand Up @@ -707,11 +689,11 @@ func TestTerraformDestroy(t *testing.T) {
},
},
want: want{
errMsg: "The referenced provider could not be retrieved",
errMsg: "jobs.batch \"\" not found",
},
},
{
name: "provider is ready",
name: "referenced provider is not available",
args: args{
r: r2,
configuration: configuration,
Expand All @@ -738,7 +720,7 @@ func TestTerraformDestroy(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
err := tc.args.r.terraformDestroy(ctx, tc.args.namespace, *tc.args.configuration, tc.args.meta)
if err != nil {
if err != nil || tc.want.errMsg != "" {
if !strings.Contains(err.Error(), tc.want.errMsg) {
t.Errorf("terraformDestroy() error = %v, wantErr %v", err, tc.want.errMsg)
return
Expand Down