Skip to content
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,61 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"
)

var log = logf.Log.WithName("controller_wmc")
const (
// ControllerName is the name of the WMC controller
ControllerName = "windowsmachineconfig-controller"
)

/**
* 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.*
*/
var log = logf.Log.WithName("controller_wmc")

// Add creates a new WindowsMachineConfig 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))
reconciler, err := newReconciler(mgr)
if err != nil {
return errors.Wrapf(err, "could not create %s reconciler", ControllerName)
}
return add(mgr, reconciler)
}

// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
func newReconciler(mgr manager.Manager) (reconcile.Reconciler, error) {
// TODO: This should be moved out to validation for reconciler struct.
// Jira story: https://issues.redhat.com/browse/WINC-277
clientset, err := kubernetes.NewForConfig(mgr.GetConfig())
if err != nil {
log.Error(err, "error while getting clientset")
return nil, errors.Wrap(err, "error creating kubernetes clientset")
}
return &ReconcileWindowsMachineConfig{client: mgr.GetClient(), scheme: mgr.GetScheme(), k8sclientset: clientset}

windowsVMs := make(map[types.WindowsVM]bool)
vmTracker, err := tracker.NewTracker(clientset, windowsVMs)
if err != nil {
return nil, errors.Wrap(err, "failed to instantiate tracker")
}

return &ReconcileWindowsMachineConfig{client: mgr.GetClient(),
scheme: mgr.GetScheme(),
k8sclientset: clientset,
tracker: vmTracker,
windowsVMs: windowsVMs,
},
nil
}

// 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("windowsmachineconfig-controller", mgr, controller.Options{Reconciler: r})
c, err := controller.New(ControllerName, mgr, controller.Options{Reconciler: r})
if err != nil {
return err
return errors.Wrapf(err, "could not create %s", ControllerName)
}
// TODO: Add a predicate here. As of now, we get event notifications for all the WindowsMachineConfig objects, we
// want the predicate to filter the WMC object called `instance`
// Jira Story: https://issues.redhat.com/browse/WINC-282
// Watch for changes to primary resource WindowsMachineConfig
err = c.Watch(&source.Kind{Type: &wmcapi.WindowsMachineConfig{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
return errors.Wrap(err, "could not create watch on WindowsMachineConfig objects")
}

// TODO(user): Modify this to be the types you create that are owned by the primary resource
Expand Down Expand Up @@ -145,13 +162,6 @@ func (r *ReconcileWindowsMachineConfig) Reconcile(request reconcile.Request) (re
return reconcile.Result{}, fmt.Errorf("error instantiating cloud provider: %v", err)
}
}
if r.k8sclientset == nil {
Comment thread
sebsoto marked this conversation as resolved.
return reconcile.Result{}, nil
}
if r.windowsVMs == nil {
// populate the windowsVM map here from ConfigMap as source of truth
r.windowsVMs = make(map[types.WindowsVM]bool)
}
// Get the current number of Windows VMs created by WMCO.
// TODO: Get all the running Windows nodes in the cluster
// jira story: https://issues.redhat.com/browse/WINC-280
Expand All @@ -177,14 +187,8 @@ func (r *ReconcileWindowsMachineConfig) Reconcile(request reconcile.Request) (re
// cluster
func (r *ReconcileWindowsMachineConfig) reconcileWindowsNodes(desired, current int) bool {
log.Info("replicas", "current", current, "desired", desired)
if r.tracker == nil {
var err error
r.tracker, err = tracker.NewTracker(r.k8sclientset, r.windowsVMs)
if err != nil {
log.Error(err, "tracker instantiation failed")
}
}
var vmCount bool

Comment thread
sebsoto marked this conversation as resolved.
Outdated
if desired < current {
vmCount = r.deleteWindowsVMs(current - desired)
} else if desired > current {
Expand Down