From 64b76161b32d585e00fd519bf09c508947e9eb0c Mon Sep 17 00:00:00 2001 From: Dale Hamel Date: Wed, 6 Mar 2019 10:54:23 -0500 Subject: [PATCH] Make downloading headers conditional --- pkg/tracejob/job.go | 194 ++++++++++++++++++++++------------------- pkg/version/version.go | 27 ------ 2 files changed, 105 insertions(+), 116 deletions(-) diff --git a/pkg/tracejob/job.go b/pkg/tracejob/job.go index f4f63d7c..b465da9d 100644 --- a/pkg/tracejob/job.go +++ b/pkg/tracejob/job.go @@ -253,79 +253,6 @@ func (t *TraceJobClient) CreateJob(nj TraceJob) (*batchv1.Job, error) { }, }, }, - apiv1.Volume{ - Name: "lsb-release", - VolumeSource: apiv1.VolumeSource{ - HostPath: &apiv1.HostPathVolumeSource{ - Path: "/etc/lsb-release", - }, - }, - }, - apiv1.Volume{ - Name: "os-release", - VolumeSource: apiv1.VolumeSource{ - HostPath: &apiv1.HostPathVolumeSource{ - Path: "/etc/os-release", - }, - }, - }, - apiv1.Volume{ - Name: "modules-dir", - VolumeSource: apiv1.VolumeSource{ - HostPath: &apiv1.HostPathVolumeSource{ - Path: "/var/cache/linux-headers/modules_dir", - }, - }, - }, - apiv1.Volume{ - Name: "linux-headers-generated", - VolumeSource: apiv1.VolumeSource{ - HostPath: &apiv1.HostPathVolumeSource{ - Path: "/var/cache/linux-headers/generated", - }, - }, - }, - }, - InitContainers: []apiv1.Container{ - apiv1.Container{ - Name: "kubectl-trace-init", - Image: version.InitImageNameTag(), - Resources: apiv1.ResourceRequirements{ - Requests: apiv1.ResourceList{ - apiv1.ResourceCPU: resource.MustParse("100m"), - apiv1.ResourceMemory: resource.MustParse("100Mi"), - }, - Limits: apiv1.ResourceList{ - apiv1.ResourceCPU: resource.MustParse("1"), - apiv1.ResourceMemory: resource.MustParse("1G"), - }, - }, - VolumeMounts: []apiv1.VolumeMount{ - apiv1.VolumeMount{ - Name: "lsb-release", - MountPath: "/etc/lsb-release.host", - ReadOnly: true, - }, - apiv1.VolumeMount{ - Name: "os-release", - MountPath: "/etc/os-release.host", - ReadOnly: true, - }, - apiv1.VolumeMount{ - Name: "modules-dir", - MountPath: "/lib/modules", - }, - apiv1.VolumeMount{ - Name: "modules-host", - MountPath: "/lib/modules.host", - ReadOnly: true, - }, - apiv1.VolumeMount{ - Name: "linux-headers-generated", - MountPath: "/usr/src/", - }, - }, - }, }, Containers: []apiv1.Container{ apiv1.Container{ @@ -355,21 +282,6 @@ func (t *TraceJobClient) CreateJob(nj TraceJob) (*batchv1.Job, error) { MountPath: "/sys", ReadOnly: true, }, - apiv1.VolumeMount{ - Name: "modules-dir", - MountPath: "/lib/modules", - ReadOnly: true, - }, - apiv1.VolumeMount{ - Name: "modules-host", - MountPath: "/lib/modules.host", - ReadOnly: true, - }, - apiv1.VolumeMount{ - Name: "linux-headers-generated", - MountPath: "/usr/src/", - ReadOnly: true, - }, }, SecurityContext: &apiv1.SecurityContext{ Privileged: boolPtr(true), @@ -399,6 +311,110 @@ func (t *TraceJobClient) CreateJob(nj TraceJob) (*batchv1.Job, error) { }, } + if nj.FetchHeaders { + // If we aren't downloading headers, add the initContainer and set up mounts + job.Spec.Template.Spec.InitContainers = []apiv1.Container{ + apiv1.Container{ + Name: "kubectl-trace-init", + Image: nj.InitImageNameTag, + Resources: apiv1.ResourceRequirements{ + Requests: apiv1.ResourceList{ + apiv1.ResourceCPU: resource.MustParse("100m"), + apiv1.ResourceMemory: resource.MustParse("100Mi"), + }, + Limits: apiv1.ResourceList{ + apiv1.ResourceCPU: resource.MustParse("1"), + apiv1.ResourceMemory: resource.MustParse("1G"), + }, + }, + VolumeMounts: []apiv1.VolumeMount{ + apiv1.VolumeMount{ + Name: "lsb-release", + MountPath: "/etc/lsb-release.host", + ReadOnly: true, + }, + apiv1.VolumeMount{ + Name: "os-release", + MountPath: "/etc/os-release.host", + ReadOnly: true, + }, + apiv1.VolumeMount{ + Name: "modules-dir", + MountPath: "/lib/modules", + }, + apiv1.VolumeMount{ + Name: "modules-host", + MountPath: "/lib/modules.host", + ReadOnly: true, + }, + apiv1.VolumeMount{ + Name: "linux-headers-generated", + MountPath: "/usr/src/", + }, + }, + }, + } + + job.Spec.Template.Spec.Volumes = append(job.Spec.Template.Spec.Volumes, + apiv1.Volume{ + Name: "lsb-release", + VolumeSource: apiv1.VolumeSource{ + HostPath: &apiv1.HostPathVolumeSource{ + Path: "/etc/lsb-release", + }, + }, + }, + apiv1.Volume{ + Name: "os-release", + VolumeSource: apiv1.VolumeSource{ + HostPath: &apiv1.HostPathVolumeSource{ + Path: "/etc/os-release", + }, + }, + }, + apiv1.Volume{ + Name: "modules-dir", + VolumeSource: apiv1.VolumeSource{ + HostPath: &apiv1.HostPathVolumeSource{ + Path: "/var/cache/linux-headers/modules_dir", + }, + }, + }, + apiv1.Volume{ + Name: "linux-headers-generated", + VolumeSource: apiv1.VolumeSource{ + HostPath: &apiv1.HostPathVolumeSource{ + Path: "/var/cache/linux-headers/generated", + }, + }, + }) + + job.Spec.Template.Spec.Containers[0].VolumeMounts = append(job.Spec.Template.Spec.Containers[0].VolumeMounts, + apiv1.VolumeMount{ + Name: "modules-dir", + MountPath: "/lib/modules", + ReadOnly: true, + }, + apiv1.VolumeMount{ + Name: "modules-host", + MountPath: "/lib/modules.host", + ReadOnly: true, + }, + apiv1.VolumeMount{ + Name: "linux-headers-generated", + MountPath: "/usr/src/", + ReadOnly: true, + }) + + } else { + // If we aren't downloading headers, unconditionally used the ones linked in /lib/modules + job.Spec.Template.Spec.Containers[0].VolumeMounts = append(job.Spec.Template.Spec.Containers[0].VolumeMounts, + apiv1.VolumeMount{ + Name: "modules-host", + MountPath: "/lib/modules", + ReadOnly: true, + }) + } if _, err := t.ConfigClient.Create(cm); err != nil { return nil, err } @@ -476,4 +492,4 @@ func jobStatus(j batchv1.Job) TraceJobStatus { return TraceJobFailed } return TraceJobUnknown -} \ No newline at end of file +} diff --git a/pkg/version/version.go b/pkg/version/version.go index d367ed40..cc0e129b 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -10,39 +10,12 @@ import ( var gitCommit string var buildTime string var versionFormat = "git commit: %s\nbuild date: %s" -var imageNameTagFormat = "%s:%s" -var defaultImageName = "quay.io/fntlnz/kubectl-trace-bpftrace" -var defaultImageTag = "latest" -var defaultInitImageName = "quay.io/dalehamel/kubectl-trace-init" -var defaultInitImageTag = "latest" - -// ImageName returns the container image name defined in Makefile -func ImageName() string { - return imageName -} // GitCommit returns the git commit func GitCommit() string { return gitCommit } -func ImageNameTag() string { - imageName := ImageName() - tag := GitCommit() - if len(tag) == 0 { - tag = defaultImageTag - } - if len(imageName) == 0 { - imageName = defaultImageName - } - return fmt.Sprintf(imageNameTagFormat, imageName, tag) -} - -// InitImageNameTag returns the full image path and tag for the initContainer -func InitImageNameTag() string { - return fmt.Sprintf(imageNameTagFormat, defaultInitImageName, defaultInitImageTag) -} - // Time returns the build time func Time() *time.Time { if len(buildTime) == 0 {