Skip to content

Commit

Permalink
controller-runtime: implement a cache.Informer compatible Source
Browse files Browse the repository at this point in the history
Can be reverted if kubernetes-sigs/controller-runtime#383 merges.
  • Loading branch information
ironcladlou committed Apr 4, 2019
1 parent 1873dc3 commit 6f50d47
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller"
certcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/certificate"
certpublishercontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/certificate-publisher"
utilsource "github.com/openshift/cluster-ingress-operator/pkg/util/source"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -34,7 +35,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

var (
Expand Down Expand Up @@ -121,7 +121,7 @@ func New(config operatorconfig.Config, dnsManager dns.Manager, kubeConfig *rest.
if err != nil {
return nil, fmt.Errorf("failed to get informer for %v: %v", obj, err)
}
operatorController.Watch(&source.Informer{Informer: informer}, &handler.EnqueueRequestsFromMapFunc{
operatorController.Watch(&utilsource.Informer{Informer: informer}, &handler.EnqueueRequestsFromMapFunc{
ToRequests: handler.ToRequestsFunc(func(a handler.MapObject) []reconcile.Request {
labels := a.Meta.GetLabels()
if ingressName, ok := labels[manifests.OwningClusterIngressLabel]; ok {
Expand Down
178 changes: 178 additions & 0 deletions pkg/util/source/internal/eventsource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package internal

import (
"fmt"

logf "github.com/openshift/cluster-ingress-operator/pkg/log"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

var log = logf.Logger.WithName("source").WithName("EventHandler")

var _ cache.ResourceEventHandler = EventHandler{}

// EventHandler adapts a eventhandler.EventHandler interface to a cache.ResourceEventHandler interface
type EventHandler struct {
EventHandler handler.EventHandler
Queue workqueue.RateLimitingInterface
Predicates []predicate.Predicate
}

// OnAdd creates CreateEvent and calls Create on EventHandler
func (e EventHandler) OnAdd(obj interface{}) {
c := event.CreateEvent{}

// Pull metav1.Object out of the object
if o, err := meta.Accessor(obj); err == nil {
c.Meta = o
} else {
log.Error(err, "OnAdd missing Meta",
"object", obj, "type", fmt.Sprintf("%T", obj))
return
}

// Pull the runtime.Object out of the object
if o, ok := obj.(runtime.Object); ok {
c.Object = o
} else {
log.Error(nil, "OnAdd missing runtime.Object",
"object", obj, "type", fmt.Sprintf("%T", obj))
return
}

for _, p := range e.Predicates {
if !p.Create(c) {
return
}
}

// Invoke create handler
e.EventHandler.Create(c, e.Queue)
}

// OnUpdate creates UpdateEvent and calls Update on EventHandler
func (e EventHandler) OnUpdate(oldObj, newObj interface{}) {
u := event.UpdateEvent{}

// Pull metav1.Object out of the object
if o, err := meta.Accessor(oldObj); err == nil {
u.MetaOld = o
} else {
log.Error(err, "OnUpdate missing MetaOld",
"object", oldObj, "type", fmt.Sprintf("%T", oldObj))
return
}

// Pull the runtime.Object out of the object
if o, ok := oldObj.(runtime.Object); ok {
u.ObjectOld = o
} else {
log.Error(nil, "OnUpdate missing ObjectOld",
"object", oldObj, "type", fmt.Sprintf("%T", oldObj))
return
}

// Pull metav1.Object out of the object
if o, err := meta.Accessor(newObj); err == nil {
u.MetaNew = o
} else {
log.Error(err, "OnUpdate missing MetaNew",
"object", newObj, "type", fmt.Sprintf("%T", newObj))
return
}

// Pull the runtime.Object out of the object
if o, ok := newObj.(runtime.Object); ok {
u.ObjectNew = o
} else {
log.Error(nil, "OnUpdate missing ObjectNew",
"object", oldObj, "type", fmt.Sprintf("%T", oldObj))
return
}

for _, p := range e.Predicates {
if !p.Update(u) {
return
}
}

// Invoke update handler
e.EventHandler.Update(u, e.Queue)
}

// OnDelete creates DeleteEvent and calls Delete on EventHandler
func (e EventHandler) OnDelete(obj interface{}) {
d := event.DeleteEvent{}

// Deal with tombstone events by pulling the object out. Tombstone events wrap the object in a
// DeleteFinalStateUnknown struct, so the object needs to be pulled out.
// Copied from sample-controller
// This should never happen if we aren't missing events, which we have concluded that we are not
// and made decisions off of this belief. Maybe this shouldn't be here?
var ok bool
if _, ok = obj.(metav1.Object); !ok {
// If the object doesn't have Metadata, assume it is a tombstone object of type DeletedFinalStateUnknown
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
log.Error(nil, "Error decoding objects. Expected cache.DeletedFinalStateUnknown",
"type", fmt.Sprintf("%T", obj),
"object", obj)
return
}

// Set obj to the tombstone obj
obj = tombstone.Obj
}

// Pull metav1.Object out of the object
if o, err := meta.Accessor(obj); err == nil {
d.Meta = o
} else {
log.Error(err, "OnDelete missing Meta",
"object", obj, "type", fmt.Sprintf("%T", obj))
return
}

// Pull the runtime.Object out of the object
if o, ok := obj.(runtime.Object); ok {
d.Object = o
} else {
log.Error(nil, "OnDelete missing runtime.Object",
"object", obj, "type", fmt.Sprintf("%T", obj))
return
}

for _, p := range e.Predicates {
if !p.Delete(d) {
return
}
}

// Invoke delete handler
e.EventHandler.Delete(d, e.Queue)
}
69 changes: 69 additions & 0 deletions pkg/util/source/source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package source

import (
"fmt"

"github.com/openshift/cluster-ingress-operator/pkg/util/source/internal"

"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/handler"

"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// Informer is used to provide a source of events originating inside the cluster from Watches (e.g. Pod Create)
type Informer struct {
// Informer is the controller-runtime Informer
Informer cache.Informer
}

var _ source.Source = &Informer{}

// Start is internal and should be called only by the Controller to register an EventHandler with the Informer
// to enqueue reconcile.Requests.
func (is *Informer) Start(handler handler.EventHandler, queue workqueue.RateLimitingInterface,
prct ...predicate.Predicate) error {

// Informer should have been specified by the user.
if is.Informer == nil {
return fmt.Errorf("must specify Informer.Informer")
}

is.Informer.AddEventHandler(internal.EventHandler{Queue: queue, EventHandler: handler, Predicates: prct})
return nil
}

func (is *Informer) String() string {
return fmt.Sprintf("informer source: %p", is.Informer)
}

// Func is a function that implements Source
type Func func(handler.EventHandler, workqueue.RateLimitingInterface, ...predicate.Predicate) error

// Start implements Source
func (f Func) Start(evt handler.EventHandler, queue workqueue.RateLimitingInterface,
pr ...predicate.Predicate) error {
return f(evt, queue, pr...)
}

func (f Func) String() string {
return fmt.Sprintf("func source: %p", f)
}

0 comments on commit 6f50d47

Please sign in to comment.