Skip to content

Commit b296b83

Browse files
authored
[Feature] Add member replacement condition (#898)
1 parent 024a7b7 commit b296b83

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
44
- Do not check License V2 on Community images
55
- Add status.members.<group>.
6+
- Define MemberReplacementRequired condition
67

78
## [1.2.7](https://github.com/arangodb/kube-arangodb/tree/1.2.7) (2022-01-17)
89
- Add Plan BackOff functionality

pkg/apis/deployment/v1/conditions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ const (
7373
ConditionTypePendingRestart ConditionType = "PendingRestart"
7474
// ConditionTypeRestart indicates that restart will be started
7575
ConditionTypeRestart ConditionType = "Restart"
76+
// MemberReplacementRequired indicates that the member requires a replacement to proceed with next actions.
77+
MemberReplacementRequired ConditionType = "MemberReplacementRequired"
7678

7779
// ConditionTypePendingTLSRotation indicates that TLS rotation is pending
7880
ConditionTypePendingTLSRotation ConditionType = "PendingTLSRotation"

pkg/apis/deployment/v2alpha1/conditions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ const (
7373
ConditionTypePendingRestart ConditionType = "PendingRestart"
7474
// ConditionTypeRestart indicates that restart will be started
7575
ConditionTypeRestart ConditionType = "Restart"
76+
// MemberReplacementRequired indicates that the member requires a replacement to proceed with next actions.
77+
MemberReplacementRequired ConditionType = "MemberReplacementRequired"
7678

7779
// ConditionTypePendingTLSRotation indicates that TLS rotation is pending
7880
ConditionTypePendingTLSRotation ConditionType = "PendingTLSRotation"
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

pkg/deployment/reconcile/plan_builder_high.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func createHighPlan(ctx context.Context, log zerolog.Logger, apiObject k8sutil.A
5151
ApplyIfEmpty(createCleanOutPlan).
5252
ApplyIfEmpty(updateMemberUpdateConditionsPlan).
5353
ApplyIfEmpty(updateMemberRotationConditionsPlan).
54+
ApplyIfEmpty(createMemberRecreationConditionsPlan).
5455
ApplyIfEmpty(createTopologyMemberUpdatePlan).
5556
ApplyIfEmptyWithBackOff(LicenseCheck, 30*time.Second, updateClusterLicense).
5657
ApplyIfEmpty(createTopologyMemberConditionPlan).

pkg/deployment/reconcile/plan_builder_utils.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,24 @@ func updateConditionActionV2(actionReason string, conditionType api.ConditionTyp
7878
AddParam(setConditionActionV2KeyMessage, message).
7979
AddParam(setConditionActionV2KeyHash, hash)
8080
}
81+
82+
func removeMemberConditionActionV2(actionReason string, conditionType api.ConditionType, group api.ServerGroup, member string) api.Action {
83+
return api.NewAction(api.ActionTypeSetMemberConditionV2, group, member, actionReason).
84+
AddParam(setConditionActionV2KeyAction, string(conditionType)).
85+
AddParam(setConditionActionV2KeyType, setConditionActionV2KeyTypeRemove)
86+
}
87+
88+
func updateMemberConditionActionV2(actionReason string, conditionType api.ConditionType, group api.ServerGroup, member string, status bool, reason, message, hash string) api.Action {
89+
statusBool := core.ConditionTrue
90+
if !status {
91+
statusBool = core.ConditionFalse
92+
}
93+
94+
return api.NewAction(api.ActionTypeSetMemberConditionV2, group, member, actionReason).
95+
AddParam(setConditionActionV2KeyAction, string(conditionType)).
96+
AddParam(setConditionActionV2KeyType, setConditionActionV2KeyTypeAdd).
97+
AddParam(setConditionActionV2KeyStatus, string(statusBool)).
98+
AddParam(setConditionActionV2KeyReason, reason).
99+
AddParam(setConditionActionV2KeyMessage, message).
100+
AddParam(setConditionActionV2KeyHash, hash)
101+
}

0 commit comments

Comments
 (0)