Skip to content

Instantly share code, notes, and snippets.

@jstoner
Created November 11, 2020 07:09
Show Gist options
  • Save jstoner/1a609799712bbf0be60f31d5fee79e29 to your computer and use it in GitHub Desktop.
Save jstoner/1a609799712bbf0be60f31d5fee79e29 to your computer and use it in GitHub Desktop.
adding a channel of reconcile events
package main
import(
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/source"
ctrl "sigs.k8s.io/controller-runtime"
...
)
// First, in the main entry point code for our operator,
func main(){
// ...
// ...we created a channel:
reconcileEvents := make(chan event.GenericEvent)
// ...then, we created a Source to contain that channel:
src := &source.Channel{Source: rotateEvents}
// ...then we created the Controller, using a Manager we had already
// created, and passed in the Source:
if err := ctrl.NewControllerManagedBy(mgr).
For(&secretsv1.XXX{}).
Watches(src, &handler.EnqueueRequestForObject{}).
Complete(reconciler); err != nil {
setupLog.Error(err, “unable to create controller”, “controller”, “XXX”)
os.Exit(1)
}
// ...then we passed the channel to the object that updates our credentials
// (the Rotor that rotates passwords on a reconcile event):
rotorObj := rotor.Rotor{
// ...
Channel: rotateEvents,
}
go rotorObj.Run()
// ...
}
// ...
func (r Rotor) Rotate(ctx context.Context, items []XXX) error {
for item := range items {
// ... and that object puts an event in the channel, forcing a reconcile, (and
// resets our ObservedGeneration so that event actually does something):
item.Status.ObservedGeneration = 0
if err := r.Client.Status().Update(ctx, &item); err != nil {
return fmt.Errorf(“marking XXX as needing an update: %s”, err)
}
// send event to controller for rotation
r.Channel <- event.GenericEvent{
Meta: &item.ObjectMeta,
Object: &item,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment