Skip to content

Commit 29b0951

Browse files
author
Jelle Dijkstra
committed
TTL
1 parent 57d7165 commit 29b0951

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-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
pdoknlv3 "github.com/pdok/mapserver-operator/api/v3"
1013
"github.com/pdok/mapserver-operator/internal/controller/types"
@@ -26,6 +29,26 @@ const (
2629
InitScriptsName = "init-scripts"
2730
)
2831

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

internal/controller/wfs_controller.go

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

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

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

internal/controller/wfs_controller_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import (
2929
"fmt"
3030
"os"
3131

32+
"github.com/pdok/smooth-operator/model"
33+
smoothoperatorutils "github.com/pdok/smooth-operator/pkg/util"
34+
3235
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo bdd
3336
. "github.com/onsi/gomega" //nolint:revive // ginkgo bdd
3437
pdoknlv3 "github.com/pdok/mapserver-operator/api/v3"
@@ -175,6 +178,34 @@ var _ = Describe("Testing WFS Controller", func() {
175178
}, "10s", "1s").Should(BeTrue())
176179
})
177180

181+
It("Respects the TTL of the WFS", func() {
182+
By("Creating a new resource for the Kind WFS")
183+
184+
ttlName := testWfs.GetName() + "-ttl"
185+
ttlWfs := testWfs.DeepCopy()
186+
ttlWfs.Name = ttlName
187+
ttlWfs.Spec.Lifecycle = &model.Lifecycle{TTLInDays: smoothoperatorutils.Pointer(int32(0))}
188+
objectKeyTTLWFS := client.ObjectKeyFromObject(ttlWfs)
189+
190+
err := k8sClient.Get(ctx, objectKeyTTLWFS, ttlWfs)
191+
Expect(client.IgnoreNotFound(err)).To(Not(HaveOccurred()))
192+
if err != nil && apierrors.IsNotFound(err) {
193+
Expect(k8sClient.Create(ctx, ttlWfs)).To(Succeed())
194+
}
195+
196+
// Reconcile
197+
_, err = getWFSReconciler().Reconcile(ctx, reconcile.Request{NamespacedName: objectKeyTTLWFS})
198+
Expect(err).To(Not(HaveOccurred()))
199+
200+
// Check the WFS cannot be found anymore
201+
Eventually(func() bool {
202+
err = k8sClient.Get(ctx, objectKeyTTLWFS, ttlWfs)
203+
return apierrors.IsNotFound(err)
204+
}, "10s", "1s").Should(BeTrue())
205+
206+
// Not checking owned resources because the test env does not do garbage collection
207+
})
208+
178209
It("Should cleanup the cluster", func() {
179210
err := k8sClient.Get(ctx, objectKeyWfs, clusterWfs)
180211
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
@@ -108,6 +108,13 @@ func (r *WMSReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result
108108
return result, client.IgnoreNotFound(err)
109109
}
110110

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

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

internal/controller/wms_controller_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import (
2929
"fmt"
3030
"os"
3131

32+
"github.com/pdok/smooth-operator/model"
33+
smoothoperatorutils "github.com/pdok/smooth-operator/pkg/util"
34+
3235
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo bdd
3336
. "github.com/onsi/gomega" //nolint:revive // ginkgo bdd
3437
pdoknlv3 "github.com/pdok/mapserver-operator/api/v3"
@@ -179,6 +182,34 @@ var _ = Describe("Testing WMS Controller", func() {
179182
}, "10s", "1s").Should(BeTrue())
180183
})
181184

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

0 commit comments

Comments
 (0)