Skip to content

Commit 9c1cc43

Browse files
authored
Use workqueue (#182)
1 parent b3ee076 commit 9c1cc43

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"strings"
77

88
v "github.com/appscode/go/version"
9+
"github.com/appscode/stash/client/scheme"
910
"github.com/jpillora/go-ogle-analytics"
1011
"github.com/spf13/cobra"
1112
"github.com/spf13/pflag"
13+
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
1214
)
1315

1416
const (
@@ -34,6 +36,7 @@ func NewCmdStash(version string) *cobra.Command {
3436
client.Send(ga.NewEvent(parts[0], strings.Join(parts[1:], "/")).Label(version))
3537
}
3638
}
39+
scheme.AddToScheme(clientsetscheme.Scheme)
3740
},
3841
}
3942
rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)

run.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import (
99
stringz "github.com/appscode/go/strings"
1010
"github.com/appscode/pat"
1111
sapi "github.com/appscode/stash/apis/stash"
12-
scs "github.com/appscode/stash/client/typed/stash/v1alpha1"
12+
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
1313
"github.com/appscode/stash/pkg/controller"
1414
"github.com/appscode/stash/pkg/docker"
1515
"github.com/appscode/stash/pkg/migrator"
1616
"github.com/prometheus/client_golang/prometheus/promhttp"
1717
"github.com/spf13/cobra"
1818
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
19-
clientset "k8s.io/client-go/kubernetes"
19+
"k8s.io/client-go/kubernetes"
2020
"k8s.io/client-go/tools/clientcmd"
2121
)
2222

2323
var (
24-
kubeClient clientset.Interface
25-
stashClient scs.ResticsGetter
24+
kubeClient kubernetes.Interface
25+
stashClient cs.StashV1alpha1Interface
2626

2727
scratchDir string = "/tmp"
2828
)
@@ -31,29 +31,32 @@ func NewCmdRun(version string) *cobra.Command {
3131
var (
3232
masterURL string
3333
kubeconfigPath string
34-
tag string = stringz.Val(version, "canary")
35-
address string = ":56790"
36-
resyncPeriod time.Duration = 5 * time.Minute
34+
address string = ":56790"
35+
opts = controller.Options{
36+
SidecarImageTag: stringz.Val(version, "canary"),
37+
ResyncPeriod: 5 * time.Minute,
38+
MaxNumRequeues: 5,
39+
}
3740
)
3841

3942
cmd := &cobra.Command{
4043
Use: "run",
4144
Short: "Run Stash operator",
4245
DisableAutoGenTag: true,
4346
Run: func(cmd *cobra.Command, args []string) {
44-
if err := docker.CheckDockerImageVersion(docker.ImageOperator, tag); err != nil {
45-
log.Fatalf(`Image %v:%v not found.`, docker.ImageOperator, tag)
47+
if err := docker.CheckDockerImageVersion(docker.ImageOperator, opts.SidecarImageTag); err != nil {
48+
log.Fatalf(`Image %v:%v not found.`, docker.ImageOperator, opts.SidecarImageTag)
4649
}
4750

4851
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
4952
if err != nil {
5053
log.Fatalln(err)
5154
}
52-
kubeClient = clientset.NewForConfigOrDie(config)
53-
stashClient = scs.NewForConfigOrDie(config)
55+
kubeClient = kubernetes.NewForConfigOrDie(config)
56+
stashClient = cs.NewForConfigOrDie(config)
5457
crdClient := apiextensionsclient.NewForConfigOrDie(config)
5558

56-
ctrl := controller.New(kubeClient, crdClient, stashClient, tag, resyncPeriod)
59+
ctrl := controller.New(kubeClient, crdClient, stashClient, opts)
5760
err = ctrl.Setup()
5861
if err != nil {
5962
log.Fatalln(err)
@@ -64,7 +67,10 @@ func NewCmdRun(version string) *cobra.Command {
6467
}
6568

6669
log.Infoln("Starting operator...")
67-
ctrl.Run()
70+
// Now let's start the controller
71+
stop := make(chan struct{})
72+
defer close(stop)
73+
go ctrl.Run(1, stop)
6874

6975
m := pat.New()
7076
m.Get("/metrics", promhttp.Handler())
@@ -82,7 +88,7 @@ func NewCmdRun(version string) *cobra.Command {
8288
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
8389
cmd.Flags().StringVar(&address, "address", address, "Address to listen on for web interface and telemetry.")
8490
cmd.Flags().StringVar(&scratchDir, "scratch-dir", scratchDir, "Directory used to store temporary files. Use an `emptyDir` in Kubernetes.")
85-
cmd.Flags().DurationVar(&resyncPeriod, "resync-period", resyncPeriod, "If non-zero, will re-list this often. Otherwise, re-list will be delayed aslong as possible (until the upstream source closes the watch or times out.")
91+
cmd.Flags().DurationVar(&opts.ResyncPeriod, "resync-period", opts.ResyncPeriod, "If non-zero, will re-list this often. Otherwise, re-list will be delayed aslong as possible (until the upstream source closes the watch or times out.")
8692

8793
return cmd
8894
}

schedule.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88

99
"github.com/appscode/go/log"
1010
"github.com/appscode/kutil"
11-
scs "github.com/appscode/stash/client/typed/stash/v1alpha1"
11+
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
1212
"github.com/appscode/stash/pkg/scheduler"
1313
"github.com/spf13/cobra"
1414
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15-
clientset "k8s.io/client-go/kubernetes"
15+
"k8s.io/client-go/kubernetes"
1616
"k8s.io/client-go/tools/clientcmd"
1717
)
1818

@@ -40,8 +40,8 @@ func NewCmdSchedule() *cobra.Command {
4040
if err != nil {
4141
log.Fatalf("Could not get Kubernetes config: %s", err)
4242
}
43-
kubeClient = clientset.NewForConfigOrDie(config)
44-
stashClient = scs.NewForConfigOrDie(config)
43+
kubeClient = kubernetes.NewForConfigOrDie(config)
44+
stashClient = cs.NewForConfigOrDie(config)
4545

4646
opt.NodeName = os.Getenv("NODE_NAME")
4747
if opt.NodeName == "" {

snapshot_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
_ "net/http/pprof"
77

88
"github.com/appscode/pat"
9-
sapi "github.com/appscode/stash/apis/stash/v1alpha1"
9+
api "github.com/appscode/stash/apis/stash/v1alpha1"
1010
"github.com/appscode/stash/pkg/cli"
1111
kerr "k8s.io/apimachinery/pkg/api/errors"
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -37,7 +37,7 @@ func ExportSnapshots(w http.ResponseWriter, r *http.Request) {
3737
}
3838
resticCLI := cli.New(scratchDir)
3939

40-
var resource *sapi.Restic
40+
var resource *api.Restic
4141
resource, err := stashClient.Restics(namespace).Get(name, metav1.GetOptions{})
4242
if kerr.IsNotFound(err) {
4343
http.Error(w, err.Error(), http.StatusNotFound)

0 commit comments

Comments
 (0)