2323package resources
2424
2525import (
26+ "time"
27+
2628 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
2729
2830 api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha"
2931)
3032
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+
3137// CleanupTerminatedPods removes all pods in Terminated state that belong to a member in Created state.
3238func (r * Resources ) CleanupTerminatedPods () error {
3339 log := r .log
@@ -47,20 +53,29 @@ func (r *Resources) CleanupTerminatedPods() error {
4753 }
4854
4955 // Check pod state
50- if ! (k8sutil .IsPodSucceeded (& p ) || k8sutil .IsPodFailed (& p )) {
56+ if ! (k8sutil .IsPodSucceeded (& p ) || k8sutil .IsPodFailed (& p ) || k8sutil . IsPodTerminating ( & p ) ) {
5157 continue
5258 }
5359
5460 // Find member status
55- memberStatus , _ , found := status .Members .MemberStatusByPodName (p .GetName ())
61+ memberStatus , group , found := status .Members .MemberStatusByPodName (p .GetName ())
5662 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+ }
6479 }
6580
6681 // Ok, we can delete the pod
0 commit comments