This repository was archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcontroller.go
More file actions
151 lines (122 loc) · 3.68 KB
/
controller.go
File metadata and controls
151 lines (122 loc) · 3.68 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/container-object-storage-interface/api/apis/objectstorage.k8s.io/v1alpha1"
bucketclientset "github.com/container-object-storage-interface/api/clientset"
bucketcontroller "github.com/container-object-storage-interface/api/controller"
kubeclientset "k8s.io/client-go/kubernetes"
"github.com/golang/glog"
)
var ctx context.Context
var cmd = &cobra.Command{
Use: os.Args[0],
Short: "Sample controller for listening and responding to bucket* and bucketAccess* API objects",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(c *cobra.Command, args []string) error {
return run(args)
},
DisableFlagsInUseLine: true,
}
var kubeConfig string
func init() {
viper.AutomaticEnv()
cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
flag.Set("logtostderr", "true")
strFlag := func(c *cobra.Command, ptr *string, name string, short string, dfault string, desc string) {
c.PersistentFlags().
StringVarP(ptr, name, short, dfault, desc)
}
strFlag(cmd, &kubeConfig, "kube-config", "", kubeConfig, "path to kubeconfig file")
hideFlag := func(name string) {
cmd.PersistentFlags().MarkHidden(name)
}
hideFlag("alsologtostderr")
hideFlag("log_backtrace_at")
hideFlag("log_dir")
hideFlag("logtostderr")
hideFlag("master")
hideFlag("stderrthreshold")
hideFlag("vmodule")
// suppress the incorrect prefix in glog output
flag.CommandLine.Parse([]string{})
viper.BindPFlags(cmd.PersistentFlags())
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(context.Background())
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGSEGV)
go func() {
s := <-sigs
cancel()
panic(fmt.Sprintf("%s %s", s.String(), "Signal received. Exiting"))
}()
}
func main() {
if err := cmd.Execute(); err != nil {
log.Fatal(err.Error())
}
}
type bucketListener struct {
kubeClient kubeclientset.Interface
bucketClient bucketclientset.Interface
}
func (b *bucketListener) InitializeKubeClient(k kubeclientset.Interface) {
b.kubeClient = k
}
func (b *bucketListener) InitializeBucketClient(bc bucketclientset.Interface) {
b.bucketClient = bc
}
func (b *bucketListener) Add(ctx context.Context, obj *v1alpha1.Bucket) error {
glog.V(1).Infof("add called for bucket %s", obj.Name)
return nil
}
func (b *bucketListener) Update(ctx context.Context, old, new *v1alpha1.Bucket) error {
return nil
}
func (b *bucketListener) Delete(ctx context.Context, obj *v1alpha1.Bucket) error {
return nil
}
type bucketAccessListener struct {
kubeClient kubeclientset.Interface
bucketClient bucketclientset.Interface
count int
}
func (b *bucketAccessListener) InitializeKubeClient(k kubeclientset.Interface) {
b.kubeClient = k
}
func (b *bucketAccessListener) InitializeBucketClient(bc bucketclientset.Interface) {
b.bucketClient = bc
}
func (b *bucketAccessListener) Add(ctx context.Context, obj *v1alpha1.BucketAccess) error {
b.count += 1
if b.count < 5 {
return errors.New("failing")
}
glog.V(1).Infof("add called for bucketAccess %s", obj.Name)
return nil
}
func (b *bucketAccessListener) Update(ctx context.Context, old, new *v1alpha1.BucketAccess) error {
return nil
}
func (b *bucketAccessListener) Delete(ctx context.Context, obj *v1alpha1.BucketAccess) error {
return nil
}
func run(args []string) error {
ctrl, err := bucketcontroller.NewDefaultObjectStorageController("sample-controller", "leader-lock", 40)
if err != nil {
glog.Error(err)
return err
}
ctrl.AddBucketListener(&bucketListener{})
ctrl.AddBucketAccessListener(&bucketAccessListener{})
return ctrl.Run(ctx)
}