Skip to content

Controller v2 libs review #48

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
68 changes: 68 additions & 0 deletions pkg/event/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
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 event

import (
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

Copy link
Contributor

@droot droot Jun 22, 2018

Choose a reason for hiding this comment

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

May be a few comments explaining where this pkg fit in the over-all picture. Who will be the producer/consumer of these objects etc ?

// CreateEvent is an event where a Kubernetes object was created.
type CreateEvent struct {
// Meta is the ObjectMeta of the Kubernetes Type that was created
Meta v1.Object

// Object is the object from the event
Object runtime.Object
}

// UpdateEvent is an event where a Kubernetes object was updated.
type UpdateEvent struct {
// MetaOld is the ObjectMeta of the Kubernetes Type that was updated (before the update)
MetaOld v1.Object

// ObjectOld is the object from the event
ObjectOld runtime.Object

// MetaNew is the ObjectMeta of the Kubernetes Type that was updated (after the update)
MetaNew v1.Object

// ObjectNew is the object from the event
ObjectNew runtime.Object
}

// DeleteEvent is an event where a Kubernetes object was deleted.
type DeleteEvent struct {
// Meta is the ObjectMeta of the Kubernetes Type that was deleted
Meta v1.Object

// Object is the object from the event
Object runtime.Object

// DeleteStateUnknown is true if the Delete event was missed but we identified the object
// as having been deleted.
DeleteStateUnknown bool
}

// GenericEvent is an event where the operation type is unknown (e.g. polling or event originating outside the cluster).
type GenericEvent struct {
// Meta is the ObjectMeta of a Kubernetes Type this event is for
Meta v1.Object

// Object is the object from the event
Object runtime.Object
}
30 changes: 30 additions & 0 deletions pkg/handler/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
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 handler defines EventHandlers that enqueue reconcile.Requests in response to Create, Update, Deletion Events
observed from Watching Kubernetes APIs.

EventHandlers

Enqueue - Enqueues a reconcile.Request containing the Name and Namespace of the object in the Event.

EnqueueOwner - Enqueues a reconcile.Request containing the Name and Namespace of the Owner of the object in the Event.

EnqueueMapped - Enqueues Reconcile.Requests resulting from a user provided transformation function run against the
object in the Event.
*/
package handler
89 changes: 89 additions & 0 deletions pkg/handler/enqueue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
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 handler

import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)

var enqueueLog = logf.KBLog.WithName("eventhandler").WithName("Enqueue")

var _ EventHandler = &Enqueue{}

// Enqueue enqueues a Request containing the Name and Namespace of the object for each event.
type Enqueue struct{}

// Create implements EventHandler
func (e *Enqueue) Create(q workqueue.RateLimitingInterface, evt event.CreateEvent) {
if evt.Meta == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

:-/ I think we want to Inject a logger if we can, so that we can track which enqueue is having issues.

enqueueLog.Error(nil, "CreateEvent received with no metadata", "CreateEvent", evt)
return
}
q.AddRateLimited(reconcile.Request{NamespacedName: types.NamespacedName{
Name: evt.Meta.GetName(),
Namespace: evt.Meta.GetNamespace(),
}})
}

// Update implements EventHandler
func (e *Enqueue) Update(q workqueue.RateLimitingInterface, evt event.UpdateEvent) {
if evt.MetaOld != nil {
q.AddRateLimited(reconcile.Request{NamespacedName: types.NamespacedName{
Name: evt.MetaOld.GetName(),
Namespace: evt.MetaOld.GetNamespace(),
}})
} else {
enqueueLog.Error(nil, "UpdateEvent received with no old metadata", "UpdateEvent", evt)
}

if evt.MetaNew != nil {
q.AddRateLimited(reconcile.Request{NamespacedName: types.NamespacedName{
Name: evt.MetaNew.GetName(),
Namespace: evt.MetaNew.GetNamespace(),
}})
} else {
enqueueLog.Error(nil, "UpdateEvent received with no new metadata", "UpdateEvent", evt)
}
}

// Delete implements EventHandler
func (e *Enqueue) Delete(q workqueue.RateLimitingInterface, evt event.DeleteEvent) {
if evt.Meta == nil {
enqueueLog.Error(nil, "DeleteEvent received with no metadata", "DeleteEvent", evt)
return
}
q.AddRateLimited(reconcile.Request{NamespacedName: types.NamespacedName{
Name: evt.Meta.GetName(),
Namespace: evt.Meta.GetNamespace(),
}})
}

// Generic implements EventHandler
func (e *Enqueue) Generic(q workqueue.RateLimitingInterface, evt event.GenericEvent) {
if evt.Meta == nil {
enqueueLog.Error(nil, "GenericEvent received with no metadata", "GenericEvent", evt)
return
}
q.AddRateLimited(reconcile.Request{NamespacedName: types.NamespacedName{
Name: evt.Meta.GetName(),
Namespace: evt.Meta.GetNamespace(),
}})
}
88 changes: 88 additions & 0 deletions pkg/handler/enqueue_mapped.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
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 handler

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var _ EventHandler = &EnqueueMapped{}

// EnqueueMapped enqueues Requests by running a transformation function on each Event.
//
// For UpdateEvents which contain both a new and old object, the transformation function is run on both
// objects and both sets of Requests are enqueue.
type EnqueueMapped struct {
// Mapper transforms the argument into a slice of keys to be reconciled
ToRequests Mapper
}

// Create implements EventHandler
func (e *EnqueueMapped) Create(q workqueue.RateLimitingInterface, evt event.CreateEvent) {
e.mapAndEnqueue(q, MapObject{Meta: evt.Meta, Object: evt.Object})
}

// Update implements EventHandler
func (e *EnqueueMapped) Update(q workqueue.RateLimitingInterface, evt event.UpdateEvent) {
e.mapAndEnqueue(q, MapObject{Meta: evt.MetaOld, Object: evt.ObjectOld})
e.mapAndEnqueue(q, MapObject{Meta: evt.MetaNew, Object: evt.ObjectNew})
}

// Delete implements EventHandler
func (e *EnqueueMapped) Delete(q workqueue.RateLimitingInterface, evt event.DeleteEvent) {
e.mapAndEnqueue(q, MapObject{Meta: evt.Meta, Object: evt.Object})
}

// Generic implements EventHandler
func (e *EnqueueMapped) Generic(q workqueue.RateLimitingInterface, evt event.GenericEvent) {
e.mapAndEnqueue(q, MapObject{Meta: evt.Meta, Object: evt.Object})
}

func (e *EnqueueMapped) mapAndEnqueue(q workqueue.RateLimitingInterface, object MapObject) {
for _, req := range e.ToRequests.Map(object) {
q.AddRateLimited(req)
}
}

// Mapper maps an object to a collection of keys to be enqueued
type Mapper interface {
// Map maps an object
Map(MapObject) []reconcile.Request
}

// MapObject contains information from an event to be transformed into a Request.
type MapObject struct {
// Meta is the meta data for an object from an event.
Meta metav1.Object

// Object is the object from an event.
Object runtime.Object
}

var _ Mapper = ToRequestsFunc(nil)

// ToRequestsFunc implements Mapper using a function.
type ToRequestsFunc func(MapObject) []reconcile.Request

// Map implements Mapper
func (m ToRequestsFunc) Map(i MapObject) []reconcile.Request {
return m(i)
}
Loading