/
containers.go
107 lines (85 loc) · 1.94 KB
/
containers.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
package spec
import (
"fmt"
"strconv"
"github.com/jw-s/redis-operator/pkg/apis/redis/v1"
apiv1 "k8s.io/api/core/v1"
)
func GetRedisMasterName(redis *v1.Redis) string {
return redis.Name
}
func GetVolumeMounts() []apiv1.VolumeMount {
return []apiv1.VolumeMount{
{
Name: ConfigVolumeName,
MountPath: ConfMountPath,
},
{
Name: DataVolumeName,
MountPath: DataMountPath,
},
}
}
func RedisContainer(baseImage, version string) apiv1.Container {
c := apiv1.Container{
Name: "redis",
Image: baseImage + ":" + version,
Args: []string{
"--bind", "0.0.0.0",
"--slave-read-only", "no",
},
}
return c
}
func RedisSentinel(container apiv1.Container) apiv1.Container {
container.Name = "redis-sentinel"
container.Ports = []apiv1.ContainerPort{
{
Name: "sentinel",
ContainerPort: int32(RedisSentinelPort),
Protocol: apiv1.ProtocolTCP,
},
}
container.VolumeMounts = GetVolumeMounts()
container.Args = append([]string{
fmt.Sprintf("%s/%s", DataMountPath, ConfigMapConfKeyName),
"--sentinel",
},
container.Args...,
)
return container
}
func RedisMaster(redis apiv1.Container) apiv1.Container {
redis.Name = "redis-master"
redis.Ports = []apiv1.ContainerPort{
{
Name: "main",
ContainerPort: int32(RedisPort),
Protocol: apiv1.ProtocolTCP,
},
}
return redis
}
func RedisSlave(redis apiv1.Container, spec *v1.Redis) apiv1.Container {
redis.Name = "redis-slave"
redis.Ports = []apiv1.ContainerPort{
{
Name: "main",
ContainerPort: int32(RedisPort),
Protocol: apiv1.ProtocolTCP,
},
}
redis.SecurityContext = &apiv1.SecurityContext{
RunAsUser: spec.Spec.GetRedisRunAsUser(),
}
redis.VolumeMounts = []apiv1.VolumeMount{
{
Name: SlavePersistentVolumeClaimName,
MountPath: DataMountPath,
},
}
redis.Args = append(redis.Args,
"--slaveof", GetMasterServiceName(spec.Name), strconv.Itoa(RedisPort),
)
return redis
}