Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gaurav-dalvi/7810b64ace25bc59d67d48ab85635ba2 to your computer and use it in GitHub Desktop.
Save gaurav-dalvi/7810b64ace25bc59d67d48ab85635ba2 to your computer and use it in GitHub Desktop.
controller.go
package kubewatch
import (
"context"
"reflect"
noironetworksv1 "github.com/gaurav-dalvi/kubewatch/pkg/apis/noironetworks/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/source"
)
var log = logf.Log.WithName("controller_kubewatch")
/**
* USER ACTION REQUIRED: This is a scaffold file intended for the user to modify with their own Controller
* business logic. Delete these comments after modifying this file.*
*/
// Add creates a new Kubewatch Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
return add(mgr, newReconciler(mgr))
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
return &ReconcileKubewatch{client: mgr.GetClient(), scheme: mgr.GetScheme()}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New("kubewatch-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to primary resource Kubewatch
err = c.Watch(&source.Kind{Type: &noironetworksv1.Kubewatch{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
// TODO(user): Modify this to be the types you create that are owned by the primary resource
// Watch for changes to secondary resource Pods and requeue the owner Kubewatch
err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
return nil
}
var _ reconcile.Reconciler = &ReconcileKubewatch{}
// ReconcileKubewatch reconciles a Kubewatch object
type ReconcileKubewatch struct {
// This client, initialized using mgr.Client() above, is a split client
// that reads objects from the cache and writes to the apiserver
client client.Client
scheme *runtime.Scheme
}
// Reconcile reads that state of the cluster for a Kubewatch object and makes changes based on the state read
// and what is in the Kubewatch.Spec
// TODO(user): Modify this Reconcile function to implement your Controller logic. This example creates
// a Pod as an example
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *ReconcileKubewatch) Reconcile(request reconcile.Request) (reconcile.Result, error) {
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling Kubewatch")
// Fetch the Kubewatch instance
instance := &noironetworksv1.Kubewatch{}
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
// // Define a new Pod object
// pod := newPodForCR(instance)
// // Set Kubewatch instance as the owner and controller
// if err := controllerutil.SetControllerReference(instance, pod, r.scheme); err != nil {
// return reconcile.Result{}, err
// }
// // Check if this Pod already exists
// found := &corev1.Pod{}
// err = r.client.Get(context.TODO(), types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}, found)
// if err != nil && errors.IsNotFound(err) {
// reqLogger.Info("Creating a new Pod", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name)
// err = r.client.Create(context.TODO(), pod)
// if err != nil {
// return reconcile.Result{}, err
// }
// // Pod created successfully - don't requeue
// return reconcile.Result{}, nil
// } else if err != nil {
// return reconcile.Result{}, err
// }
// Update the status if necessary
status := noironetworksv1.KubewatchStatus{
Name: request.Name,
}
if !reflect.DeepEqual(instance.Status, status) {
instance.Status = status
err := r.client.Update(context.TODO(), instance)
if err != nil {
reqLogger.Error(err, "failed to update the kubewatch instance")
return reconcile.Result{}, err
}
reqLogger.Info("Inside Upate status", "Pod.Namespace", request.Namespace, "Pod.Name", request.Name)
}
// Pod already exists - don't requeue
reqLogger.Info("Skip reconcile: Pod already exists", "Pod.Namespace", request.Namespace, "Pod.Name", request.Name)
return reconcile.Result{}, nil
}
// // newPodForCR returns a busybox pod with the same name/namespace as the cr
// func newPodForCR(cr *noironetworksv1.Kubewatch) *corev1.Pod {
// labels := map[string]string{
// "app": cr.Name,
// }
// return &corev1.Pod{
// ObjectMeta: metav1.ObjectMeta{
// Name: cr.Name + "-pod",
// Namespace: cr.Namespace,
// Labels: labels,
// },
// Spec: corev1.PodSpec{
// Containers: []corev1.Container{
// {
// Name: "busybox",
// Image: "busybox",
// Command: []string{"sleep", "3600"},
// },
// },
// },
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment