-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add garbage collector runner to remove orphan PVs
Signed-off-by: Ryotaro Banno <[email protected]>
- Loading branch information
1 parent
1990152
commit 44add37
Showing
8 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
mantlev1 "github.com/cybozu-go/mantle/api/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
aerrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/selection" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
type GarbageCollectorRunner struct { | ||
client client.Client | ||
interval time.Duration | ||
} | ||
|
||
func NewGarbageCollectorRunner( | ||
client client.Client, | ||
interval time.Duration, | ||
) *GarbageCollectorRunner { | ||
return &GarbageCollectorRunner{ | ||
client: client, | ||
interval: interval, | ||
} | ||
} | ||
|
||
func (r *GarbageCollectorRunner) Start(ctx context.Context) error { | ||
logger := log.FromContext(ctx) | ||
|
||
for { | ||
ctxSleep, cancelSleep := context.WithTimeout(ctx, r.interval) | ||
<-ctxSleep.Done() | ||
cancelSleep() | ||
if ctx.Err() != nil { | ||
break | ||
} | ||
|
||
logger.Info("garbage collection started") | ||
|
||
if err := r.deleteOrphanPVs(ctx); err != nil { | ||
logger.Error(err, "failed to delete orphan PVs", "error", err) | ||
} | ||
|
||
logger.Info("garbage collection finished") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *GarbageCollectorRunner) deleteOrphanPVs(ctx context.Context) error { | ||
logger := log.FromContext(ctx) | ||
|
||
requirement, err := labels.NewRequirement(labelRestoringPVKey, selection.Exists, []string{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create a new labels requirement: %w", err) | ||
} | ||
selector := labels.ValidatedSetSelector{}.Add(*requirement) | ||
|
||
var pvList corev1.PersistentVolumeList | ||
if err := r.client.List(ctx, &pvList, &client.ListOptions{ | ||
LabelSelector: selector, | ||
}); err != nil { | ||
return fmt.Errorf("failed to list PVs: %w", err) | ||
} | ||
|
||
for _, pv := range pvList.Items { | ||
shouldDelete, err := r.isMantleRestoreAlreadyDeleted(ctx, &pv) | ||
if err != nil { | ||
return fmt.Errorf("failed to check if a PV should be deleted: %w", err) | ||
} | ||
if !shouldDelete { | ||
continue | ||
} | ||
if err := r.client.Delete(ctx, &pv, &client.DeleteOptions{ | ||
Preconditions: &metav1.Preconditions{UID: &pv.ObjectMeta.UID, ResourceVersion: &pv.ObjectMeta.ResourceVersion}, | ||
}); err != nil { | ||
return fmt.Errorf("failed to delete PV: %w", err) | ||
} | ||
logger.Info("an orphan PV is removed", "name", pv.GetName()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *GarbageCollectorRunner) isMantleRestoreAlreadyDeleted(ctx context.Context, pv *corev1.PersistentVolume) (bool, error) { | ||
restoreUID, ok := pv.GetAnnotations()[PVAnnotationRestoredBy] | ||
if !ok { | ||
return false, fmt.Errorf("failed to find annotation: %s", PVAnnotationRestoredBy) | ||
} | ||
restoreName, ok := pv.GetAnnotations()[PVAnnotationRestoredByName] | ||
if !ok { | ||
return false, fmt.Errorf("failed to find annotation: %s", PVAnnotationRestoredByName) | ||
} | ||
restoreNamespace, ok := pv.GetAnnotations()[PVAnnotationRestoredByNamespace] | ||
if !ok { | ||
return false, fmt.Errorf("failed to find annotation: %s", PVAnnotationRestoredByNamespace) | ||
} | ||
|
||
var restore mantlev1.MantleRestore | ||
if err := r.client.Get( | ||
ctx, | ||
types.NamespacedName{Name: restoreName, Namespace: restoreNamespace}, | ||
&restore, | ||
); err != nil { | ||
if aerrors.IsNotFound(err) { | ||
return true, nil | ||
} else { | ||
return false, fmt.Errorf("failed to get MantleRestore: %w", err) | ||
} | ||
} | ||
|
||
if string(restore.GetUID()) != restoreUID { | ||
return true, nil | ||
} | ||
|
||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
controller: | ||
overwriteMBCSchedule: "* * * * *" | ||
gcInterval: 1s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
controller: | ||
gcInterval: 1s |