Skip to content

Commit 0b1d530

Browse files
authored
Merge pull request #149 from hasbro17/haseeb/add-resync-period-option
*: add resyncPeriod arg to Watch
2 parents 5e96f27 + 4b1f9d3 commit 0b1d530

File tree

5 files changed

+10
-7
lines changed

5 files changed

+10
-7
lines changed

doc/milestone/user_guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ By default, the memcached-operator watches `Memcached` resource events as shown
5151

5252
```Go
5353
func main() {
54-
sdk.Watch("cache.example.com/v1alpha1", "Memcached", "default")
54+
sdk.Watch("cache.example.com/v1alpha1", "Memcached", "default", 5)
5555
sdk.Handle(stub.NewHandler())
5656
sdk.Run(context.TODO())
5757
}

pkg/generator/generator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func printVersion() {
5050
5151
func main() {
5252
printVersion()
53-
sdk.Watch("app.example.com/v1alpha1", "App", "default")
53+
sdk.Watch("app.example.com/v1alpha1", "App", "default", 5)
5454
sdk.Handle(stub.NewHandler())
5555
sdk.Run(context.TODO())
5656
}

pkg/generator/main_tmpl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func printVersion() {
3636
3737
func main() {
3838
printVersion()
39-
sdk.Watch("{{.APIVersion}}", "{{.Kind}}", "default")
39+
sdk.Watch("{{.APIVersion}}", "{{.Kind}}", "default", 5)
4040
sdk.Handle(stub.NewHandler())
4141
sdk.Run(context.TODO())
4242
}

pkg/sdk/api.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,19 @@ var (
3535
// - Pods have Group "Core" and Version "v1" giving the APIVersion "v1"
3636
// - The custom resource Memcached might have Group "cache.example.com" and Version "v1alpha1" giving the APIVersion "cache.example.com/v1alpha1"
3737
// kind is the Kind of the resource, e.g "Pod" for pods
38+
// resyncPeriod is the time period in seconds for how often an event with the latest resource version will be sent to the handler, even if there is no change.
39+
// - 0 means no periodic events will be sent
3840
// Consult the API reference for the Group, Version and Kind of a resource: https://kubernetes.io/docs/reference/
3941
// namespace is the Namespace to watch for the resource
4042
// TODO: support opts for specifying label selector
41-
func Watch(apiVersion, kind, namespace string) {
43+
func Watch(apiVersion, kind, namespace string, resyncPeriod int) {
4244
resourceClient, resourcePluralName, err := k8sclient.GetResourceClient(apiVersion, kind, namespace)
4345
// TODO: Better error handling, e.g retry
4446
if err != nil {
4547
logrus.Errorf("failed to get resource client for (apiVersion:%s, kind:%s, ns:%s): %v", apiVersion, kind, namespace, err)
4648
panic(err)
4749
}
48-
informer := sdkInformer.New(resourcePluralName, namespace, resourceClient)
50+
informer := sdkInformer.New(resourcePluralName, namespace, resourceClient, resyncPeriod)
4951
informers = append(informers, informer)
5052
}
5153

pkg/sdk/informer/informer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ type informer struct {
4141
context context.Context
4242
}
4343

44-
func New(resourcePluralName, namespace string, resourceClient dynamic.ResourceInterface) Informer {
44+
func New(resourcePluralName, namespace string, resourceClient dynamic.ResourceInterface, resyncPeriod int) Informer {
4545
i := &informer{
4646
resourcePluralName: resourcePluralName,
4747
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), resourcePluralName),
4848
namespace: namespace,
4949
}
5050

51+
resyncDuration := time.Duration(resyncPeriod) * time.Second
5152
i.sharedIndexInformer = cache.NewSharedIndexInformer(
52-
newListWatcherFromResourceClient(resourceClient), &unstructured.Unstructured{}, 0, cache.Indexers{},
53+
newListWatcherFromResourceClient(resourceClient), &unstructured.Unstructured{}, resyncDuration, cache.Indexers{},
5354
)
5455
i.sharedIndexInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
5556
AddFunc: i.handleAddResourceEvent,

0 commit comments

Comments
 (0)