Skip to content

Commit

Permalink
Update/k8s client (#3)
Browse files Browse the repository at this point in the history
* update k8s client to 0.22.12

* Refactored kubehandler to contain context

* changes to mark a new module of kubehandler since changes are backward incompatible

* removed use of stop channel and replaced it with context

* updated README.md to reflect new interface
Mentioned note about K8s compatibility and v0 branch
  • Loading branch information
punit-kulal authored Aug 5, 2022
1 parent 0bd438f commit 486a6ac
Show file tree
Hide file tree
Showing 10 changed files with 554 additions and 151 deletions.
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ type EventHandler interface {
GetName() string
GetSynced() cache.InformerSynced
GetInformer() cache.SharedInformer
AddFunc(namespace, name string) error
UpdateFunc(namespace, name string) error
DeleteFunc(namespace, name string) error
AddFunc(ctx context.Context, namespace, name string) error
UpdateFunc(ctx context.Context, namespace, name string) error
DeleteFunc(ctx context.Context, namespace, name string) error
}
```

Each of the Add/Update/Delete Funcs receive the namespace and the name of the
Each of the Add/Update/Delete Funcs receive a context and the namespace and the name of the
resource that has been modified (or created or deleted).
`kubehandler.DefaultHandler` implements a DefaultHandler that accepts all
events and does nothing. In order to make use of this behaviour, you can use
Expand Down Expand Up @@ -60,15 +60,18 @@ Finally, run the EventLoop.

```
threadiness := 2
stopCh := make(chan struct{})
ctx := context.Background()
loop.Run(threadiness, stopCh)
loop.Run(ctx, threadiness)
```

You may use the channel passed in to `EventLoop.Run` to stop the EventLoop.
You may use the context passed in to `EventLoop.Run` to stop the EventLoop.

```
close(stopCh)
ctx, cancelFunc := context.WithCancel(context.Background())
loop.Run(ctx, threadiness)
cancelFunc()
```

## DefaultHandler
Expand All @@ -85,7 +88,10 @@ Returns defaultHandler.Informer

The following functions are no-ops.
```
AddFunc(namespace, name string) error
UpdateFunc(namespace, name string) error
DeleteFunc(namespace, name string) error
AddFunc(ctx context.Context, namespace, name string) error
UpdateFunc(ctx context.Context, namespace, name string) error
DeleteFunc(ctx context.Context, namespace, name string) error
```

Note: The current module supports Kubernetes 1.22 and above.
Kubernetes 1.21 and lower compatible module can be found in branch v0.
12 changes: 8 additions & 4 deletions defaulthandler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package kubehandler

import "k8s.io/client-go/tools/cache"
import (
"context"

"k8s.io/client-go/tools/cache"
)

type DefaultHandler struct {
Synced cache.InformerSynced
Expand All @@ -18,14 +22,14 @@ func (handler *DefaultHandler) GetInformer() cache.SharedInformer {
return handler.Informer
}

func (handler *DefaultHandler) AddFunc(namespace, name string) error {
func (handler *DefaultHandler) AddFunc(ctx context.Context, namespace, name string) error {
return nil
}

func (handler *DefaultHandler) UpdateFunc(namespace, name string) error {
func (handler *DefaultHandler) UpdateFunc(ctx context.Context, namespace, name string) error {
return nil
}

func (handler *DefaultHandler) DeleteFunc(namespace, name string) error {
func (handler *DefaultHandler) DeleteFunc(ctx context.Context, namespace, name string) error {
return nil
}
12 changes: 8 additions & 4 deletions eventhandler.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package kubehandler

import "k8s.io/client-go/tools/cache"
import (
"context"

"k8s.io/client-go/tools/cache"
)

//EventHandler represents Event Handling for a Resource
type EventHandler interface {
GetName() string
GetSynced() cache.InformerSynced
GetInformer() cache.SharedInformer
AddFunc(namespace, name string) error
UpdateFunc(namespace, name string) error
DeleteFunc(namespace, name string) error
AddFunc(ctx context.Context, namespace, name string) error
UpdateFunc(ctx context.Context, namespace, name string) error
DeleteFunc(ctx context.Context, namespace, name string) error
}
7 changes: 4 additions & 3 deletions eventloop.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package kubehandler

import (
"context"
"reflect"

"k8s.io/client-go/tools/cache"
)

//EventLoop represents a central EventHandler registry which runs in a loop
type EventLoop interface {
Run(threadiness int, stopCh <-chan struct{}) error
Run(ctx context.Context, threadiness int) error
Register(handler EventHandler)
}

type eventLoop struct {
workqueue WorkQueue
}

func (loop *eventLoop) Run(threadiness int, stopCh <-chan struct{}) error {
return loop.workqueue.Run(threadiness, stopCh)
func (loop *eventLoop) Run(ctx context.Context, threadiness int) error {
return loop.workqueue.Run(ctx, threadiness)
}

func (loop *eventLoop) Register(handler EventHandler) {
Expand Down
5 changes: 3 additions & 2 deletions eventloop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
)

func TestShouldGetResourceVersionFromK8sObjects(t *testing.T) {
assert := assert.New(t)
var route interface{}
route = routev1.Route{ObjectMeta: metav1.ObjectMeta{ResourceVersion: "test"}}
assert.NotNil(t, route)

version, ok := resourceVersion(route)
assert.True(t, ok)
assert.Equal(t, "test", version)
assert.True(ok)
assert.Equal("test", version)
}
63 changes: 35 additions & 28 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
module github.com/gojektech/kubehandler
module github.com/gojektech/kubehandler/v2

go 1.12
go 1.18

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/gogo/protobuf v1.0.0 // indirect
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
github.com/google/btree v0.0.0-20180124185431-e89373fe6b4a // indirect
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect
github.com/googleapis/gnostic v0.1.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/golang-lru v0.0.0-20180201235237-0fb14efe8c47 // indirect
github.com/imdario/mergo v0.3.4 // indirect
github.com/json-iterator/go v1.1.5 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v0.0.0-20180228065516-1df9eeb2bb81 // indirect
github.com/openshift/api v3.9.0+incompatible
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/stretchr/testify v1.7.0
k8s.io/api v0.22.12
k8s.io/apimachinery v0.22.12
k8s.io/client-go v0.22.12
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v0.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/imdario/mergo v0.3.5 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.1 // indirect
github.com/stretchr/testify v1.2.1
golang.org/x/crypto v0.0.0-20180411161317-d6449816ce06 // indirect
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421 // indirect
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 // indirect
golang.org/x/sys v0.0.0-20180406135729-3b87a42e500a // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.2.1 // indirect
k8s.io/api v0.0.0-20190222213804-5cb15d344471
k8s.io/apimachinery v0.0.0-20190221213512-86fb29eff628
k8s.io/client-go v10.0.0+incompatible
k8s.io/klog v0.2.0 // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/klog/v2 v2.9.0 // indirect
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)
Loading

0 comments on commit 486a6ac

Please sign in to comment.