Skip to content

Commit d9debd9

Browse files
authored
Merge pull request #44 from ucloud/v0.2.4
V0.2.4
2 parents 7494848 + 052fcc2 commit d9debd9

File tree

10 files changed

+305
-28
lines changed

10 files changed

+305
-28
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ARG GIT_SHA=0000000
1313

1414
WORKDIR /src
1515

16-
COPY go.mod ./ go.sum ./
16+
COPY go.mod go.sum ./
1717
RUN go mod download
1818

1919
COPY pkg ./ cmd ./ version ./

pkg/apis/redis/v1alpha1/default.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import (
1212
)
1313

1414
const (
15-
minMasterSize = 3
16-
minClusterReplicas = 1
17-
defaultRedisImage = "redis:5.0.4-alpine"
15+
minMasterSize = 3
16+
minClusterReplicas = 1
17+
defaultRedisImage = "redis:5.0.4-alpine"
18+
defaultMonitorImage = "oliver006/redis_exporter:latest"
1819
)
1920

2021
func (in *DistributedRedisCluster) DefaultSpec(log logr.Logger) bool {
@@ -41,6 +42,11 @@ func (in *DistributedRedisCluster) DefaultSpec(log logr.Logger) bool {
4142

4243
mon := in.Spec.Monitor
4344
if mon != nil {
45+
if mon.Image == "" {
46+
mon.Image = defaultMonitorImage
47+
update = true
48+
}
49+
4450
if mon.Prometheus == nil {
4551
mon.Prometheus = &PrometheusSpec{}
4652
update = true

pkg/apis/redis/v1alpha1/distributedrediscluster_types.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ type DistributedRedisClusterSpec struct {
2424
ClusterReplicas int32 `json:"clusterReplicas,omitempty"`
2525
ServiceName string `json:"serviceName,omitempty"`
2626
Config map[string]string `json:"config,omitempty"`
27-
Affinity *corev1.Affinity `json:"affinity,omitempty"`
28-
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
29-
ToleRations []corev1.Toleration `json:"toleRations,omitempty"`
30-
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
31-
Annotations map[string]string `json:"annotations,omitempty"`
32-
Storage *RedisStorage `json:"storage,omitempty"`
33-
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
34-
PasswordSecret *corev1.LocalObjectReference `json:"passwordSecret,omitempty"`
35-
Monitor *AgentSpec `json:"monitor,omitempty"`
36-
Init *InitSpec `json:"init,omitempty"`
27+
// Set RequiredAntiAffinity to force the master-slave node anti-affinity.
28+
RequiredAntiAffinity bool `json:"requiredAntiAffinity,omitempty"`
29+
Affinity *corev1.Affinity `json:"affinity,omitempty"`
30+
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
31+
ToleRations []corev1.Toleration `json:"toleRations,omitempty"`
32+
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
33+
Annotations map[string]string `json:"annotations,omitempty"`
34+
Storage *RedisStorage `json:"storage,omitempty"`
35+
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
36+
PasswordSecret *corev1.LocalObjectReference `json:"passwordSecret,omitempty"`
37+
Monitor *AgentSpec `json:"monitor,omitempty"`
38+
Init *InitSpec `json:"init,omitempty"`
3739
}
3840

3941
type AgentSpec struct {

pkg/apis/redis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/controller/manager/ensurer.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,25 @@ func shouldUpdateRedis(cluster *redisv1alpha1.DistributedRedisCluster, sts *apps
119119
if result := expectResource.Limits.Cpu().Cmp(*currentResource.Limits.Cpu()); result != 0 {
120120
return true
121121
}
122-
return false
122+
return monitorChanged(cluster, sts)
123+
}
124+
125+
func monitorChanged(cluster *redisv1alpha1.DistributedRedisCluster, sts *appsv1.StatefulSet) bool {
126+
if cluster.Spec.Monitor != nil {
127+
for _, container := range sts.Spec.Template.Spec.Containers {
128+
if container.Name == statefulsets.ExporterContainerName {
129+
return false
130+
}
131+
}
132+
return true
133+
} else {
134+
for _, container := range sts.Spec.Template.Spec.Containers {
135+
if container.Name == statefulsets.ExporterContainerName {
136+
return true
137+
}
138+
}
139+
return false
140+
}
123141
}
124142

125143
func (r *realEnsureResource) ensureRedisPDB(cluster *redisv1alpha1.DistributedRedisCluster, name string, labels map[string]string) error {

pkg/controller/redisclusterbackup/sync_handler.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,14 @@ func (r *ReconcileRedisClusterBackup) getBackupJob(reqLogger logr.Logger, backup
267267
},
268268
}
269269
if backup.Spec.PodSpec != nil {
270-
podSpec := job.Spec.Template.Spec
271-
podSpec.NodeSelector = backup.Spec.PodSpec.NodeSelector
272-
podSpec.Affinity = backup.Spec.PodSpec.Affinity
273-
podSpec.SchedulerName = backup.Spec.PodSpec.SchedulerName
274-
podSpec.Tolerations = backup.Spec.PodSpec.Tolerations
275-
podSpec.PriorityClassName = backup.Spec.PodSpec.PriorityClassName
276-
podSpec.Priority = backup.Spec.PodSpec.Priority
277-
podSpec.SecurityContext = backup.Spec.PodSpec.SecurityContext
278-
podSpec.ImagePullSecrets = backup.Spec.PodSpec.ImagePullSecrets
270+
job.Spec.Template.Spec.NodeSelector = backup.Spec.PodSpec.NodeSelector
271+
job.Spec.Template.Spec.Affinity = backup.Spec.PodSpec.Affinity
272+
job.Spec.Template.Spec.SchedulerName = backup.Spec.PodSpec.SchedulerName
273+
job.Spec.Template.Spec.Tolerations = backup.Spec.PodSpec.Tolerations
274+
job.Spec.Template.Spec.PriorityClassName = backup.Spec.PodSpec.PriorityClassName
275+
job.Spec.Template.Spec.Priority = backup.Spec.PodSpec.Priority
276+
job.Spec.Template.Spec.SecurityContext = backup.Spec.PodSpec.SecurityContext
277+
job.Spec.Template.Spec.ImagePullSecrets = backup.Spec.PodSpec.ImagePullSecrets
279278
}
280279
if backup.Spec.Backend.Local != nil {
281280
job.Spec.Template.Spec.Volumes = append(job.Spec.Template.Spec.Volumes, corev1.Volume{

pkg/redisutil/admin.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"time"
99

1010
"github.com/go-logr/logr"
11+
12+
"github.com/ucloud/redis-cluster-operator/pkg/utils"
1113
)
1214

1315
const (
@@ -333,6 +335,24 @@ func (a *Admin) GetAllConfig(c IClient, addr string) (map[string]string, error)
333335
return raw, nil
334336
}
335337

338+
var parseConfigMap = map[string]int8{
339+
"maxmemory": 0,
340+
"proto-max-bulk-len": 0,
341+
"client-query-buffer-limit": 0,
342+
"repl-backlog-size": 0,
343+
"auto-aof-rewrite-min-size": 0,
344+
"active-defrag-ignore-bytes": 0,
345+
"hash-max-ziplist-entries": 0,
346+
"hash-max-ziplist-value": 0,
347+
"stream-node-max-bytes": 0,
348+
"set-max-intset-entries": 0,
349+
"zset-max-ziplist-entries": 0,
350+
"zset-max-ziplist-value": 0,
351+
"hll-sparse-max-bytes": 0,
352+
// TODO parse client-output-buffer-limit
353+
//"client-output-buffer-limit": 0,
354+
}
355+
336356
// SetConfigIfNeed set redis config
337357
func (a *Admin) SetConfigIfNeed(newConfig map[string]string) error {
338358
for addr, c := range a.Connections().GetAll() {
@@ -342,6 +362,14 @@ func (a *Admin) SetConfigIfNeed(newConfig map[string]string) error {
342362
}
343363

344364
for key, value := range newConfig {
365+
var err error
366+
if _, ok := parseConfigMap[key]; ok {
367+
value, err = utils.ParseRedisMemConf(value)
368+
if err != nil {
369+
a.log.Error(err, "redis config format err", "key", key, "value", value)
370+
continue
371+
}
372+
}
345373
if value != oldConfig[key] {
346374
a.log.V(3).Info("CONFIG SET", key, value)
347375
resp := c.Cmd("CONFIG", "SET", key, value)

pkg/resources/statefulsets/statefulset.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
redisRestoreLocalVolumeName = "redis-local"
2525
redisServerName = "redis"
2626
hostnameTopologyKey = "kubernetes.io/hostname"
27+
ExporterContainerName = "exporter"
2728

2829
graceTime = 30
2930

@@ -61,7 +62,7 @@ func NewStatefulSetForCR(cluster *redisv1alpha1.DistributedRedisCluster, ssName,
6162
},
6263
Spec: corev1.PodSpec{
6364
ImagePullSecrets: cluster.Spec.ImagePullSecrets,
64-
Affinity: getAffinity(spec.Affinity, labels),
65+
Affinity: getAffinity(cluster, labels),
6566
Tolerations: spec.ToleRations,
6667
SecurityContext: spec.SecurityContext,
6768
NodeSelector: cluster.Spec.NodeSelector,
@@ -96,11 +97,37 @@ func NewStatefulSetForCR(cluster *redisv1alpha1.DistributedRedisCluster, ssName,
9697
return ss, nil
9798
}
9899

99-
func getAffinity(affinity *corev1.Affinity, labels map[string]string) *corev1.Affinity {
100+
func getAffinity(cluster *redisv1alpha1.DistributedRedisCluster, labels map[string]string) *corev1.Affinity {
101+
affinity := cluster.Spec.Affinity
100102
if affinity != nil {
101103
return affinity
102104
}
103105

106+
if cluster.Spec.RequiredAntiAffinity {
107+
return &corev1.Affinity{
108+
PodAntiAffinity: &corev1.PodAntiAffinity{
109+
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
110+
{
111+
Weight: 100,
112+
PodAffinityTerm: corev1.PodAffinityTerm{
113+
TopologyKey: hostnameTopologyKey,
114+
LabelSelector: &metav1.LabelSelector{
115+
MatchLabels: map[string]string{redisv1alpha1.LabelClusterName: cluster.Name},
116+
},
117+
},
118+
},
119+
},
120+
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
121+
{
122+
TopologyKey: hostnameTopologyKey,
123+
LabelSelector: &metav1.LabelSelector{
124+
MatchLabels: labels,
125+
},
126+
},
127+
},
128+
},
129+
}
130+
}
104131
// return a SOFT anti-affinity by default
105132
return &corev1.Affinity{
106133
PodAntiAffinity: &corev1.PodAntiAffinity{
@@ -110,7 +137,7 @@ func getAffinity(affinity *corev1.Affinity, labels map[string]string) *corev1.Af
110137
PodAffinityTerm: corev1.PodAffinityTerm{
111138
TopologyKey: hostnameTopologyKey,
112139
LabelSelector: &metav1.LabelSelector{
113-
MatchLabels: labels,
140+
MatchLabels: map[string]string{redisv1alpha1.LabelClusterName: cluster.Name},
114141
},
115142
},
116143
},
@@ -282,7 +309,7 @@ func redisServerContainer(cluster *redisv1alpha1.DistributedRedisCluster, passwo
282309

283310
func redisExporterContainer(cluster *redisv1alpha1.DistributedRedisCluster, password *corev1.EnvVar) corev1.Container {
284311
container := corev1.Container{
285-
Name: "exporter",
312+
Name: ExporterContainerName,
286313
Args: append([]string{
287314
fmt.Sprintf("--web.listen-address=:%v", cluster.Spec.Monitor.Prometheus.Port),
288315
fmt.Sprintf("--web.telemetry-path=%v", redisv1alpha1.PrometheusExporterTelemetryPath),

pkg/utils/parse.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package utils
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
)
7+
8+
func ParseRedisMemConf(p string) (string, error) {
9+
var mul int64 = 1
10+
u := strings.ToLower(p)
11+
digits := u
12+
13+
if strings.HasSuffix(u, "k") {
14+
digits = u[:len(u)-len("k")]
15+
mul = 1000
16+
} else if strings.HasSuffix(u, "kb") {
17+
digits = u[:len(u)-len("kb")]
18+
mul = 1024
19+
} else if strings.HasSuffix(u, "m") {
20+
digits = u[:len(u)-len("m")]
21+
mul = 1000 * 1000
22+
} else if strings.HasSuffix(u, "mb") {
23+
digits = u[:len(u)-len("mb")]
24+
mul = 1024 * 1024
25+
} else if strings.HasSuffix(u, "g") {
26+
digits = u[:len(u)-len("g")]
27+
mul = 1000 * 1000 * 1000
28+
} else if strings.HasSuffix(u, "gb") {
29+
digits = u[:len(u)-len("gb")]
30+
mul = 1024 * 1024 * 1024
31+
} else if strings.HasSuffix(u, "b") {
32+
digits = u[:len(u)-len("b")]
33+
mul = 1
34+
}
35+
36+
val, err := strconv.ParseInt(digits, 10, 64)
37+
if err != nil {
38+
return "", err
39+
}
40+
41+
return strconv.FormatInt(val*mul, 10), nil
42+
}

0 commit comments

Comments
 (0)