Skip to content

Commit c75c325

Browse files
authored
Merge pull request #7152 from walidghallab/sanitize
Sanitize ENV from podSpec to improve scheduling simulation performance.
2 parents c27e40d + e6ee407 commit c75c325

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

cluster-autoscaler/utils/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,19 @@ func PodSpecSemanticallyEqual(p1 apiv1.PodSpec, p2 apiv1.PodSpec) bool {
7070
func sanitizePodSpec(podSpec apiv1.PodSpec) apiv1.PodSpec {
7171
dropProjectedVolumesAndMounts(&podSpec)
7272
dropHostname(&podSpec)
73+
dropEnv(&podSpec)
7374
return podSpec
7475
}
7576

77+
func dropEnv(podSpec *apiv1.PodSpec) {
78+
for i := range podSpec.Containers {
79+
podSpec.Containers[i].Env = nil
80+
}
81+
for i := range podSpec.InitContainers {
82+
podSpec.InitContainers[i].Env = nil
83+
}
84+
}
85+
7686
func dropProjectedVolumesAndMounts(podSpec *apiv1.PodSpec) {
7787
projectedVolumeNames := map[string]bool{}
7888
var volumes []apiv1.Volume

cluster-autoscaler/utils/utils_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ limitations under the License.
1717
package utils
1818

1919
import (
20+
"testing"
21+
2022
"github.com/stretchr/testify/assert"
2123
apiv1 "k8s.io/api/core/v1"
2224
"k8s.io/autoscaler/cluster-autoscaler/utils/test"
23-
"testing"
2425
)
2526

2627
func TestPodSpecSemanticallyEqual(t *testing.T) {
@@ -46,6 +47,26 @@ func TestPodSpecSemanticallyEqual(t *testing.T) {
4647
},
4748
result: true,
4849
},
50+
{
51+
name: "two pods with different ENV",
52+
p1Spec: apiv1.PodSpec{
53+
Containers: []apiv1.Container{
54+
{Env: []apiv1.EnvVar{{Name: "foo", Value: "bar"}}},
55+
},
56+
InitContainers: []apiv1.Container{
57+
{Env: []apiv1.EnvVar{{Name: "foo", Value: "bar"}}},
58+
},
59+
},
60+
p2Spec: apiv1.PodSpec{
61+
Containers: []apiv1.Container{
62+
{Env: []apiv1.EnvVar{{Name: "baz", Value: "foo"}}},
63+
},
64+
InitContainers: []apiv1.Container{
65+
{Env: []apiv1.EnvVar{{Name: "baz", Value: "bar"}}},
66+
},
67+
},
68+
result: true,
69+
},
4970
{
5071
name: "two pods with different volumes",
5172
p1Spec: apiv1.PodSpec{
@@ -72,6 +93,7 @@ func TestPodSpecSemanticallyEqual(t *testing.T) {
7293
{Image: "foo/baz", Name: "foobaz"},
7394
},
7495
},
96+
result: false,
7597
},
7698
{
7799
name: "two pods with different hostnames",
@@ -180,6 +202,27 @@ func TestSanitizePodSpec(t *testing.T) {
180202
},
181203
},
182204
},
205+
{
206+
name: "pod spec with env",
207+
inputPodSpec: apiv1.PodSpec{
208+
NodeSelector: map[string]string{"foo": "bar"},
209+
Containers: []apiv1.Container{
210+
{Image: "foo/bar", Name: "foobar", Env: []apiv1.EnvVar{{Name: "foo", Value: "bar"}}},
211+
},
212+
InitContainers: []apiv1.Container{
213+
{Image: "foo/baz", Name: "foobaz", Env: []apiv1.EnvVar{{Name: "foo2", Value: "bar2"}}},
214+
},
215+
},
216+
outputPodSpec: apiv1.PodSpec{
217+
NodeSelector: map[string]string{"foo": "bar"},
218+
Containers: []apiv1.Container{
219+
{Image: "foo/bar", Name: "foobar"},
220+
},
221+
InitContainers: []apiv1.Container{
222+
{Image: "foo/baz", Name: "foobaz"},
223+
},
224+
},
225+
},
183226
}
184227

185228
for _, tt := range tests {

0 commit comments

Comments
 (0)