diff --git a/controllers/sriovoperatorconfig_controller.go b/controllers/sriovoperatorconfig_controller.go index c47f29ef4f..92e10075c1 100644 --- a/controllers/sriovoperatorconfig_controller.go +++ b/controllers/sriovoperatorconfig_controller.go @@ -78,6 +78,11 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl. Name: constants.DEFAULT_CONFIG_NAME, Namespace: namespace}, defaultConfig) if err != nil { if errors.IsNotFound(err) { + singleNode, err := utils.IsSingleNodeCluster(r.Client) + if err != nil { + return reconcile.Result{}, fmt.Errorf("Couldn't check the anount of nodes in the cluster") + } + // Default Config object not found, create it. defaultConfig.SetNamespace(namespace) defaultConfig.SetName(constants.DEFAULT_CONFIG_NAME) @@ -86,7 +91,9 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl. EnableOperatorWebhook: func() *bool { b := enableAdmissionController; return &b }(), ConfigDaemonNodeSelector: map[string]string{}, LogLevel: 2, + DisableDrain: singleNode, } + err = r.Create(context.TODO(), defaultConfig) if err != nil { logger.Error(err, "Failed to create default Operator Config", "Namespace", diff --git a/main.go b/main.go index 63a84bac17..d6aa493b74 100644 --- a/main.go +++ b/main.go @@ -232,6 +232,13 @@ 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 + singleNode, err := utils.IsSingleNodeCluster(c) + if err != nil { + return fmt.Errorf("Couldn't check the anount of nodes in the cluster") + } + enableAdmissionController := os.Getenv("ENABLE_ADMISSION_CONTROLLER") == "true" config := &sriovnetworkv1.SriovOperatorConfig{ Spec: sriovnetworkv1.SriovOperatorConfigSpec{ @@ -239,6 +246,7 @@ func createDefaultOperatorConfig(cfg *rest.Config) error { EnableOperatorWebhook: func() *bool { b := enableAdmissionController; return &b }(), ConfigDaemonNodeSelector: map[string]string{}, LogLevel: 2, + DisableDrain: singleNode, }, } name := "default" diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index 138b3fb6a0..b3efc98713 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -498,25 +498,27 @@ func (dn *Daemon) nodeStateSyncHandler(generation int64) error { } if reqDrain { - ctx, cancel := context.WithCancel(context.TODO()) - defer cancel() - - glog.Infof("nodeStateSyncHandler(): get drain lock for sriov daemon") - done := make(chan bool) - go dn.getDrainLock(ctx, done) - <-done + if !dn.disableDrain { + ctx, cancel := context.WithCancel(context.TODO()) + defer cancel() - glog.Infof("nodeStateSyncHandler(): pause MCP") - if err := dn.pauseMCP(); err != nil { - return err + glog.Infof("nodeStateSyncHandler(): get drain lock for sriov daemon") + done := make(chan bool) + go dn.getDrainLock(ctx, done) + <-done } - if !dn.disableDrain { - glog.Info("nodeStateSyncHandler(): drain node") - if err := dn.drainNode(); err != nil { + if utils.ClusterType == utils.ClusterTypeOpenshift { + glog.Infof("nodeStateSyncHandler(): pause MCP") + if err := dn.pauseMCP(); err != nil { return err } } + + glog.Info("nodeStateSyncHandler(): drain node") + if err := dn.drainNode(); err != nil { + return err + } } if !reqReboot { @@ -819,14 +821,9 @@ func (dn *Daemon) getDrainLock(ctx context.Context, done chan bool) { } func (dn *Daemon) pauseMCP() error { - glog.Info("pauseMCP(): check if pausing MCP is possible") + glog.Info("pauseMCP(): pausing MCP") var err error - if utils.ClusterType != utils.ClusterTypeOpenshift { - glog.Infof("pauseMCP(): skipping MCP pause as the cluster is not an openshift cluster") - return nil - } - mcpInformerFactory := mcfginformers.NewSharedInformerFactory(dn.mcClient, time.Second*30, ) @@ -913,6 +910,11 @@ func (dn *Daemon) pauseMCP() error { } func (dn *Daemon) drainNode() error { + if dn.disableDrain { + glog.Info("drainNode(): disable drain is true skipping drain") + return nil + } + glog.Info("drainNode(): Update prepared") var err error diff --git a/pkg/utils/cluster.go b/pkg/utils/cluster.go new file mode 100644 index 0000000000..1421e1e62d --- /dev/null +++ b/pkg/utils/cluster.go @@ -0,0 +1,26 @@ +package utils + +import ( + "context" + + "github.com/golang/glog" + + corev1 "k8s.io/api/core/v1" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func IsSingleNodeCluster(c client.Client) (bool, error) { + nodeList := &corev1.NodeList{} + err := c.List(context.TODO(), nodeList) + if err != nil { + glog.Errorf("IsSingleNodeCluster(): Failed to list nodes: %v", err) + return false, err + } + + if len(nodeList.Items) == 1 { + glog.Infof("IsSingleNodeCluster(): one node found in the cluster") + return true, nil + } + + return false, nil +}