diff --git a/pkg/controller/cassandracluster/generator.go b/pkg/controller/cassandracluster/generator.go index 67d3c7e5c..218cbf801 100644 --- a/pkg/controller/cassandracluster/generator.go +++ b/pkg/controller/cassandracluster/generator.go @@ -631,8 +631,8 @@ func initContainerEnvVar(cc *api.CassandraCluster, status *api.CassandraClusterS image := strings.Split(cc.Spec.CassandraImage, ":") serverVersion := cc.Spec.ServerVersion if serverVersion == "" { - if len(image) == 2 { - version := strings.Split(image[1], "-") + if len(image) >= 2 { + version := strings.Split(image[len(image)-1], "-") serverVersion = version[0] if len(version) != 1 { serverVersion += ".0" diff --git a/pkg/controller/cassandracluster/generator_test.go b/pkg/controller/cassandracluster/generator_test.go index 6a6500bf9..ee18b755b 100644 --- a/pkg/controller/cassandracluster/generator_test.go +++ b/pkg/controller/cassandracluster/generator_test.go @@ -65,6 +65,15 @@ func helperInitCluster(t *testing.T, name string) (*ReconcileCassandraCluster, * return &rcc, &cc } +func GetEnvVarByName(envVars []v1.EnvVar, name string) *v1.EnvVar { + for _, envVar := range envVars { + if envVar.Name == name { + return &envVar + } + } + return nil +} + func TestCreateNodeAffinity(t *testing.T) { assert := assert.New(t) @@ -330,6 +339,28 @@ func TestInitContainerConfigFileData(t *testing.T) { assert.Equal(configFileData.String(), initEnvVar[0].Value) } +func TestInitContainerServerVersionDetect(t *testing.T) { + // Check of Cassandra version detection in case of different image formats + dcName := "dc1" + rackName := "rack1" + dcRackName := fmt.Sprintf("%s-%s", dcName, rackName) + + _, cc := helperInitCluster(t, "cassandracluster-1DC.yaml") + imageTag := "1.0.0" + images := []string{ + fmt.Sprintf("artifactory-name/repo/cassandra:%s", imageTag), + fmt.Sprintf("artifactory-name:44301/repo/cassandra:%s", imageTag), + } + assert := assert.New(t) + for _, image := range images { + cc.Spec.CassandraImage = image + cassieResources := cc.Spec.Resources + initEnvVar := initContainerEnvVar(cc, &cc.Status, cassieResources, dcRackName) + prodVer := GetEnvVarByName(initEnvVar, "PRODUCT_VERSION") + assert.Equal(imageTag, prodVer.Value) + } +} + func TestGenerateCassandraStatefulSet(t *testing.T) { assert := assert.New(t) dcName := "dc1"