/
redis-standalone.go
191 lines (173 loc) · 7.04 KB
/
redis-standalone.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package k8sutils
import redisv1beta2 "github.com/cafe24-dhkim05/redis-operator/api/v1beta2"
var (
enableMetrics bool
)
// CreateStandaloneService method will create standalone service for Redis
func CreateStandaloneService(cr *redisv1beta2.Redis) error {
logger := serviceLogger(cr.Namespace, cr.ObjectMeta.Name)
labels := getRedisLabels(cr.ObjectMeta.Name, "standalone", "standalone", cr.ObjectMeta.Labels)
annotations := generateServiceAnots(cr.ObjectMeta, nil)
if cr.Spec.RedisExporter != nil && cr.Spec.RedisExporter.Enabled {
enableMetrics = true
} else {
enableMetrics = false
}
additionalServiceAnnotations := map[string]string{}
if cr.Spec.KubernetesConfig.Service != nil {
additionalServiceAnnotations = cr.Spec.KubernetesConfig.Service.ServiceAnnotations
}
objectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name, cr.Namespace, labels, annotations)
headlessObjectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name+"-headless", cr.Namespace, labels, annotations)
additionalObjectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name+"-additional", cr.Namespace, labels, generateServiceAnots(cr.ObjectMeta, additionalServiceAnnotations))
err := CreateOrUpdateService(cr.Namespace, headlessObjectMetaInfo, redisAsOwner(cr), false, true, "ClusterIP")
if err != nil {
logger.Error(err, "Cannot create standalone headless service for Redis")
return err
}
err = CreateOrUpdateService(cr.Namespace, objectMetaInfo, redisAsOwner(cr), enableMetrics, false, "ClusterIP")
if err != nil {
logger.Error(err, "Cannot create standalone service for Redis")
return err
}
additionalServiceType := "ClusterIP"
if cr.Spec.KubernetesConfig.Service != nil {
additionalServiceType = cr.Spec.KubernetesConfig.Service.ServiceType
}
err = CreateOrUpdateService(cr.Namespace, additionalObjectMetaInfo, redisAsOwner(cr), false, false, additionalServiceType)
if err != nil {
logger.Error(err, "Cannot create additional service for Redis")
return err
}
return nil
}
// CreateStandaloneRedis will create a standalone redis setup
func CreateStandaloneRedis(cr *redisv1beta2.Redis) error {
logger := statefulSetLogger(cr.Namespace, cr.ObjectMeta.Name)
labels := getRedisLabels(cr.ObjectMeta.Name, "standalone", "standalone", cr.ObjectMeta.Labels)
annotations := generateStatefulSetsAnots(cr.ObjectMeta)
objectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name, cr.Namespace, labels, annotations)
err := CreateOrUpdateStateFul(cr.Namespace,
objectMetaInfo,
generateRedisStandaloneParams(cr),
redisAsOwner(cr),
generateRedisStandaloneInitContainerParams(cr),
generateRedisStandaloneContainerParams(cr),
cr.Spec.Sidecars,
)
if err != nil {
logger.Error(err, "Cannot create standalone statefulset for Redis")
return err
}
return nil
}
// generateRedisStandalone generates Redis standalone information
func generateRedisStandaloneParams(cr *redisv1beta2.Redis) statefulSetParameters {
replicas := int32(1)
res := statefulSetParameters{
Replicas: &replicas,
ClusterMode: false,
NodeConfVolume: false,
NodeSelector: cr.Spec.NodeSelector,
PodSecurityContext: cr.Spec.PodSecurityContext,
PriorityClassName: cr.Spec.PriorityClassName,
Affinity: cr.Spec.Affinity,
TerminationGracePeriodSeconds: cr.Spec.TerminationGracePeriodSeconds,
Tolerations: cr.Spec.Tolerations,
UpdateStrategy: cr.Spec.KubernetesConfig.UpdateStrategy,
}
if cr.Spec.KubernetesConfig.ImagePullSecrets != nil {
res.ImagePullSecrets = cr.Spec.KubernetesConfig.ImagePullSecrets
}
if cr.Spec.Storage != nil {
res.PersistentVolumeClaim = cr.Spec.Storage.VolumeClaimTemplate
}
if cr.Spec.RedisConfig != nil {
res.ExternalConfig = cr.Spec.RedisConfig.AdditionalRedisConfig
}
if cr.Spec.RedisExporter != nil {
res.EnableMetrics = cr.Spec.RedisExporter.Enabled
}
if cr.Spec.ServiceAccountName != nil {
res.ServiceAccountName = cr.Spec.ServiceAccountName
}
if _, found := cr.ObjectMeta.GetAnnotations()[AnnotationKeyRecreateStatefulset]; found {
res.RecreateStatefulSet = true
}
return res
}
// generateRedisStandaloneContainerParams generates Redis container information
func generateRedisStandaloneContainerParams(cr *redisv1beta2.Redis) containerParameters {
trueProperty := true
falseProperty := false
containerProp := containerParameters{
Role: "standalone",
Image: cr.Spec.KubernetesConfig.Image,
ImagePullPolicy: cr.Spec.KubernetesConfig.ImagePullPolicy,
Resources: cr.Spec.KubernetesConfig.Resources,
SecurityContext: cr.Spec.SecurityContext,
}
if cr.Spec.Storage != nil {
containerProp.AdditionalVolume = cr.Spec.Storage.VolumeMount.Volume
containerProp.AdditionalMountPath = cr.Spec.Storage.VolumeMount.MountPath
}
if cr.Spec.KubernetesConfig.ExistingPasswordSecret != nil {
containerProp.EnabledPassword = &trueProperty
containerProp.SecretName = cr.Spec.KubernetesConfig.ExistingPasswordSecret.Name
containerProp.SecretKey = cr.Spec.KubernetesConfig.ExistingPasswordSecret.Key
} else {
containerProp.EnabledPassword = &falseProperty
}
if cr.Spec.RedisExporter != nil {
containerProp.RedisExporterImage = cr.Spec.RedisExporter.Image
containerProp.RedisExporterImagePullPolicy = cr.Spec.RedisExporter.ImagePullPolicy
if cr.Spec.RedisExporter.Resources != nil {
containerProp.RedisExporterResources = cr.Spec.RedisExporter.Resources
}
if cr.Spec.RedisExporter.EnvVars != nil {
containerProp.RedisExporterEnv = cr.Spec.RedisExporter.EnvVars
}
}
if cr.Spec.ReadinessProbe != nil {
containerProp.ReadinessProbe = &cr.Spec.ReadinessProbe.Probe
}
if cr.Spec.LivenessProbe != nil {
containerProp.LivenessProbe = &cr.Spec.LivenessProbe.Probe
}
if cr.Spec.Storage != nil {
containerProp.PersistenceEnabled = &trueProperty
}
if cr.Spec.TLS != nil {
containerProp.TLSConfig = cr.Spec.TLS
}
if cr.Spec.ACL != nil {
containerProp.ACLConfig = cr.Spec.ACL
}
return containerProp
}
// generateRedisStandaloneInitContainerParams generates Redis initcontainer information
func generateRedisStandaloneInitContainerParams(cr *redisv1beta2.Redis) initContainerParameters {
trueProperty := true
initcontainerProp := initContainerParameters{}
if cr.Spec.InitContainer != nil {
initContainer := cr.Spec.InitContainer
initcontainerProp = initContainerParameters{
Enabled: initContainer.Enabled,
Role: "standalone",
Image: initContainer.Image,
ImagePullPolicy: initContainer.ImagePullPolicy,
Resources: initContainer.Resources,
AdditionalEnvVariable: initContainer.EnvVars,
Command: initContainer.Command,
Arguments: initContainer.Args,
}
if cr.Spec.Storage != nil {
initcontainerProp.AdditionalVolume = cr.Spec.Storage.VolumeMount.Volume
initcontainerProp.AdditionalMountPath = cr.Spec.Storage.VolumeMount.MountPath
}
if cr.Spec.Storage != nil {
initcontainerProp.PersistenceEnabled = &trueProperty
}
}
return initcontainerProp
}