1
1
package cmds
2
2
3
3
import (
4
- "github.com/appscode/go/crypto/rand"
4
+ "fmt"
5
+ "strings"
6
+ "time"
7
+
5
8
"github.com/appscode/go/log"
6
9
"github.com/spf13/cobra"
7
10
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8
11
"k8s.io/client-go/kubernetes"
9
12
"k8s.io/client-go/tools/clientcmd"
10
13
"k8s.io/client-go/tools/reference"
14
+ "k8s.io/kubernetes/pkg/apis/core"
11
15
core_util "kmodules.xyz/client-go/core/v1"
16
+ "kmodules.xyz/client-go/discovery"
12
17
"kmodules.xyz/client-go/meta"
18
+ appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
19
+ ocapps "kmodules.xyz/openshift/apis/apps/v1"
20
+ oc_cs "kmodules.xyz/openshift/client/clientset/versioned"
21
+ "stash.appscode.dev/stash/apis"
13
22
api_v1beta1 "stash.appscode.dev/stash/apis/stash/v1beta1"
14
23
cs "stash.appscode.dev/stash/client/clientset/versioned"
15
24
stash_scheme "stash.appscode.dev/stash/client/clientset/versioned/scheme"
16
25
v1beta1_util "stash.appscode.dev/stash/client/clientset/versioned/typed/stash/v1beta1/util"
26
+ "stash.appscode.dev/stash/pkg/eventer"
17
27
"stash.appscode.dev/stash/pkg/util"
18
28
)
19
29
20
30
type options struct {
21
- name string
22
- namespace string
23
- k8sClient kubernetes.Interface
24
- stashClient cs.Interface
31
+ name string
32
+ namespace string
33
+ k8sClient kubernetes.Interface
34
+ stashClient cs.Interface
35
+ appcatalogClient appcatalog_cs.Interface
36
+ ocClient oc_cs.Interface
25
37
}
26
38
27
39
func NewCmdCreateBackupSession () * cobra.Command {
@@ -45,6 +57,11 @@ func NewCmdCreateBackupSession() *cobra.Command {
45
57
}
46
58
opt .k8sClient = kubernetes .NewForConfigOrDie (config )
47
59
opt .stashClient = cs .NewForConfigOrDie (config )
60
+ opt .appcatalogClient = appcatalog_cs .NewForConfigOrDie (config )
61
+ // if cluster has OpenShift DeploymentConfig then generate OcClient
62
+ if discovery .IsPreferredAPIResource (opt .k8sClient .Discovery (), ocapps .GroupVersion .String (), apis .KindDeploymentConfig ) {
63
+ opt .ocClient = oc_cs .NewForConfigOrDie (config )
64
+ }
48
65
49
66
err = opt .createBackupSession ()
50
67
if err != nil {
@@ -64,7 +81,8 @@ func NewCmdCreateBackupSession() *cobra.Command {
64
81
65
82
func (opt * options ) createBackupSession () error {
66
83
bsMeta := metav1.ObjectMeta {
67
- Name : rand .WithUniqSuffix (opt .name ),
84
+ // Name format: <BackupConfiguration name>-<timestamp in unix format>
85
+ Name : fmt .Sprintf ("%s-%d" , opt .name , time .Now ().Unix ()),
68
86
Namespace : opt .namespace ,
69
87
OwnerReferences : []metav1.OwnerReference {},
70
88
}
@@ -74,15 +92,36 @@ func (opt *options) createBackupSession() error {
74
92
}
75
93
// skip if BackupConfiguration paused
76
94
if backupConfiguration .Spec .Paused {
77
- log .Infof ("Skipping creating BackupSession. Reason: Backup Configuration %s/%s is paused." , backupConfiguration .Namespace , backupConfiguration .Name )
78
- return nil
95
+ msg := fmt .Sprintf ("Skipping creating BackupSession. Reason: Backup Configuration %s/%s is paused." , backupConfiguration .Namespace , backupConfiguration .Name )
96
+ log .Infoln (msg )
97
+
98
+ // write event to BackupConfiguration denoting that backup session has been skipped
99
+ return writeBackupSessionSkippedEvent (opt .k8sClient , backupConfiguration , msg )
100
+ }
101
+ // if target does not exist then skip creating BackupSession
102
+ wc := util.WorkloadClients {
103
+ KubeClient : opt .k8sClient ,
104
+ StashClient : opt .stashClient ,
105
+ AppCatalogClient : opt .appcatalogClient ,
106
+ OcClient : opt .ocClient ,
107
+ }
108
+
109
+ if backupConfiguration .Spec .Target != nil && ! wc .IsTargetExist (backupConfiguration .Spec .Target .Ref , backupConfiguration .Namespace ) {
110
+ msg := fmt .Sprintf ("Skipping creating BackupSession. Reason: Target workload %s/%s does not exist." ,
111
+ strings .ToLower (backupConfiguration .Spec .Target .Ref .Kind ), backupConfiguration .Spec .Target .Ref .Name )
112
+ log .Infoln (msg )
113
+
114
+ // write event to BackupConfiguration denoting that backup session has been skipped
115
+ return writeBackupSessionSkippedEvent (opt .k8sClient , backupConfiguration , msg )
79
116
}
117
+
118
+ // create BackupSession
80
119
ref , err := reference .GetReference (stash_scheme .Scheme , backupConfiguration )
81
120
if err != nil {
82
121
return err
83
122
}
84
123
_ , _ , err = v1beta1_util .CreateOrPatchBackupSession (opt .stashClient .StashV1beta1 (), bsMeta , func (in * api_v1beta1.BackupSession ) * api_v1beta1.BackupSession {
85
- // Set Backupconfiguration as Backupsession Owner
124
+ // Set BackupConfiguration as BackupSession Owner
86
125
core_util .EnsureOwnerReference (& in .ObjectMeta , ref )
87
126
in .Spec .BackupConfiguration .Name = opt .name
88
127
if in .Labels == nil {
@@ -94,3 +133,15 @@ func (opt *options) createBackupSession() error {
94
133
})
95
134
return err
96
135
}
136
+
137
+ func writeBackupSessionSkippedEvent (kubeClient kubernetes.Interface , backupConfiguration * api_v1beta1.BackupConfiguration , msg string ) error {
138
+ _ , err := eventer .CreateEvent (
139
+ kubeClient ,
140
+ eventer .EventSourceBackupTriggeringCronJob ,
141
+ backupConfiguration ,
142
+ core .EventTypeNormal ,
143
+ eventer .EventReasonBackupSkipped ,
144
+ msg ,
145
+ )
146
+ return err
147
+ }
0 commit comments