-
Notifications
You must be signed in to change notification settings - Fork 23
/
kuberlogicbackupschedule_types.go
116 lines (99 loc) · 4.08 KB
/
kuberlogicbackupschedule_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* CloudLinux Software Inc 2019-2021 All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package v1
import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type KuberLogicBackupScheduleSpec struct {
// Type of the backup storage
// +kubebuilder:validation:Enum=s3
Type string `json:"type"`
// Cluster name
// TODO: need to implement validation based on webhook - https://book.kubebuilder.io/cronjob-tutorial/webhook-implementation.html
// it gives the ability to check cluster name is exists or not
ClusterName string `json:"name"`
// credentials for storage type
SecretName string `json:"secret"`
// schedule for the backup
Schedule string `json:"schedule"`
// what database need to backup
Database string `json:"database,omitempty"`
}
// KuberLogicBackupScheduleStatus defines the observed state of KuberLogicBackupSchedule
type KuberLogicBackupScheduleStatus struct {
Conditions []metav1.Condition `json:"conditions"`
}
// +kubebuilder:object:root=true
// +kubebuilder:printcolumn:name="Last backup status",type=string,JSONPath=".status.conditions[?(@.type == 'LastBackupSuccessful')].reason",description="Current backup status"
// +kubebuilder:printcolumn:name="Cluster name",type=string,JSONPath=`.spec.name`,description="The cluster name"
// +kubebuilder:printcolumn:name="Type",type=string,JSONPath=`.spec.type`,description="The backup type"
// +kubebuilder:printcolumn:name="Schedule",type=string,JSONPath=`.spec.schedule`,description="The backup schedule"
// +kubebuilder:resource:shortName=klb,categories=kuberlogic
// +kubebuilder:subresource:status
type KuberLogicBackupSchedule struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec KuberLogicBackupScheduleSpec `json:"spec,omitempty"`
Status KuberLogicBackupScheduleStatus `json:"status,omitempty"`
}
// KuberLogicBackupScheduleList contains a list of KuberLogicBackupSchedule
// +kubebuilder:object:root=true
type KuberLogicBackupScheduleList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []KuberLogicBackupSchedule `json:"items"`
}
const (
lastJobStateCond = "LastBackupSuccessful"
runningCondType = "Running"
)
func (klb *KuberLogicBackupSchedule) MarkFailed(r string) {
klb.setConditionStatus(lastJobStateCond, false, r, BackupFailedStatus)
}
func (klb *KuberLogicBackupSchedule) MarkUnknown(m string) {
klb.setConditionStatus(lastJobStateCond, false, m, BackupUnknownStatus)
}
func (klb *KuberLogicBackupSchedule) MarkSuccessful(m string) {
klb.setConditionStatus(lastJobStateCond, true, m, BackupSuccessStatus)
}
func (klb *KuberLogicBackupSchedule) MarkRunning(j string) {
klb.setConditionStatus(runningCondType, true, j+" backup job is running", "BackupJobRunning")
}
func (klb *KuberLogicBackupSchedule) MarkNotRunning() {
klb.setConditionStatus(runningCondType, false, "", "NoJobRunning")
}
func (klb *KuberLogicBackupSchedule) IsRunning() bool {
return meta.IsStatusConditionTrue(klb.Status.Conditions, runningCondType)
}
func (klb *KuberLogicBackupSchedule) IsSuccessful() bool {
return meta.IsStatusConditionTrue(klb.Status.Conditions, lastJobStateCond)
}
func (klb *KuberLogicBackupSchedule) setConditionStatus(cond string, status bool, msg, reason string) {
c := metav1.Condition{
Type: cond,
Status: metav1.ConditionFalse,
Message: msg,
Reason: reason,
}
if status {
c.Status = metav1.ConditionTrue
}
meta.SetStatusCondition(&klb.Status.Conditions, c)
}
func init() {
SchemeBuilder.Register(&KuberLogicBackupSchedule{}, &KuberLogicBackupScheduleList{})
}