Commit e45f4a98 authored by Claudio Gonçalves's avatar Claudio Gonçalves Committed by Claudio Netto

controller: allow custom sidecar container image name from config file

parent 0ae4871d
Pipeline #72529439 failed with stages
in 4 minutes and 32 seconds
......@@ -6,6 +6,7 @@ import (
"net/url"
"sort"
tsuruConfig "github.com/tsuru/config"
"github.com/tsuru/nginx-operator/pkg/apis/nginx/v1alpha1"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
......@@ -30,7 +31,7 @@ const (
healthcheckPath = "/healthcheck"
healthcheckSidecarName = "nginx-healthchecker"
sidecarContainerImage = "tsuru/nginx-operator-sidecar:latest"
defaultSidecarContainerImage = "tsuru/nginx-operator-sidecar:latest"
// Mount path where nginx.conf will be placed
configMountPath = "/etc/nginx"
......@@ -63,6 +64,9 @@ var postStartCommand = []string{
// NewDeployment creates a deployment for a given Nginx resource.
func NewDeployment(n *v1alpha1.Nginx) (*appv1.Deployment, error) {
n.Spec.Image = valueOrDefault(n.Spec.Image, defaultNginxImage)
customSidecarContainerImage, _ := tsuruConfig.GetString("nginx-controller:sidecar:image")
deployment := appv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
......@@ -127,7 +131,7 @@ func NewDeployment(n *v1alpha1.Nginx) (*appv1.Deployment, error) {
},
{
Name: healthcheckSidecarName,
Image: sidecarContainerImage,
Image: valueOrDefault(customSidecarContainerImage, defaultSidecarContainerImage),
},
},
Affinity: n.Spec.PodTemplate.Affinity,
......
......@@ -7,6 +7,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
"github.com/stretchr/testify/assert"
tsuruConfig "github.com/tsuru/config"
"github.com/tsuru/nginx-operator/pkg/apis/nginx/v1alpha1"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
......@@ -117,7 +118,7 @@ func baseDeployment() appv1.Deployment {
},
{
Name: healthcheckSidecarName,
Image: sidecarContainerImage,
Image: defaultSidecarContainerImage,
},
},
},
......@@ -128,9 +129,10 @@ func baseDeployment() appv1.Deployment {
func Test_NewDeployment(t *testing.T) {
tests := []struct {
name string
nginxFn func(n v1alpha1.Nginx) v1alpha1.Nginx
deployFn func(d appv1.Deployment) appv1.Deployment
name string
nginxFn func(n v1alpha1.Nginx) v1alpha1.Nginx
deployFn func(d appv1.Deployment) appv1.Deployment
teardownFn func()
}{
{
name: "empty-spec",
......@@ -535,6 +537,20 @@ func Test_NewDeployment(t *testing.T) {
return d
},
},
{
name: "when NGINX controller is configured to use a custom sidecar container image",
nginxFn: func(n v1alpha1.Nginx) v1alpha1.Nginx {
return n
},
deployFn: func(d appv1.Deployment) appv1.Deployment {
tsuruConfig.Set("nginx-controller:sidecar:image", "private.registry.example.com/tsuru/nginx-operator-sidecar")
d.Spec.Template.Spec.Containers[1].Image = "private.registry.example.com/tsuru/nginx-operator-sidecar"
return d
},
teardownFn: func() {
tsuruConfig.Unset("nginx-controller:sidecar:image")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
......@@ -553,6 +569,9 @@ func Test_NewDeployment(t *testing.T) {
assert.NoError(t, err)
want.Annotations[generatedFromAnnotation] = string(spec)
assertDeployment(t, &want, dep)
if tt.teardownFn != nil {
tt.teardownFn()
}
})
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment