|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2020 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 Adam Janikowski |
| 21 | +// |
| 22 | + |
| 23 | +package reconcile |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + |
| 28 | + "github.com/arangodb/go-driver" |
| 29 | + |
| 30 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 31 | + "github.com/rs/zerolog" |
| 32 | +) |
| 33 | + |
| 34 | +func init() { |
| 35 | + registerAction(api.ActionTypeClusterMemberCleanup, newClusterMemberCleanupAction) |
| 36 | +} |
| 37 | + |
| 38 | +// newClusterMemberCleanupAction creates a new Action that implements the given |
| 39 | +// planned ClusterMemberCleanup action. |
| 40 | +func newClusterMemberCleanupAction(log zerolog.Logger, action api.Action, actionCtx ActionContext) Action { |
| 41 | + a := &actionClusterMemberCleanup{} |
| 42 | + |
| 43 | + a.actionImpl = newActionImplDefRef(log, action, actionCtx, addMemberTimeout) |
| 44 | + |
| 45 | + return a |
| 46 | +} |
| 47 | + |
| 48 | +// actionClusterMemberCleanup implements an ClusterMemberCleanup. |
| 49 | +type actionClusterMemberCleanup struct { |
| 50 | + // actionImpl implement timeout and member id functions |
| 51 | + actionImpl |
| 52 | + |
| 53 | + // actionEmptyCheckProgress implement check progress with empty implementation |
| 54 | + actionEmptyCheckProgress |
| 55 | +} |
| 56 | + |
| 57 | +// Start performs the start of the action. |
| 58 | +// Returns true if the action is completely finished, false in case |
| 59 | +// the start time needs to be recorded and a ready condition needs to be checked. |
| 60 | +func (a *actionClusterMemberCleanup) Start(ctx context.Context) (bool, error) { |
| 61 | + if err := a.start(ctx); err != nil { |
| 62 | + a.log.Warn().Err(err).Msgf("Unable to clean cluster member") |
| 63 | + } |
| 64 | + |
| 65 | + return true, nil |
| 66 | +} |
| 67 | + |
| 68 | +func (a *actionClusterMemberCleanup) start(ctx context.Context) error { |
| 69 | + id := driver.ServerID(a.MemberID()) |
| 70 | + |
| 71 | + c, err := a.actionCtx.GetDatabaseClient(ctx) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + cluster, err := c.Cluster(ctx) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + health, err := cluster.Health(ctx) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + if _, ok := health.Health[id]; !ok { |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + if err := cluster.RemoveServer(ctx, id); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
0 commit comments