|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package resources |
| 24 | + |
| 25 | +import ( |
| 26 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 27 | + |
| 28 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha" |
| 29 | +) |
| 30 | + |
| 31 | +// CleanupTerminatedPods removes all pods in Terminated state that belong to a member in Created state. |
| 32 | +func (r *Resources) CleanupTerminatedPods() error { |
| 33 | + log := r.log |
| 34 | + |
| 35 | + pods, err := r.context.GetOwnedPods() |
| 36 | + if err != nil { |
| 37 | + log.Debug().Err(err).Msg("Failed to get owned pods") |
| 38 | + return maskAny(err) |
| 39 | + } |
| 40 | + |
| 41 | + // Update member status from all pods found |
| 42 | + status := r.context.GetStatus() |
| 43 | + for _, p := range pods { |
| 44 | + if k8sutil.IsArangoDBImageIDAndVersionPod(p) { |
| 45 | + // Image ID pods are not relevant to inspect here |
| 46 | + continue |
| 47 | + } |
| 48 | + |
| 49 | + // Check pod state |
| 50 | + if !(k8sutil.IsPodSucceeded(&p) || k8sutil.IsPodFailed(&p)) { |
| 51 | + continue |
| 52 | + } |
| 53 | + |
| 54 | + // Find member status |
| 55 | + memberStatus, _, found := status.Members.MemberStatusByPodName(p.GetName()) |
| 56 | + 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 |
| 64 | + } |
| 65 | + |
| 66 | + // Ok, we can delete the pod |
| 67 | + log.Debug().Str("pod-name", p.GetName()).Msg("Cleanup terminated pod") |
| 68 | + if err := r.context.CleanupPod(p); err != nil { |
| 69 | + log.Warn().Err(err).Str("pod-name", p.GetName()).Msg("Failed to cleanup pod") |
| 70 | + } |
| 71 | + } |
| 72 | + return nil |
| 73 | +} |
0 commit comments