Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 39 additions & 10 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 @@ -54,17 +55,46 @@ func (p *Pruner) NamespacePruner() {
continue
}

if val, ok := ns.Labels[ExpirationLabel]; 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))
delete := int64(0)

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
}
continue // on to the next NS
delete = i
}
expiryDate := time.Unix(i, 0)
}

// if the configmap doesn't exist, check the namespace labels
if delete == 0 {
if val, ok := ns.Labels[ExpirationLabel]; 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 @@ -77,7 +107,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
Loading