Skip to content

Commit

Permalink
Merge pull request #458 from willise/master
Browse files Browse the repository at this point in the history
Add more example code of book
  • Loading branch information
k8s-ci-robot authored Oct 25, 2018
2 parents 0e67e88 + d82a96b commit 11875cb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
71 changes: 47 additions & 24 deletions docs/book/beyond_basics/controller_watches.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ err := c.Watch(
&source.Kind{Type: &v1.Pod{}},
&handler.EnqueueRequestForObject{})
if err != nil {
return err
return err
}
```
{% endmethod %}
Expand Down Expand Up @@ -64,11 +64,11 @@ correct RBAC rules are in place and informers have been started.
// Watch for Pod events, and enqueue a reconcile.Request for the ReplicaSet in the OwnerReferences
err := c.Watch(
&source.Kind{Type: &corev1.Pod{}},
&handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &appsv1.ReplicaSet{}})
&handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &appsv1.ReplicaSet{}})
if err != nil {
return err
return err
}
```
{% endmethod %}
Expand Down Expand Up @@ -103,26 +103,49 @@ correct RBAC rules are in place and informers have been started.
// objects to Reconcile
mapFn := handler.ToRequestsFunc(
func(a handler.MapObject) []reconcile.Request {
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: a.Meta.GetName() + "-1",
Namespace: a.Meta.GetNamespace(),
}},
{NamespacedName: types.NamespacedName{
Name: a.Meta.GetName() + "-2",
Namespace: a.Meta.GetNamespace(),
}},
}
})
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: a.Meta.GetName() + "-1",
Namespace: a.Meta.GetNamespace(),
}},
{NamespacedName: types.NamespacedName{
Name: a.Meta.GetName() + "-2",
Namespace: a.Meta.GetNamespace(),
}},
}
})


// 'UpdateFunc' and 'CreateFunc' used to judge if a event about the object is
// what we want. If that is true, the event will be processed by the reconciler.
p := predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
// The object doesn't contain label "foo", so the event will be
// ignored.
if _, ok := e.MetaOld.GetLabels()["foo"]; !ok {
return false
}
return e.ObjectOld != e.ObjectNew
},
CreateFunc: func(e event.CreateEvent) bool {
if _, ok := e.Meta.GetLabels()["foo"]; !ok {
return false
}
return true
},
}

// Watch Deployments and trigger Reconciles for objects
// mapped from the Deployment in the event
err := c.Watch(
&source.Kind{Type: &appsv1.Deployment{}},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: mapFn,
})
&source.Kind{Type: &appsv1.Deployment{}},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: mapFn,
},
// Comment it if default predicate fun is used.
p)
if err != nil {
return err
return err
}
```
{% endmethod %}
Expand All @@ -140,11 +163,11 @@ object with the external state that would trigger the Reconcile.
```go
events := make(chan event.GenericEvent)
err := ctrl.Watch(
&source.Channel{Source: events},
&handler.EnqueueRequestForObject{},
&source.Channel{Source: events},
&handler.EnqueueRequestForObject{},
)
if err != nil {
return err
return err
}
```
{% endmethod %}
6 changes: 4 additions & 2 deletions docs/book/beyond_basics/using_finalizers.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ func (r *Reconciler) Reconcile(request reconcile.Request) (reconcile.Result, err
// The object is being deleted
if containsString(instance.ObjectMeta.Finalizers, myFinalizerName) {
// our finalizer is present, so lets handle our external dependency
err := r.deleteExternalDependency(instance)
if err != nil {
if err := r.deleteExternalDependency(instance); err != nil {
// if fail to delete the external dependency here, return with error
// so that it can be retried
return reconcile.Result{}, err
Expand All @@ -59,6 +58,9 @@ func (r *Reconciler) Reconcile(request reconcile.Request) (reconcile.Result, err
return reconcile.Result{Requeue: true}, nil
}
}

// Our finalizer has finished, so the reconciler can do nothing.
return reconcile.Result{}, nil
}
....
....
Expand Down

0 comments on commit 11875cb

Please sign in to comment.