Skip to content

Commit 7152fb7

Browse files
authored
pkg/api: switch ints to time.Duration
* Switch ints to time.duration * update templates * line spacing * fix import order * updated godoc and removed multiplier * move from ns to seconds * update godoc
1 parent b3413a6 commit 7152fb7

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

pkg/generator/generator_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ const mainExp = `package main
418418
import (
419419
"context"
420420
"runtime"
421+
"time"
421422
422423
stub "github.com/example-inc/app-operator/pkg/stub"
423424
sdk "github.com/operator-framework/operator-sdk/pkg/sdk"
@@ -445,7 +446,7 @@ func main() {
445446
if err != nil {
446447
logrus.Fatalf("failed to get watch namespace: %v", err)
447448
}
448-
resyncPeriod := 5
449+
resyncPeriod := time.Duration(5) * time.Second
449450
logrus.Infof("Watching %s, %s, %s, %d", resource, kind, namespace, resyncPeriod)
450451
sdk.Watch(resource, kind, namespace, resyncPeriod)
451452
sdk.Handle(stub.NewHandler())

pkg/generator/templates.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ const mainTmpl = `package main
132132
import (
133133
"context"
134134
"runtime"
135+
"time"
135136
136137
stub "{{.StubImport}}"
137138
sdk "{{.OperatorSDKImport}}"
@@ -159,7 +160,7 @@ func main() {
159160
if err != nil {
160161
logrus.Fatalf("failed to get watch namespace: %v", err)
161162
}
162-
resyncPeriod := 5
163+
resyncPeriod := time.Duration(5) * time.Second
163164
logrus.Infof("Watching %s, %s, %s, %d", resource, kind, namespace, resyncPeriod)
164165
sdk.Watch(resource, kind, namespace, resyncPeriod)
165166
sdk.Handle(stub.NewHandler())

pkg/sdk/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package sdk
1616

1717
import (
1818
"context"
19+
"time"
1920

2021
"github.com/operator-framework/operator-sdk/pkg/k8sclient"
2122
"github.com/operator-framework/operator-sdk/pkg/sdk/internal/metrics"
@@ -35,12 +36,12 @@ var (
3536
// - Pods have Group "Core" and Version "v1" giving the APIVersion "v1"
3637
// - The custom resource Memcached might have Group "cache.example.com" and Version "v1alpha1" giving the APIVersion "cache.example.com/v1alpha1"
3738
// 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+
// 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.
3940
// - 0 means no periodic events will be sent
4041
// Consult the API reference for the Group, Version and Kind of a resource: https://kubernetes.io/docs/reference/
4142
// namespace is the Namespace to watch for the resource
4243
// TODO: support opts for specifying label selector
43-
func Watch(apiVersion, kind, namespace string, resyncPeriod int, opts ...watchOption) {
44+
func Watch(apiVersion, kind, namespace string, resyncPeriod time.Duration, opts ...watchOption) {
4445
resourceClient, resourcePluralName, err := k8sclient.GetResourceClient(apiVersion, kind, namespace)
4546
// TODO: Better error handling, e.g retry
4647
if err != nil {

pkg/sdk/informer.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type informer struct {
4646
numWorkers int
4747
}
4848

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

59-
resyncDuration := time.Duration(resyncPeriod) * time.Second
6059
i.sharedIndexInformer = cache.NewSharedIndexInformer(
61-
newListWatcherFromResourceClient(resourceClient), &unstructured.Unstructured{}, resyncDuration, cache.Indexers{},
60+
newListWatcherFromResourceClient(resourceClient), &unstructured.Unstructured{}, resyncPeriod, cache.Indexers{},
6261
)
6362
i.sharedIndexInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
6463
AddFunc: i.handleAddResourceEvent,

0 commit comments

Comments
 (0)