23
23
package resources
24
24
25
25
import (
26
+ "time"
27
+
26
28
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
27
29
28
30
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha"
29
31
)
30
32
33
+ const (
34
+ statelessTerminationPeriod = time .Minute // We wait this long for a stateless server to terminate on it's own. Afterwards we kill it.
35
+ )
36
+
31
37
// CleanupTerminatedPods removes all pods in Terminated state that belong to a member in Created state.
32
38
func (r * Resources ) CleanupTerminatedPods () error {
33
39
log := r .log
@@ -47,20 +53,29 @@ func (r *Resources) CleanupTerminatedPods() error {
47
53
}
48
54
49
55
// Check pod state
50
- if ! (k8sutil .IsPodSucceeded (& p ) || k8sutil .IsPodFailed (& p )) {
56
+ if ! (k8sutil .IsPodSucceeded (& p ) || k8sutil .IsPodFailed (& p ) || k8sutil . IsPodTerminating ( & p ) ) {
51
57
continue
52
58
}
53
59
54
60
// Find member status
55
- memberStatus , _ , found := status .Members .MemberStatusByPodName (p .GetName ())
61
+ memberStatus , group , found := status .Members .MemberStatusByPodName (p .GetName ())
56
62
if ! found {
57
- log .Debug ().Str ("pod" , p .GetName ()).Msg ("no memberstatus found for pod" )
58
- continue
59
- }
60
-
61
- // Check member termination condition
62
- if ! memberStatus .Conditions .IsTrue (api .ConditionTypeTerminated ) {
63
- continue
63
+ log .Debug ().Str ("pod" , p .GetName ()).Msg ("no memberstatus found for pod. Performing cleanup" )
64
+ } else {
65
+ // Check member termination condition
66
+ if ! memberStatus .Conditions .IsTrue (api .ConditionTypeTerminated ) {
67
+ if ! group .IsStateless () {
68
+ // For statefull members, we have to wait for confirmed termination
69
+ continue
70
+ } else {
71
+ // If a stateless server does not terminate within a reasonable amount or time, we kill it.
72
+ t := p .GetDeletionTimestamp ()
73
+ if t == nil || t .Add (statelessTerminationPeriod ).After (time .Now ()) {
74
+ // Either delete timestamp is not set, or not yet waiting long enough
75
+ continue
76
+ }
77
+ }
78
+ }
64
79
}
65
80
66
81
// Ok, we can delete the pod
0 commit comments