2018-05-11 15:34:36 +00:00
|
|
|
//
|
|
|
|
// DISCLAIMER
|
|
|
|
//
|
2024-09-13 11:43:51 +00:00
|
|
|
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
|
2018-05-11 15:34:36 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
|
|
|
//
|
|
|
|
|
|
|
|
package deployment
|
|
|
|
|
|
|
|
import (
|
2021-03-23 15:47:28 +00:00
|
|
|
"context"
|
|
|
|
|
2021-04-26 08:30:06 +00:00
|
|
|
core "k8s.io/api/core/v1"
|
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
2020-06-08 11:30:32 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util"
|
2022-02-22 15:55:33 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/constants"
|
2022-07-11 11:49:47 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/globals"
|
2018-05-11 15:34:36 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
|
2021-03-10 13:30:47 +00:00
|
|
|
inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
|
2022-03-23 22:19:36 +00:00
|
|
|
pvcv1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/persistentvolumeclaim/v1"
|
|
|
|
podv1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/pod/v1"
|
2022-11-02 07:29:46 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/kerrors"
|
2018-05-11 15:34:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// removePodFinalizers removes all finalizers from all pods owned by us.
|
2022-02-22 15:55:33 +00:00
|
|
|
func (d *Deployment) removePodFinalizers(ctx context.Context, cachedStatus inspectorInterface.Inspector) (bool, error) {
|
2022-06-14 07:26:07 +00:00
|
|
|
log := d.sectionLogger("pod-finalizer")
|
2020-06-08 11:30:32 +00:00
|
|
|
|
2022-02-22 15:55:33 +00:00
|
|
|
found := false
|
|
|
|
|
2022-03-23 22:19:36 +00:00
|
|
|
if err := cachedStatus.Pod().V1().Iterate(func(pod *core.Pod) error {
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Str("pod", pod.GetName()).Info("Removing Pod Finalizer")
|
2024-09-13 11:43:51 +00:00
|
|
|
if count, err := k8sutil.RemoveSelectedFinalizers[*core.Pod](ctx, cachedStatus.Pod().V1().Read(), cachedStatus.PodsModInterface().V1(), pod, constants.ManagedFinalizers(), true); err != nil {
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Err(err).Warn("Failed to remove pod finalizers")
|
2020-06-08 11:30:32 +00:00
|
|
|
return err
|
2022-02-22 15:55:33 +00:00
|
|
|
} else if count > 0 {
|
|
|
|
found = true
|
2020-06-08 11:30:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 12:04:48 +00:00
|
|
|
ctxChild, cancel := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx)
|
2021-04-26 08:30:06 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-10-08 15:11:18 +00:00
|
|
|
if err := d.PodsModInterface().Delete(ctxChild, pod.GetName(), meta.DeleteOptions{
|
2023-05-24 13:12:27 +00:00
|
|
|
GracePeriodSeconds: util.NewType[int64](0),
|
2020-06-08 11:30:32 +00:00
|
|
|
}); err != nil {
|
2022-11-02 07:29:46 +00:00
|
|
|
if !kerrors.IsNotFound(err) {
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Err(err).Warn("Failed to remove pod")
|
2020-06-08 11:30:32 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-05-11 15:34:36 +00:00
|
|
|
}
|
2020-06-08 11:30:32 +00:00
|
|
|
return nil
|
2022-03-23 22:19:36 +00:00
|
|
|
}, podv1.FilterPodsByLabels(k8sutil.LabelsForDeployment(d.GetName(), ""))); err != nil {
|
2022-02-22 15:55:33 +00:00
|
|
|
return false, err
|
2018-05-11 15:34:36 +00:00
|
|
|
}
|
2020-06-08 11:30:32 +00:00
|
|
|
|
2022-02-22 15:55:33 +00:00
|
|
|
return found, nil
|
2018-05-11 15:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// removePVCFinalizers removes all finalizers from all PVCs owned by us.
|
2022-02-22 15:55:33 +00:00
|
|
|
func (d *Deployment) removePVCFinalizers(ctx context.Context, cachedStatus inspectorInterface.Inspector) (bool, error) {
|
2022-06-14 07:26:07 +00:00
|
|
|
log := d.sectionLogger("pvc-finalizer")
|
2020-06-08 11:30:32 +00:00
|
|
|
|
2022-02-22 15:55:33 +00:00
|
|
|
found := false
|
|
|
|
|
2022-03-23 22:19:36 +00:00
|
|
|
if err := cachedStatus.PersistentVolumeClaim().V1().Iterate(func(pvc *core.PersistentVolumeClaim) error {
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Str("pvc", pvc.GetName()).Info("Removing PVC Finalizer")
|
2024-09-13 11:43:51 +00:00
|
|
|
if count, err := k8sutil.RemoveSelectedFinalizers[*core.PersistentVolumeClaim](ctx, cachedStatus.PersistentVolumeClaim().V1().Read(), cachedStatus.PersistentVolumeClaimsModInterface().V1(), pvc, constants.ManagedFinalizers(), true); err != nil {
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Err(err).Warn("Failed to remove PVC finalizers")
|
2020-06-08 11:30:32 +00:00
|
|
|
return err
|
2022-02-22 15:55:33 +00:00
|
|
|
} else if count > 0 {
|
|
|
|
found = true
|
2018-05-11 15:34:36 +00:00
|
|
|
}
|
2020-06-08 11:30:32 +00:00
|
|
|
return nil
|
2022-03-23 22:19:36 +00:00
|
|
|
}, pvcv1.FilterPersistentVolumeClaimsByLabels(k8sutil.LabelsForDeployment(d.GetName(), ""))); err != nil {
|
2022-02-22 15:55:33 +00:00
|
|
|
return false, err
|
2018-05-11 15:34:36 +00:00
|
|
|
}
|
2020-06-08 11:30:32 +00:00
|
|
|
|
2022-02-22 15:55:33 +00:00
|
|
|
return found, nil
|
2018-05-11 15:34:36 +00:00
|
|
|
}
|