-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Closed
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad0c1eb
Do not review. Already reviewed
pwittrock b9b5807
Controller Event package
pwittrock 08d6ea0
Controller handler package
pwittrock 044e754
Controller Reconcile pkg
pwittrock 66f3905
Controller source package
pwittrock 0358f59
Controller predicate package
pwittrock 973ead5
Controller manager package
pwittrock fbd8207
Controller controller package
pwittrock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
|
||
// 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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(), | ||
}}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 ?