|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2020-2021 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 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 27 | + "k8s.io/apimachinery/pkg/util/uuid" |
| 28 | +) |
| 29 | + |
| 30 | +type phaseMapFunc func(action api.Action, m *api.MemberStatus) |
| 31 | +type phaseMapTo map[api.MemberPhase]phaseMapFunc |
| 32 | +type phaseMap map[api.MemberPhase]phaseMapTo |
| 33 | + |
| 34 | +var phase = phaseMap{ |
| 35 | + api.MemberPhaseNone: { |
| 36 | + api.MemberPhasePending: func(action api.Action, m *api.MemberStatus) { |
| 37 | + // Clean conditions |
| 38 | + m.Conditions.Remove(api.ConditionTypeReady) |
| 39 | + m.Conditions.Remove(api.ConditionTypeTerminated) |
| 40 | + m.Conditions.Remove(api.ConditionTypeTerminating) |
| 41 | + m.Conditions.Remove(api.ConditionTypeAgentRecoveryNeeded) |
| 42 | + m.Conditions.Remove(api.ConditionTypeAutoUpgrade) |
| 43 | + m.Conditions.Remove(api.ConditionTypeUpgradeFailed) |
| 44 | + m.Conditions.Remove(api.ConditionTypePendingTLSRotation) |
| 45 | + m.Conditions.Remove(api.ConditionTypePendingRestart) |
| 46 | + m.Conditions.Remove(api.ConditionTypeRestart) |
| 47 | + m.Conditions.Remove(api.ConditionTypePendingUpdate) |
| 48 | + m.Conditions.Remove(api.ConditionTypeUpdating) |
| 49 | + m.Conditions.Remove(api.ConditionTypeUpdateFailed) |
| 50 | + m.Conditions.Remove(api.ConditionTypeCleanedOut) |
| 51 | + |
| 52 | + // Change member RID |
| 53 | + m.RID = uuid.NewUUID() |
| 54 | + |
| 55 | + // Clean Pod details |
| 56 | + m.PodUID = "" |
| 57 | + }, |
| 58 | + }, |
| 59 | +} |
| 60 | + |
| 61 | +func (p phaseMap) empty(action api.Action, m *api.MemberStatus) { |
| 62 | + |
| 63 | +} |
| 64 | + |
| 65 | +func (p phaseMap) getFunc(from, to api.MemberPhase) phaseMapFunc { |
| 66 | + if f, ok := p[from]; ok { |
| 67 | + if t, ok := f[to]; ok { |
| 68 | + return t |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return p.empty |
| 73 | +} |
| 74 | + |
| 75 | +func (p phaseMap) Execute(m *api.MemberStatus, action api.Action, to api.MemberPhase) bool { |
| 76 | + from := m.Phase |
| 77 | + |
| 78 | + if from == to { |
| 79 | + return false |
| 80 | + } |
| 81 | + |
| 82 | + f := p.getFunc(from, to) |
| 83 | + |
| 84 | + m.Phase = to |
| 85 | + |
| 86 | + f(action, m) |
| 87 | + |
| 88 | + return true |
| 89 | +} |
0 commit comments