-
Notifications
You must be signed in to change notification settings - Fork 77
/
images.go
286 lines (234 loc) · 7.08 KB
/
images.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright DataStax, Inc.
// Please see the included license file for details.
package images
import (
"fmt"
"os"
"regexp"
"strings"
corev1 "k8s.io/api/core/v1"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)
var ValidDsePrefixes = []string{"6.8"}
var ValidOssPrefixes = []string{"3.11", "4.0"}
const (
envDefaultRegistryOverride = "DEFAULT_CONTAINER_REGISTRY_OVERRIDE"
envDefaultRegistryOverridePullSecrets = "DEFAULT_CONTAINER_REGISTRY_OVERRIDE_PULL_SECRETS"
EnvBaseImageOS = "BASE_IMAGE_OS"
ValidDseVersionRegexp = "6\\.8\\.\\d+"
ValidOssVersionRegexp = "(3\\.11\\.\\d+)|(4\\.0\\.\\d+)"
UbiImageSuffix = "-ubi7"
)
// How to add new images:
//
// 1. Add a new Image enum value below
// 2. Update imageLookupMap with image url
// 3. If the image is a Cassandra/DSE image, also update maps:
// - versionToOSSCassandra
// - versionToDSE
// 4. Additionally, if there a is a UBI Cassandra/DSE image
// available, also update:
// - versionToUBIOSSCassandra
// - versionToUBIDSE
//
type Image int
// IMPORTANT: Only Image enum values (and ImageEnumLength) should go in this
// const expression
const (
Cassandra_3_11_6 Image = iota
Cassandra_3_11_7
Cassandra_4_0_0
UBICassandra_3_11_6
UBICassandra_3_11_7
UBICassandra_4_0_0
DSE_6_8_0
DSE_6_8_1
DSE_6_8_2
DSE_6_8_3
DSE_6_8_4
UBIDSE_6_8_0
UBIDSE_6_8_1
UBIDSE_6_8_2
UBIDSE_6_8_3
UBIDSE_6_8_4
ConfigBuilder
UBIConfigBuilder
BusyBox
Reaper
BaseImageOS
// NOTE: This line MUST be last in the const expression
ImageEnumLength int = iota
)
var imageLookupMap map[Image]string = map[Image]string{
Cassandra_3_11_6: "datastax/cassandra-mgmtapi-3_11_6:v0.1.5",
Cassandra_3_11_7: "datastax/cassandra-mgmtapi-3_11_7:v0.1.13",
Cassandra_4_0_0: "datastax/cassandra-mgmtapi-4_0_0:v0.1.12",
UBICassandra_3_11_6: "datastax/cassandra:3.11.6-ubi7",
UBICassandra_3_11_7: "datastax/cassandra:3.11.7-ubi7",
UBICassandra_4_0_0: "datastax/cassandra:4.0-ubi7",
DSE_6_8_0: "datastax/dse-server:6.8.0",
DSE_6_8_1: "datastax/dse-server:6.8.1",
DSE_6_8_2: "datastax/dse-server:6.8.2",
DSE_6_8_3: "datastax/dse-server:6.8.3",
DSE_6_8_4: "datastax/dse-server:6.8.4",
UBIDSE_6_8_0: "datastax/dse-server:6.8.0-ubi7",
UBIDSE_6_8_1: "datastax/dse-server:6.8.1-ubi7",
UBIDSE_6_8_2: "datastax/dse-server:6.8.2-ubi7",
UBIDSE_6_8_3: "datastax/dse-server:6.8.3-ubi7",
UBIDSE_6_8_4: "datastax/dse-server:6.8.4-ubi7",
ConfigBuilder: "datastax/cass-config-builder:1.0.3",
UBIConfigBuilder: "datastax/cass-config-builder:1.0.3-ubi7",
BusyBox: "busybox:1.32.0-uclibc",
Reaper: "thelastpickle/cassandra-reaper:2.0.5",
}
var versionToOSSCassandra map[string]Image = map[string]Image{
"3.11.6": Cassandra_3_11_6,
"3.11.7": Cassandra_3_11_7,
"4.0.0": Cassandra_4_0_0,
}
var versionToUBIOSSCassandra map[string]Image = map[string]Image{
"3.11.6": UBICassandra_3_11_6,
"3.11.7": UBICassandra_3_11_7,
"4.0.0": UBICassandra_4_0_0,
}
var versionToDSE map[string]Image = map[string]Image{
"6.8.0": DSE_6_8_0,
"6.8.1": DSE_6_8_1,
"6.8.2": DSE_6_8_2,
"6.8.3": DSE_6_8_3,
"6.8.4": DSE_6_8_4,
}
var versionToUBIDSE map[string]Image = map[string]Image{
"6.8.0": UBIDSE_6_8_0,
"6.8.1": UBIDSE_6_8_1,
"6.8.2": UBIDSE_6_8_2,
"6.8.3": UBIDSE_6_8_3,
"6.8.4": UBIDSE_6_8_4,
}
var log = logf.Log.WithName("images")
func IsDseVersionSupported(version string) bool {
validVersions := regexp.MustCompile(ValidDseVersionRegexp)
return validVersions.MatchString(version)
}
func IsOssVersionSupported(version string) bool {
validVersions := regexp.MustCompile(ValidOssVersionRegexp)
return validVersions.MatchString(version)
}
func stripRegistry(image string) string {
comps := strings.Split(image, "/")
if len(comps) > 1 && strings.Contains(comps[0], ".") || strings.Contains(comps[0], ":") {
return strings.Join(comps[1:], "/")
} else {
return image
}
}
func applyDefaultRegistryOverride(image string) string {
customRegistry := os.Getenv(envDefaultRegistryOverride)
customRegistry = strings.TrimSuffix(customRegistry, "/")
if customRegistry == "" {
return image
} else {
imageNoRegistry := stripRegistry(image)
return fmt.Sprintf("%s/%s", customRegistry, imageNoRegistry)
}
}
// Calculate if this Docker Image run as the cassandra user?
// This is meant to be used when the CassandraDatacenter does not
// explicitly set the DockerImageRunsAsCassandra field.
func CalculateDockerImageRunsAsCassandra(version string) bool {
if version == "3.11.6" || version == "3.11.7" || version == "4.0.0" {
return false
}
// Otherwise, we assume the image is running as the "cassandra" user
return true
}
func GetImage(name Image) string {
image, ok := imageLookupMap[name]
if !ok {
if name == BaseImageOS {
image = os.Getenv(EnvBaseImageOS)
} else {
// This should never happen as we have a unit test
// to ensure imageLookupMap is fully populated.
log.Error(nil, "Could not find image", "image", int(name))
return ""
}
}
if image != "" {
return applyDefaultRegistryOverride(image)
} else {
return ""
}
}
func (image Image) String() string {
return GetImage(image)
}
func shouldUseUBI() bool {
baseImageOs := os.Getenv(EnvBaseImageOS)
return baseImageOs != ""
}
func GetCassandraImage(serverType, version string) (string, error) {
var imageKey Image
var found bool
dseMap := versionToDSE
cassandraMap := versionToOSSCassandra
if shouldUseUBI() {
dseMap = versionToUBIDSE
cassandraMap = versionToUBIOSSCassandra
}
switch serverType {
case "dse":
imageKey, found = dseMap[version]
case "cassandra":
imageKey, found = cassandraMap[version]
default:
return "", fmt.Errorf("Unknown server type '%s'", serverType)
}
if !found {
// For fallback images, just return the image name directly
fallbackImageName := ""
if serverType == "dse" {
if !IsDseVersionSupported(version) {
return "", fmt.Errorf("server 'dse' and version '%s' do not work together", version)
}
fallbackImageName = fmt.Sprintf("datastax/dse-server:%s", version)
} else {
if !IsOssVersionSupported(version) {
return "", fmt.Errorf("server 'cassandra' and version '%s' do not work together", version)
}
// We will fall back to the "mutable" cassandra image without the explicit mgmt-api version
fallbackImageName = fmt.Sprintf("datastax/cassandra-mgmtapi:%s", version)
}
if shouldUseUBI() {
return fmt.Sprintf("%s%s", fallbackImageName, UbiImageSuffix), nil
}
return fallbackImageName, nil
}
return GetImage(imageKey), nil
}
func GetConfigBuilderImage() string {
if shouldUseUBI() {
return GetImage(UBIConfigBuilder)
} else {
return GetImage(ConfigBuilder)
}
}
func GetReaperImage() string {
return GetImage(Reaper)
}
func GetSystemLoggerImage() string {
if shouldUseUBI() {
return GetImage(BaseImageOS)
}
return GetImage(BusyBox)
}
func AddDefaultRegistryImagePullSecrets(podSpec *corev1.PodSpec) bool {
secretName := os.Getenv(envDefaultRegistryOverridePullSecrets)
if secretName != "" {
podSpec.ImagePullSecrets = append(
podSpec.ImagePullSecrets,
corev1.LocalObjectReference{Name: secretName})
return true
}
return false
}