Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch ints to time.Duration #427

Merged
merged 7 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ const mainExp = `package main
import (
"context"
"runtime"
"time"

stub "github.com/example-inc/app-operator/pkg/stub"
sdk "github.com/operator-framework/operator-sdk/pkg/sdk"
Expand Down Expand Up @@ -445,7 +446,7 @@ func main() {
if err != nil {
logrus.Fatalf("failed to get watch namespace: %v", err)
}
resyncPeriod := 5
resyncPeriod := time.Duration(5) * time.Second
logrus.Infof("Watching %s, %s, %s, %d", resource, kind, namespace, resyncPeriod)
sdk.Watch(resource, kind, namespace, resyncPeriod)
sdk.Handle(stub.NewHandler())
Expand Down
3 changes: 2 additions & 1 deletion pkg/generator/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ const mainTmpl = `package main
import (
"context"
"runtime"
"time"

stub "{{.StubImport}}"
sdk "{{.OperatorSDKImport}}"
Expand Down Expand Up @@ -159,7 +160,7 @@ func main() {
if err != nil {
logrus.Fatalf("failed to get watch namespace: %v", err)
}
resyncPeriod := 5
resyncPeriod := time.Duration(5) * time.Second
logrus.Infof("Watching %s, %s, %s, %d", resource, kind, namespace, resyncPeriod)
sdk.Watch(resource, kind, namespace, resyncPeriod)
sdk.Handle(stub.NewHandler())
Expand Down
5 changes: 3 additions & 2 deletions pkg/sdk/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package sdk

import (
"context"
"time"

"github.com/operator-framework/operator-sdk/pkg/k8sclient"
"github.com/operator-framework/operator-sdk/pkg/sdk/internal/metrics"
Expand All @@ -35,12 +36,12 @@ var (
// - Pods have Group "Core" and Version "v1" giving the APIVersion "v1"
// - The custom resource Memcached might have Group "cache.example.com" and Version "v1alpha1" giving the APIVersion "cache.example.com/v1alpha1"
// kind is the Kind of the resource, e.g "Pod" for pods
// 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.
// resyncPeriod is the time period for how often an event with the latest resource version will be sent to the handler, even if there is no change.
// - 0 means no periodic events will be sent
// Consult the API reference for the Group, Version and Kind of a resource: https://kubernetes.io/docs/reference/
// namespace is the Namespace to watch for the resource
// TODO: support opts for specifying label selector
func Watch(apiVersion, kind, namespace string, resyncPeriod int, opts ...watchOption) {
func Watch(apiVersion, kind, namespace string, resyncPeriod time.Duration, opts ...watchOption) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update the godoc for this change as well?

resourceClient, resourcePluralName, err := k8sclient.GetResourceClient(apiVersion, kind, namespace)
// TODO: Better error handling, e.g retry
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions pkg/sdk/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type informer struct {
numWorkers int
}

func NewInformer(resourcePluralName, namespace string, resourceClient dynamic.ResourceInterface, resyncPeriod int, c *metrics.Collector, n int) Informer {
func NewInformer(resourcePluralName, namespace string, resourceClient dynamic.ResourceInterface, resyncPeriod time.Duration, c *metrics.Collector, n int) Informer {
i := &informer{
resourcePluralName: resourcePluralName,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), resourcePluralName),
Expand All @@ -56,9 +56,8 @@ func NewInformer(resourcePluralName, namespace string, resourceClient dynamic.Re
numWorkers: n,
}

resyncDuration := time.Duration(resyncPeriod) * time.Second
i.sharedIndexInformer = cache.NewSharedIndexInformer(
newListWatcherFromResourceClient(resourceClient), &unstructured.Unstructured{}, resyncDuration, cache.Indexers{},
newListWatcherFromResourceClient(resourceClient), &unstructured.Unstructured{}, resyncPeriod, cache.Indexers{},
)
i.sharedIndexInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: i.handleAddResourceEvent,
Expand Down