Skip to content

Commit

Permalink
Add new field (imageNamespace) and tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm committed Sep 23, 2024
1 parent eadf7de commit 5c7c2b6
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 14 deletions.
2 changes: 2 additions & 0 deletions apis/config/v1beta1/imageconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type ImagePolicy struct {
ImagePullSecret corev1.LocalObjectReference `json:"imagePullSecret,omitempty"`

ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`

ImageNamespace string `json:"imageNamespace,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
1 change: 1 addition & 0 deletions config/manager/image_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ images:
# dse:
# "6.8.999": "datastax/dse-server-prototype:latest"
# imageRegistry: "localhost:5000"
# imageNamespace: "internal"
# imagePullPolicy: Always
# imagePullSecret:
# name: my-secret-pull-registry
Expand Down
34 changes: 26 additions & 8 deletions pkg/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ func stripRegistry(image string) string {
}
}

func applyNamespaceOverride(image string) string {
namespace := GetImageConfig().ImageNamespace

if namespace == "" {
return image
}

// It can be first or second..
imageNoRegistry := stripRegistry(image)
comps := strings.Split(imageNoRegistry, "/")
if len(comps) > 1 {
noNamespace := strings.Join(comps[1:], "/")
return fmt.Sprintf("%s/%s", namespace, noNamespace)
} else {
return image // We can't process this correctly, we only have 1 component
}
}

func applyDefaultRegistryOverride(customRegistry, image string) string {
customRegistry = strings.TrimSuffix(customRegistry, "/")

Expand Down Expand Up @@ -117,7 +135,7 @@ func getRegistryOverride(imageType string) string {
return defaultRegistry
}

func applyRegistry(imageType, image string) string {
func applyOverrides(imageType, image string) string {
registry := getRegistryOverride(imageType)

return applyDefaultRegistryOverride(registry, image)
Expand Down Expand Up @@ -179,7 +197,7 @@ func getImageComponents(serverType string) (string, string) {

func GetCassandraImage(serverType, version string) (string, error) {
if found, image := getCassandraContainerImageOverride(serverType, version); found {
return applyRegistry(serverType, image), nil
return applyOverrides(serverType, image), nil
}

switch serverType {
Expand All @@ -201,15 +219,15 @@ func GetCassandraImage(serverType, version string) (string, error) {

prefix, suffix := getImageComponents(serverType)

return applyRegistry(serverType, fmt.Sprintf("%s:%s%s", prefix, version, suffix)), nil
return applyOverrides(serverType, fmt.Sprintf("%s:%s%s", prefix, version, suffix)), nil
}

func GetConfiguredImage(imageType, image string) string {
return applyRegistry(imageType, image)
return applyOverrides(imageType, image)
}

func GetImage(imageType string) string {
return applyRegistry(imageType, GetImageConfig().Images.Others[imageType])
return applyOverrides(imageType, GetImageConfig().Images.Others[imageType])
}

func GetImagePullPolicy(imageType string) corev1.PullPolicy {
Expand All @@ -233,15 +251,15 @@ func GetImagePullPolicy(imageType string) corev1.PullPolicy {
}

func GetConfigBuilderImage() string {
return applyRegistry(configv1beta1.ConfigBuilderImageComponent, GetImageConfig().Images.ConfigBuilder)
return applyOverrides(configv1beta1.ConfigBuilderImageComponent, GetImageConfig().Images.ConfigBuilder)
}

func GetClientImage() string {
return applyRegistry(configv1beta1.ClientImageComponent, GetImageConfig().Images.Client)
return applyOverrides(configv1beta1.ClientImageComponent, GetImageConfig().Images.Client)
}

func GetSystemLoggerImage() string {
return applyRegistry(configv1beta1.SystemLoggerImageComponent, GetImageConfig().Images.SystemLogger)
return applyOverrides(configv1beta1.SystemLoggerImageComponent, GetImageConfig().Images.SystemLogger)
}

func AddDefaultRegistryImagePullSecrets(podSpec *corev1.PodSpec, imageTypes ...string) {
Expand Down
54 changes: 50 additions & 4 deletions pkg/images/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ func TestImageConfigParsing(t *testing.T) {
assert.True(strings.HasPrefix(GetImageConfig().Images.ConfigBuilder, "datastax/cass-config-builder:"))
assert.True(strings.Contains(GetImageConfig().Images.Client, "k8ssandra/k8ssandra-client:"))

assert.Equal("k8ssandra/cass-management-api", GetImageConfig().DefaultImages.ImageComponents[configv1beta1.CassandraImageComponent].Repository)
assert.Equal("datastax/dse-mgmtapi-6_8", GetImageConfig().DefaultImages.ImageComponents[configv1beta1.DSEImageComponent].Repository)
assert.Equal("cr.k8ssandra.io/k8ssandra/cass-management-api", GetImageConfig().DefaultImages.ImageComponents[configv1beta1.CassandraImageComponent].Repository)
assert.Equal("cr.dtsx.io/datastax/dse-mgmtapi-6_8", GetImageConfig().DefaultImages.ImageComponents[configv1beta1.DSEImageComponent].Repository)

assert.Equal("localhost:5000", GetImageConfig().ImageRegistry)
assert.Equal(corev1.PullAlways, GetImageConfig().ImagePullPolicy)
Expand Down Expand Up @@ -144,9 +144,9 @@ func TestExtendedImageConfigParsing(t *testing.T) {
assert.NotNil(GetImageConfig().DefaultImages)

medusaImage := GetImage("medusa")
assert.Equal("localhost:5005/k8ssandra/medusa:latest", medusaImage)
assert.Equal("localhost:5005/enterprise/medusa:latest", medusaImage)
reaperImage := GetImage("reaper")
assert.Equal("localhost:5000/k8ssandra/reaper:latest", reaperImage)
assert.Equal("localhost:5000/enterprise/reaper:latest", reaperImage)

assert.Equal(corev1.PullAlways, GetImagePullPolicy(configv1beta1.SystemLoggerImageComponent))
assert.Equal(corev1.PullIfNotPresent, GetImagePullPolicy(configv1beta1.CassandraImageComponent))
Expand Down Expand Up @@ -188,6 +188,52 @@ func TestPullPolicyOverride(t *testing.T) {
assert.Equal("my-secret-pull-registry", podSpec.ImagePullSecrets[0].Name)
}

func TestRepositoryAndNamespaceOverride(t *testing.T) {
assert := assert.New(t)
imageConfig = configv1beta1.ImageConfig{}
imageConfig.Images = &configv1beta1.Images{}
imageConfig.DefaultImages = &configv1beta1.DefaultImages{}

path, err := GetCassandraImage("dse", "6.8.44")
assert.NoError(err)
assert.Equal("datastax/dse-mgmtapi-6_8:6.8.44", path)

imageConfig.ImageRegistry = "ghcr.io"
path, err = GetCassandraImage("dse", "6.8.44")
assert.NoError(err)
assert.Equal("ghcr.io/datastax/dse-mgmtapi-6_8:6.8.44", path)

imageConfig.ImageNamespace = "enterprise"
path, err = GetCassandraImage("dse", "6.8.44")
assert.NoError(err)
assert.Equal("ghcr.io/enterprise/dse-mgmtapi-6_8:6.8.44", path)

imageConfig = configv1beta1.ImageConfig{}
imageConfig.Images = &configv1beta1.Images{}
imageConfig.DefaultImages = &configv1beta1.DefaultImages{}
imageConfig.ImageNamespace = "enterprise"
path, err = GetCassandraImage("dse", "6.8.44")
assert.NoError(err)
assert.Equal("enterprise/dse-mgmtapi-6_8:6.8.44", path)

imageConfig = configv1beta1.ImageConfig{}
imageConfig.Images = &configv1beta1.Images{}
imageConfig.DefaultImages = &configv1beta1.DefaultImages{
ImageComponents: map[string]configv1beta1.ImageComponent{
configv1beta1.DSEImageComponent: {
Repository: "cr.dtsx.io/datastax/dse-mgmtapi-6_8",
},
},
}
path, err = GetCassandraImage("dse", "6.8.44")
assert.NoError(err)
assert.Equal("cr.dtsx.io/datastax/dse-mgmtapi-6_8:6.8.44", path)
imageConfig.ImageNamespace = "internal"
path, err = GetCassandraImage("dse", "6.8.44")
assert.NoError(err)
assert.Equal("cr.dtsx.io/internal/dse-mgmtapi-6_8:6.8.44", path)
}

func TestImageConfigByteParsing(t *testing.T) {
require := require.New(t)
imageConfig := configv1beta1.ImageConfig{
Expand Down
4 changes: 2 additions & 2 deletions tests/testdata/image_config_parsing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ imagePullSecret:
defaults:
# Note, postfix is ignored if repository is not set
cassandra:
repository: "k8ssandra/cass-management-api"
repository: "cr.k8ssandra.io/k8ssandra/cass-management-api"
suffix: "-ubi8"
dse:
repository: "datastax/dse-mgmtapi-6_8"
repository: "cr.dtsx.io/datastax/dse-mgmtapi-6_8"
suffix: "-ubi8"
1 change: 1 addition & 0 deletions tests/testdata/image_config_parsing_more_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ imageRegistry: "localhost:5000"
imagePullPolicy: Always
imagePullSecret:
name: my-secret-pull-registry
imageNamespace: "enterprise"
defaults:
# Note, suffix is ignored if repository is not set
cassandra:
Expand Down

0 comments on commit 5c7c2b6

Please sign in to comment.