From 2ecde7c9c6552fc0b486915d4dec75c714e3d30b Mon Sep 17 00:00:00 2001 From: binoue Date: Wed, 9 Dec 2020 15:09:16 +0900 Subject: [PATCH] ceph: remove deplicated env value Currently, mgr deployment has two overlapping environment variables, ROOK_POD_IP. This causes kube-apiserver to create error logs Signed-off-by: binoue --- pkg/operator/ceph/cluster/mgr/spec.go | 5 +--- pkg/operator/ceph/cluster/mgr/spec_test.go | 1 - pkg/operator/ceph/cluster/osd/osd_test.go | 14 +--------- .../ceph/cluster/osd/provision_spec.go | 2 +- pkg/operator/ceph/cluster/osd/spec.go | 26 +------------------ pkg/operator/ceph/cluster/osd/spec_test.go | 7 ++++- pkg/operator/ceph/test/containers.go | 16 ++++++++++++ pkg/operator/k8sutil/pod.go | 24 +++++++++++++++++ 8 files changed, 50 insertions(+), 45 deletions(-) diff --git a/pkg/operator/ceph/cluster/mgr/spec.go b/pkg/operator/ceph/cluster/mgr/spec.go index 98a352ade..a79f0084d 100644 --- a/pkg/operator/ceph/cluster/mgr/spec.go +++ b/pkg/operator/ceph/cluster/mgr/spec.go @@ -202,10 +202,7 @@ func (c *Cluster) makeSetServerAddrInitContainer(mgrConfig *mgrConfig, mgrModule keyring.VolumeMount().Admin(), ), Env: append( - append( - controller.DaemonEnvVars(c.spec.CephVersion.Image), - k8sutil.PodIPEnvVar(podIPEnvVar), - ), + controller.DaemonEnvVars(c.spec.CephVersion.Image), c.cephMgrOrchestratorModuleEnvs()..., ), Resources: cephv1.GetMgrResources(c.spec.Resources), diff --git a/pkg/operator/ceph/cluster/mgr/spec_test.go b/pkg/operator/ceph/cluster/mgr/spec_test.go index d44d41a31..2ff7aa9d6 100644 --- a/pkg/operator/ceph/cluster/mgr/spec_test.go +++ b/pkg/operator/ceph/cluster/mgr/spec_test.go @@ -76,7 +76,6 @@ func TestPodSpec(t *testing.T) { "200", "100", "500", "250", /* resources */ "my-priority-class") assert.Equal(t, 2, len(d.Spec.Template.Annotations)) - } func TestServiceSpec(t *testing.T) { diff --git a/pkg/operator/ceph/cluster/osd/osd_test.go b/pkg/operator/ceph/cluster/osd/osd_test.go index c974527a3..394423c2b 100644 --- a/pkg/operator/ceph/cluster/osd/osd_test.go +++ b/pkg/operator/ceph/cluster/osd/osd_test.go @@ -17,7 +17,6 @@ package osd import ( "context" - "fmt" "os" "testing" @@ -394,7 +393,7 @@ func TestGetOSDInfo(t *testing.T) { } d1, _ := c.makeDeployment(osdProp, osd1, dataPathMap) osds1, _ := c.getOSDInfo(d1) - findDuplicateEnvVars(t, d1.Spec.Template.Spec) + assert.Equal(t, 1, len(osds1)) assert.Equal(t, osd1.ID, osds1[0].ID) assert.Equal(t, osd1.BlockPath, osds1[0].BlockPath) @@ -415,17 +414,6 @@ func TestGetOSDInfo(t *testing.T) { assert.NotNil(t, err) } -func findDuplicateEnvVars(t *testing.T, pod v1.PodSpec) { - for _, container := range pod.Containers { - envVars := map[string]string{} - for _, v := range container.Env { - _, ok := envVars[v.Name] - assert.False(t, ok, fmt.Sprintf("env var duplicated: %s", v.Name)) - envVars[v.Name] = v.Value - } - } -} - func TestOSDPlacement(t *testing.T) { // no placement prop := osdProperties{} diff --git a/pkg/operator/ceph/cluster/osd/provision_spec.go b/pkg/operator/ceph/cluster/osd/provision_spec.go index 2fc26f6f1..12e09ac0d 100644 --- a/pkg/operator/ceph/cluster/osd/provision_spec.go +++ b/pkg/operator/ceph/cluster/osd/provision_spec.go @@ -158,7 +158,7 @@ func (c *Cluster) provisionPodTemplateSpec(osdProps osdProperties, restart v1.Re } else { osdProps.getPreparePlacement().ApplyToPodSpec(&podSpec) } - removeDuplicateEnvVars(&podSpec) + k8sutil.RemoveDuplicateEnvVars(&podSpec) podMeta := metav1.ObjectMeta{ Name: AppName, diff --git a/pkg/operator/ceph/cluster/osd/spec.go b/pkg/operator/ceph/cluster/osd/spec.go index fa495c41c..9036a0ca7 100644 --- a/pkg/operator/ceph/cluster/osd/spec.go +++ b/pkg/operator/ceph/cluster/osd/spec.go @@ -564,7 +564,7 @@ func (c *Cluster) makeDeployment(osdProps osdProperties, osd OSDInfo, provisionC } } - removeDuplicateEnvVars(&podTemplateSpec.Spec) + k8sutil.RemoveDuplicateEnvVars(&podTemplateSpec.Spec) deployment := &apps.Deployment{ ObjectMeta: metav1.ObjectMeta{ @@ -625,30 +625,6 @@ func (c *Cluster) makeDeployment(osdProps osdProperties, osd OSDInfo, provisionC return deployment, nil } -func removeDuplicateEnvVars(pod *v1.PodSpec) { - for i := range pod.Containers { - removeDuplicateEnvVarsFromContainer(&pod.Containers[i]) - } - for i := range pod.InitContainers { - removeDuplicateEnvVarsFromContainer(&pod.InitContainers[i]) - } -} - -func removeDuplicateEnvVarsFromContainer(container *v1.Container) { - foundVars := map[string]string{} - vars := []v1.EnvVar{} - for _, v := range container.Env { - if _, ok := foundVars[v.Name]; ok { - logger.Debugf("duplicate env var %q skipped on container %q", v.Name, container.Name) - continue - } - - vars = append(vars, v) - foundVars[v.Name] = v.Value - } - container.Env = vars -} - // To get rook inside the container, the config init container needs to copy "tini" and "rook" binaries into a volume. // Get the config flag so rook will copy the binaries and create the volume and mount that will be shared between // the init container and the daemon container diff --git a/pkg/operator/ceph/cluster/osd/spec_test.go b/pkg/operator/ceph/cluster/osd/spec_test.go index bfc9a6b71..e4e109986 100644 --- a/pkg/operator/ceph/cluster/osd/spec_test.go +++ b/pkg/operator/ceph/cluster/osd/spec_test.go @@ -27,6 +27,7 @@ import ( cephclient "github.com/rook/rook/pkg/daemon/ceph/client" "github.com/rook/rook/pkg/operator/ceph/cluster/osd/config" opconfig "github.com/rook/rook/pkg/operator/ceph/config" + operatortest "github.com/rook/rook/pkg/operator/ceph/test" cephver "github.com/rook/rook/pkg/operator/ceph/version" exectest "github.com/rook/rook/pkg/util/exec/test" "github.com/stretchr/testify/assert" @@ -63,7 +64,11 @@ func TestPodContainer(t *testing.T) { assert.Equal(t, "ceph", container.Args[2]) assert.Equal(t, "osd", container.Args[3]) assert.Equal(t, "provision", container.Args[4]) - findDuplicateEnvVars(t, c.Spec) + + for _, c := range c.Spec.Containers { + vars := operatortest.FindDuplicateEnvVars(c) + assert.Equal(t, 0, len(vars)) + } } func TestDaemonset(t *testing.T) { diff --git a/pkg/operator/ceph/test/containers.go b/pkg/operator/ceph/test/containers.go index 254214a64..093ff0ddc 100644 --- a/pkg/operator/ceph/test/containers.go +++ b/pkg/operator/ceph/test/containers.go @@ -132,6 +132,8 @@ func (ct *ContainersTester) AssertEnvVarsContainCephRequirements() { "POD_CPU_REQUEST env var does not have the appropriate source:", e) } } + vars := FindDuplicateEnvVars(c) + assert.Equal(ct.t, 0, len(vars), fmt.Sprintf("found duplicate env vars: %v", vars)) } } @@ -247,3 +249,17 @@ func (ct *ContainersTester) allNonrequiredVarNames() []string { } return all } + +// FindDuplicateEnvVars finds duplicated environment variables and return the variable name list. +func FindDuplicateEnvVars(container v1.Container) []string { + var duplicateEnvVars []string + envVars := map[string]string{} + for _, v := range container.Env { + _, ok := envVars[v.Name] + if ok { + duplicateEnvVars = append(duplicateEnvVars, v.Name) + } + envVars[v.Name] = v.Value + } + return duplicateEnvVars +} diff --git a/pkg/operator/k8sutil/pod.go b/pkg/operator/k8sutil/pod.go index cca3f1259..8f442ea44 100644 --- a/pkg/operator/k8sutil/pod.go +++ b/pkg/operator/k8sutil/pod.go @@ -386,3 +386,27 @@ func ForceDeletePodIfStuck(clusterdContext *clusterd.Context, pod v1.Pod) error logger.Infof("pod %q deletion succeeded", pod.Name) return nil } + +func RemoveDuplicateEnvVars(pod *v1.PodSpec) { + for i := range pod.Containers { + removeDuplicateEnvVarsFromContainer(&pod.Containers[i]) + } + for i := range pod.InitContainers { + removeDuplicateEnvVarsFromContainer(&pod.InitContainers[i]) + } +} + +func removeDuplicateEnvVarsFromContainer(container *v1.Container) { + foundVars := map[string]string{} + vars := []v1.EnvVar{} + for _, v := range container.Env { + if _, ok := foundVars[v.Name]; ok { + logger.Debugf("duplicate env var %q skipped on container %q", v.Name, container.Name) + continue + } + + vars = append(vars, v) + foundVars[v.Name] = v.Value + } + container.Env = vars +}