Skip to content

Commit bdea110

Browse files
committed
chore: push the rest
1 parent 4ae6f21 commit bdea110

29 files changed

+2316
-653
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,13 @@ vet: ## Run go vet against code.
111111
go vet ./...
112112

113113
.PHONY: test
114-
test: manifests generate fmt vet ## Run tests.
114+
test: manifests generate fmt vet ## Run tests (Ginkgo suite only).
115115
go test ./tests/ -v -ginkgo.v -coverprofile cover.out
116116

117+
.PHONY: test-unit
118+
test-unit: ## Run unit tests (excludes Ginkgo suite).
119+
go test -tags=unit ./internal/... -v -count=1
120+
117121
##@ Build
118122

119123
.PHONY: build

api/v1alpha1/common.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package v1alpha1
2+
3+
import "sigs.k8s.io/controller-runtime/pkg/client"
4+
5+
// +kubebuilder:object:generate=false
6+
type Object2 interface {
7+
client.Object
8+
9+
SetPhase(phase string)
10+
GetPhase() string
11+
12+
SetObservedGeneration(generation int64)
13+
}

api/v1alpha1/mongodbbackup_types.go

Lines changed: 120 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,100 @@
11
package v1alpha1
22

33
import (
4+
"github.com/RocketChat/airlock/internal/rules"
45
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
56
)
67

8+
const (
9+
MongoDBBackupControllerName = "MongoDBBackup"
10+
11+
BackupConditionBucketStoreReady = "BucketStoreReady"
12+
BackupConditionAccessRequestReady = "MongoDBAccessRequestReady"
13+
BackupConditionJobScheduled = "JobScheduled"
14+
BackupConditionJobCompleted = "JobCompleted"
15+
BackupConditionJobFailed = "JobFailed"
16+
17+
BackupPhasePending = "Pending"
18+
BackupPhaseRunning = "Running"
19+
BackupPhaseCompleted = "Completed"
20+
BackupPhaseFailed = "Failed"
21+
22+
BackupReasonBackupNotStarted = "BackupNotStarted"
23+
24+
BackupReasonBackupStoreNotFound = "BackupStoreNotFound"
25+
BackupReasonAccessRequestNotFound = "AccessRequestNotFound"
26+
BackupReasonAccessRequestNotReady = "AccessRequestNotReady"
27+
BackupReasonAccessRequestReady = "AccessRequestReady"
28+
29+
BackupReasonBackupStoreReady = "BackupStoreReady"
30+
BackupReasonJobScheduled = "JobScheduled"
31+
BackupReasonJobCompleted = "JobCompleted"
32+
BackupReasonJobFailed = "JobFailed"
33+
)
34+
35+
var BackupPhaseRules = []rules.PhaseRule{
36+
// a successful backup
37+
rules.NewPhaseRule(
38+
BackupPhaseCompleted,
39+
rules.ConditionsAll(
40+
// store is ready
41+
rules.ConditionEquals(BackupConditionBucketStoreReady, metav1.ConditionTrue),
42+
// accessrequest is ready
43+
rules.ConditionEquals(BackupConditionAccessRequestReady, metav1.ConditionTrue),
44+
// job is scheduled
45+
rules.ConditionEquals(BackupConditionJobCompleted, metav1.ConditionTrue),
46+
),
47+
),
48+
// for a running backup, check that consumes most conditions first
49+
rules.NewPhaseRule(
50+
BackupPhaseRunning,
51+
rules.ConditionsAll(
52+
// store is ready
53+
rules.ConditionEquals(BackupConditionBucketStoreReady, metav1.ConditionTrue),
54+
// accessrequest is ready
55+
rules.ConditionEquals(BackupConditionAccessRequestReady, metav1.ConditionTrue),
56+
// job is scheduled
57+
rules.ConditionEquals(BackupConditionJobScheduled, metav1.ConditionTrue),
58+
),
59+
),
60+
// a failed backup can consist of any of the conditions being false-y
61+
rules.NewPhaseRule(
62+
BackupPhaseFailed,
63+
rules.ConditionsAny(
64+
// store is not ready
65+
rules.ConditionEquals(BackupConditionBucketStoreReady, metav1.ConditionFalse),
66+
// accessrequest is not ready
67+
rules.ConditionEquals(BackupConditionAccessRequestReady, metav1.ConditionFalse),
68+
// job is failed
69+
rules.ConditionEquals(BackupConditionJobFailed, metav1.ConditionTrue),
70+
// job schedule failed
71+
// rules.ConditionEquals(BackupConditionJobScheduled, metav1.ConditionFalse),
72+
),
73+
),
74+
// pending backup is an amalgamation of all the conditions that are not yet met
75+
// when any of the precursors are in unknown state
76+
rules.NewPhaseRule(
77+
BackupPhasePending,
78+
rules.ConditionsAny(
79+
// store is in unknown state
80+
rules.ConditionEquals(BackupConditionBucketStoreReady, metav1.ConditionUnknown),
81+
// accessrequest is in unknown state
82+
rules.ConditionEquals(BackupConditionAccessRequestReady, metav1.ConditionUnknown),
83+
),
84+
),
85+
rules.NewPhaseRule(
86+
BackupPhasePending,
87+
rules.ConditionsAll(
88+
// store is either ready or hasn't been checked yet by their controller
89+
rules.ConditionEquals(BackupConditionBucketStoreReady, metav1.ConditionTrue, metav1.ConditionUnknown),
90+
// accessrequest is either ready or hasn't been checked yet by access requyest controller
91+
rules.ConditionEquals(BackupConditionAccessRequestReady, metav1.ConditionTrue, metav1.ConditionUnknown),
92+
// job is not scheduled yet
93+
rules.ConditionEquals(BackupConditionJobScheduled, metav1.ConditionUnknown, metav1.ConditionFalse),
94+
),
95+
),
96+
}
97+
798
// MongoDBBackupSpec defines the desired state of MongoDBBackup
899
// +kubebuilder:object:generate=true
9100
// +k8s:deepcopy-gen=true
@@ -33,12 +124,23 @@ type MongoDBBackupStoreRef struct {
33124
// +kubebuilder:object:generate=true
34125
// +k8s:deepcopy-gen=true
35126
type MongoDBBackupStatus struct {
36-
Phase string `json:"phase,omitempty"`
37-
StartTime *metav1.Time `json:"startTime,omitempty"`
38-
CompletionTime *metav1.Time `json:"completionTime,omitempty"`
39-
BackupPath string `json:"backupPath,omitempty"`
40-
Size string `json:"size,omitempty"`
41-
Conditions []metav1.Condition `json:"conditions,omitempty"`
127+
Phase string `json:"phase,omitempty"`
128+
ObservedGeneration *int64 `json:"observedGeneration,omitempty"`
129+
StartTime *metav1.Time `json:"startTime,omitempty"`
130+
CompletionTime *metav1.Time `json:"completionTime,omitempty"`
131+
Conditions []metav1.Condition `json:"conditions,omitempty"`
132+
133+
Result *MongoDBBackupStatusResult `json:"result,omitempty"`
134+
}
135+
136+
type MongoDBBackupStatusResult struct {
137+
Path string `json:"path,omitempty"`
138+
Job *MongoDBBackupStatusJob `json:"job,omitempty"`
139+
}
140+
141+
type MongoDBBackupStatusJob struct {
142+
ID string `json:"id,omitempty"`
143+
Name string `json:"name,omitempty"`
42144
}
43145

44146
// +kubebuilder:object:root=true
@@ -64,3 +166,15 @@ type MongoDBBackupList struct {
64166
func init() {
65167
SchemeBuilder.Register(&MongoDBBackup{}, &MongoDBBackupList{})
66168
}
169+
170+
func (o *MongoDBBackup) SetPhase(phase string) {
171+
o.Status.Phase = phase
172+
}
173+
174+
func (o *MongoDBBackup) GetPhase() string {
175+
return o.Status.Phase
176+
}
177+
178+
func (o *MongoDBBackup) SetObservedGeneration(generation int64) {
179+
o.Status.ObservedGeneration = &generation
180+
}

api/v1alpha1/mongodbbackupschedule_types.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,58 @@
11
package v1alpha1
22

33
import (
4+
"github.com/RocketChat/airlock/internal/rules"
45
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
56
)
67

8+
const (
9+
BackupSchedulePhaseRunning = "Running"
10+
BackupSchedulePhasePending = "Pending"
11+
BackupSchedulePhaseFailed = "Failed"
12+
13+
BackupScheduleConditionBucketStoreReady = "BucketStoreReady"
14+
BackupScheduleConditionBackupCreateFailed = "BackupCreateFailed"
15+
BackupScheduleConditionInternalTaskScheduleFailed = "InternalTaskScheduleFailed"
16+
17+
BackupScheduleReasonBackupStoreNotFound = "BackupStoreNotFound"
18+
BackupScheduleReasonBackupStoreNotReady = "BackupStoreNotReady"
19+
BackupScheduleReasonBackupCreated = "BackupCreated"
20+
)
21+
22+
var BackupSchedulePhaseRules = []rules.PhaseRule{
23+
rules.NewPhaseRule(
24+
BackupSchedulePhaseRunning,
25+
// store must be ready
26+
// backup creation did not fail
27+
// schedule must not be suspended
28+
rules.ConditionsAll(
29+
rules.ConditionEquals(BackupScheduleConditionBucketStoreReady, metav1.ConditionTrue),
30+
rules.ConditionEquals(BackupScheduleConditionBackupCreateFailed, metav1.ConditionFalse),
31+
rules.ConditionEquals(BackupScheduleConditionInternalTaskScheduleFailed, metav1.ConditionFalse),
32+
),
33+
),
34+
rules.NewPhaseRule(
35+
BackupSchedulePhaseFailed,
36+
// backup creation failed
37+
// or internal task schedule failed
38+
rules.ConditionsAny(
39+
rules.ConditionEquals(BackupScheduleConditionBackupCreateFailed, metav1.ConditionTrue),
40+
rules.ConditionEquals(BackupScheduleConditionInternalTaskScheduleFailed, metav1.ConditionTrue),
41+
),
42+
),
43+
rules.NewPhaseRule(
44+
BackupSchedulePhasePending,
45+
// store may niot be ready yet, keep itr in pending state
46+
// backup creation unknown, pending is ok
47+
// suspended, pending
48+
rules.ConditionsAny(
49+
rules.ConditionEquals(BackupScheduleConditionBucketStoreReady, metav1.ConditionUnknown, metav1.ConditionFalse),
50+
rules.ConditionEquals(BackupScheduleConditionBackupCreateFailed, metav1.ConditionUnknown),
51+
rules.ConditionEquals(BackupScheduleConditionInternalTaskScheduleFailed, metav1.ConditionUnknown),
52+
),
53+
),
54+
}
55+
756
// MongoDBBackupScheduleSpec defines the desired state of MongoDBBackupSchedule
857
// +kubebuilder:object:generate=true
958
// +k8s:deepcopy-gen=true
@@ -36,9 +85,14 @@ type MongoDBBackupScheduleSpec struct {
3685
// +k8s:deepcopy-gen=true
3786
type MongoDBBackupScheduleStatus struct {
3887
// Phase indicates the overall status of the schedule
39-
// +kubebuilder:validation:Enum=Succeeding;Failing
88+
// +kubebuilder:validation:Enum=Running;Pending;Failed;
4089
Phase string `json:"phase,omitempty"`
4190

91+
// ObservedGeneration is the generation of the schedule that was last processed by the controller
92+
// +kubebuilder:validation:Format=int64
93+
// +kubebuilder:validation:Minimum=0
94+
ObservedGeneration *int64 `json:"observedGeneration,omitempty"`
95+
4296
// LastBackupTime is the time of the last successful backup
4397
LastBackupTime *metav1.Time `json:"lastBackupTime,omitempty"`
4498

@@ -83,3 +137,15 @@ type MongoDBBackupScheduleList struct {
83137
func init() {
84138
SchemeBuilder.Register(&MongoDBBackupSchedule{}, &MongoDBBackupScheduleList{})
85139
}
140+
141+
func (o *MongoDBBackupSchedule) SetPhase(phase string) {
142+
o.Status.Phase = phase
143+
}
144+
145+
func (o *MongoDBBackupSchedule) GetPhase() string {
146+
return o.Status.Phase
147+
}
148+
149+
func (o *MongoDBBackupSchedule) SetObservedGeneration(generation int64) {
150+
o.Status.ObservedGeneration = &generation
151+
}

api/v1alpha1/mongodbbackupstore_types.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
package v1alpha1
22

33
import (
4+
"github.com/RocketChat/airlock/internal/rules"
45
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
56
)
67

8+
const (
9+
StoreConditionBucketExists = "BucketExists"
10+
11+
MongoDBBackupStoreControllerName = "MongoDBBackupStore"
12+
13+
StoreReasonBucketUnknown = "BucketUnknown"
14+
StoreReasonBucketNotExists = "BucketNotExists"
15+
StoreReasonBucketExists = "BucketExists"
16+
17+
StorePhaseNotReady = "NotReady"
18+
StorePhaseReady = "Ready"
19+
)
20+
21+
var BackupStorePhaseRules = []rules.PhaseRule{
22+
rules.NewPhaseRule(
23+
// if bucket exists
24+
StorePhaseReady,
25+
rules.ConditionsAny(
26+
rules.ConditionEquals(StoreConditionBucketExists, metav1.ConditionTrue),
27+
),
28+
),
29+
// if bucket does not exist
30+
rules.NewPhaseRule(
31+
StorePhaseNotReady,
32+
rules.ConditionsAny(
33+
rules.ConditionEquals(StoreConditionBucketExists, metav1.ConditionFalse, metav1.ConditionUnknown),
34+
),
35+
),
36+
}
37+
738
// MongoDBBackupStoreSpec defines the desired state of MongoDBBackupStore
839
// +kubebuilder:object:generate=true
940
// +k8s:deepcopy-gen=true
@@ -38,9 +69,9 @@ type ToKeyMap struct {
3869
// +kubebuilder:object:generate=true
3970
// +k8s:deepcopy-gen=true
4071
type MongoDBBackupStoreStatus struct {
41-
Phase string `json:"phase,omitempty"`
42-
LastTested *metav1.Time `json:"lastTested,omitempty"`
43-
Conditions []metav1.Condition `json:"conditions,omitempty"`
72+
ObservedGeneration *int64 `json:"observedGeneration,omitempty"`
73+
Phase string `json:"phase,omitempty"`
74+
Conditions []metav1.Condition `json:"conditions,omitempty"`
4475
}
4576

4677
// +kubebuilder:object:root=true
@@ -66,3 +97,16 @@ type MongoDBBackupStoreList struct {
6697
func init() {
6798
SchemeBuilder.Register(&MongoDBBackupStore{}, &MongoDBBackupStoreList{})
6899
}
100+
101+
// implements Object2
102+
func (o *MongoDBBackupStore) SetPhase(phase string) {
103+
o.Status.Phase = phase
104+
}
105+
106+
func (o *MongoDBBackupStore) GetPhase() string {
107+
return o.Status.Phase
108+
}
109+
110+
func (o *MongoDBBackupStore) SetObservedGeneration(generation int64) {
111+
o.Status.ObservedGeneration = &generation
112+
}

0 commit comments

Comments
 (0)