Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Feb 7, 2024
1 parent 81d7119 commit 41d7f68
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 228 deletions.
31 changes: 27 additions & 4 deletions internal/webhooks/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down Expand Up @@ -435,7 +436,7 @@ func (webhook *Cluster) validateTopologyVersion(ctx context.Context, fldPath *fi
}

// minor version cannot be increased if MachineDeployments are upgrading or not yet on the current version
if err := validateTopologyMachineDeploymentVersions(ctx, fldPath, webhook.Client, oldCluster); err != nil {
if err := validateTopologyMachineDeploymentVersions(ctx, fldPath, webhook.Client, oldCluster, oldVersion); err != nil {
allErrs = append(allErrs, field.Invalid(
fldPath,
fldValue,
Expand All @@ -444,7 +445,7 @@ func (webhook *Cluster) validateTopologyVersion(ctx context.Context, fldPath *fi
}

// minor version cannot be increased if MachinePools are upgrading or not yet on the current version
if err := validateTopologyMachinePoolVersions(ctx, fldPath, webhook.Client, webhook.Tracker, oldCluster); err != nil {
if err := validateTopologyMachinePoolVersions(ctx, fldPath, webhook.Client, webhook.Tracker, oldCluster, oldVersion); err != nil {
allErrs = append(allErrs, field.Invalid(
fldPath,
fldValue,
Expand Down Expand Up @@ -499,7 +500,7 @@ func validateTopologyControlPlaneVersion(ctx context.Context, fldPath *field.Pat
return nil
}

func validateTopologyMachineDeploymentVersions(ctx context.Context, fldPath *field.Path, ctrlClient client.Reader, oldCluster *clusterv1.Cluster) error {
func validateTopologyMachineDeploymentVersions(ctx context.Context, fldPath *field.Path, ctrlClient client.Reader, oldCluster *clusterv1.Cluster, oldVersion semver.Version) error {
// FROM: desired_state.go getCurrentMachineDeploymentState

// List all the machine deployments in the current cluster and in a managed topology.
Expand All @@ -524,6 +525,17 @@ func validateTopologyMachineDeploymentVersions(ctx context.Context, fldPath *fie
for i := range mds.Items {
md := &mds.Items[i]

mdVersion, err := semver.ParseTolerant(*md.Spec.Template.Spec.Version)
if err != nil {
// NOTE: this should never happen. Nevertheless, handling this for extra caution.
return field.InternalError(fldPath, errors.Wrapf(err, "failed to parse MachineDeployment's %q version %q", klog.KObj(md), *md.Spec.Template.Spec.Version))
}

if mdVersion.NE(oldVersion) {
mdUpgradingNames = append(mdUpgradingNames, md.Name)
continue
}

upgrading, err := check.IsMachineDeploymentUpgrading(ctx, ctrlClient, md)
if err != nil {
return errors.Wrap(err, "failed to list upgrading MachinePools")
Expand All @@ -540,7 +552,7 @@ func validateTopologyMachineDeploymentVersions(ctx context.Context, fldPath *fie
return nil
}

func validateTopologyMachinePoolVersions(ctx context.Context, fldPath *field.Path, ctrlClient client.Reader, tracker ClusterCacheTrackerReader, oldCluster *clusterv1.Cluster) error {
func validateTopologyMachinePoolVersions(ctx context.Context, fldPath *field.Path, ctrlClient client.Reader, tracker ClusterCacheTrackerReader, oldCluster *clusterv1.Cluster, oldVersion semver.Version) error {
// FROM: desired_state.go getCurrentMachinePoolState

// List all the machine pools in the current cluster and in a managed topology.
Expand Down Expand Up @@ -571,6 +583,17 @@ func validateTopologyMachinePoolVersions(ctx context.Context, fldPath *field.Pat
for i := range mps.Items {
mp := &mps.Items[i]

mpVersion, err := semver.ParseTolerant(*mp.Spec.Template.Spec.Version)
if err != nil {
// NOTE: this should never happen. Nevertheless, handling this for extra caution.
return field.InternalError(fldPath, errors.Wrapf(err, "failed to parse MachinePool's %q version %q", klog.KObj(mp), *mp.Spec.Template.Spec.Version))
}

if mpVersion.NE(oldVersion) {
mpUpgradingNames = append(mpUpgradingNames, mp.Name)
continue
}

upgrading, err := check.IsMachinePoolUpgrading(ctx, wlClient, mp)
if err != nil {
return errors.Wrap(err, "failed to list upgrading MachinePools")
Expand Down
Loading

0 comments on commit 41d7f68

Please sign in to comment.