|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2022 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 | + "strings" |
| 26 | + |
| 27 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 28 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 29 | + inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector" |
| 30 | + "github.com/rs/zerolog" |
| 31 | +) |
| 32 | + |
| 33 | +func createMemberRecreationConditionsPlan(ctx context.Context, |
| 34 | + log zerolog.Logger, apiObject k8sutil.APIObject, |
| 35 | + spec api.DeploymentSpec, status api.DeploymentStatus, |
| 36 | + cachedStatus inspectorInterface.Inspector, context PlanBuilderContext) api.Plan { |
| 37 | + var p api.Plan |
| 38 | + |
| 39 | + for _, m := range status.Members.AsList() { |
| 40 | + resp, recreate := EvaluateMemberRecreationCondition(ctx, log, apiObject, spec, status, m.Group, m.Member, cachedStatus, context) |
| 41 | + |
| 42 | + if !recreate { |
| 43 | + if _, ok := m.Member.Conditions.Get(api.MemberReplacementRequired); ok { |
| 44 | + // Unset condition |
| 45 | + p = append(p, removeMemberConditionActionV2("Member replacement not required", api.MemberReplacementRequired, m.Group, m.Member.ID)) |
| 46 | + } |
| 47 | + } else { |
| 48 | + if c, ok := m.Member.Conditions.Get(api.MemberReplacementRequired); !ok || !c.IsTrue() || c.Message != resp { |
| 49 | + // Update condition |
| 50 | + p = append(p, updateMemberConditionActionV2("Member replacement required", api.MemberReplacementRequired, m.Group, m.Member.ID, true, "Member replacement required", resp, "")) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return p |
| 56 | +} |
| 57 | + |
| 58 | +type MemberRecreationConditionEvaluator func(ctx context.Context, |
| 59 | + log zerolog.Logger, apiObject k8sutil.APIObject, |
| 60 | + spec api.DeploymentSpec, status api.DeploymentStatus, |
| 61 | + group api.ServerGroup, member api.MemberStatus, |
| 62 | + cachedStatus inspectorInterface.Inspector, context PlanBuilderContext) (string, bool) |
| 63 | + |
| 64 | +func EvaluateMemberRecreationCondition(ctx context.Context, |
| 65 | + log zerolog.Logger, apiObject k8sutil.APIObject, |
| 66 | + spec api.DeploymentSpec, status api.DeploymentStatus, |
| 67 | + group api.ServerGroup, member api.MemberStatus, |
| 68 | + cachedStatus inspectorInterface.Inspector, context PlanBuilderContext, evaluators ...MemberRecreationConditionEvaluator) (string, bool) { |
| 69 | + args := make([]string, 0, len(evaluators)) |
| 70 | + |
| 71 | + for _, e := range evaluators { |
| 72 | + if s, ok := e(ctx, log, apiObject, spec, status, group, member, cachedStatus, context); ok { |
| 73 | + args = append(args, s) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return strings.Join(args, ", "), len(args) > 0 |
| 78 | +} |
0 commit comments