@@ -18,8 +18,9 @@ import (
1818 "github.com/urfave/cli/v2"
1919 "gopkg.in/yaml.v2"
2020 corev1 "k8s.io/api/core/v1"
21+ "k8s.io/apimachinery/pkg/api/errors"
2122 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22- "k8s.io/apimachinery/pkg/labels "
23+ "k8s.io/apimachinery/pkg/types "
2324 "sigs.k8s.io/controller-runtime/pkg/client"
2425 k8syaml "sigs.k8s.io/yaml"
2526
@@ -212,7 +213,8 @@ var joinCommand = &cli.Command{
212213 }
213214
214215 logrus .Debugf ("creating systemd unit files" )
215- if err := createSystemdUnitFiles (jcmd .K0sJoinCommand ); err != nil {
216+ // both controller and worker nodes will have 'worker' in the join command
217+ if err := createSystemdUnitFiles (! strings .Contains (jcmd .K0sJoinCommand , "controller" )); err != nil {
216218 err := fmt .Errorf ("unable to create systemd unit files: %w" , err )
217219 metrics .ReportJoinFailed (c .Context , jcmd .MetricsBaseURL , jcmd .ClusterID , err )
218220 return err
@@ -386,22 +388,22 @@ func systemdUnitFileName() string {
386388
387389// createSystemdUnitFiles links the k0s systemd unit file. this also creates a new
388390// systemd unit file for the local artifact mirror service.
389- func createSystemdUnitFiles (fullcmd string ) error {
391+ func createSystemdUnitFiles (isWorker bool ) error {
390392 dst := systemdUnitFileName ()
391- if _ , err := os .Stat (dst ); err == nil {
393+ if _ , err := os .Lstat (dst ); err == nil {
392394 if err := os .Remove (dst ); err != nil {
393395 return err
394396 }
395397 }
396398 src := "/etc/systemd/system/k0scontroller.service"
397- if strings . Contains ( fullcmd , "worker" ) {
399+ if isWorker {
398400 src = "/etc/systemd/system/k0sworker.service"
399401 }
400402 if err := os .Symlink (src , dst ); err != nil {
401- return err
403+ return fmt . Errorf ( "failed to create symlink: %w" , err )
402404 }
403405 if _ , err := helpers .RunCommand ("systemctl" , "daemon-reload" ); err != nil {
404- return err
406+ return fmt . Errorf ( "unable to get reload systemctl daemon: %w" , err )
405407 }
406408 return installAndEnableLocalArtifactMirror ()
407409}
@@ -459,17 +461,16 @@ func canEnableHA(ctx context.Context, kcli client.Client) (bool, error) {
459461 if installation .Spec .HighAvailability {
460462 return false , nil
461463 }
462- var nodes corev1.NodeList
463- labelSelector := labels .Set (map [string ]string {
464- "node-role.kubernetes.io/control-plane" : "true" ,
465- }).AsSelector ()
466- if err := kcli .List (ctx , & nodes , & client.ListOptions {LabelSelector : labelSelector }); err != nil {
467- return false , fmt .Errorf ("unable to list nodes: %w" , err )
464+ if err := kcli .Get (ctx , types.NamespacedName {Name : ecRestoreStateCMName , Namespace : "embedded-cluster" }, & corev1.ConfigMap {}); err == nil {
465+ return false , nil // cannot enable HA during a restore
466+ } else if ! errors .IsNotFound (err ) {
467+ return false , fmt .Errorf ("unable to get restore state configmap: %w" , err )
468468 }
469- if len (nodes .Items ) < 3 {
470- return false , nil
469+ ncps , err := kubeutils .NumOfControlPlaneNodes (ctx , kcli )
470+ if err != nil {
471+ return false , fmt .Errorf ("unable to check control plane nodes: %w" , err )
471472 }
472- return true , nil
473+ return ncps >= 3 , nil
473474}
474475
475476// enableHA enables high availability in the installation object
0 commit comments