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

Remove Report instance when Cleaner is deleted #42

Merged
merged 1 commit into from
Jan 29, 2024
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
38 changes: 35 additions & 3 deletions internal/controller/cleaner_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,31 @@ func (r *CleanerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
logger = logger.WithValues("cleaner", cleaner.Name)

if !cleaner.DeletionTimestamp.IsZero() {
r.reconcileDelete(cleanerScope, logger)
return ctrl.Result{}, nil
return reconcile.Result{}, r.reconcileDelete(ctx, cleanerScope, logger)
}

return r.reconcileNormal(ctx, cleanerScope, logger)
}

func (r *CleanerReconciler) reconcileDelete(cleanerScope *scope.CleanerScope, logger logr.Logger) {
func (r *CleanerReconciler) reconcileDelete(ctx context.Context,
cleanerScope *scope.CleanerScope, logger logr.Logger) error {

logger.Info("reconcileDelete")

removeQueuedJobs(cleanerScope)

err := r.removeReport(ctx, cleanerScope, logger)
if err != nil {
return err
}

if controllerutil.ContainsFinalizer(cleanerScope.Cleaner, appsv1alpha1.CleanerFinalizer) {
controllerutil.RemoveFinalizer(cleanerScope.Cleaner, appsv1alpha1.CleanerFinalizer)
}

logger.Info("reconcileDelete succeeded")

return nil
}

func (r *CleanerReconciler) reconcileNormal(ctx context.Context, cleanerScope *scope.CleanerScope,
Expand Down Expand Up @@ -172,6 +180,30 @@ func (r *CleanerReconciler) addFinalizer(ctx context.Context, cleaner *appsv1alp
return r.Get(ctx, types.NamespacedName{Name: cleaner.Name}, cleaner)
}

// removeReport deletes (if present) Report generated for this Cleaner
// instance
func (r *CleanerReconciler) removeReport(ctx context.Context,
cleanerScope *scope.CleanerScope, logger logr.Logger) error {

report := &appsv1alpha1.Report{}
err := r.Get(ctx, types.NamespacedName{Name: cleanerScope.Cleaner.GetName()}, report)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}

logger.Info(fmt.Sprintf("failed to get Report: %v", err))
return err
}

err = r.Delete(ctx, report)
if err != nil {
return err
}

return fmt.Errorf("report instance still present")
}

func schedule(ctx context.Context, cleanerScope *scope.CleanerScope, logger logr.Logger) (*time.Time, error) {
newLastRunTime := cleanerScope.Cleaner.Status.LastRunTime

Expand Down
53 changes: 53 additions & 0 deletions internal/controller/cleaner_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2/textlogger"

appsv1alpha1 "gianlucam76/k8s-cleaner/api/v1alpha1"
"gianlucam76/k8s-cleaner/internal/controller"
"gianlucam76/k8s-cleaner/pkg/scope"
)

var _ = Describe("CleanerClient", func() {
Expand Down Expand Up @@ -92,6 +94,57 @@ var _ = Describe("CleanerClient", func() {
Expect(nextSchedule.Minute()).To(Equal(minute))
})

It("removeReport removes corresponding Report instance", func() {
cleaner := &appsv1alpha1.Cleaner{
ObjectMeta: metav1.ObjectMeta{
Name: randomString(),
},
}

report1 := appsv1alpha1.Report{
ObjectMeta: metav1.ObjectMeta{
Name: cleaner.Name,
},
Spec: appsv1alpha1.ReportSpec{
Action: appsv1alpha1.ActionScan,
ResourceInfo: []appsv1alpha1.ResourceInfo{{}},
},
}
Expect(k8sClient.Create(context.TODO(), &report1)).To(Succeed())

report2 := appsv1alpha1.Report{
ObjectMeta: metav1.ObjectMeta{
Name: randomString(),
},
Spec: appsv1alpha1.ReportSpec{
Action: appsv1alpha1.ActionScan,
ResourceInfo: []appsv1alpha1.ResourceInfo{{}},
},
}
Expect(k8sClient.Create(context.TODO(), &report2)).To(Succeed())

reconciler := &controller.CleanerReconciler{
Client: k8sClient,
Scheme: testEnv.Scheme,
}

scope, err := scope.NewCleanerScope(scope.CleanerScopeParams{
Cleaner: cleaner,
Client: k8sClient,
})
Expect(err).To(BeNil())
Expect(scope).ToNot(BeNil())

logger := textlogger.NewLogger(textlogger.NewConfig())
err = controller.RemoveReport(reconciler, context.TODO(), scope, logger)
// Expect err to not be nil cause Report was present before above call
Expect(err).ToNot(BeNil())

err = controller.RemoveReport(reconciler, context.TODO(), scope, logger)
// Expect err to be nil cause Report is not present anymore
Expect(err).To(BeNil())
})

It("addFinalizer adds finalizer", func() {
cleaner := &appsv1alpha1.Cleaner{
ObjectMeta: metav1.ObjectMeta{
Expand Down
1 change: 1 addition & 0 deletions internal/controller/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ var (
GetNextScheduleTime = getNextScheduleTime

AddFinalizer = (*CleanerReconciler).addFinalizer
RemoveReport = (*CleanerReconciler).removeReport
)
Loading