|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2023 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 | + |
| 21 | +package reconcile |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + |
| 26 | + apiErrors "k8s.io/apimachinery/pkg/api/errors" |
| 27 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + |
| 29 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 30 | + "github.com/arangodb/kube-arangodb/pkg/deployment/agency" |
| 31 | + "github.com/arangodb/kube-arangodb/pkg/util/errors" |
| 32 | + "github.com/arangodb/kube-arangodb/pkg/util/globals" |
| 33 | +) |
| 34 | + |
| 35 | +// newRemoveMemberPVCAction creates a new Action that implements the given |
| 36 | +// planned RemoveMemberPVC action. |
| 37 | +func newRemoveMemberPVCAction(action api.Action, actionCtx ActionContext) Action { |
| 38 | + a := &actionRemoveMemberPVC{} |
| 39 | + |
| 40 | + a.actionImpl = newActionImplDefRef(action, actionCtx) |
| 41 | + |
| 42 | + return a |
| 43 | +} |
| 44 | + |
| 45 | +// actionRemoveMemberPVC implements an RemoveMemberPVCAction. |
| 46 | +type actionRemoveMemberPVC struct { |
| 47 | + // actionImpl implement timeout and member id functions |
| 48 | + actionImpl |
| 49 | + |
| 50 | + // actionEmptyCheckProgress implement check progress with empty implementation |
| 51 | + actionEmptyCheckProgress |
| 52 | +} |
| 53 | + |
| 54 | +// Start performs the start of the action. |
| 55 | +// Returns true if the action is completely finished, false in case |
| 56 | +// the start time needs to be recorded and a ready condition needs to be checked. |
| 57 | +func (a *actionRemoveMemberPVC) Start(ctx context.Context) (bool, error) { |
| 58 | + m, ok := a.actionCtx.GetMemberStatusByID(a.action.MemberID) |
| 59 | + if !ok { |
| 60 | + return true, nil |
| 61 | + } |
| 62 | + |
| 63 | + pvcUID, ok := a.action.GetParam("pvc") |
| 64 | + if !ok { |
| 65 | + return true, errors.Newf("PVC UID Parameter is missing") |
| 66 | + } |
| 67 | + |
| 68 | + cache, ok := a.actionCtx.ACS().ClusterCache(m.ClusterID) |
| 69 | + if !ok { |
| 70 | + return true, errors.Newf("Cluster is not ready") |
| 71 | + } |
| 72 | + |
| 73 | + agencyCache, ok := a.actionCtx.GetAgencyCache() |
| 74 | + if !ok { |
| 75 | + return true, errors.Newf("Agency is not ready") |
| 76 | + } |
| 77 | + |
| 78 | + if agencyCache.PlanLeaderServers().Contains(agency.Server(m.ID)) { |
| 79 | + return true, errors.Newf("Server is still used in cluster") |
| 80 | + } |
| 81 | + |
| 82 | + // We are safe to remove PVC |
| 83 | + if pvcStatus := m.PersistentVolumeClaim; pvcStatus != nil { |
| 84 | + if n := pvcStatus.GetName(); n != "" { |
| 85 | + nctx, c := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx) |
| 86 | + defer c() |
| 87 | + err := cache.PersistentVolumeClaimsModInterface().V1().Delete(nctx, n, meta.DeleteOptions{ |
| 88 | + Preconditions: meta.NewUIDPreconditions(pvcUID), |
| 89 | + }) |
| 90 | + |
| 91 | + if err != nil { |
| 92 | + if apiErrors.IsNotFound(err) { |
| 93 | + // PVC is already gone |
| 94 | + return true, nil |
| 95 | + } |
| 96 | + |
| 97 | + if apiErrors.IsConflict(err) { |
| 98 | + // UID Changed, all fine |
| 99 | + return true, nil |
| 100 | + } |
| 101 | + |
| 102 | + return true, err |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return true, nil |
| 108 | +} |
0 commit comments