Skip to content

Commit

Permalink
add http proxy env vars to containers and initContainers created by G…
Browse files Browse the repository at this point in the history
…itJob
  • Loading branch information
raulcabello committed Dec 13, 2023
1 parent 1d4a2bb commit 1fd5789
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
145 changes: 145 additions & 0 deletions pkg/controller/gitjob/generatejob_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package gitjob

import (
"os"
"testing"

"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
"github.com/rancher/gitjob/internal/mocks"
v1 "github.com/rancher/gitjob/pkg/apis/gitjob.cattle.io/v1"
corev1controller "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -321,6 +323,149 @@ func TestGenerateJob(t *testing.T) {
}
}

func TestGenerateJob_EnvVars(t *testing.T) {
tests := map[string]struct {
gitjob *v1.GitJob
osEnv map[string]string
expectedContainerEnvVars []corev1.EnvVar
expectedInitContainerEnvVars []corev1.EnvVar
}{
"no proxy": {
gitjob: &v1.GitJob{
Spec: v1.GitJobSpec{
JobSpec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Env: []corev1.EnvVar{{
Name: "foo",
Value: "bar",
}},
},
},
},
},
},
},
Status: v1.GitJobStatus{
GitEvent: v1.GitEvent{
Commit: "commit",
GithubMeta: v1.GithubMeta{
Event: "event",
},
},
},
},
expectedContainerEnvVars: []corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
{
Name: "COMMIT",
Value: "commit",
},
{
Name: "EVENT_TYPE",
Value: "event",
},
},
},
"proxy": {
gitjob: &v1.GitJob{
Spec: v1.GitJobSpec{
JobSpec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Env: []corev1.EnvVar{{
Name: "foo",
Value: "bar",
}},
},
},
},
},
},
},
Status: v1.GitJobStatus{
GitEvent: v1.GitEvent{
Commit: "commit",
GithubMeta: v1.GithubMeta{
Event: "event",
},
},
},
},
expectedContainerEnvVars: []corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
{
Name: "COMMIT",
Value: "commit",
},
{
Name: "EVENT_TYPE",
Value: "event",
},
{
Name: "HTTP_PROXY",
Value: "httpProxy",
},
{
Name: "HTTPS_PROXY",
Value: "httpsProxy",
},
},
expectedInitContainerEnvVars: []corev1.EnvVar{
{
Name: "HTTP_PROXY",
Value: "httpProxy",
},
{
Name: "HTTPS_PROXY",
Value: "httpsProxy",
},
},
osEnv: map[string]string{"HTTP_PROXY": "httpProxy", "HTTPS_PROXY": "httpsProxy"},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
h := Handler{
image: "test",
}
for k, v := range test.osEnv {
err := os.Setenv(k, v)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
job, err := h.generateJob(test.gitjob)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !cmp.Equal(job.Spec.Template.Spec.Containers[0].Env, test.expectedContainerEnvVars) {
t.Errorf("unexpected envVars. expected %v, but got %v", test.expectedContainerEnvVars, job.Spec.Template.Spec.Containers[0].Env)
}
if !cmp.Equal(job.Spec.Template.Spec.InitContainers[0].Env, test.expectedInitContainerEnvVars) {
t.Errorf("unexpected envVars. expected %v, but got %v", test.expectedInitContainerEnvVars, job.Spec.Template.Spec.InitContainers[0].Env)
}
for k := range test.osEnv {
err := os.Unsetenv(k)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
})
}
}

func httpSecretMock(ctrl *gomock.Controller) corev1controller.SecretCache {
secretmock := mocks.NewMockSecretCache(ctrl)
secretmock.EXPECT().Get(gomock.Any(), gomock.Any()).Return(&corev1.Secret{
Expand Down
14 changes: 14 additions & 0 deletions pkg/controller/gitjob/gitjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gitjob
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -243,6 +244,7 @@ func (h Handler) generateJob(obj *v1.GitJob) (*batchv1.Job, error) {
Value: obj.Status.Event,
},
)
job.Spec.Template.Spec.Containers[i].Env = append(job.Spec.Template.Spec.Containers[i].Env, proxyEnvVars()...)
}

return job, nil
Expand Down Expand Up @@ -311,6 +313,7 @@ func (h Handler) generateInitContainer(obj *v1.GitJob) (corev1.Container, error)
Image: h.image,
Name: "gitcloner-initializer",
VolumeMounts: volumeMounts,
Env: proxyEnvVars(),
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: &[]bool{false}[0],
ReadOnlyRootFilesystem: &[]bool{true}[0],
Expand All @@ -323,3 +326,14 @@ func (h Handler) generateInitContainer(obj *v1.GitJob) (corev1.Container, error)
},
}, nil
}

func proxyEnvVars() []corev1.EnvVar {
var envVars []corev1.EnvVar
for _, envVar := range []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"} {
if val, ok := os.LookupEnv(envVar); ok {
envVars = append(envVars, corev1.EnvVar{Name: envVar, Value: val})
}
}

return envVars
}

0 comments on commit 1fd5789

Please sign in to comment.