Skip to content

Commit

Permalink
feat: support namespace expiry configmap
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Mar 4, 2024
1 parent 23db49f commit 16d11be
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions internal/utilities/pruner/namespace_pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"time"

"github.com/uselagoon/remote-controller/internal/helpers"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
Expand Down Expand Up @@ -48,6 +49,7 @@ func (p *Pruner) NamespacePruner() {
continue
}

delete := int64(0)
if val, ok := ns.Labels[ExpirationLabel]; ok {
i, err := strconv.ParseInt(val, 10, 64)
if err != nil {
Expand All @@ -58,7 +60,32 @@ func (p *Pruner) NamespacePruner() {
}
continue //on to the next NS
}
expiryDate := time.Unix(i, 0)
delete = i
}

// fallback to checking for the expiry configmap
var expireConfig corev1.ConfigMap
err := p.Client.Get(ctx, client.ObjectKey{Name: "lagoon-namespace-expiry", Namespace: x.Namespace}, &expireConfig)
if helpers.IgnoreNotFound(err) != nil {
opLog.Error(err, fmt.Sprintf("unable to check namespace for lagoon-namespace-expiry: %s", ns.Name))
continue
} else {
if val, ok := expireConfig.Data["timestamp"]; ok {
i, err := strconv.ParseInt(val, 10, 64)
if err != nil {
opLog.Error(err, fmt.Sprintf("Unable to convert %v from a unix timestamp - pausing deletion of namespace for manual intervention", val))
ierr := p.labelNamespace(ctx, ns, ExpirationPausedLabel, "true")
if ierr != nil {
opLog.Error(ierr, fmt.Sprintf("Unable to annotate namespace %s with %s", ns.Name, ExpirationPausedLabel))
}
continue //on to the next NS
}
delete = i
}
}

if delete > 0 {
expiryDate := time.Unix(delete, 0)
if expiryDate.Before(time.Now()) {
opLog.Info(fmt.Sprintf("Preparing to delete namespace: %v", x.Name))
err := p.DeletionHandler.ProcessDeletion(ctx, opLog, ns)
Expand All @@ -71,7 +98,6 @@ func (p *Pruner) NamespacePruner() {
continue
}
opLog.Info(fmt.Sprintf("Deleted namespace: %s", ns.Name))

} else {
opLog.Info(fmt.Sprintf("namespace %v is expiring later, so we skip", x.Name))
opLog.Info("Annotating namespace with future deletion details")
Expand Down

0 comments on commit 16d11be

Please sign in to comment.