2018-06-07 14:22:02 +00:00
|
|
|
//
|
|
|
|
// DISCLAIMER
|
|
|
|
//
|
2022-01-10 11:35:49 +00:00
|
|
|
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
|
2018-06-07 14:22:02 +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 (
|
|
|
|
"context"
|
|
|
|
|
2022-06-30 18:39:07 +00:00
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2018-06-07 14:22:02 +00:00
|
|
|
|
2019-11-04 07:49:24 +00:00
|
|
|
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
|
2018-06-07 14:22:02 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned"
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/constants"
|
2022-07-11 11:49:47 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/errors"
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/globals"
|
2018-06-07 14:22:02 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
|
2022-07-11 11:49:47 +00:00
|
|
|
inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
|
2018-06-07 14:22:02 +00:00
|
|
|
)
|
|
|
|
|
2022-08-25 11:44:28 +00:00
|
|
|
var expectedFinalizers = []string{
|
|
|
|
constants.FinalizerDeplRemoveChildFinalizers,
|
|
|
|
}
|
|
|
|
|
2018-06-07 14:22:02 +00:00
|
|
|
// ensureFinalizers adds all required finalizers to the given deployment (in memory).
|
2022-02-16 00:36:45 +00:00
|
|
|
func ensureFinalizers(depl *api.ArangoDeployment) bool {
|
2022-08-25 11:44:28 +00:00
|
|
|
fx := make(map[string]bool, len(expectedFinalizers))
|
|
|
|
|
|
|
|
st := len(depl.Finalizers)
|
|
|
|
|
|
|
|
for _, fn := range expectedFinalizers {
|
|
|
|
fx[fn] = false
|
|
|
|
}
|
|
|
|
|
2018-06-07 14:22:02 +00:00
|
|
|
for _, f := range depl.GetFinalizers() {
|
2022-08-25 11:44:28 +00:00
|
|
|
if _, ok := fx[f]; ok {
|
|
|
|
fx[f] = true
|
2018-06-07 14:22:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-16 00:36:45 +00:00
|
|
|
|
2022-08-25 11:44:28 +00:00
|
|
|
for _, fn := range expectedFinalizers {
|
|
|
|
if !fx[fn] {
|
|
|
|
depl.Finalizers = append(depl.Finalizers, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set finalizers
|
|
|
|
return st != len(depl.Finalizers)
|
2018-06-07 14:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runDeploymentFinalizers goes through the list of ArangoDeployoment finalizers to see if they can be removed.
|
2021-03-10 13:30:47 +00:00
|
|
|
func (d *Deployment) runDeploymentFinalizers(ctx context.Context, cachedStatus inspectorInterface.Inspector) error {
|
2018-06-07 14:22:02 +00:00
|
|
|
var removalList []string
|
|
|
|
|
2022-02-28 18:53:01 +00:00
|
|
|
depls := d.deps.Client.Arango().DatabaseV1().ArangoDeployments(d.GetNamespace())
|
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()
|
2022-08-25 11:44:28 +00:00
|
|
|
updated, err := depls.Get(ctxChild, d.currentObject.GetName(), meta.GetOptions{})
|
2018-06-07 14:22:02 +00:00
|
|
|
if err != nil {
|
2021-01-08 14:35:38 +00:00
|
|
|
return errors.WithStack(err)
|
2018-06-07 14:22:02 +00:00
|
|
|
}
|
|
|
|
for _, f := range updated.ObjectMeta.GetFinalizers() {
|
|
|
|
switch f {
|
|
|
|
case constants.FinalizerDeplRemoveChildFinalizers:
|
2022-06-14 07:26:07 +00:00
|
|
|
d.log.Debug("Inspecting 'remove child finalizers' finalizer")
|
|
|
|
if retry, err := d.inspectRemoveChildFinalizers(ctx, updated, cachedStatus); err == nil && !retry {
|
2018-06-07 14:22:02 +00:00
|
|
|
removalList = append(removalList, f)
|
2022-02-22 15:55:33 +00:00
|
|
|
} else if retry {
|
2022-06-14 07:26:07 +00:00
|
|
|
d.log.Str("finalizer", f).Debug("Retry on finalizer removal")
|
2018-06-07 14:22:02 +00:00
|
|
|
} else {
|
2022-06-14 07:26:07 +00:00
|
|
|
d.log.Err(err).Str("finalizer", f).Debug("Cannot remove finalizer yet")
|
2018-06-07 14:22:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove finalizers (if needed)
|
|
|
|
if len(removalList) > 0 {
|
2022-06-14 07:26:07 +00:00
|
|
|
if err := removeDeploymentFinalizers(ctx, d.deps.Client.Arango(), updated, removalList); err != nil {
|
|
|
|
d.log.Err(err).Debug("Failed to update ArangoDeployment (to remove finalizers)")
|
2021-01-08 14:35:38 +00:00
|
|
|
return errors.WithStack(err)
|
2018-06-07 14:22:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// inspectRemoveChildFinalizers checks the finalizer condition for remove-child-finalizers.
|
|
|
|
// It returns nil if the finalizer can be removed.
|
2022-06-14 07:26:07 +00:00
|
|
|
func (d *Deployment) inspectRemoveChildFinalizers(ctx context.Context, _ *api.ArangoDeployment, cachedStatus inspectorInterface.Inspector) (bool, error) {
|
2022-02-22 15:55:33 +00:00
|
|
|
retry := false
|
|
|
|
|
|
|
|
if found, err := d.removePodFinalizers(ctx, cachedStatus); err != nil {
|
|
|
|
return false, errors.WithStack(err)
|
|
|
|
} else if found {
|
|
|
|
retry = true
|
2018-06-07 14:22:02 +00:00
|
|
|
}
|
2022-02-22 15:55:33 +00:00
|
|
|
if found, err := d.removePVCFinalizers(ctx, cachedStatus); err != nil {
|
|
|
|
return false, errors.WithStack(err)
|
|
|
|
} else if found {
|
|
|
|
retry = true
|
2018-06-07 14:22:02 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:55:33 +00:00
|
|
|
return retry, nil
|
2018-06-07 14:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// removeDeploymentFinalizers removes the given finalizers from the given PVC.
|
2022-06-14 07:26:07 +00:00
|
|
|
func removeDeploymentFinalizers(ctx context.Context, cli versioned.Interface,
|
2021-04-26 08:30:06 +00:00
|
|
|
depl *api.ArangoDeployment, finalizers []string) error {
|
2019-11-04 07:49:24 +00:00
|
|
|
depls := cli.DatabaseV1().ArangoDeployments(depl.GetNamespace())
|
2022-06-30 18:39:07 +00:00
|
|
|
getFunc := func() (meta.Object, error) {
|
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()
|
|
|
|
|
2022-06-30 18:39:07 +00:00
|
|
|
result, err := depls.Get(ctxChild, depl.GetName(), meta.GetOptions{})
|
2018-06-07 14:22:02 +00:00
|
|
|
if err != nil {
|
2021-01-08 14:35:38 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2018-06-07 14:22:02 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2022-06-30 18:39:07 +00:00
|
|
|
updateFunc := func(updated meta.Object) error {
|
2018-06-07 14:22:02 +00:00
|
|
|
updatedDepl := updated.(*api.ArangoDeployment)
|
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()
|
|
|
|
|
2022-06-30 18:39:07 +00:00
|
|
|
result, err := depls.Update(ctxChild, updatedDepl, meta.UpdateOptions{})
|
2018-06-07 14:22:02 +00:00
|
|
|
if err != nil {
|
2021-01-08 14:35:38 +00:00
|
|
|
return errors.WithStack(err)
|
2018-06-07 14:22:02 +00:00
|
|
|
}
|
|
|
|
*depl = *result
|
|
|
|
return nil
|
|
|
|
}
|
2018-06-12 11:30:43 +00:00
|
|
|
ignoreNotFound := false
|
2022-06-14 07:26:07 +00:00
|
|
|
if _, err := k8sutil.RemoveFinalizers(finalizers, getFunc, updateFunc, ignoreNotFound); err != nil {
|
2021-01-08 14:35:38 +00:00
|
|
|
return errors.WithStack(err)
|
2018-06-07 14:22:02 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|