Skip to content

Commit 3057c55

Browse files
committed
Machine: ignore attached Volumes referred by pods ignored during drain
1 parent 9569cd6 commit 3057c55

File tree

7 files changed

+585
-79
lines changed

7 files changed

+585
-79
lines changed

internal/controllers/machine/drain/cache.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ const (
3232
expirationInterval = 10 * time.Hour
3333
)
3434

35-
// CacheEntry is an entry of the drain cache. It stores at which time a Machine was drained the last time.
35+
// CacheEntry is an entry of the requeue cache. It stores at which time a Machine was proceeded the last time.
3636
type CacheEntry struct {
37-
Machine types.NamespacedName
38-
LastDrain time.Time
37+
Machine types.NamespacedName
38+
LastProceed time.Time
3939
}
4040

41-
// Cache caches the time when the last drain was done for a Machine.
42-
// Specifically we only use it to ensure we only retry drains
41+
// Cache caches the time when the a Machine was processed last.
42+
// Specifically we use it to ensure we only drain or wait for volume detachment
4343
// at a specific interval and not more often.
4444
type Cache interface {
4545
// Add adds the given entry to the Cache.
@@ -53,7 +53,7 @@ type Cache interface {
5353

5454
// NewCache creates a new cache.
5555
func NewCache() Cache {
56-
r := &drainCache{
56+
r := &retryCache{
5757
Store: cache.NewTTLStore(func(obj interface{}) (string, error) {
5858
// We only add CacheEntries to the cache, so it's safe to cast to CacheEntry.
5959
return obj.(CacheEntry).Machine.String(), nil
@@ -72,13 +72,13 @@ func NewCache() Cache {
7272
return r
7373
}
7474

75-
type drainCache struct {
75+
type retryCache struct {
7676
cache.Store
7777
}
7878

7979
// Add adds the given entry to the Cache.
8080
// Note: entries expire after the ttl.
81-
func (r *drainCache) Add(entry CacheEntry) {
81+
func (r *retryCache) Add(entry CacheEntry) {
8282
// Note: We can ignore the error here because by only allowing CacheEntries
8383
// and providing the corresponding keyFunc ourselves we can guarantee that
8484
// the error never occurs.
@@ -87,7 +87,7 @@ func (r *drainCache) Add(entry CacheEntry) {
8787

8888
// Has checks if the given key (still) exists in the Cache.
8989
// Note: entries expire after the ttl.
90-
func (r *drainCache) Has(machineName types.NamespacedName) (CacheEntry, bool) {
90+
func (r *retryCache) Has(machineName types.NamespacedName) (CacheEntry, bool) {
9191
// Note: We can ignore the error here because GetByKey never returns an error.
9292
item, exists, _ := r.Store.GetByKey(machineName.String())
9393
if exists {

internal/controllers/machine/drain/drain.go

+4-21
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import (
3434
"k8s.io/klog/v2"
3535
ctrl "sigs.k8s.io/controller-runtime"
3636
"sigs.k8s.io/controller-runtime/pkg/client"
37+
38+
clog "sigs.k8s.io/cluster-api/util/log"
3739
)
3840

3941
// Helper contains the parameters to control the behaviour of the drain helper.
@@ -351,33 +353,14 @@ func (r EvictionResult) ConditionMessage() string {
351353

352354
// podDeleteListToString returns a comma-separated list of the first n entries of the PodDelete list.
353355
func podDeleteListToString(podList []PodDelete, n int) string {
354-
return listToString(podList, func(pd PodDelete) string {
356+
return clog.ListToString(podList, func(pd PodDelete) string {
355357
return klog.KObj(pd.Pod).String()
356358
}, n)
357359
}
358360

359361
// PodListToString returns a comma-separated list of the first n entries of the Pod list.
360362
func PodListToString(podList []*corev1.Pod, n int) string {
361-
return listToString(podList, func(p *corev1.Pod) string {
363+
return clog.ListToString(podList, func(p *corev1.Pod) string {
362364
return klog.KObj(p).String()
363365
}, n)
364366
}
365-
366-
// listToString returns a comma-separated list of the first n entries of the list (strings are calculated via stringFunc).
367-
func listToString[T any](list []T, stringFunc func(T) string, n int) string {
368-
shortenedBy := 0
369-
if len(list) > n {
370-
shortenedBy = len(list) - n
371-
list = list[:n]
372-
}
373-
stringList := []string{}
374-
for _, p := range list {
375-
stringList = append(stringList, stringFunc(p))
376-
}
377-
378-
if shortenedBy > 0 {
379-
stringList = append(stringList, fmt.Sprintf("... (%d more)", shortenedBy))
380-
}
381-
382-
return strings.Join(stringList, ", ")
383-
}

internal/controllers/machine/drain/filters.go

+11
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ func (l *PodDeleteList) Pods() []*corev1.Pod {
6262
return pods
6363
}
6464

65+
// IgnoredPods returns a list of Pods that have to be ignored before the Node can be considered completely drained.
66+
func (l *PodDeleteList) IgnoredPods() []*corev1.Pod {
67+
pods := []*corev1.Pod{}
68+
for _, i := range l.items {
69+
if !i.Status.Delete {
70+
pods = append(pods, i.Pod)
71+
}
72+
}
73+
return pods
74+
}
75+
6576
func (l *PodDeleteList) errors() []error {
6677
failedPods := make(map[string][]string)
6778
for _, i := range l.items {

0 commit comments

Comments
 (0)