-
Notifications
You must be signed in to change notification settings - Fork 2
/
redisshard_types.go
161 lines (135 loc) · 4.94 KB
/
redisshard_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"strconv"
"strings"
"github.com/3scale-ops/basereconciler/util"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
redisShardDefaultImage defaultImageSpec = defaultImageSpec{
Name: util.Pointer("redis"),
Tag: util.Pointer("4.0.11-alpine"),
PullPolicy: (*corev1.PullPolicy)(util.Pointer(string(corev1.PullIfNotPresent))),
}
redisShardDefaultMasterIndex int32 = 0
redisShardDefaultCommand string = "redis-server /redis/redis.conf"
RedisShardDefaultReplicas int32 = 3
)
// RedisShardSpec defines the desired state of RedisShard
type RedisShardSpec struct {
// Image specification for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Image *ImageSpec `json:"image,omitempty"`
// MasterIndex is the StatefulSet Pod index of the redis server
// with the master role. The other Pods are slaves of the master one.
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
MasterIndex *int32 `json:"masterIndex,omitempty"`
// SlaveCount is the number of redis slaves
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
SlaveCount *int32 `json:"slaveCount,omitempty"`
// Command overrides the redis container command
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Command *string `json:"command,omitempty"`
}
// Default implements defaulting for RedisShardSpec
func (spec *RedisShardSpec) Default() {
spec.Image = InitializeImageSpec(spec.Image, redisShardDefaultImage)
spec.MasterIndex = intOrDefault(spec.MasterIndex, &redisShardDefaultMasterIndex)
spec.SlaveCount = intOrDefault(spec.SlaveCount, util.Pointer(RedisShardDefaultReplicas-1))
spec.Command = stringOrDefault(spec.Command, &redisShardDefaultCommand)
}
type RedisShardNodes struct {
// Master is the node that acts as master role in the redis shard
// +operator-sdk:csv:customresourcedefinitions:type=status
// +optional
Master map[string]string `json:"master,omitempty"`
// Slaves are the nodes that act as master role in the redis shard
// +operator-sdk:csv:customresourcedefinitions:type=status
// +optional
Slaves map[string]string `json:"slaves,omitempty"`
}
func (rsn *RedisShardNodes) MasterHostPort() string {
for _, hostport := range rsn.Master {
return hostport
}
return ""
}
func (rsn *RedisShardNodes) GetNodeByPodIndex(podIndex int) (string, string) {
nodes := util.MergeMaps(map[string]string{}, rsn.Master, rsn.Slaves)
for alias, hostport := range nodes {
i := alias[strings.LastIndex(alias, "-")+1:]
index, _ := strconv.Atoi(i)
if index == podIndex {
return alias, hostport
}
}
return "", ""
}
func (rsn *RedisShardNodes) GetHostPortByPodIndex(podIndex int) string {
_, hostport := rsn.GetNodeByPodIndex(podIndex)
return hostport
}
func (rsn *RedisShardNodes) GetAliasByPodIndex(podIndex int) string {
alias, _ := rsn.GetNodeByPodIndex(podIndex)
return alias
}
func (rsn *RedisShardNodes) GetIndexByHostPort(hostport string) int {
nodes := util.MergeMaps(map[string]string{}, rsn.Master, rsn.Slaves)
for alias, hp := range nodes {
if hostport == hp {
i := alias[strings.LastIndex(alias, "-")+1:]
index, _ := strconv.Atoi(i)
return index
}
}
return -1
}
// RedisShardStatus defines the observed state of RedisShard
type RedisShardStatus struct {
// ShardNodes describes the nodes in the redis shard
// +operator-sdk:csv:customresourcedefinitions:type=status
// +optional
ShardNodes *RedisShardNodes `json:"shardNodes,omitempty"`
}
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
// RedisShard is the Schema for the redisshards API
// +kubebuilder:printcolumn:JSONPath=".status.shardNodes.master",name=Master,type=string
// +kubebuilder:printcolumn:JSONPath=".status.shardNodes.slaves",name=Slaves,type=string
type RedisShard struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec RedisShardSpec `json:"spec,omitempty"`
Status RedisShardStatus `json:"status,omitempty"`
}
// Default implements defaulting for the RedisShard resource
func (rs *RedisShard) Default() {
rs.Spec.Default()
}
//+kubebuilder:object:root=true
// RedisShardList contains a list of RedisShard
type RedisShardList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []RedisShard `json:"items"`
}
func init() {
SchemeBuilder.Register(&RedisShard{}, &RedisShardList{})
}