Skip to content

Commit 0339bc0

Browse files
committed
feat: support namespace expiry configmap
1 parent 0d80ebc commit 0339bc0

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

internal/utilities/pruner/namespace_pruner.go

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strconv"
77
"time"
88

9+
"github.com/uselagoon/remote-controller/internal/helpers"
910
corev1 "k8s.io/api/core/v1"
1011
"k8s.io/apimachinery/pkg/labels"
1112
"k8s.io/apimachinery/pkg/selection"
@@ -54,17 +55,46 @@ func (p *Pruner) NamespacePruner() {
5455
continue
5556
}
5657

57-
if val, ok := ns.Labels[ExpirationLabel]; ok {
58-
i, err := strconv.ParseInt(val, 10, 64)
59-
if err != nil {
60-
opLog.Error(err, fmt.Sprintf("Unable to convert %v from a unix timestamp - pausing deletion of namespace for manual intervention", val))
61-
ierr := p.labelNamespace(ctx, ns, ExpirationPausedLabel, "true")
62-
if ierr != nil {
63-
opLog.Error(ierr, fmt.Sprintf("Unable to annotate namespace %s with %s", ns.Name, ExpirationPausedLabel))
58+
delete := int64(0)
59+
60+
var expireConfig corev1.ConfigMap
61+
err := p.Client.Get(ctx, client.ObjectKey{Name: "lagoon-namespace-expiry", Namespace: x.Namespace}, &expireConfig)
62+
if helpers.IgnoreNotFound(err) != nil {
63+
opLog.Error(err, fmt.Sprintf("unable to check namespace for lagoon-namespace-expiry: %s", ns.Name))
64+
continue
65+
} else {
66+
if val, ok := expireConfig.Data["timestamp"]; ok {
67+
i, err := strconv.ParseInt(val, 10, 64)
68+
if err != nil {
69+
opLog.Error(err, fmt.Sprintf("Unable to convert %v from a unix timestamp - pausing deletion of namespace for manual intervention", val))
70+
ierr := p.labelNamespace(ctx, ns, ExpirationPausedLabel, "true")
71+
if ierr != nil {
72+
opLog.Error(ierr, fmt.Sprintf("Unable to annotate namespace %s with %s", ns.Name, ExpirationPausedLabel))
73+
}
74+
continue //on to the next NS
6475
}
65-
continue //on to the next NS
76+
delete = i
6677
}
67-
expiryDate := time.Unix(i, 0)
78+
}
79+
80+
// if the configmap doesn't exist, check the namespace labels
81+
if delete == 0 {
82+
if val, ok := ns.Labels[ExpirationLabel]; ok {
83+
i, err := strconv.ParseInt(val, 10, 64)
84+
if err != nil {
85+
opLog.Error(err, fmt.Sprintf("Unable to convert %v from a unix timestamp - pausing deletion of namespace for manual intervention", val))
86+
ierr := p.labelNamespace(ctx, ns, ExpirationPausedLabel, "true")
87+
if ierr != nil {
88+
opLog.Error(ierr, fmt.Sprintf("Unable to annotate namespace %s with %s", ns.Name, ExpirationPausedLabel))
89+
}
90+
continue //on to the next NS
91+
}
92+
delete = i
93+
}
94+
}
95+
96+
if delete > 0 {
97+
expiryDate := time.Unix(delete, 0)
6898
if expiryDate.Before(time.Now()) {
6999
opLog.Info(fmt.Sprintf("Preparing to delete namespace: %v", x.Name))
70100
err := p.DeletionHandler.ProcessDeletion(ctx, opLog, ns)
@@ -77,7 +107,6 @@ func (p *Pruner) NamespacePruner() {
77107
continue
78108
}
79109
opLog.Info(fmt.Sprintf("Deleted namespace: %s", ns.Name))
80-
81110
} else {
82111
opLog.Info(fmt.Sprintf("namespace %v is expiring later, so we skip", x.Name))
83112
opLog.Info("Annotating namespace with future deletion details")

0 commit comments

Comments
 (0)