Skip to content

Commit d3a6c05

Browse files
authored
[Feature] BackupInProgress & Maintenace Conditions (#956)
1 parent 2e3186a commit d3a6c05

File tree

10 files changed

+168
-10
lines changed

10 files changed

+168
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- (Bugfix) ArangoSync port fix
88
- (Bugfix) Fix GetClient lock system
99
- (Feature) Backup InProgress Agency key discovery
10+
- (Feature) Backup & Maintenance Conditions
1011

1112
## [1.2.9](https://github.com/arangodb/kube-arangodb/tree/1.2.9) (2022-03-30)
1213
- (Feature) Improve Kubernetes clientsets management

pkg/apis/deployment/v1/conditions.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ const (
9696

9797
// ConditionTypeLicenseSet indicates that license V2 is set on cluster.
9898
ConditionTypeLicenseSet ConditionType = "LicenseSet"
99+
100+
// ConditionTypeBackupInProgress indicates that there is Backup in progress on cluster
101+
ConditionTypeBackupInProgress ConditionType = "BackupInProgress"
102+
103+
// ConditionTypeMaintenance indicates that maintenance is enabled on cluster
104+
ConditionTypeMaintenance ConditionType = "Maintenance"
99105
)
100106

101107
// Condition represents one current condition of a deployment or deployment member.

pkg/apis/deployment/v2alpha1/conditions.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ const (
9696

9797
// ConditionTypeLicenseSet indicates that license V2 is set on cluster.
9898
ConditionTypeLicenseSet ConditionType = "LicenseSet"
99+
100+
// ConditionTypeBackupInProgress indicates that there is Backup in progress on cluster
101+
ConditionTypeBackupInProgress ConditionType = "BackupInProgress"
102+
103+
// ConditionTypeMaintenance indicates that maintenance is enabled on cluster
104+
ConditionTypeMaintenance ConditionType = "Maintenance"
99105
)
100106

101107
// Condition represents one current condition of a deployment or deployment member.

pkg/deployment/agency/state.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/arangodb/go-driver"
2929
"github.com/arangodb/go-driver/agency"
30+
"github.com/arangodb/kube-arangodb/pkg/util"
3031
"github.com/arangodb/kube-arangodb/pkg/util/errors"
3132
)
3233

@@ -113,18 +114,31 @@ type StateSupervision struct {
113114
Maintenance StateExists `json:"Maintenance,omitempty"`
114115
}
115116

116-
type StateExists bool
117+
type StateExists []byte
117118

118-
func (d *StateExists) Exists() bool {
119+
func (d StateExists) Hash() string {
119120
if d == nil {
120-
return false
121+
return ""
121122
}
122123

123-
return bool(*d)
124+
return util.SHA256(d)
125+
}
126+
127+
func (d StateExists) Exists() bool {
128+
return d != nil
124129
}
125130

126131
func (d *StateExists) UnmarshalJSON(bytes []byte) error {
127-
*d = bytes != nil
132+
if bytes == nil {
133+
*d = nil
134+
return nil
135+
}
136+
137+
z := make([]byte, len(bytes))
138+
139+
copy(z, bytes)
140+
141+
*d = z
128142
return nil
129143
}
130144

pkg/deployment/agency/target.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ type StateTarget struct {
2525
}
2626

2727
type StateTargetHotBackup struct {
28-
Create *StateExists `json:"Create,omitempty"`
28+
Create StateExists `json:"Create,omitempty"`
2929
}

pkg/deployment/reconcile/action_maintenance_condition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (a *actionSetMaintenanceCondition) Start(ctx context.Context) (bool, error)
6060
} else {
6161

6262
if err := a.actionCtx.WithStatusUpdate(ctx, func(s *api.DeploymentStatus) bool {
63-
if agencyState.Supervision.Maintenance {
63+
if agencyState.Supervision.Maintenance.Exists() {
6464
return s.Conditions.Update(api.ConditionTypeMaintenanceMode, true, "Maintenance", "Maintenance enabled")
6565
} else {
6666
return s.Conditions.Remove(api.ConditionTypeMaintenanceMode)

pkg/deployment/reconcile/action_resign_leadership.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (a *actionResignLeadership) Start(ctx context.Context) (bool, error) {
8080
if agencyState, agencyOK := a.actionCtx.GetAgencyCache(); !agencyOK {
8181
log.Warn().Err(err).Msgf("Maintenance is enabled, skipping action")
8282
return true, errors.WithStack(err)
83-
} else if agencyState.Supervision.Maintenance {
83+
} else if agencyState.Supervision.Maintenance.Exists() {
8484
// We are done, action cannot be handled on maintenance mode
8585
log.Warn().Msgf("Maintenance is enabled, skipping action")
8686
return true, nil
@@ -129,7 +129,7 @@ func (a *actionResignLeadership) CheckProgress(ctx context.Context) (bool, bool,
129129
if agencyState, agencyOK := a.actionCtx.GetAgencyCache(); !agencyOK {
130130
log.Error().Msgf("Unable to get maintenance mode")
131131
return false, false, nil
132-
} else if agencyState.Supervision.Maintenance {
132+
} else if agencyState.Supervision.Maintenance.Exists() {
133133
log.Warn().Msgf("Maintenance is enabled, skipping action")
134134
// We are done, action cannot be handled on maintenance mode
135135
m.CleanoutJobID = ""

pkg/deployment/reconcile/plan_builder_high.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ func createHighPlan(ctx context.Context, log zerolog.Logger, apiObject k8sutil.A
5858
ApplyIfEmptyWithBackOff(LicenseCheck, 30*time.Second, updateClusterLicense).
5959
ApplyIfEmpty(createTopologyMemberConditionPlan).
6060
ApplyIfEmpty(createRebalancerCheckPlan).
61-
ApplyWithBackOff(BackOffCheck, time.Minute, emptyPlanBuilder))
61+
ApplyWithBackOff(BackOffCheck, time.Minute, emptyPlanBuilder)).
62+
Apply(createBackupInProgressConditionPlan). // Discover backups always
63+
Apply(createMaintenanceConditionPlan) // Discover maintenance always
6264

6365
return r.Plan(), r.BackOff(), true
6466
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
26+
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
27+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
28+
inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
29+
"github.com/rs/zerolog"
30+
)
31+
32+
func createBackupInProgressConditionPlan(ctx context.Context,
33+
log zerolog.Logger, apiObject k8sutil.APIObject,
34+
spec api.DeploymentSpec, status api.DeploymentStatus,
35+
cachedStatus inspectorInterface.Inspector, context PlanBuilderContext) api.Plan {
36+
37+
if spec.Mode.Get() != api.DeploymentModeCluster {
38+
return nil
39+
}
40+
41+
cache, ok := context.GetAgencyCache()
42+
if !ok {
43+
return nil
44+
}
45+
46+
currentCondition, currentConditionExists := status.Conditions.Get(api.ConditionTypeBackupInProgress)
47+
48+
backupInProgress := cache.Target.HotBackup.Create
49+
50+
if currentConditionExists {
51+
// Condition exists
52+
if !backupInProgress.Exists() {
53+
// Condition needs to be removed
54+
return api.Plan{
55+
removeConditionActionV2("Backup not in progress", api.ConditionTypeBackupInProgress),
56+
}
57+
}
58+
59+
// Backup is in progress
60+
61+
hash := backupInProgress.Hash()
62+
63+
if !currentCondition.IsTrue() || currentCondition.Hash != hash {
64+
return api.Plan{
65+
updateConditionActionV2("Backup in progress", api.ConditionTypeBackupInProgress, true, "Backup In Progress", "", hash),
66+
}
67+
}
68+
69+
return nil
70+
} else {
71+
if backupInProgress.Exists() {
72+
return api.Plan{
73+
updateConditionActionV2("Backup in progress", api.ConditionTypeBackupInProgress, true, "Backup In Progress", "", backupInProgress.Hash()),
74+
}
75+
}
76+
77+
return nil
78+
}
79+
}
80+
81+
func createMaintenanceConditionPlan(ctx context.Context,
82+
log zerolog.Logger, apiObject k8sutil.APIObject,
83+
spec api.DeploymentSpec, status api.DeploymentStatus,
84+
cachedStatus inspectorInterface.Inspector, context PlanBuilderContext) api.Plan {
85+
86+
if spec.Mode.Get() != api.DeploymentModeCluster {
87+
return nil
88+
}
89+
90+
cache, ok := context.GetAgencyCache()
91+
if !ok {
92+
return nil
93+
}
94+
95+
currentCondition, currentConditionExists := status.Conditions.Get(api.ConditionTypeMaintenance)
96+
97+
backupInProgress := cache.Target.HotBackup.Create
98+
99+
if currentConditionExists {
100+
// Condition exists
101+
if !backupInProgress.Exists() {
102+
// Condition needs to be removed
103+
return api.Plan{
104+
removeConditionActionV2("Backup not in progress", api.ConditionTypeMaintenance),
105+
}
106+
}
107+
108+
// Backup is in progress
109+
110+
hash := backupInProgress.Hash()
111+
112+
if !currentCondition.IsTrue() || currentCondition.Hash != hash {
113+
return api.Plan{
114+
updateConditionActionV2("Backup in progress", api.ConditionTypeMaintenance, true, "Backup In Progress", "", hash),
115+
}
116+
}
117+
118+
return nil
119+
} else {
120+
if backupInProgress.Exists() {
121+
return api.Plan{
122+
updateConditionActionV2("Backup in progress", api.ConditionTypeMaintenance, true, "Backup In Progress", "", backupInProgress.Hash()),
123+
}
124+
}
125+
126+
return nil
127+
}
128+
}

pkg/deployment/reconcile/plan_builder_utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func removeConditionActionV2(actionReason string, conditionType api.ConditionTyp
7777
AddParam(setConditionActionV2KeyType, setConditionActionV2KeyTypeRemove)
7878
}
7979

80+
//nolint:unparam
8081
func updateConditionActionV2(actionReason string, conditionType api.ConditionType, status bool, reason, message, hash string) api.Action {
8182
statusBool := core.ConditionTrue
8283
if !status {

0 commit comments

Comments
 (0)