Skip to content

Commit

Permalink
Add the DisableDrain when running one a single node
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Sch <[email protected]>
  • Loading branch information
SchSeba committed Dec 8, 2021
1 parent 616062f commit cd10a0a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions controllers/sriovoperatorconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.
ConfigDaemonNodeSelector: map[string]string{},
LogLevel: 2,
}

err = r.Create(context.TODO(), defaultConfig)
if err != nil {
logger.Error(err, "Failed to create default Operator Config", "Namespace",
Expand All @@ -99,6 +100,25 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.
return reconcile.Result{}, err
}

// Check if we only have one node
nodeList := &corev1.NodeList{}
err = r.List(context.TODO(), nodeList)
if err != nil {
logger.Error(err, "Failed to list nodes")
return reconcile.Result{}, err
}

// Update the disableDrain field if needed
if len(nodeList.Items) == 1 && !defaultConfig.Spec.DisableDrain {
defaultConfig.Spec.DisableDrain = true
err = r.Update(context.TODO(), defaultConfig)
if err != nil {
logger.Error(err, "Failed to update default Operator Config ", "Namespace",
namespace, "Name", constants.DEFAULT_CONFIG_NAME)
return reconcile.Result{}, err
}
}

if req.Namespace != namespace {
return reconcile.Result{}, nil
}
Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand Down Expand Up @@ -232,13 +233,23 @@ func createDefaultOperatorConfig(cfg *rest.Config) error {
if err != nil {
return fmt.Errorf("Couldn't create client: %v", err)
}

// Check if we only have one node
nodeList := &corev1.NodeList{}
err = c.List(context.TODO(), nodeList)
if err != nil {
logger.Error(err, "Failed to list nodes")
return err
}

enableAdmissionController := os.Getenv("ENABLE_ADMISSION_CONTROLLER") == "true"
config := &sriovnetworkv1.SriovOperatorConfig{
Spec: sriovnetworkv1.SriovOperatorConfigSpec{
EnableInjector: func() *bool { b := enableAdmissionController; return &b }(),
EnableOperatorWebhook: func() *bool { b := enableAdmissionController; return &b }(),
ConfigDaemonNodeSelector: map[string]string{},
LogLevel: 2,
DisableDrain: len(nodeList.Items) == 1,
},
}
name := "default"
Expand Down

0 comments on commit cd10a0a

Please sign in to comment.