@@ -416,23 +416,6 @@ func ensureCertificates(ctx context.Context, cluster *clusterv1.Cluster, kcp *cp
416416 return certificates .LookupOrGenerateCached (ctx , scope .secretCachingClient , scope .client , capiutil .ObjectKey (cluster ), owner )
417417}
418418
419- // FormatStatusVersion formats the status version to match the spec version format.
420- // If spec.version doesn't contain "-k0s." suffix, it removes the suffix from status.version as well.
421- func FormatStatusVersion (specVersion , statusVersion string ) string {
422- specHasK0sSuffix := strings .Contains (specVersion , "-k0s." )
423-
424- // Adjust status.version to match the format of spec.version
425- if ! specHasK0sSuffix && strings .Contains (statusVersion , "-k0s." ) {
426- // If spec.version doesn't have the -k0s. suffix, remove it from status.version as well
427- parts := strings .Split (statusVersion , "-k0s." )
428- if len (parts ) > 0 {
429- return parts [0 ]
430- }
431- }
432-
433- return statusVersion
434- }
435-
436419func (c * K0smotronController ) computeStatus (ctx context.Context , cluster * clusterv1.Cluster , kcp * cpv1beta1.K0smotronControlPlane , scope * kmcScope ) error {
437420 var kmc kapi.Cluster
438421 err := c .Client .Get (ctx , types.NamespacedName {Name : cluster .Name , Namespace : cluster .Namespace }, & kmc )
@@ -462,8 +445,9 @@ func (c *K0smotronController) computeStatus(ctx context.Context, cluster *cluste
462445 var updatedReplicas , readyReplicas , unavailableReplicas int
463446
464447 desiredVersionStr := kcp .Spec .Version
465- if ! strings .Contains (desiredVersionStr , "-" ) {
466- desiredVersionStr = fmt .Sprintf ("%s-%s" , desiredVersionStr , kapi .DefaultK0SSuffix )
448+ if ! strings .Contains (desiredVersionStr , "-k0s." ) && ! strings .Contains (desiredVersionStr , "+k0s." ) {
449+ // Use default k0s suffix format when spec version does not contain either -k0s. or +k0s.
450+ desiredVersionStr = fmt .Sprintf ("%s+%s" , desiredVersionStr , kapi .DefaultK0SSuffix )
467451 }
468452 desiredVersion , err := version .NewVersion (desiredVersionStr )
469453 if err != nil {
@@ -488,7 +472,12 @@ func (c *K0smotronController) computeStatus(ctx context.Context, cluster *cluste
488472 continue
489473 }
490474
491- currentVersion , err := scope .getComparableK0sVersionRunningInPod (ctx , & pod )
475+ currentVersion , err := scope .getK0sVersionRunningInPod (ctx , & pod )
476+ if err != nil {
477+ return err
478+ }
479+ // Align version format in spec to the current version format for comparison. DO NOT modify version format in spec.
480+ currentVersion , err = alignToSpecVersionFormat (desiredVersion , currentVersion )
492481 if err != nil {
493482 return err
494483 }
@@ -507,9 +496,7 @@ func (c *K0smotronController) computeStatus(ctx context.Context, cluster *cluste
507496 kcp .Status .UnavailableReplicas = int32 (unavailableReplicas )
508497
509498 if kcp .Status .ReadyReplicas > 0 {
510- statusVersion := minimumVersion .String ()
511- // Store the formatted version for Cluster API compatibility
512- kcp .Status .Version = FormatStatusVersion (kcp .Spec .Version , statusVersion )
499+ kcp .Status .Version = minimumVersion .String ()
513500 }
514501
515502 c .computeAvailability (ctx , cluster , kcp )
@@ -519,14 +506,41 @@ func (c *K0smotronController) computeStatus(ctx context.Context, cluster *cluste
519506 // Additionally, if the ControlPlaneReadyCondition is false (e.g., due to DNS resolution failures),
520507 // we should also requeue to retry the connection.
521508 if kcp .Status .UnavailableReplicas > 0 ||
522- FormatStatusVersion ( kcp . Spec . Version , desiredVersion .String () ) != kcp .Status .Version ||
509+ desiredVersion .String () != kcp .Status .Version ||
523510 ! conditions .IsTrue (kcp , cpv1beta1 .ControlPlaneReadyCondition ) {
524511 return ErrNotReady
525512 }
526513
527514 return nil
528515}
529516
517+ // alignToSpecVersionFormat ensures that the currentVersion format matches the desiredVersion format.
518+ func alignToSpecVersionFormat (specVersion , currentVersion * version.Version ) (* version.Version , error ) {
519+ specVersionUsesDefaultK0SSuffix := strings .Contains (specVersion .String (), "+k0s." )
520+ currentVersionUsesDefaultK0SSuffix := strings .Contains (currentVersion .String (), "+k0s." )
521+
522+ isFormatAligned := (specVersionUsesDefaultK0SSuffix && currentVersionUsesDefaultK0SSuffix ) ||
523+ (! specVersionUsesDefaultK0SSuffix && ! currentVersionUsesDefaultK0SSuffix )
524+
525+ if isFormatAligned {
526+ return currentVersion , nil
527+ }
528+
529+ currentVersionStr := currentVersion .String ()
530+ if ! specVersionUsesDefaultK0SSuffix {
531+ // Spec version format is like "vX.Y.Z-k0s.0".
532+ // Current version format is like "vX.Y.Z+k0s.0".
533+ // Convert currentVersion to match it.
534+ currentVersionAlignedStr := strings .Replace (currentVersionStr , "+k0s." , "-k0s." , 1 )
535+ return version .NewVersion (currentVersionAlignedStr )
536+ }
537+ // Spec version format is like "vX.Y.Z+k0s.0".
538+ // Current version format is like "vX.Y.Z-k0s.0".
539+ // Convert currentVersion to match it.
540+ currentVersionAlignedStr := strings .Replace (currentVersionStr , "-k0s." , "+k0s." , 1 )
541+ return version .NewVersion (currentVersionAlignedStr )
542+ }
543+
530544// computeAvailability checks if the control plane is ready by connecting to the API server
531545// and checking if the control plane is initialized
532546func (c * K0smotronController ) computeAvailability (ctx context.Context , cluster * clusterv1.Cluster , kcp * cpv1beta1.K0smotronControlPlane ) {
@@ -574,16 +588,12 @@ func (c *K0smotronController) computeAvailability(ctx context.Context, cluster *
574588 })
575589}
576590
577- func (scope * kmcScope ) getComparableK0sVersionRunningInPod (ctx context.Context , pod * corev1.Pod ) (* version.Version , error ) {
591+ func (scope * kmcScope ) getK0sVersionRunningInPod (ctx context.Context , pod * corev1.Pod ) (* version.Version , error ) {
578592 currentVersionOutput , err := exec .PodExecCmdOutput (ctx , scope .clientSet , scope .restConfig , pod .GetName (), pod .GetNamespace (), "k0s version" )
579593 if err != nil {
580594 return nil , err
581595 }
582596 currentVersionStr , _ := strings .CutSuffix (currentVersionOutput , "\n " )
583- // In order to compare the version reported by the 'k0s version' command executed in the pod running
584- // the controlplane with the version declared in K0smotronControlPlane.spec this transformation is
585- // necessary to match their format.
586- currentVersionStr = strings .Replace (currentVersionStr , "+" , "-" , 1 )
587597 return version .NewVersion (currentVersionStr )
588598}
589599
0 commit comments