Skip to content

Commit

Permalink
Add support to override the default registry (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
johntrimble committed Sep 16, 2020
1 parent 55656b7 commit f7ef6c8
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 133 deletions.
29 changes: 26 additions & 3 deletions charts/cass-operator-chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ spec:
name: cass-operator
spec:
serviceAccountName: {{ .Values.serviceAccountName }}
{{- if .Values.imagePullSecret }}
{{- $imagePullSecrets := list -}}
{{- if .Values.imagePullSecret }}
{{- $imagePullSecrets = append $imagePullSecrets .Values.imagePullSecret }}
{{- end }}
{{- if .Values.registryUsername }}
{{- $imagePullSecrets = append $imagePullSecrets "cass-operator-registry-override-regcred" }}
{{- end }}
{{- if empty $imagePullSecrets | not }}
imagePullSecrets:
- name: {{ .Values.imagePullSecret }}
{{- end }}
{{- range $imagePullSecrets }}
- name: {{ . | quote }}
{{- end }}
{{- end }}
volumes:
- name: tmpconfig-volume
emptyDir:
Expand All @@ -26,7 +35,13 @@ spec:
secretName: cass-operator-webhook-config
containers:
- name: cass-operator
{{- if .Values.image }}
image: {{ .Values.image }}
{{- else if .Values.registryName }}
image: {{ printf "%s/%s" .Values.registryName .Values.defaultImage }}
{{- else }}
image: {{ .Values.defaultImage }}
{{- end }}
imagePullPolicy: {{ .Values.imagePullPolicy }}
volumeMounts:
- mountPath: /tmp/k8s-webhook-server/serving-certs
Expand Down Expand Up @@ -63,6 +78,14 @@ spec:
- name: ENABLE_VMWARE_PSP
value: "true"
{{- end }}
{{- if .Values.registryName }}
- name: DEFAULT_CONTAINER_REGISTRY_OVERRIDE
value: {{ .Values.registryName }}
{{- end }}
{{- if .Values.registryUsername }}
- name: DEFAULT_CONTAINER_REGISTRY_OVERRIDE_PULL_SECRETS
value: cass-operator-registry-override-regcred
{{- end }}
{{- if .Values.clusterWideInstall }}
- name: WATCH_NAMESPACE
value: ""
Expand Down
9 changes: 9 additions & 0 deletions charts/cass-operator-chart/templates/registry-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{ if .Values.registryUsername }}
apiVersion: v1
kind: Secret
type: kubernetes.io/dockerconfigjson
metadata:
name: cass-operator-registry-override-regcred
data:
.dockerconfigjson: {{ printf "{\"auths\": {\"%s\": {\"auth\": \"%s\"}}}" .Values.registryName (printf "%s:%s" .Values.registryUsername .Values.registryPassword | b64enc) | b64enc }}
{{- end }}
2 changes: 1 addition & 1 deletion charts/cass-operator-chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ webhookClusterRoleName: cass-operator-webhook
webhookClusterRoleBindingName: cass-operator-webhook
deploymentName: cass-operator
deploymentReplicas: 1
image: "datastax/cass-operator:1.4.1"
defaultImage: "datastax/cass-operator:1.4.1"
imagePullPolicy: IfNotPresent
imagePullSecret: ""
8 changes: 6 additions & 2 deletions mage/ginkgo/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,12 @@ func CreateDockerRegistrySecret(name string, namespace string) {
k.InNamespace(namespace).ExecVCapture()
}

func (ns NsWrapper) HelmInstall(chartPath string) {
HelmInstallWithOverrides(chartPath, ns.Namespace, map[string]string{})
func (ns NsWrapper) HelmInstall(chartPath string, overrides ...string) {
overridesMap := map[string]string{}
for i := 0; i < len(overrides) - 1; i = i+2 {
overridesMap[overrides[i]] = overrides[i+1]
}
HelmInstallWithOverrides(chartPath, ns.Namespace, overridesMap)
}

func (ns NsWrapper) HelmInstallWithPSPEnabled(chartPath string) {
Expand Down
5 changes: 3 additions & 2 deletions operator/cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager/signals"

api "github.com/datastax/cass-operator/operator/pkg/apis/cassandra/v1beta1"
"github.com/datastax/cass-operator/operator/pkg/images"
)

// Change below variables to serve metrics on different host or port.
Expand Down Expand Up @@ -224,8 +225,8 @@ func readBaseOsIntoEnv() error {
}

baseOs := strings.TrimSpace(string(rawVal))
os.Setenv(api.EnvBaseImageOs, baseOs)
log.Info(fmt.Sprintf("%s set to '%s'", api.EnvBaseImageOs, baseOs))
os.Setenv(images.EnvBaseImageOS, baseOs)
log.Info(fmt.Sprintf("%s set to '%s'", images.EnvBaseImageOS, baseOs))

return nil
}
Expand Down
107 changes: 4 additions & 103 deletions operator/pkg/apis/cassandra/v1beta1/cassandradatacenter_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package v1beta1
import (
"encoding/json"
"fmt"
"os"

"github.com/Jeffail/gabs"
"github.com/pkg/errors"
Expand All @@ -16,6 +15,7 @@ import (

"github.com/datastax/cass-operator/operator/pkg/serverconfig"
"github.com/datastax/cass-operator/operator/pkg/utils"
"github.com/datastax/cass-operator/operator/pkg/images"
)

const (
Expand Down Expand Up @@ -49,101 +49,6 @@ const (
// This type exists so there's no chance of pushing random strings to our progress status
type ProgressState string

const (
defaultConfigBuilderImage = "datastax/cass-config-builder:1.0.3"
ubi_defaultConfigBuilderImage = "datastax/cass-config-builder:1.0.3-ubi7"

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.12"
cassandra_4_0_0 = "datastax/cassandra-mgmtapi-4_0_0:v0.1.12"

ubi_cassandra_3_11_6 = "datastax/cassandra:3.11.6-ubi7"
ubi_cassandra_3_11_7 = "datastax/cassandra:3.11.7-ubi7"
ubi_cassandra_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"

ubi_dse_6_8_0 = "datastax/dse-server:6.8.0-ubi7"
ubi_dse_6_8_1 = "datastax/dse-server:6.8.1-ubi7"
ubi_dse_6_8_2 = "datastax/dse-server:6.8.2-ubi7"
ubi_dse_6_8_3 = "datastax/dse-server:6.8.3-ubi7"

EnvBaseImageOs = "BASE_IMAGE_OS"
)

// getImageForServerVersion tries to look up a known image for a server type and version number.
// In the event that no image is found, an error is returned
func getImageForServerVersion(server, version string) (string, error) {
baseImageOs := os.Getenv(EnvBaseImageOs)

var imageCalc func(string) (string, bool)
var img string
var success bool
var errMsg string

if baseImageOs == "" {
imageCalc = getImageForDefaultBaseOs
errMsg = fmt.Sprintf("server '%s' and version '%s' do not work together", server, version)
} else {
// if this operator was compiled using a UBI base image
// such as registry.access.redhat.com/ubi7/ubi-minimal:7.8
// then we use specific cassandra and init container coordinates
// that are built accordingly
errMsg = fmt.Sprintf("server '%s' and version '%s', along with the specified base OS '%s', do not work together", server, version, baseImageOs)
imageCalc = getImageForUniversalBaseOs
}

img, success = imageCalc(server + "-" + version)
if !success {
return "", fmt.Errorf(errMsg)
}

return img, nil
}

func getImageForDefaultBaseOs(sv string) (string, bool) {
switch sv {
case "dse-6.8.0":
return dse_6_8_0, true
case "dse-6.8.1":
return dse_6_8_1, true
case "dse-6.8.2":
return dse_6_8_2, true
case "dse-6.8.3":
return dse_6_8_3, true
case "cassandra-3.11.6":
return cassandra_3_11_6, true
case "cassandra-3.11.7":
return cassandra_3_11_7, true
case "cassandra-4.0.0":
return cassandra_4_0_0, true
}
return "", false
}

func getImageForUniversalBaseOs(sv string) (string, bool) {
switch sv {
case "dse-6.8.0":
return ubi_dse_6_8_0, true
case "dse-6.8.1":
return ubi_dse_6_8_1, true
case "dse-6.8.2":
return ubi_dse_6_8_2, true
case "dse-6.8.3":
return ubi_dse_6_8_3, true
case "cassandra-3.11.6":
return ubi_cassandra_3_11_6, true
case "cassandra-3.11.7":
return ubi_cassandra_3_11_7, true
case "cassandra-4.0.0":
return ubi_cassandra_4_0_0, true
}
return "", false
}

type CassandraUser struct {
SecretName string `json:"secretName"`
Superuser bool `json:"superuser"`
Expand Down Expand Up @@ -440,15 +345,11 @@ func init() {
}

func (dc *CassandraDatacenter) GetConfigBuilderImage() string {
var image string
if dc.Spec.ConfigBuilderImage != "" {
image = dc.Spec.ConfigBuilderImage
} else if baseImageOs := os.Getenv(EnvBaseImageOs); baseImageOs != "" {
image = ubi_defaultConfigBuilderImage
return dc.Spec.ConfigBuilderImage
} else {
image = defaultConfigBuilderImage
return images.GetConfigBuilderImage()
}
return image
}

// GetServerImage produces a fully qualified container image to pull
Expand All @@ -468,7 +369,7 @@ func (dc *CassandraDatacenter) GetServerImage() (string, error) {
// In the event that no image is found, an error is returned
func makeImage(serverType, serverVersion, serverImage string) (string, error) {
if serverImage == "" {
return getImageForServerVersion(serverType, serverVersion)
return images.GetCassandraImage(serverType, serverVersion)
}
return serverImage, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestCassandraDatacenter_GetServerImage(t *testing.T) {
},
},
want: "",
errString: "server '' and version '9000' do not work together",
errString: "Unknown server type ''",
},
}
for _, tt := range tests {
Expand Down
Loading

0 comments on commit f7ef6c8

Please sign in to comment.