Skip to content

Commit

Permalink
fix: Up error status clear on condition (#21)
Browse files Browse the repository at this point in the history
* fix: Up error atatus clear on condition
* Update README.md
* Add test case for fixing error
  • Loading branch information
samirtahir91 authored Apr 1, 2024
1 parent 1108aa6 commit e6cc0ca
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,12 @@ kubectl apply -k config/samples/
### Testing

Current integration tests cover the scenarios:
- Modifying an access token secret triggers reconcile of new access token
- Deleting an access token secret triggers reconcile of a new access token secret
- Reconcile of access token is valid

- Modifying an access token secret triggers reconcile of new access token.
- Deleting an access token secret triggers reconcile of a new access token secret.
- Reconcile of access token is valid.
- Reconcile error is recorded in a `GithubApp` object's `status.error` field
- The `status.error` field is cleared on succesful reconcile for a `GithubApp` object.
- Pods are deleted matching a label if defined in `spec.restartPods.labels` for a `GithubApp`.

**Run the controller in the foreground for testing:**
```sh
Expand Down
14 changes: 8 additions & 6 deletions internal/controller/githubapp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,22 @@ func (r *GithubAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
if updateErr := r.updateStatusWithError(ctx, githubApp, err.Error()); updateErr != nil {
l.Error(updateErr, "Failed to update status field 'Error'")
}
return requeueResult, err
return ctrl.Result{}, err
}

// Clear the error field
githubApp.Status.Error = ""
if err := r.Status().Update(ctx, githubApp); err != nil {
l.Error(err, "Failed to clear status field 'Error' for GithubApp")
return ctrl.Result{}, err
if githubApp.Status.Error != "" {
githubApp.Status.Error = ""
if err := r.Status().Update(ctx, githubApp); err != nil {
l.Error(err, "Failed to clear status field 'Error' for GithubApp")
return ctrl.Result{}, err
}
}

// Log and return
l.Info("End Reconcile")
fmt.Println()
return ctrl.Result{}, nil
return requeueResult, nil
}

// Function to delete the access token secret owned by the GithubApp
Expand Down
40 changes: 40 additions & 0 deletions internal/controller/githubapp_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,44 @@ var _ = Describe("GithubApp controller", func() {
}, "60s", "5s").Should(BeTrue(), "Failed to set status.Error field within timeout")
})
})

Context("When reconciling a GithubApp that is in error state after fixing the error", func() {
It("Should reflect reconcile with no errors and clear the `status.error` field", func() {
ctx := context.Background()

By("Creating the privateKeySecret in namespace3")
// Decode base64-encoded private key
decodedPrivateKey, err := base64.StdEncoding.DecodeString(privateKey)
Expect(err).NotTo(HaveOccurred(), "error decoding base64-encoded private key")

secret1Obj := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: privateKeySecret,
Namespace: namespace3,
},
Data: map[string][]byte{"privateKey": []byte(decodedPrivateKey)},
}
Expect(k8sClient.Create(ctx, &secret1Obj)).Should(Succeed())

// Wait for the access token Secret to be recreated
var retrievedSecret corev1.Secret
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: secretName, Namespace: namespace3}, &retrievedSecret)
return err == nil
}, "30s", "5s").Should(BeTrue(), fmt.Sprintf("Expected Secret %s/%s not recreated", namespace3, secretName))

// Check if the status.Error field gets populated with the expected error message
Eventually(func() bool {
// Retrieve the GitHubApp object
key := types.NamespacedName{Name: githubAppName3, Namespace: namespace3}
retrievedGithubApp := &githubappv1.GithubApp{}
err := k8sClient.Get(ctx, key, retrievedGithubApp)
if err != nil {
return false // Unable to retrieve the GitHubApp
}
// Check if the status.Error field has been cleared of errors
return retrievedGithubApp.Status.Error == ""
}, "30s", "5s").Should(BeTrue(), "Failed to clear status.Error field within timeout")
})
})
})

0 comments on commit e6cc0ca

Please sign in to comment.