Skip to content

Commit 9fd7f95

Browse files
redis >=6 skip patch version compare (#115)
Issue #, if available: Description of changes: The purpose of this changes is to avoid the controller to compare patch versions when Redis version is >=6, since It's managed by AWS. More details [here](aws-controllers-k8s/community#1737). By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 5ea3ebc commit 9fd7f95

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

pkg/resource/replication_group/delta_util.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package replication_group
1616
import (
1717
"encoding/json"
1818
"reflect"
19-
"regexp"
19+
"strconv"
2020
"strings"
2121

2222
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
@@ -82,13 +82,14 @@ func engineVersionsMatch(
8282
return true
8383
}
8484

85-
// if the last character of desiredEV is "x", only check for a major version match
85+
dMaj, dMin, _ := versionNumbersFromString(desiredEV)
86+
lMaj, lMin, _ := versionNumbersFromString(latestEV)
8687
last := len(desiredEV) - 1
87-
if desiredEV[last:] == "x" {
88-
// cut off the "x" and replace all occurrences of '.' with '\.' (as '.' is a special regex character)
89-
desired := strings.Replace(desiredEV[:last], ".", "\\.", -1)
90-
r, _ := regexp.Compile(desired + ".*")
91-
return r.MatchString(latestEV)
88+
89+
// if the last character of desiredEV is "x" or the major version is higher than 5, ignore patch version when comparing.
90+
// See https://github.com/aws-controllers-k8s/community/issues/1737
91+
if dMaj > 5 || desiredEV[last:] == "x" {
92+
return dMaj == lMaj && (dMin < 0 || dMin == lMin)
9293
}
9394

9495
return false
@@ -187,3 +188,28 @@ func primaryClusterIDRequiresUpdate(desired *resource, latest *resource) (bool,
187188

188189
return false, nil
189190
}
191+
192+
// versionNumbersFromString takes a version string like "6.2", "6.x" or "7.0.4" and
193+
// returns the major, minor and patch numbers. If no minor or patch numbers are present
194+
// or contain the "x" placeholder, -1 is returned for that version number.
195+
func versionNumbersFromString(version string) (int, int, int) {
196+
parts := strings.Split(version, ".")
197+
major := -1
198+
minor := -1
199+
patch := -1
200+
if len(parts) == 0 {
201+
return major, minor, patch
202+
}
203+
major, _ = strconv.Atoi(parts[0])
204+
if len(parts) > 1 {
205+
if !strings.EqualFold(parts[1], "x") {
206+
minor, _ = strconv.Atoi(parts[1])
207+
}
208+
}
209+
if len(parts) > 2 {
210+
if !strings.EqualFold(parts[2], "x") {
211+
patch, _ = strconv.Atoi(parts[2])
212+
}
213+
}
214+
return major, minor, patch
215+
}

pkg/resource/replication_group/delta_util_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import "github.com/stretchr/testify/require"
1919
func TestEngineVersionsMatch(t *testing.T) {
2020
require := require.New(t)
2121

22+
require.False(engineVersionsMatch("6.3", "6.2.6"))
23+
require.True(engineVersionsMatch("6.2", "6.2.6"))
2224
require.True(engineVersionsMatch("6.x", "6.0.5"))
2325
require.False(engineVersionsMatch("13.x", "6.0.6"))
2426
require.True(engineVersionsMatch("5.0.3", "5.0.3"))

0 commit comments

Comments
 (0)