Skip to content

Commit

Permalink
Use spec.Version field as primarily source
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandr Demicev <alexandr.demicev@suse.com>
  • Loading branch information
alexander-demicev committed Apr 23, 2024
1 parent c260c32 commit 4e173ad
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 24 deletions.
17 changes: 14 additions & 3 deletions bootstrap/internal/controllers/rke2config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ type Scope struct {
ControlPlane *controlplanev1.RKE2ControlPlane
}

func (s *Scope) getDesiredVersion() string {
if s.Machine.Spec.Version != nil && bsutil.IsRKE2Version(*s.Machine.Spec.Version) {
return *s.Machine.Spec.Version
}

return s.Config.Spec.AgentConfig.Version
}

// SetupWithManager sets up the controller with the Manager.
func (r *RKE2ConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
if r.RKE2InitLock == nil {
Expand Down Expand Up @@ -363,6 +371,7 @@ func (r *RKE2ConfigReconciler) handleClusterNotInitialized(ctx context.Context,
AgentConfig: scope.Config.Spec.AgentConfig,
Ctx: ctx,
Client: r.Client,
Version: scope.getDesiredVersion(),
})
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -417,7 +426,7 @@ func (r *RKE2ConfigReconciler) handleClusterNotInitialized(ctx context.Context,
PreRKE2Commands: scope.Config.Spec.PreRKE2Commands,
PostRKE2Commands: scope.Config.Spec.PostRKE2Commands,
ConfigFile: initConfigFile,
RKE2Version: scope.Config.Spec.AgentConfig.Version,
RKE2Version: scope.getDesiredVersion(),
WriteFiles: files,
NTPServers: ntpServers,
AdditionalCloudInit: scope.Config.Spec.AgentConfig.AdditionalUserData.Config,
Expand Down Expand Up @@ -567,6 +576,7 @@ func (r *RKE2ConfigReconciler) joinControlplane(ctx context.Context, scope *Scop
AgentConfig: scope.Config.Spec.AgentConfig,
Ctx: ctx,
Client: r.Client,
Version: scope.getDesiredVersion(),
},
)
if err != nil {
Expand Down Expand Up @@ -627,7 +637,7 @@ func (r *RKE2ConfigReconciler) joinControlplane(ctx context.Context, scope *Scop
PreRKE2Commands: scope.Config.Spec.PreRKE2Commands,
PostRKE2Commands: scope.Config.Spec.PostRKE2Commands,
ConfigFile: initConfigFile,
RKE2Version: scope.Config.Spec.AgentConfig.Version,
RKE2Version: scope.getDesiredVersion(),
WriteFiles: files,
NTPServers: ntpServers,
AdditionalCloudInit: scope.Config.Spec.AgentConfig.AdditionalUserData.Config,
Expand Down Expand Up @@ -700,6 +710,7 @@ func (r *RKE2ConfigReconciler) joinWorker(ctx context.Context, scope *Scope) (re
Client: r.Client,
CloudProviderName: scope.ControlPlane.Spec.ServerConfig.CloudProviderName,
CloudProviderConfigMap: scope.ControlPlane.Spec.ServerConfig.CloudProviderConfigMap,
Version: scope.getDesiredVersion(),
})
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -738,7 +749,7 @@ func (r *RKE2ConfigReconciler) joinWorker(ctx context.Context, scope *Scope) (re
CISEnabled: scope.Config.Spec.AgentConfig.CISProfile != "",
PostRKE2Commands: scope.Config.Spec.PostRKE2Commands,
ConfigFile: wkJoinConfigFile,
RKE2Version: scope.Config.Spec.AgentConfig.Version,
RKE2Version: scope.getDesiredVersion(),
WriteFiles: files,
NTPServers: ntpServers,
AdditionalCloudInit: scope.Config.Spec.AgentConfig.AdditionalUserData.Config,
Expand Down
13 changes: 12 additions & 1 deletion controlplane/api/v1beta1/rke2controlplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1beta1

import (
"regexp"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -437,9 +439,18 @@ func (r *RKE2ControlPlane) SetConditions(conditions clusterv1.Conditions) {

// GetDesiredVersion returns the desired version of the RKE2ControlPlane using Spec.Version field as a default field.
func (r *RKE2ControlPlane) GetDesiredVersion() string {
if r.Spec.Version != "" {
if r.Spec.Version != "" && isRKE2Version(r.Spec.Version) {
return r.Spec.Version
}

return r.Spec.AgentConfig.Version
}

// isRKE2Version checks if a string is an RKE2 version.
func isRKE2Version(rke2Version string) bool {
regexStr := "v(\\d\\.\\d{2}\\.\\d)\\+rke2r\\d"

regex, _ := regexp.Compile(regexStr)

return regex.MatchString(rke2Version)
}
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,9 @@ func (r *RKE2ControlPlaneReconciler) reconcileEtcdMembers(ctx context.Context, c
return errors.Wrap(err, "cannot get remote client to workload cluster")
}

parsedVersion, err := semver.ParseTolerant(controlPlane.RCP.Spec.AgentConfig.Version)
parsedVersion, err := semver.ParseTolerant(controlPlane.RCP.GetDesiredVersion())
if err != nil {
return errors.Wrapf(err, "failed to parse kubernetes version %q", controlPlane.RCP.Spec.AgentConfig.Version)
return errors.Wrapf(err, "failed to parse kubernetes version %q", controlPlane.RCP.GetDesiredVersion())
}

removedMembers, err := workloadCluster.ReconcileEtcdMembers(ctx, nodeNames, parsedVersion)
Expand Down
13 changes: 4 additions & 9 deletions controlplane/internal/controllers/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package controllers
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

Expand All @@ -42,7 +41,6 @@ import (
bootstrapv1 "github.com/rancher-sandbox/cluster-api-provider-rke2/bootstrap/api/v1beta1"
controlplanev1 "github.com/rancher-sandbox/cluster-api-provider-rke2/controlplane/api/v1beta1"
rke2 "github.com/rancher-sandbox/cluster-api-provider-rke2/pkg/rke2"
bsutil "github.com/rancher-sandbox/cluster-api-provider-rke2/pkg/util"
)

func (r *RKE2ControlPlaneReconciler) initializeControlPlane(
Expand Down Expand Up @@ -413,14 +411,11 @@ func (r *RKE2ControlPlaneReconciler) generateMachine(
bootstrapRef *corev1.ObjectReference,
failureDomain *string,
) error {
newVersion, err := bsutil.Rke2ToKubeVersion(rcp.Spec.AgentConfig.Version)
if err != nil {
return fmt.Errorf("failed to convert rke2 version to kubernetes version: %w", err)
}

logger := log.FromContext(ctx)

logger.Info("Version checking...", "rke2-version", rcp.Spec.AgentConfig.Version, "machine-version: ", newVersion)
rke2Version := rcp.GetDesiredVersion()

logger.Info("Version checking...", "rke2-version", rke2Version)

machine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -433,7 +428,7 @@ func (r *RKE2ControlPlaneReconciler) generateMachine(
},
Spec: clusterv1.MachineSpec{
ClusterName: cluster.Name,
Version: &newVersion,
Version: &rke2Version,
InfrastructureRef: *infraRef,
Bootstrap: clusterv1.Bootstrap{
ConfigRef: bootstrapRef,
Expand Down
8 changes: 6 additions & 2 deletions pkg/rke2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ type ServerConfigOpts struct {
AgentConfig bootstrapv1.RKE2AgentConfig
Ctx context.Context
Client client.Client
Version string
}

func newRKE2ServerConfig(opts ServerConfigOpts) (*rke2ServerConfig, []bootstrapv1.File, error) { // nolint:gocyclo
Expand Down Expand Up @@ -403,6 +404,7 @@ type AgentConfigOpts struct {
Client client.Client
CloudProviderName string
CloudProviderConfigMap *corev1.ObjectReference
Version string
}

func newRKE2AgentConfig(opts AgentConfigOpts) (*rke2AgentConfig, []bootstrapv1.File, error) {
Expand All @@ -411,8 +413,8 @@ func newRKE2AgentConfig(opts AgentConfigOpts) (*rke2AgentConfig, []bootstrapv1.F
rke2AgentConfig.ContainerRuntimeEndpoint = opts.AgentConfig.ContainerRuntimeEndpoint

if opts.AgentConfig.CISProfile != "" {
if !bsutil.ProfileCompliant(opts.AgentConfig.CISProfile, opts.AgentConfig.Version) {
return nil, nil, fmt.Errorf("profile %q is not supported for version %q", opts.AgentConfig.CISProfile, opts.AgentConfig.Version)
if !bsutil.ProfileCompliant(opts.AgentConfig.CISProfile, opts.Version) {
return nil, nil, fmt.Errorf("profile %q is not supported for version %q", opts.AgentConfig.CISProfile, opts.Version)
}

files = append(files, bootstrapv1.File{
Expand Down Expand Up @@ -550,6 +552,7 @@ func GenerateInitControlPlaneConfig(opts ServerConfigOpts) (*rke2ServerConfig, [
Client: opts.Client,
Ctx: opts.Ctx,
Token: opts.Token,
Version: opts.Version,
})
if err != nil {
return nil, nil, fmt.Errorf("failed to generate rke2 agent config: %w", err)
Expand Down Expand Up @@ -581,6 +584,7 @@ func GenerateJoinControlPlaneConfig(opts ServerConfigOpts) (*rke2ServerConfig, [
Ctx: opts.Ctx,
ServerURL: opts.ServerURL,
Token: opts.Token,
Version: opts.Version,
})
if err != nil {
return nil, nil, fmt.Errorf("failed to generate rke2 agent config: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/rke2/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ var _ = Describe("RKE2 Agent Config", func() {
ExtraEnv: map[string]string{"testenv": "testenv"},
ExtraMounts: map[string]string{"testmount": "testmount"},
},
Version: "v1.25.2",
},
Version: "v1.25.2",
}
})

Expand Down
4 changes: 3 additions & 1 deletion pkg/rke2/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ func (c *ControlPlane) FailureDomains() clusterv1.FailureDomains {

// Version returns the RKE2ControlPlane's version.
func (c *ControlPlane) Version() *string {
return &c.RCP.Spec.AgentConfig.Version
version := c.RCP.GetDesiredVersion()

return &version
}

// InfrastructureRef returns the RKE2ControlPlane's infrastructure template.
Expand Down
2 changes: 1 addition & 1 deletion pkg/rke2/machine_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func matchesRCPConfiguration(
rcp *controlplanev1.RKE2ControlPlane,
) func(machine *clusterv1.Machine) bool {
return collections.And(
matchesKubernetesVersion(rcp.Spec.AgentConfig.Version),
matchesKubernetesVersion(rcp.GetDesiredVersion()),
matchesRKE2BootstrapConfig(machineConfigs, rcp),
matchesTemplateClonedFrom(infraConfigs, rcp),
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/rke2/machine_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var _ = Describe("matchAgentConfig", func() {
var _ = Describe("matching Kubernetes Version", func() {
It("should match version", func() {
machineCollection := collections.FromMachines(&machine)
matches := machineCollection.AnyFilter(matchesKubernetesVersion(rcp.Spec.AgentConfig.Version))
matches := machineCollection.AnyFilter(matchesKubernetesVersion(rcp.GetDesiredVersion()))
Expect(len(matches)).To(Equal(1))
})
})
9 changes: 9 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ func Rke2ToKubeVersion(rk2Version string) (kubeVersion string, err error) {
return kubeVersion, nil
}

// IsRKE2Version checks if a string is an RKE2 version.
func IsRKE2Version(rke2Version string) bool {
regexStr := "v(\\d\\.\\d{2}\\.\\d)\\+rke2r\\d"

regex, _ := regexp.Compile(regexStr)

return regex.MatchString(rke2Version)
}

// AppendIfNotPresent appends a string to a slice only if the value does not already exist.
func AppendIfNotPresent(origSlice []string, strItem string) (resultSlice []string) {
present := false
Expand Down
13 changes: 13 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ var _ = Describe(("Testing GetMapKeysAsString"), func() {
Expect(keysValid).To(BeTrue())
})
})

var _ = Describe("Testing IsRKE2Version", func() {
k8sVersion := "1.24.6"
rke2Version := "v1.24.6+rke2r1"

It("Should return true if string is RKE2 version", func() {
Expect(IsRKE2Version(rke2Version)).To(BeTrue())
})

It("Should return false if string is not RKE2 version", func() {
Expect(IsRKE2Version(k8sVersion)).To(BeFalse())
})
})
5 changes: 2 additions & 3 deletions test/e2e/data/infrastructure/cluster-template-docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ metadata:
name: ${CLUSTER_NAME}-control-plane
spec:
replicas: ${CONTROL_PLANE_MACHINE_COUNT}
version: ${KUBERNETES_VERSION}+rke2r1
agentConfig:
version: ${KUBERNETES_VERSION}+rke2r1
nodeAnnotations:
test: "true"
serverConfig:
Expand Down Expand Up @@ -135,7 +135,7 @@ spec:
cluster.x-k8s.io/cluster-name: ${CLUSTER_NAME}
template:
spec:
version: ${KUBERNETES_VERSION}
version: ${KUBERNETES_VERSION}+rke2r1
clusterName: ${CLUSTER_NAME}
bootstrap:
configRef:
Expand Down Expand Up @@ -165,6 +165,5 @@ spec:
template:
spec:
agentConfig:
version: ${KUBERNETES_VERSION}+rke2r1
nodeAnnotations:
test: "true"

0 comments on commit 4e173ad

Please sign in to comment.