Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make container name be fixed #197

Closed
Closed
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
2 changes: 1 addition & 1 deletion pkg/tracejob/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func (nj *TraceJob) Job() *batchv1.Job {
},
Containers: []apiv1.Container{
apiv1.Container{
Name: nj.Name,
Name: "kubectl-trace",
Image: nj.ImageNameTag,
Command: traceCmd,
TTY: true,
Expand Down
4 changes: 2 additions & 2 deletions pkg/tracejob/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (j *jobSuite) TestCreateJob() {
assert.NotNil(j.T(), joblist)

assert.Len(j.T(), joblist.Items, 1)
assert.Equal(j.T(), joblist.Items[0].Spec.Template.Spec.Containers[0].Name, testJobName)
assert.Equal(j.T(), joblist.Items[0].Spec.Template.Spec.Containers[0].Name, "kubectl-trace")
}

func (j *jobSuite) TestCreateJobWithGoogleAppSecret() {
Expand All @@ -64,7 +64,7 @@ func (j *jobSuite) TestCreateJobWithGoogleAppSecret() {
assert.NotNil(j.T(), joblist)

assert.Len(j.T(), joblist.Items, 1)
assert.Equal(j.T(), joblist.Items[0].Spec.Template.Spec.Containers[0].Name, testJobName)
assert.Equal(j.T(), joblist.Items[0].Spec.Template.Spec.Containers[0].Name, "kubectl-trace")

assert.Len(j.T(), joblist.Items[0].Spec.Template.Spec.Containers[0].Env, 1)
assert.Equal(j.T(), joblist.Items[0].Spec.Template.Spec.Containers[0].Env[0].Name, "GOOGLE_APPLICATION_CREDENTIALS")
Expand Down
154 changes: 154 additions & 0 deletions pkg/tracejob/patch_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)

// TODO (dalehamel): move this back into job_test.go
Expand Down Expand Up @@ -38,6 +40,30 @@ spec:
hostPID: true
`)

// merge key for PodSpec.Volumes is "name", and Container.volumeMounts is "mountPath"
// ref: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#podspec-v1-core
var addHostVolumeMountMerge = []byte(`
spec:
template:
spec:
containers:
- name: kubectl-trace
volumeMounts:
- name: trace-output-2
mountPath: /tmp/kubectl-trace
- name: extra-volume
mountPath: /home/some/volume
readOnly: true
volumes:
- name: trace-output
emptyDir:
sizeLimit: 10Mi
- name: extra-volume
hostPath:
path: /home/some/volume
type: Directory
`)

var testCases = []patchTest{
{patchType: "json", patch: patchJSON1},
{patchType: "json", patch: patchJSON2},
Expand Down Expand Up @@ -75,3 +101,131 @@ func getJob() *batchv1.Job {
},
}
}

func getJobWithContainer() *batchv1.Job {
return &batchv1.Job{
Spec: batchv1.JobSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "kubectl-trace",
Image: "hello",
VolumeMounts: []v1.VolumeMount{
{
Name: "sys",
MountPath: "/sys",
ReadOnly: true,
},
{
Name: "trace-output",
MountPath: "/tmp/kubectl-trace",
ReadOnly: true,
},
},
},
},
Volumes: []v1.Volume{
{
Name: "sys",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/sys",
},
},
},
{
Name: "trace-output",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{
SizeLimit: quantityPtr(resource.MustParse("1Gi")),
},
},
},
},
},
},
},
}
}

func newHostPathType(pathType string) *v1.HostPathType {
hostPathType := new(v1.HostPathType)
*hostPathType = v1.HostPathType(pathType)
return hostPathType
}

func getPatchedJobWithContainer() *batchv1.Job {
return &batchv1.Job{
Spec: batchv1.JobSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "kubectl-trace",
Image: "hello",
VolumeMounts: []v1.VolumeMount{
{
Name: "sys",
MountPath: "/sys",
ReadOnly: true,
},
{
Name: "trace-output-2",
MountPath: "/tmp/kubectl-trace",
ReadOnly: true,
},
{
Name: "extra-volume",
MountPath: "/home/some/volume",
ReadOnly: true,
},
},
},
},
Volumes: []v1.Volume{
{
Name: "sys",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/sys",
},
},
},
{
Name: "trace-output",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{
SizeLimit: quantityPtr(resource.MustParse("10Mi")),
},
},
},
{
Name: "extra-volume",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/home/some/volume",
Type: newHostPathType(string(v1.HostPathDirectory)),
},
},
},
},
},
},
},
}
}

func TestContainerStrategicPatch(t *testing.T) {
job := getJobWithContainer()
newJob, err := patchJob(job, "strategic", addHostVolumeMountMerge)
if err != nil {
t.Error(err)
}

expected := getPatchedJobWithContainer()

if !reflect.DeepEqual(newJob, expected) {
t.Errorf("patch host volume job does not match expected")
}
}