Skip to content

Commit 61f861c

Browse files
Merge pull request #66 from PDOK/jd/ttl
TTL
2 parents e032337 + 5a7f87a commit 61f861c

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

internal/controller/shared_controller.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"fmt"
66
"strconv"
77
"strings"
8+
"time"
9+
10+
"github.com/pdok/smooth-operator/model"
811

912
"github.com/pdok/mapserver-operator/internal/controller/constants"
1013

@@ -21,6 +24,26 @@ const (
2124
AppLabelKey = "app"
2225
)
2326

27+
func ttlExpired[O pdoknlv3.WMSWFS](obj O) bool {
28+
var lifecycle *model.Lifecycle
29+
switch any(obj).(type) {
30+
case *pdoknlv3.WFS:
31+
wfs := any(obj).(*pdoknlv3.WFS)
32+
lifecycle = wfs.Spec.Lifecycle
33+
case *pdoknlv3.WMS:
34+
wms := any(obj).(*pdoknlv3.WMS)
35+
lifecycle = wms.Spec.Lifecycle
36+
}
37+
38+
if lifecycle != nil && lifecycle.TTLInDays != nil {
39+
expiresAt := obj.GetCreationTimestamp().Add(time.Duration(*lifecycle.TTLInDays) * 24 * time.Hour)
40+
41+
return expiresAt.Before(time.Now())
42+
}
43+
44+
return false
45+
}
46+
2447
func ensureLabel[O pdoknlv3.WMSWFS](obj O, key, value string) {
2548
labels := obj.GetLabels()
2649
if _, ok := labels[key]; !ok {

internal/controller/wfs_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ func (r *WFSReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result
9999
return result, err
100100
}
101101

102+
// Check TTL, delete if expired
103+
if ttlExpired(wfs) {
104+
err = r.Client.Delete(ctx, wfs)
105+
106+
return result, err
107+
}
108+
102109
ensureLabel(wfs, "pdok.nl/service-type", "wfs")
103110

104111
lgr.Info("creating resources for wfs", "wfs", wfs)

internal/controller/wfs_controller_test.go

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

3232
"github.com/pdok/mapserver-operator/internal/controller/types"
33+
"github.com/pdok/smooth-operator/model"
34+
smoothoperatorutils "github.com/pdok/smooth-operator/pkg/util"
3335
k8stypes "k8s.io/apimachinery/pkg/types"
3436

3537
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo bdd
@@ -177,6 +179,34 @@ var _ = Describe("Testing WFS Controller", func() {
177179
}, "10s", "1s").Should(BeTrue())
178180
})
179181

182+
It("Respects the TTL of the WFS", func() {
183+
By("Creating a new resource for the Kind WFS")
184+
185+
ttlName := testWfs.GetName() + "-ttl"
186+
ttlWfs := testWfs.DeepCopy()
187+
ttlWfs.Name = ttlName
188+
ttlWfs.Spec.Lifecycle = &model.Lifecycle{TTLInDays: smoothoperatorutils.Pointer(int32(0))}
189+
objectKeyTTLWFS := client.ObjectKeyFromObject(ttlWfs)
190+
191+
err := k8sClient.Get(ctx, objectKeyTTLWFS, ttlWfs)
192+
Expect(client.IgnoreNotFound(err)).To(Not(HaveOccurred()))
193+
if err != nil && apierrors.IsNotFound(err) {
194+
Expect(k8sClient.Create(ctx, ttlWfs)).To(Succeed())
195+
}
196+
197+
// Reconcile
198+
_, err = getWFSReconciler().Reconcile(ctx, reconcile.Request{NamespacedName: objectKeyTTLWFS})
199+
Expect(err).To(Not(HaveOccurred()))
200+
201+
// Check the WFS cannot be found anymore
202+
Eventually(func() bool {
203+
err = k8sClient.Get(ctx, objectKeyTTLWFS, ttlWfs)
204+
return apierrors.IsNotFound(err)
205+
}, "10s", "1s").Should(BeTrue())
206+
207+
// Not checking owned resources because the test env does not do garbage collection
208+
})
209+
180210
It("Should cleanup the cluster", func() {
181211
err := k8sClient.Get(ctx, objectKeyWfs, clusterWfs)
182212
Expect(client.IgnoreNotFound(err)).NotTo(HaveOccurred())

internal/controller/wms_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ func (r *WMSReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result
110110
return result, client.IgnoreNotFound(err)
111111
}
112112

113+
// Check TTL, delete if expired
114+
if ttlExpired(wms) {
115+
err = r.Client.Delete(ctx, wms)
116+
117+
return result, err
118+
}
119+
113120
ensureLabel(wms, "pdok.nl/service-type", "wms")
114121

115122
lgr.Info("creating resources for wms", "wms", wms)

internal/controller/wms_controller_test.go

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

3232
"github.com/pdok/mapserver-operator/internal/controller/types"
33+
"github.com/pdok/smooth-operator/model"
34+
smoothoperatorutils "github.com/pdok/smooth-operator/pkg/util"
3335

3436
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo bdd
3537
. "github.com/onsi/gomega" //nolint:revive // ginkgo bdd
@@ -181,6 +183,34 @@ var _ = Describe("Testing WMS Controller", func() {
181183
}, "10s", "1s").Should(BeTrue())
182184
})
183185

186+
It("Respects the TTL of the WMS", func() {
187+
By("Creating a new resource for the Kind WMS")
188+
189+
ttlName := testWMS.GetName() + "-ttl"
190+
ttlWms := testWMS.DeepCopy()
191+
ttlWms.Name = ttlName
192+
ttlWms.Spec.Lifecycle = &model.Lifecycle{TTLInDays: smoothoperatorutils.Pointer(int32(0))}
193+
objectKeyTTLWMS := client.ObjectKeyFromObject(ttlWms)
194+
195+
err := k8sClient.Get(ctx, objectKeyTTLWMS, ttlWms)
196+
Expect(client.IgnoreNotFound(err)).To(Not(HaveOccurred()))
197+
if err != nil && apierrors.IsNotFound(err) {
198+
Expect(k8sClient.Create(ctx, ttlWms)).To(Succeed())
199+
}
200+
201+
// Reconcile
202+
_, err = getWMSReconciler().Reconcile(ctx, reconcile.Request{NamespacedName: objectKeyTTLWMS})
203+
Expect(err).To(Not(HaveOccurred()))
204+
205+
// Check the WMS cannot be found anymore
206+
Eventually(func() bool {
207+
err = k8sClient.Get(ctx, objectKeyTTLWMS, ttlWms)
208+
return apierrors.IsNotFound(err)
209+
}, "10s", "1s").Should(BeTrue())
210+
211+
// Not checking owned resources because the test env does not do garbage collection
212+
})
213+
184214
It("Should cleanup the cluster", func() {
185215
err := k8sClient.Get(ctx, objectKeyWMS, clusterWMS)
186216
Expect(client.IgnoreNotFound(err)).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)