From 42dc5569c5435fa5a4ca2b9e04b5d077258a364a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20S=C4=99k?= Date: Thu, 27 Jun 2019 22:49:52 +0200 Subject: [PATCH] #38 Omit KUBERNETES_* envs in containers --- .../jenkins/configuration/base/reconcile.go | 14 +++- .../configuration/base/reconcile_test.go | 81 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/pkg/controller/jenkins/configuration/base/reconcile.go b/pkg/controller/jenkins/configuration/base/reconcile.go index 5be3e62f..6cb35c6f 100644 --- a/pkg/controller/jenkins/configuration/base/reconcile.go +++ b/pkg/controller/jenkins/configuration/base/reconcile.go @@ -510,7 +510,7 @@ func (r *ReconcileJenkinsBaseConfiguration) compareContainers(expected corev1.Co r.logger.Info(fmt.Sprintf("Command has changed to '%+v' in container '%s', recreating pod", expected.Command, expected.Name)) return true } - if !reflect.DeepEqual(expected.Env, actual.Env) { + if !compareEnv(expected.Env, actual.Env) { r.logger.Info(fmt.Sprintf("Env has changed to '%+v' in container '%s', recreating pod", expected.Env, expected.Name)) return true } @@ -562,6 +562,18 @@ func (r *ReconcileJenkinsBaseConfiguration) compareContainers(expected corev1.Co return false } +func compareEnv(expected, actual []corev1.EnvVar) bool { + var actualEnv []corev1.EnvVar + for _, env := range actual { + if env.Name == "KUBERNETES_PORT_443_TCP_ADDR" || env.Name == "KUBERNETES_PORT" || + env.Name == "KUBERNETES_PORT_443_TCP" || env.Name == "KUBERNETES_SERVICE_HOST" { + continue + } + actualEnv = append(actualEnv, env) + } + return reflect.DeepEqual(expected, actualEnv) +} + // CompareContainerVolumeMounts returns true if two containers volume mounts are the same func CompareContainerVolumeMounts(expected corev1.Container, actual corev1.Container) bool { var withoutServiceAccount []corev1.VolumeMount diff --git a/pkg/controller/jenkins/configuration/base/reconcile_test.go b/pkg/controller/jenkins/configuration/base/reconcile_test.go index 6f2c735f..15073db1 100644 --- a/pkg/controller/jenkins/configuration/base/reconcile_test.go +++ b/pkg/controller/jenkins/configuration/base/reconcile_test.go @@ -414,3 +414,84 @@ func TestReconcileJenkinsBaseConfiguration_verifyPlugins(t *testing.T) { assert.False(t, got) }) } + +func Test_compareEnv(t *testing.T) { + t.Run("empty", func(t *testing.T) { + var expected []corev1.EnvVar + var actual []corev1.EnvVar + + got := compareEnv(expected, actual) + + assert.True(t, got) + }) + t.Run("same", func(t *testing.T) { + expected := []corev1.EnvVar{ + { + Name: "name", + Value: "value", + }, + } + actual := []corev1.EnvVar{ + { + Name: "name", + Value: "value", + }, + } + + got := compareEnv(expected, actual) + + assert.True(t, got) + }) + t.Run("with KUBERNETES envs", func(t *testing.T) { + expected := []corev1.EnvVar{ + { + Name: "name", + Value: "value", + }, + } + actual := []corev1.EnvVar{ + { + Name: "name", + Value: "value", + }, + { + Name: "KUBERNETES_PORT_443_TCP_ADDR", + Value: "KUBERNETES_PORT_443_TCP_ADDR", + }, + { + Name: "KUBERNETES_PORT", + Value: "KUBERNETES_PORT", + }, + { + Name: "KUBERNETES_PORT_443_TCP", + Value: "KUBERNETES_PORT_443_TCP", + }, + { + Name: "KUBERNETES_SERVICE_HOST", + Value: "KUBERNETES_SERVICE_HOST", + }, + } + + got := compareEnv(expected, actual) + + assert.True(t, got) + }) + t.Run("different", func(t *testing.T) { + expected := []corev1.EnvVar{ + { + Name: "name", + Value: "value", + }, + } + actual := []corev1.EnvVar{ + { + Name: "name2", + Value: "value2", + }, + } + + got := compareEnv(expected, actual) + + assert.False(t, got) + }) +}