Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
Implement minimum MySQL version validation (#169)
Browse files Browse the repository at this point in the history
Resolves: #163
  • Loading branch information
prydie authored and owainlewis committed Jul 5, 2018
1 parent 2b5a21c commit 2e5d928
Show file tree
Hide file tree
Showing 16 changed files with 1,137 additions and 25 deletions.
7 changes: 6 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ required = [
branch = "master"
name = "github.com/heptiolabs/healthcheck"

[[constraint]]
name = "github.com/coreos/go-semver"
revision = "e214231b295a8ea9479f11b70b35d5acf3556d9b"

# gengo needs to be manually pinned to the version listed in code-generators
# Gopkg.toml, because the k8s project does not produce Gopkg.toml files & dep
# does not parse the Godeps.json file to determine revisions to use.
Expand Down
49 changes: 37 additions & 12 deletions pkg/apis/mysql/v1alpha1/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,51 @@
package v1alpha1

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func TestValidVersion(t *testing.T) {
for _, version := range validVersions {
errList := validateVersion(version, field.NewPath("spec", "version"))
if len(errList) > 0 {
t.Fail()
}
func TestValidateVersion(t *testing.T) {
fldPath := field.NewPath("spec", "version")
testCases := map[string]struct {
name string
version string
expected field.ErrorList
}{
"minimum_version_valid": {
version: MinimumMySQLVersion,
expected: field.ErrorList{},
},
"next_patch_version_valid": {
version: "8.0.12",
expected: field.ErrorList{},
},
"next_minor_version_valid": {
version: "8.1.0",
expected: field.ErrorList{},
},
"previous_version_invalid": {
version: "8.0.4",
expected: field.ErrorList{
field.Invalid(fldPath, "8.0.4", fmt.Sprintf("minimum supported MySQL version is %s", MinimumMySQLVersion)),
},
},
"5.7_version_invalid": {
version: "5.7.20-1.1.2",
expected: field.ErrorList{
field.Invalid(fldPath, "5.7.20-1.1.2", fmt.Sprintf("minimum supported MySQL version is %s", MinimumMySQLVersion)),
},
},
}
}

func TestInvalidVersion(t *testing.T) {
err := validateVersion("1.2.3", field.NewPath("spec", "version"))
if err == nil {
t.Fail()
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
errs := validateVersion(tc.version, fldPath)
assert.EqualValues(t, errs, tc.expected)
})
}
}

Expand Down
7 changes: 0 additions & 7 deletions pkg/apis/mysql/v1alpha1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ const (
ClusterNameMaxLen = 28
)

// TODO (owain) we need to remove this because it's not reasonable for us to maintain a list
// of all the potential MySQL versions that can be used and in reality, it shouldn't matter
// too much. The burden of this is not worth the benfit to a user
var validVersions = []string{
defaultVersion,
}

// setOperatorVersionLabel sets the specified operator version label on the label map.
func setOperatorVersionLabel(labelMap map[string]string, label string) {
labelMap[constants.MySQLOperatorVersionLabel] = label
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/mysql/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// MinimumMySQLVersion is the minimum version of MySQL server supported by the
// MySQL Operator.
const MinimumMySQLVersion = "8.0.11"

// ClusterSpec defines the attributes a user can specify when creating a cluster
type ClusterSpec struct {
// Version defines the MySQL Docker image version.
Expand Down
24 changes: 19 additions & 5 deletions pkg/apis/mysql/v1alpha1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ import (
"fmt"
"strconv"

"github.com/oracle/mysql-operator/pkg/constants"
"github.com/coreos/go-semver/semver"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/oracle/mysql-operator/pkg/constants"
)

func validateCluster(c *Cluster) field.ErrorList {
Expand Down Expand Up @@ -67,12 +70,23 @@ func validateClusterStatus(s ClusterStatus, fldPath *field.Path) field.ErrorList

func validateVersion(version string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
for _, validVersion := range validVersions {
if version == validVersion {
return allErrs
min, err := semver.NewVersion(MinimumMySQLVersion)
if err != nil {
allErrs = append(allErrs, field.InternalError(fldPath, fmt.Errorf("unable to parse minimum MySQL version: %v", err)))
}

given, err := semver.NewVersion(version)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, version, fmt.Sprintf("unable to parse MySQL version: %v", err)))
}

if len(allErrs) == 0 {
if given.Compare(*min) == -1 {
allErrs = append(allErrs, field.Invalid(fldPath, version, fmt.Sprintf("minimum supported MySQL version is %s", MinimumMySQLVersion)))
}
}
return append(allErrs, field.Invalid(fldPath, version, "invalid version specified"))

return allErrs
}

func validateBaseServerID(baseServerID uint32, fldPath *field.Path) field.ErrorList {
Expand Down
8 changes: 8 additions & 0 deletions vendor/github.com/coreos/go-semver/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions vendor/github.com/coreos/go-semver/DCO

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2e5d928

Please sign in to comment.