Skip to content

Commit 44792d2

Browse files
authored
[Feature] Version Check V2 (#1348)
1 parent a3206f0 commit 44792d2

File tree

15 files changed

+402
-18
lines changed

15 files changed

+402
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- (Improvement) Extract Agency Timeout
99
- (Feature) Rebalancer V2
1010
- (Bugfix) Fix for ContextExceeded error during backup upload
11+
- (Feature) Version Check V2
1112

1213
## [1.2.30](https://github.com/arangodb/kube-arangodb/tree/1.2.30) (2023-06-16)
1314
- (Feature) AgencyCache Interface

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ run-unit-tests: $(SOURCES)
506506
$(REPOPATH)/pkg/storage \
507507
$(REPOPATH)/pkg/crd/... \
508508
$(REPOPATH)/pkg/util/... \
509+
$(REPOPATH)/cmd/... \
509510
$(REPOPATH)/pkg/handlers/...
510511

511512
# Release building

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ covers individual newer features separately.
6363
| Encryption Key Rotation Support | 1.2.0 | > 3.7.0 | Enterprise | 1.0.3 | NotSupported | False | --deployment.feature.encryption-rotation | N/A |
6464
| Version Check | 1.1.4 | >= 3.6.0 | Community, Enterprise | 1.1.4 | Alpha | False | --deployment.feature.upgrade-version-check | N/A |
6565
| Version Check | 1.2.23 | >= 3.6.0 | Community, Enterprise | 1.1.4 | Production | True | --deployment.feature.upgrade-version-check | N/A |
66+
| Version Check V2 | 1.2.31 | >= 3.6.0 | Community, Enterprise | 1.2.31 | Alpha | False | --deployment.feature.upgrade-version-check-v2 | N/A |
6667
| Operator Maintenance Management Support | 1.2.0 | >= 3.6.0 | Community, Enterprise | 1.0.7 | Production | True | --deployment.feature.maintenance | N/A |
6768
| Graceful Restart | 1.2.5 | >= 3.6.0 | Community, Enterprise | 1.0.7 | Production | True | --deployment.feature.graceful-shutdown | N/A |
6869
| Optional Graceful Restart | 1.2.25 | >= 3.6.0 | Community, Enterprise | 1.2.5 | Beta | True | --deployment.feature.optional-graceful-shutdown | N/A |

cmd/cmd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ func Execute() int {
242242
flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
243243

244244
if err := cmdMain.Execute(); err != nil {
245+
if v, ok := err.(CommandExitCode); ok {
246+
return v.ExitCode
247+
}
248+
245249
return 1
246250
}
247251

cmd/cmd_exit_code.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package cmd
22+
23+
import (
24+
"fmt"
25+
)
26+
27+
type CommandExitCode struct {
28+
ExitCode int
29+
}
30+
31+
func (c CommandExitCode) Error() string {
32+
return fmt.Sprintf("Command exit: %d", c.ExitCode)
33+
}
34+
35+
func Exit(code int) error {
36+
return CommandExitCode{
37+
ExitCode: code,
38+
}
39+
}

cmd/cmd_exit_code_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package cmd
22+
23+
import (
24+
"testing"
25+
26+
"github.com/stretchr/testify/require"
27+
)
28+
29+
func ensureExitCode(t *testing.T, err error, code int) {
30+
require.Error(t, err)
31+
v, ok := err.(CommandExitCode)
32+
require.True(t, ok)
33+
require.Equal(t, v.ExitCode, code)
34+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package cmd
22+
23+
import (
24+
"encoding/json"
25+
"os"
26+
27+
"github.com/pkg/errors"
28+
"github.com/rs/zerolog/log"
29+
"github.com/spf13/cobra"
30+
)
31+
32+
const cmdVersionCheckInitContainersInvalidVersionExitCode = 11
33+
34+
type cmdVersionCheckInitContainersInputStruct struct {
35+
versionPath string
36+
37+
major, minor int
38+
}
39+
40+
var (
41+
cmdVersionCheckInitContainers = &cobra.Command{
42+
Use: "version-check",
43+
RunE: cmdVersionCheckInitContainersInput.Run,
44+
}
45+
46+
cmdVersionCheckInitContainersInput cmdVersionCheckInitContainersInputStruct
47+
)
48+
49+
type cmdVersionCheckInitContainersData struct {
50+
Version int `json:"version,omitempty"`
51+
}
52+
53+
func init() {
54+
cmdInitContainers.AddCommand(cmdVersionCheckInitContainers)
55+
f := cmdVersionCheckInitContainers.Flags()
56+
f.StringVar(&cmdVersionCheckInitContainersInput.versionPath, "path", "", "Path to the VERSION file")
57+
f.IntVar(&cmdVersionCheckInitContainersInput.major, "major", 0, "Major version of the ArangoDB. 0 if check is disabled")
58+
f.IntVar(&cmdVersionCheckInitContainersInput.minor, "minor", 0, "Minor version of the ArangoDB. 0 if check is disabled")
59+
}
60+
61+
func (c cmdVersionCheckInitContainersInputStruct) Run(cmd *cobra.Command, args []string) error {
62+
if c.versionPath == "" {
63+
return errors.Errorf("Path cannot be empty")
64+
}
65+
66+
if data, err := os.ReadFile(c.versionPath); err != nil {
67+
log.Err(err).Msg("File is not readable, continue")
68+
return nil
69+
} else {
70+
major, minor, _, ok := extractVersionFromData(data)
71+
if !ok {
72+
return nil
73+
}
74+
75+
if c.major != 0 {
76+
if c.major != major {
77+
return Exit(cmdVersionCheckInitContainersInvalidVersionExitCode)
78+
}
79+
if c.minor != 0 {
80+
if c.minor != minor {
81+
return Exit(cmdVersionCheckInitContainersInvalidVersionExitCode)
82+
}
83+
}
84+
}
85+
86+
return nil
87+
}
88+
}
89+
90+
func extractVersionFromData(data []byte) (int, int, int, bool) {
91+
var c cmdVersionCheckInitContainersData
92+
93+
if err := json.Unmarshal(data, &c); err != nil {
94+
log.Err(err).Msg("Invalid json, continue")
95+
return 0, 0, 0, false
96+
}
97+
98+
return c.Version / 10000, c.Version % 10000 / 100, c.Version % 100, true
99+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package cmd
22+
23+
import (
24+
"encoding/json"
25+
"fmt"
26+
"os"
27+
"testing"
28+
29+
"github.com/stretchr/testify/require"
30+
)
31+
32+
func saveVersionFile(t *testing.T, v int, updates ...func(in *cmdVersionCheckInitContainersInputStruct)) *cmdVersionCheckInitContainersInputStruct {
33+
var q cmdVersionCheckInitContainersData
34+
35+
q.Version = v
36+
37+
d, err := json.Marshal(q)
38+
require.NoError(t, err)
39+
40+
var n cmdVersionCheckInitContainersInputStruct
41+
42+
n.versionPath = fmt.Sprintf("%s/VERSION", t.TempDir())
43+
44+
require.NoError(t, os.WriteFile(n.versionPath, d, 0644))
45+
46+
for _, u := range updates {
47+
u(&n)
48+
}
49+
50+
return &n
51+
}
52+
53+
func Test_extractVersionFromData(t *testing.T) {
54+
check := func(valid bool, name string, version int, updates ...func(in *cmdVersionCheckInitContainersInputStruct)) {
55+
t.Run(name, func(t *testing.T) {
56+
err := saveVersionFile(t, version, updates...).Run(nil, nil)
57+
if valid {
58+
require.NoError(t, err)
59+
} else {
60+
ensureExitCode(t, err, cmdVersionCheckInitContainersInvalidVersionExitCode)
61+
}
62+
})
63+
}
64+
65+
check(true, "3.9.10_optional", 30910)
66+
67+
check(true, "3.9.10_required_major", 30910, func(in *cmdVersionCheckInitContainersInputStruct) {
68+
in.major = 3
69+
})
70+
71+
check(true, "3.9.10_required_minor", 30910, func(in *cmdVersionCheckInitContainersInputStruct) {
72+
in.major = 3
73+
in.minor = 9
74+
})
75+
76+
check(false, "3.9.10_required_major_mismatch", 30910, func(in *cmdVersionCheckInitContainersInputStruct) {
77+
in.major = 4
78+
})
79+
80+
check(false, "3.9.10_required_minor_mismatch", 30910, func(in *cmdVersionCheckInitContainersInputStruct) {
81+
in.major = 3
82+
in.minor = 5
83+
})
84+
85+
check(true, "3.9.10_required_minor_only_mismatch", 30910, func(in *cmdVersionCheckInitContainersInputStruct) {
86+
in.minor = 5
87+
})
88+
}

cmd/init_containers.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package cmd
22+
23+
import "github.com/spf13/cobra"
24+
25+
var (
26+
cmdInitContainers = &cobra.Command{
27+
Use: "init-containers",
28+
Run: func(cmd *cobra.Command, args []string) {
29+
30+
},
31+
Hidden: true,
32+
}
33+
)
34+
35+
func init() {
36+
cmdMain.AddCommand(cmdInitContainers)
37+
}

pkg/deployment/features/upgrade.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ package features
2222

2323
func init() {
2424
registerFeature(upgradeVersionCheck)
25+
registerFeature(upgradeVersionCheckV2)
2526
}
2627

2728
var upgradeVersionCheck Feature = &feature{
@@ -32,6 +33,18 @@ var upgradeVersionCheck Feature = &feature{
3233
enabledByDefault: true,
3334
}
3435

36+
var upgradeVersionCheckV2 Feature = &feature{
37+
name: "upgrade-version-check-v2",
38+
description: "Enable initContainer with pre version check based by Operator",
39+
version: "3.6.0",
40+
enterpriseRequired: false,
41+
enabledByDefault: true,
42+
}
43+
3544
func UpgradeVersionCheck() Feature {
3645
return upgradeVersionCheck
3746
}
47+
48+
func UpgradeVersionCheckV2() Feature {
49+
return upgradeVersionCheckV2
50+
}

0 commit comments

Comments
 (0)