diff --git a/controllers/sriovoperatorconfig_controller.go b/controllers/sriovoperatorconfig_controller.go index c47f29ef4f..cb525ce982 100644 --- a/controllers/sriovoperatorconfig_controller.go +++ b/controllers/sriovoperatorconfig_controller.go @@ -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", @@ -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 } diff --git a/main.go b/main.go index 63a84bac17..b0e91d2165 100644 --- a/main.go +++ b/main.go @@ -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.) @@ -232,6 +233,15 @@ 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{ @@ -239,6 +249,7 @@ func createDefaultOperatorConfig(cfg *rest.Config) error { EnableOperatorWebhook: func() *bool { b := enableAdmissionController; return &b }(), ConfigDaemonNodeSelector: map[string]string{}, LogLevel: 2, + DisableDrain: len(nodeList.Items) == 1, }, } name := "default"