Skip to content

Commit 15a24b7

Browse files
Dipta Dastamalsaha
authored andcommitted
Leader election for deployment, replica set and rc (#206)
1 parent 4a75689 commit 15a24b7

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

schedule.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package cmds
22

33
import (
4-
"io/ioutil"
54
"os"
65
"strings"
76
"time"
87

98
"github.com/appscode/go/log"
109
"github.com/appscode/kutil"
10+
api "github.com/appscode/stash/apis/stash/v1alpha1"
1111
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
1212
"github.com/appscode/stash/pkg/scheduler"
1313
"github.com/appscode/stash/pkg/util"
@@ -58,28 +58,22 @@ func NewCmdSchedule() *cobra.Command {
5858
if opt.SnapshotHostname, opt.SmartPrefix, err = opt.Workload.HostnamePrefix(opt.PodName, opt.NodeName); err != nil {
5959
log.Fatalf(err.Error())
6060
}
61-
if err = util.CheckWorkloadExists(kubeClient, opt.Namespace, opt.Workload); err != nil {
61+
if err = util.WorkloadExists(kubeClient, opt.Namespace, opt.Workload); err != nil {
6262
log.Fatalf(err.Error())
6363
}
64-
65-
opt.ScratchDir = strings.TrimSuffix(opt.ScratchDir, "/")
66-
err = os.MkdirAll(opt.ScratchDir, 0755)
67-
if err != nil {
68-
log.Fatalf("Failed to create scratch dir: %s", err)
69-
}
70-
err = ioutil.WriteFile(opt.ScratchDir+"/.stash", []byte("test"), 644)
71-
if err != nil {
72-
log.Fatalf("No write access in scratch dir: %s", err)
73-
}
64+
opt.ScratchDir = strings.TrimSuffix(opt.ScratchDir, "/") // setup ScratchDir in SetupAndRun
7465

7566
ctrl := scheduler.New(kubeClient, stashClient, opt)
76-
err = ctrl.Setup()
77-
if err != nil {
78-
log.Fatalf("Failed to setup scheduler: %s", err)
67+
stopBackup := make(chan struct{})
68+
defer close(stopBackup)
69+
70+
// split code from here for leader election
71+
switch opt.Workload.Kind {
72+
case api.AppKindDeployment, api.AppKindReplicaSet, api.AppKindReplicationController:
73+
ctrl.ElectLeader(stopBackup)
74+
default:
75+
ctrl.SetupAndRun(stopBackup)
7976
}
80-
stop := make(chan struct{})
81-
defer close(stop)
82-
go ctrl.Run(1, stop)
8377

8478
// Wait forever
8579
select {}

0 commit comments

Comments
 (0)