From 6b84fee2abf2ee6dd92aeed6b3fe310b9b05c83d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Tue, 9 Apr 2019 17:36:58 +0200 Subject: [PATCH] ceph: add a CephVersionFormatted so we can consume it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let's return the expected pattern so that can be consumed by ExtractCephVersion later. We don't modify the built-in String() so we can more easily remove that once we switch the osd to be configured from the operator. Signed-off-by: Sébastien Han --- pkg/operator/ceph/version/version.go | 16 ++++++++++++++++ pkg/operator/ceph/version/version_test.go | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/operator/ceph/version/version.go b/pkg/operator/ceph/version/version.go index 6da2f2448..bc89bfa45 100644 --- a/pkg/operator/ceph/version/version.go +++ b/pkg/operator/ceph/version/version.go @@ -6,6 +6,7 @@ import ( "strconv" ) +// CephVersion represents the Ceph version format type CephVersion struct { Major int Minor int @@ -41,6 +42,12 @@ func (v *CephVersion) String() string { v.Major, v.Minor, v.Extra, v.ReleaseName()) } +// CephVersionFormatted returns the Ceph version in a human readable format +func (v *CephVersion) CephVersionFormatted() string { + return fmt.Sprintf("ceph version %d.%d.%d %s", + v.Major, v.Minor, v.Extra, v.ReleaseName()) +} + // ReleaseName is the name of the Ceph release func (v *CephVersion) ReleaseName() string { switch v.Major { @@ -57,6 +64,7 @@ func (v *CephVersion) ReleaseName() string { } } +// ExtractCephVersion extracts the major, minor and extra digit of a Ceph release func ExtractCephVersion(src string) (*CephVersion, error) { m := versionPattern.FindStringSubmatch(src) if m == nil { @@ -81,6 +89,7 @@ func ExtractCephVersion(src string) (*CephVersion, error) { return &CephVersion{major, minor, extra}, nil } +// Supported checks if a given release is supported func (v *CephVersion) Supported() bool { for _, sv := range supportedVersions { if v.isRelease(sv) { @@ -94,26 +103,31 @@ func (v *CephVersion) isRelease(other CephVersion) bool { return v.Major == other.Major } +// IsLuminous checks if the Ceph version if Luminous func (v *CephVersion) IsLuminous() bool { return v.isRelease(Luminous) } +// IsAtLeast checks a given Ceph version is at least a given one func (v *CephVersion) IsAtLeast(other CephVersion) bool { if v.Major > other.Major { return true } else if v.Major < other.Major { return false } + // If we arrive here then v.Major == other.Major if v.Minor > other.Minor { return true } else if v.Minor < other.Minor { return false } + // If we arrive here then v.Minor == other.Minor if v.Extra > other.Extra { return true } else if v.Extra < other.Extra { return false } + // If we arrive here then both versions are identical return true } @@ -122,10 +136,12 @@ func (v *CephVersion) IsAtLeastOctopus() bool { return v.IsAtLeast(Octopus) } +// IsAtLeastNautilus check that the Ceph version is at least Nautilus func (v *CephVersion) IsAtLeastNautilus() bool { return v.IsAtLeast(Nautilus) } +// IsAtLeastMimic check that the Ceph version is at least Mimic func (v *CephVersion) IsAtLeastMimic() bool { return v.IsAtLeast(Mimic) } diff --git a/pkg/operator/ceph/version/version_test.go b/pkg/operator/ceph/version/version_test.go index 28a80f0f1..55323d516 100644 --- a/pkg/operator/ceph/version/version_test.go +++ b/pkg/operator/ceph/version/version_test.go @@ -16,6 +16,12 @@ func TestToString(t *testing.T) { assert.Equal(t, expected, fmt.Sprintf("%s", &CephVersion{-1, 0, 0})) } +func TestCephVersionFormatted(t *testing.T) { + assert.Equal(t, "ceph version 14.0.0 nautilus", Nautilus.CephVersionFormatted()) + assert.Equal(t, "ceph version 13.0.0 mimic", Mimic.CephVersionFormatted()) + assert.Equal(t, "ceph version 12.0.0 luminous", Luminous.CephVersionFormatted()) +} + func TestReleaseName(t *testing.T) { assert.Equal(t, "nautilus", Nautilus.ReleaseName()) assert.Equal(t, "mimic", Mimic.ReleaseName())