Skip to content

Commit

Permalink
RedHat testing image name override
Browse files Browse the repository at this point in the history
The statefulset builder now checks if the environment variable
RELATED_IMAGE_COCKROACH is set.  If RELATED_IMAGE_COCKROACH is
set the container image is set to that value instead of the
value set in the CR. RedHat testing sets this evn variable
to test a specific image from a specific container registry.
  • Loading branch information
chrislovecnm committed Aug 31, 2020
1 parent 849696b commit 88bef80
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
21 changes: 18 additions & 3 deletions pkg/resource/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package resource
import (
"errors"
"fmt"
"os"
"strings"

"github.com/cockroachdb/cockroach-operator/pkg/labels"
Expand All @@ -40,6 +41,7 @@ const (
certsDirName = "certs"

DbContainerName = "db"
RHEnvVar = "RELATED_IMAGE_COCKROACH"
)

type StatefulSetBuilder struct {
Expand Down Expand Up @@ -153,7 +155,7 @@ func (b StatefulSetBuilder) makePodTemplate() corev1.PodTemplateSpec {
},
Spec: corev1.PodSpec{
TerminationGracePeriodSeconds: ptr.Int64(60),
Containers: b.makeContainers(),
Containers: b.MakeContainers(),
AutomountServiceAccountToken: ptr.Bool(false),
},
}
Expand All @@ -170,11 +172,24 @@ func (b StatefulSetBuilder) makePodTemplate() corev1.PodTemplateSpec {
return pod
}

func (b StatefulSetBuilder) makeContainers() []corev1.Container {
// MakeContainers creates a slice of corev1.Containers which includes a single
// corev1.Container that is based on the CR.
func (b StatefulSetBuilder) MakeContainers() []corev1.Container {

//
// This code block allows for RedHat to override the coachroach image name during
// openshift testing. They need to set the image name dynamically using a environment
// variable to allow the testing of a specific image.
//
image := os.Getenv(RHEnvVar)
if image == "" {
image = b.Spec().Image.Name
}

return []corev1.Container{
{
Name: DbContainerName,
Image: b.Spec().Image.Name,
Image: image,
ImagePullPolicy: *b.Spec().Image.PullPolicyName,
Resources: b.Spec().Resources,
Args: b.dbArgs(),
Expand Down
20 changes: 20 additions & 0 deletions pkg/resource/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"

api "github.com/cockroachdb/cockroach-operator/api/v1alpha1"
Expand Down Expand Up @@ -84,6 +85,25 @@ func TestStatefulSetBuilder(t *testing.T) {
}
}

func TestRHImage(t *testing.T) {
rhImage := "redhat-coachroach-test:v22"
os.Setenv(resource.RHEnvVar, rhImage)

cluster := resource.NewCluster(&api.CrdbCluster{})

b := resource.StatefulSetBuilder{
Cluster: &cluster,
}

container := b.MakeContainers()

if container[0].Image != rhImage {
assert.Fail(t, fmt.Sprintf("unexpected result expected image to equal: %s, got: %s", rhImage, container[0].Image))
}

os.Setenv(resource.RHEnvVar, "")
}

func load(t *testing.T, file string) []byte {
content, err := ioutil.ReadFile(file)
if err != nil {
Expand Down

0 comments on commit 88bef80

Please sign in to comment.