Skip to content

Commit 86a4af0

Browse files
hossainemruztamalsaha
authored andcommitted
Skip BackupSession creation if target does not exist + use timestamp … (#797)
* Skip BackupSession creation if target does not exist + use timestamp as BackupSession name suffix
1 parent 3abe6f6 commit 86a4af0

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

create_backupsession.go

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
package cmds
22

33
import (
4-
"github.com/appscode/go/crypto/rand"
4+
"fmt"
5+
"strings"
6+
"time"
7+
58
"github.com/appscode/go/log"
69
"github.com/spf13/cobra"
710
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
811
"k8s.io/client-go/kubernetes"
912
"k8s.io/client-go/tools/clientcmd"
1013
"k8s.io/client-go/tools/reference"
14+
"k8s.io/kubernetes/pkg/apis/core"
1115
core_util "kmodules.xyz/client-go/core/v1"
16+
"kmodules.xyz/client-go/discovery"
1217
"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"
1322
api_v1beta1 "stash.appscode.dev/stash/apis/stash/v1beta1"
1423
cs "stash.appscode.dev/stash/client/clientset/versioned"
1524
stash_scheme "stash.appscode.dev/stash/client/clientset/versioned/scheme"
1625
v1beta1_util "stash.appscode.dev/stash/client/clientset/versioned/typed/stash/v1beta1/util"
26+
"stash.appscode.dev/stash/pkg/eventer"
1727
"stash.appscode.dev/stash/pkg/util"
1828
)
1929

2030
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
2537
}
2638

2739
func NewCmdCreateBackupSession() *cobra.Command {
@@ -45,6 +57,11 @@ func NewCmdCreateBackupSession() *cobra.Command {
4557
}
4658
opt.k8sClient = kubernetes.NewForConfigOrDie(config)
4759
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+
}
4865

4966
err = opt.createBackupSession()
5067
if err != nil {
@@ -64,7 +81,8 @@ func NewCmdCreateBackupSession() *cobra.Command {
6481

6582
func (opt *options) createBackupSession() error {
6683
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()),
6886
Namespace: opt.namespace,
6987
OwnerReferences: []metav1.OwnerReference{},
7088
}
@@ -74,15 +92,36 @@ func (opt *options) createBackupSession() error {
7492
}
7593
// skip if BackupConfiguration paused
7694
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)
79116
}
117+
118+
// create BackupSession
80119
ref, err := reference.GetReference(stash_scheme.Scheme, backupConfiguration)
81120
if err != nil {
82121
return err
83122
}
84123
_, _, 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
86125
core_util.EnsureOwnerReference(&in.ObjectMeta, ref)
87126
in.Spec.BackupConfiguration.Name = opt.name
88127
if in.Labels == nil {
@@ -94,3 +133,15 @@ func (opt *options) createBackupSession() error {
94133
})
95134
return err
96135
}
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

Comments
 (0)