Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions lib/resourcebuilder/podspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resourcebuilder

import (
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
)
Expand All @@ -10,9 +11,8 @@ import (
// matching the container names.
func updatePodSpecWithProxy(podSpec *corev1.PodSpec, containerNames []string, httpProxy, httpsProxy, noProxy string) error {
hasProxy := len(httpsProxy) > 0 || len(httpProxy) > 0 || len(noProxy) > 0
if !hasProxy {
return nil
}

var notFoundContainerNames []string

for _, containerName := range containerNames {
found := false
Expand All @@ -22,26 +22,34 @@ func updatePodSpecWithProxy(podSpec *corev1.PodSpec, containerNames []string, ht
}
found = true

podSpec.Containers[i].Env = append(podSpec.Containers[i].Env, corev1.EnvVar{Name: "HTTP_PROXY", Value: httpProxy})
podSpec.Containers[i].Env = append(podSpec.Containers[i].Env, corev1.EnvVar{Name: "HTTPS_PROXY", Value: httpsProxy})
podSpec.Containers[i].Env = append(podSpec.Containers[i].Env, corev1.EnvVar{Name: "NO_PROXY", Value: noProxy})
if hasProxy {
podSpec.Containers[i].Env = append(podSpec.Containers[i].Env, corev1.EnvVar{Name: "HTTP_PROXY", Value: httpProxy})
podSpec.Containers[i].Env = append(podSpec.Containers[i].Env, corev1.EnvVar{Name: "HTTPS_PROXY", Value: httpsProxy})
podSpec.Containers[i].Env = append(podSpec.Containers[i].Env, corev1.EnvVar{Name: "NO_PROXY", Value: noProxy})
}
}
for i := range podSpec.InitContainers {
if podSpec.InitContainers[i].Name != containerName {
continue
}
found = true

podSpec.InitContainers[i].Env = append(podSpec.InitContainers[i].Env, corev1.EnvVar{Name: "HTTP_PROXY", Value: httpProxy})
podSpec.InitContainers[i].Env = append(podSpec.InitContainers[i].Env, corev1.EnvVar{Name: "HTTPS_PROXY", Value: httpsProxy})
podSpec.InitContainers[i].Env = append(podSpec.InitContainers[i].Env, corev1.EnvVar{Name: "NO_PROXY", Value: noProxy})
if hasProxy {
podSpec.InitContainers[i].Env = append(podSpec.InitContainers[i].Env, corev1.EnvVar{Name: "HTTP_PROXY", Value: httpProxy})
podSpec.InitContainers[i].Env = append(podSpec.InitContainers[i].Env, corev1.EnvVar{Name: "HTTPS_PROXY", Value: httpsProxy})
podSpec.InitContainers[i].Env = append(podSpec.InitContainers[i].Env, corev1.EnvVar{Name: "NO_PROXY", Value: noProxy})
}
}

if !found {
return fmt.Errorf("requested injection for non-existent container: %q", containerName)
notFoundContainerNames = append(notFoundContainerNames, containerName)
}
}

if len(notFoundContainerNames) > 0 {
return fmt.Errorf("requested injection for non-existent containers: %s", strings.Join(notFoundContainerNames, ", "))
}

return nil

}
Expand Down
45 changes: 44 additions & 1 deletion lib/resourcebuilder/podspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,49 @@ func TestUpdatePodSpecWithProxy(t *testing.T) {
},
},
},
{
name: "proxy info, invalid container name",
containerNames: []string{"foo", "invalid"},
httpsProxy: "httpsProxy-val",
noProxy: "noProxy-val",
input: &corev1.PodSpec{
InitContainers: []corev1.Container{
{Name: "init-foo"},
},
Containers: []corev1.Container{
{Name: "foo"},
},
},
expectedErr: "requested injection for non-existent containers: invalid",
},
{
name: "no proxy info, invalid container name",
containerNames: []string{"foo", "invalid"},
input: &corev1.PodSpec{
InitContainers: []corev1.Container{
{Name: "init-foo"},
},
Containers: []corev1.Container{
{Name: "foo"},
},
},
expectedErr: "requested injection for non-existent containers: invalid",
},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
{
name: "proxy info, multiple invalid container names",
containerNames: []string{"foo", "invalid1", "invalid2"},
httpsProxy: "httpsProxy-val",
noProxy: "noProxy-val",
input: &corev1.PodSpec{
InitContainers: []corev1.Container{
{Name: "init-foo"},
},
Containers: []corev1.Container{
{Name: "foo"},
},
},
expectedErr: "requested injection for non-existent containers: invalid1, invalid2",
},
}

for _, test := range tests {
Expand All @@ -104,7 +147,7 @@ func TestUpdatePodSpecWithProxy(t *testing.T) {
t.Fatal(err)
}

if !reflect.DeepEqual(test.input, test.expected) {
if test.expectedErr == "" && !reflect.DeepEqual(test.input, test.expected) {
t.Error(cmp.Diff(test.input, test.expected))
}
})
Expand Down