Skip to content

Commit c8cc946

Browse files
committed
feat(upgrade): specify upgrade starting version constraint
Signed-off-by: Sergei Lukianov <me@slukjanov.name>
1 parent 6c231e0 commit c8cc946

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

api/fabricator/v1beta1/fabricator_types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,9 @@ func (f *Fabricator) Validate(ctx context.Context) error {
623623
}
624624

625625
func (f *Fabricator) CalculateVersions(def Versions) error {
626-
f.Status.Versions = *f.Spec.Overrides.Versions.DeepCopy()
626+
if f.Status.Versions.Fabricator.Controller == "" {
627+
f.Status.Versions = *f.Spec.Overrides.Versions.DeepCopy()
628+
}
627629

628630
if err := mergo.Merge(&f.Status.Versions, def); err != nil {
629631
return fmt.Errorf("merging versions: %w", err)

pkg/fab/recipe/control_upgrade.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path/filepath"
1414
"time"
1515

16+
"github.com/Masterminds/semver/v3"
1617
vpcapi "go.githedgehog.com/fabric/api/vpc/v1beta1"
1718
wiringapi "go.githedgehog.com/fabric/api/wiring/v1beta1"
1819
"go.githedgehog.com/fabric/pkg/util/kubeutil"
@@ -81,6 +82,25 @@ func (c *ControlUpgrade) Run(ctx context.Context) error {
8182
return fmt.Errorf("retrying getting fabricator and control nodes: %w", err)
8283
}
8384

85+
constraint, err := semver.NewConstraint(string(fab.UpgradeConstraint))
86+
if err != nil {
87+
return fmt.Errorf("parsing upgrade constraint: %w", err)
88+
}
89+
90+
slog.Info("Currently running fabricator", "version", c.Fab.Status.Versions.Fabricator.Controller)
91+
92+
// TODO should we add some other markers on the host to make it more reliable?
93+
version, err := semver.NewVersion(string(c.Fab.Status.Versions.Fabricator.Controller))
94+
if err != nil {
95+
return fmt.Errorf("parsing fabricator version: %w", err)
96+
}
97+
98+
if !constraint.Check(version) {
99+
slog.Error("Upgrading from the current version not be supported, please upgrade to the older versions first")
100+
101+
return fmt.Errorf("fabricator version %q does not match upgrade constraint %q", version, constraint) //nolint:goerr113
102+
}
103+
84104
if err := waitKube(ctx, kube, c.Control.Name, "",
85105
&comp.Node{}, func(obj *comp.Node) (bool, error) {
86106
for _, cond := range obj.Status.Conditions {

pkg/fab/versions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
var (
15+
UpgradeConstraint = meta.Version(">=0.32.1-0") // 24.09 release; -0 is added to allow pre-release versions
1516
FabricatorVersion = meta.Version(version.Version)
1617
FabricVersion = meta.Version("v0.75.0")
1718
GatewayVersion = meta.Version("v0.5.0")

pkg/fab/versions_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"strings"
99
"testing"
1010

11+
"github.com/Masterminds/semver/v3"
1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
14+
"go.githedgehog.com/fabricator/pkg/version"
1315
"golang.org/x/mod/modfile"
1416
)
1517

@@ -56,3 +58,26 @@ func checkVersion(t *testing.T, modfile *modfile.File, path, version string) {
5658

5759
assert.Truef(t, found, "Require path %s not found in go.mod", path)
5860
}
61+
62+
func TestUpgradeConstraint(t *testing.T) {
63+
c, err := semver.NewConstraint(string(UpgradeConstraint))
64+
require.NoError(t, err, "Error parsing upgrade constraint")
65+
66+
for _, test := range []struct {
67+
name string
68+
version string
69+
expected bool
70+
}{
71+
{"current", version.Version, true},
72+
{"25.01", "v0.36.1", true},
73+
{"24.09", "v0.32.1", true},
74+
{"beta-1", "v0.30.3", false},
75+
} {
76+
t.Run(string(test.version), func(t *testing.T) {
77+
v, err := semver.NewVersion(string(test.version))
78+
require.NoError(t, err, "Error parsing version %q", test.version)
79+
80+
require.Equal(t, test.expected, c.Check(v), "Upgrade from version %q should be %v", test.version, test.expected)
81+
})
82+
}
83+
}

0 commit comments

Comments
 (0)