Skip to content

Commit 330b9cd

Browse files
author
Jelle Dijkstra
committed
Implemented TTL
1 parent 9683228 commit 330b9cd

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

internal/controller/atom_controller.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ package controller
2828
import (
2929
"context"
3030
"fmt"
31+
"time"
3132

3233
policyv1 "k8s.io/api/policy/v1"
3334
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -131,6 +132,13 @@ func (r *AtomReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resul
131132
return result, client.IgnoreNotFound(err)
132133
}
133134

135+
// Check TTL expiry
136+
if ttlExpired(atom) {
137+
err = r.Client.Delete(ctx, atom)
138+
139+
return result, err
140+
}
141+
134142
lgr.Info("creating resources for atom", "atom", atom)
135143
operationResults, err := r.createOrUpdateAllForAtom(ctx, atom, ownerInfo)
136144
if err != nil {
@@ -258,3 +266,13 @@ func getLabels(atom *pdoknlv3.Atom) map[string]string {
258266
labels[appLabelKey] = appName
259267
return labels
260268
}
269+
270+
func ttlExpired(atom *pdoknlv3.Atom) bool {
271+
if lifecycle := atom.Spec.Lifecycle; lifecycle != nil && lifecycle.TTLInDays != nil {
272+
expiresAt := atom.GetCreationTimestamp().Add(time.Duration(*lifecycle.TTLInDays) * 24 * time.Hour)
273+
274+
return expiresAt.Before(time.Now())
275+
}
276+
277+
return false
278+
}

internal/controller/atom_controller_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
"os"
3131
"testing"
3232

33+
"github.com/pdok/smooth-operator/model"
34+
3335
"github.com/google/go-cmp/cmp"
3436
"github.com/pdok/atom-generator/feeds"
3537
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -42,6 +44,7 @@ import (
4244
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo bdd
4345
. "github.com/onsi/gomega" //nolint:revive // ginkgo bdd
4446
smoothoperatorv1 "github.com/pdok/smooth-operator/api/v1"
47+
smoothoperatorutils "github.com/pdok/smooth-operator/pkg/util"
4548
smoothoperatorvalidation "github.com/pdok/smooth-operator/pkg/validation"
4649
"github.com/pkg/errors"
4750
"github.com/stretchr/testify/require"
@@ -201,6 +204,40 @@ var _ = Describe("Testing Atom Controller", func() {
201204
}, "10s", "1s").Should(BeTrue())
202205
})
203206

207+
It("Respects the TTL of the WMS", func() {
208+
By("Creating a new resource for the Kind WMS")
209+
controllerReconciler := &AtomReconciler{
210+
Client: k8sClient,
211+
Scheme: k8sClient.Scheme(),
212+
AtomGeneratorImage: testImageName1,
213+
LighttpdImage: testImageName2,
214+
}
215+
216+
ttlName := testAtom.GetName() + "-ttl"
217+
ttlAtom := testAtom.DeepCopy()
218+
ttlAtom.Name = ttlName
219+
ttlAtom.Spec.Lifecycle = &model.Lifecycle{TTLInDays: smoothoperatorutils.Pointer(int32(0))}
220+
objectKeyTTLAtom := client.ObjectKeyFromObject(ttlAtom)
221+
222+
err := k8sClient.Get(ctx, objectKeyTTLAtom, ttlAtom)
223+
Expect(client.IgnoreNotFound(err)).To(Not(HaveOccurred()))
224+
if err != nil && apierrors.IsNotFound(err) {
225+
Expect(k8sClient.Create(ctx, ttlAtom)).To(Succeed())
226+
}
227+
228+
// Reconcile
229+
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: objectKeyTTLAtom})
230+
Expect(err).To(Not(HaveOccurred()))
231+
232+
// Check the WMS cannot be found anymore
233+
Eventually(func() bool {
234+
err = k8sClient.Get(ctx, objectKeyTTLAtom, ttlAtom)
235+
return apierrors.IsNotFound(err)
236+
}, "10s", "1s").Should(BeTrue())
237+
238+
// Not checking owned resources because the test env does not do garbage collection
239+
})
240+
204241
It("Should cleanup the cluster", func() {
205242
err := k8sClient.Get(ctx, objectKeyAtom, clusterAtom)
206243
Expect(client.IgnoreNotFound(err)).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)