Skip to content

Commit 8124e51

Browse files
authored
Merge pull request #297 from hyperspike/operational_improvements
Operational improvements
2 parents e4f0e47 + 6d7f319 commit 8124e51

6 files changed

Lines changed: 599 additions & 313 deletions

File tree

Dockerfile.valkey

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM alpine:3.22.1 AS builder
22

3-
ARG VALKEY_VERSION=8.0.2
3+
ARG VALKEY_VERSION=8.1.3
44

55
WORKDIR /home/valkey
66

cfg/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var (
44
// Default Settings
55
DefaultSidecarImage string
66
DefaultValkeyImage string
7-
DefaultNodes int = 3
7+
DefaultNodes int32 = 3
88
)
99

1010
type Config struct {
@@ -13,7 +13,7 @@ type Config struct {
1313
// The default clusterwide valkey image to use
1414
ValkeyImage string `json:"valkeyImage"`
1515
// The default number of nodes to use
16-
Nodes int `json:"nodes"`
16+
Nodes int32 `json:"nodes"`
1717
}
1818

1919
func Defaults() *Config {

cmd/manager/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ func main() {
191191
config.ValkeyImage = v
192192
}
193193
if k == "nodes" && v != "" {
194-
config.Nodes, _ = strconv.Atoi(v)
194+
n, _ := strconv.ParseInt(v, 10, 32)
195+
config.Nodes = int32(n)
195196
}
196197
}
197198

config/rbac/role.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ rules:
4747
- patch
4848
- update
4949
- watch
50+
- apiGroups:
51+
- storage.k8s.io
52+
resources:
53+
- storageclasses
54+
verbs:
55+
- get
56+
- list
57+
- watch
5058
- apiGroups:
5159
- cert-manager.io
5260
resources:

internal/controller/cluster.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package controller
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"slices"
7+
8+
valkeyClient "github.com/valkey-io/valkey-go"
9+
)
10+
11+
// valkeyCluster represents a Valkey cluster. It contains a list of shards, each with its own nodes.
12+
type valkeyCluster struct {
13+
shards []*valkeyShard
14+
}
15+
16+
// valkeyShard represents a shard in the Valkey cluster. It contains slot information and a list of nodes.
17+
type valkeyShard struct {
18+
id int
19+
slotMin int
20+
slotMax int
21+
nodes []*valkeyNode
22+
}
23+
24+
// valkeyNode represents a node in the Valkey cluster.
25+
type valkeyNode struct {
26+
// id is the node id in the Valkey cluster
27+
id string
28+
// name is the pod name in Kubernetes
29+
name string
30+
// ip is the pod ip in Kubernetes
31+
ip string
32+
// port is the port of the Valkey service
33+
port int
34+
// flags are the Valkey flags for this pod
35+
flags []string
36+
// primary is the id of the primary node when this node is a replica
37+
primary string
38+
// connected is true when the pod is reachable from the operator
39+
connected bool
40+
// shard is the id of the shard this node belongs to
41+
shard int
42+
// client is the Valkey client for this node, if connected.
43+
client valkeyClient.Client
44+
}
45+
46+
// isPrimary checks if this node is a primary node for the shard in the Valkey cluster.
47+
func (vn *valkeyNode) isPrimary() bool {
48+
return slices.Contains(vn.flags, "master")
49+
}
50+
51+
// stsPodIndex extracts the pod index number from the pod name. The pod name is expected to be in
52+
// the format <name>-<number>. The pod index is the pod number from a StatefulSet.
53+
func stsPodIndex(podName string) (int, error) {
54+
pattern := `.*-(\d+)$`
55+
re := regexp.MustCompile(pattern)
56+
57+
matches := re.FindStringSubmatch(podName)
58+
if len(matches) < 2 {
59+
return 0, fmt.Errorf("no number found in pod name: %s", podName)
60+
}
61+
62+
// Convert the captured group to an integer
63+
var number int
64+
_, err := fmt.Sscanf(matches[1], "%d", &number)
65+
if err != nil {
66+
return 0, fmt.Errorf("failed to parse number: %w", err)
67+
}
68+
69+
return number, nil
70+
}

0 commit comments

Comments
 (0)