Skip to content

Commit

Permalink
Merge pull request #127 from estroz/deps/c-r-v0.7.0
Browse files Browse the repository at this point in the history
deps: bump to controller-runtime v0.7.0-alpha.5
  • Loading branch information
k8s-ci-robot committed Nov 9, 2020
2 parents 045672b + 730cbae commit 1cbf859
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 81 deletions.
17 changes: 9 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ go 1.13

require (
github.com/blang/semver v3.5.0+incompatible
github.com/evanphx/json-patch v4.5.0+incompatible
github.com/evanphx/json-patch v4.9.0+incompatible
github.com/go-git/go-git/v5 v5.1.0
github.com/go-logr/logr v0.2.1
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.0.0
github.com/prometheus/client_golang v1.7.1
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/tools v0.0.0-20200714190737-9048b464a08d
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
k8s.io/api v0.18.4
k8s.io/apimachinery v0.18.4
k8s.io/cli-runtime v0.18.4
k8s.io/client-go v0.18.4
k8s.io/api v0.19.2
k8s.io/apimachinery v0.19.2
k8s.io/cli-runtime v0.19.2
k8s.io/client-go v0.19.2
k8s.io/klog v1.0.0
k8s.io/kubectl v0.18.4
k8s.io/kubectl v0.19.2
sigs.k8s.io/cli-utils v0.16.0
sigs.k8s.io/controller-runtime v0.6.0
sigs.k8s.io/controller-runtime v0.7.0-alpha.5
sigs.k8s.io/kustomize/api v0.3.2
sigs.k8s.io/yaml v1.2.0
)
279 changes: 235 additions & 44 deletions go.sum

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions pkg/patterns/declarative/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package declarative

import (
"context"
"sync"
"time"

Expand Down Expand Up @@ -242,7 +243,8 @@ func (ot *ObjectTracker) addIfNotPresent(objects []*manifest.Object, defaultName

// addIfNotPresent is called at Reconcler.reconcileExists,
// so Controller & Manager is already running
ot.trackedGVK[gvk].start()
ctx := context.TODO()
ot.trackedGVK[gvk].start(ctx)
}

return errors.NewAggregate(errs)
Expand Down Expand Up @@ -335,8 +337,8 @@ func (gvkt *gvkTracker) deleteMetricsIfNeeded(metricsDuration int) {
}
}

func (gvkt *gvkTracker) start() {
gvkt.src.Start(gvkt.eventHandler, dummyQueue{}, gvkt.predicate)
func (gvkt *gvkTracker) start(ctx context.Context) {
gvkt.src.Start(ctx, gvkt.eventHandler, dummyQueue{}, gvkt.predicate)
}

func newGVKTracker(mgr manager.Manager, obj *unstructured.Unstructured, namespaced bool) (gvkt *gvkTracker) {
Expand Down Expand Up @@ -532,7 +534,7 @@ type recordTrigger struct {
}

func (rt recordTrigger) Create(ev event.CreateEvent, _ workqueue.RateLimitingInterface) {
ns, name := ev.Meta.GetNamespace(), ev.Meta.GetName()
ns, name := ev.Object.GetNamespace(), ev.Object.GetName()

if rt.namespaced {
if len(ns) == 0 {
Expand All @@ -549,8 +551,8 @@ func (rt recordTrigger) Create(ev event.CreateEvent, _ workqueue.RateLimitingInt

func (rt recordTrigger) Update(ev event.UpdateEvent, _ workqueue.RateLimitingInterface) {
var nsnp nsnPairs = make(map[string][]string)
ons, oname := ev.MetaOld.GetNamespace(), ev.MetaOld.GetName()
nns, nname := ev.MetaNew.GetNamespace(), ev.MetaNew.GetName()
ons, oname := ev.ObjectOld.GetNamespace(), ev.ObjectOld.GetName()
nns, nname := ev.ObjectNew.GetNamespace(), ev.ObjectNew.GetName()

if rt.namespaced {
if len(ons) == 0 {
Expand Down Expand Up @@ -582,7 +584,7 @@ func (rt recordTrigger) Update(ev event.UpdateEvent, _ workqueue.RateLimitingInt
}

func (rt recordTrigger) Delete(ev event.DeleteEvent, _ workqueue.RateLimitingInterface) {
ns, name := ev.Meta.GetNamespace(), ev.Meta.GetName()
ns, name := ev.Object.GetNamespace(), ev.Object.GetName()

if rt.namespaced {
if len(ns) == 0 {
Expand Down
7 changes: 4 additions & 3 deletions pkg/patterns/declarative/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package declarative

import (
"bytes"
"context"
"errors"
"os"
"os/exec"
Expand Down Expand Up @@ -262,9 +263,9 @@ func TestAddIfNotPresent(t *testing.T) {
t.Error(err)
}

stopC := make(chan struct{})
ctx := context.TODO()
go func() {
_ = mgr.GetCache().Start(stopC)
_ = mgr.GetCache().Start(ctx)
}()

// Set up kubectl command
Expand Down Expand Up @@ -557,7 +558,7 @@ func TestAddIfNotPresent(t *testing.T) {
// Wait for reflector sees K8s object change in K8s API server & adds it to DeltaFIFO
// then controller pops it and eventhandler updates metrics
// If we ommit it, there is a chance call of testutil.CollectAndCompare is too fast & fails.
_ = mgr.GetCache().WaitForCacheSync(stopC)
_ = mgr.GetCache().WaitForCacheSync(ctx)
time.Sleep(time.Second * 10)

// Check for metrics
Expand Down
12 changes: 11 additions & 1 deletion pkg/patterns/declarative/pkg/watch/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -86,6 +88,14 @@ func (dw *dynamicWatch) Add(trigger schema.GroupVersionKind, options metav1.List
return nil
}

var _ client.Object = clientObject{}

// clientObject is a concrete client.Object to pass to watch events.
type clientObject struct {
runtime.Object
*metav1.ObjectMeta
}

// A Watch will be closed when the pod loses connection to the API server.
// If a Watch is opened with no ResourceVersion then we will recieve an 'ADDED'
// event for all Watch objects[1]. This will result in 'overnotification'
Expand All @@ -109,7 +119,7 @@ func (dw *dynamicWatch) watchUntilClosed(client dynamic.ResourceInterface, trigg

for clientEvent := range events.ResultChan() {
log.WithValues("type", clientEvent.Type).WithValues("kind", trigger.String()).Info("broadcasting event")
dw.events <- event.GenericEvent{Object: clientEvent.Object, Meta: &target}
dw.events <- event.GenericEvent{Object: clientObject{Object: clientEvent.Object, ObjectMeta: &target}}
}

log.WithValues("kind", trigger.String()).WithValues("namespace", target.Namespace).WithValues("labels", options.LabelSelector).Info("watch closed")
Expand Down
3 changes: 1 addition & 2 deletions pkg/patterns/declarative/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ func (r *Reconciler) Init(mgr manager.Manager, prototype DeclarativeObject, opts
}

// +rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
func (r *Reconciler) Reconcile(request reconcile.Request) (result reconcile.Result, err error) {
ctx := context.TODO()
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (result reconcile.Result, err error) {
log := log.Log
defer r.collectMetrics(request, result, err)

Expand Down
13 changes: 6 additions & 7 deletions pkg/test/mocks/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
toolscache "sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -13,30 +12,30 @@ import (
type FakeCache struct {
}

func (FakeCache) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object) error {
func (FakeCache) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error {
return errors.NewNotFound(schema.GroupResource{}, "")
}

func (FakeCache) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {
func (FakeCache) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
panic("implement me")
}

func (FakeCache) GetInformer(ctx context.Context, obj runtime.Object) (toolscache.Informer, error) {
func (FakeCache) GetInformer(ctx context.Context, obj client.Object) (toolscache.Informer, error) {
panic("implement me")
}

func (FakeCache) GetInformerForKind(gctx context.Context, vk schema.GroupVersionKind) (toolscache.Informer, error) {
panic("implement me")
}

func (FakeCache) Start(stopCh <-chan struct{}) error {
func (FakeCache) Start(ctx context.Context) error {
panic("implement me")
}

func (FakeCache) WaitForCacheSync(stop <-chan struct{}) bool {
func (FakeCache) WaitForCacheSync(ctx context.Context) bool {
panic("implement me")
}

func (FakeCache) IndexField(ctx context.Context, obj runtime.Object, field string, extractValue client.IndexerFunc) error {
func (FakeCache) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {
panic("implement me")
}
24 changes: 16 additions & 8 deletions pkg/test/mocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewClient(clientScheme *runtime.Scheme) FakeClient {
}
}

func (f FakeClient) Get(ctx context.Context, key client.ObjectKey, out runtime.Object) error {
func (f FakeClient) Get(ctx context.Context, key client.ObjectKey, out client.Object) error {
gvr, err := getGVRFromObject(out, f.scheme)
if err != nil {
return err
Expand All @@ -54,11 +54,11 @@ func (f FakeClient) Get(ctx context.Context, key client.ObjectKey, out runtime.O
return err
}

func (FakeClient) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {
func (FakeClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
panic("not implemented")
}

func (f FakeClient) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOption) error {
func (f FakeClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
createOptions := &client.CreateOptions{}
createOptions.ApplyOptions(opts)

Expand All @@ -79,27 +79,35 @@ func (f FakeClient) Create(ctx context.Context, obj runtime.Object, opts ...clie
return f.tracker.Create(gvr, obj, accessor.GetNamespace())
}

func (FakeClient) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOption) error {
func (FakeClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
return nil
}

func (FakeClient) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...client.DeleteAllOfOption) error {
func (FakeClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
return nil
}

func (FakeClient) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOption) error {
func (FakeClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
return nil
}

func (FakeClient) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error {
func (FakeClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return nil
}

func (FakeClient) Status() client.StatusWriter {
panic("not implemented")
}

func getGVRFromObject(obj runtime.Object, scheme *runtime.Scheme) (schema.GroupVersionResource, error) {
func (FakeClient) RESTMapper() meta.RESTMapper {
return nil
}

func (FakeClient) Scheme() *runtime.Scheme {
return scheme.Scheme
}

func getGVRFromObject(obj client.Object, scheme *runtime.Scheme) (schema.GroupVersionResource, error) {
gvk, err := apiutil.GVKForObject(obj, scheme)
if err != nil {
return schema.GroupVersionResource{}, err
Expand Down
8 changes: 7 additions & 1 deletion pkg/test/mocks/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ limitations under the License.
package mocks

import (
"context"
"net/http"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -57,7 +59,7 @@ func (Manager) SetFields(interface{}) error {
panic("implement me")
}

func (Manager) Start(<-chan struct{}) error {
func (Manager) Start(context.Context) error {
panic("implement me")
}

Expand Down Expand Up @@ -122,3 +124,7 @@ func (Manager) AddMetricsExtraHandler(path string, handler http.Handler) error {
func (Manager) Elected() <-chan struct{} {
panic("implement me")
}

func (Manager) GetLogger() logr.Logger {
panic("implement me")
}

0 comments on commit 1cbf859

Please sign in to comment.