Skip to content

Commit

Permalink
remove workspace from the termination message path to be used by the …
Browse files Browse the repository at this point in the history
…image output digest exporter

- use termination message instead of std out to allow the image exporter binary to log erros or warnings
- move the default image output folder to /builder/home since this will always be mounted in the containers
- move the termination log file to be in the same path as output images and pass it to the binary to reduce
hard-coding
  • Loading branch information
nader-ziada committed Jun 29, 2019
1 parent 5e01e74 commit fa54c9a
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 55 deletions.
3 changes: 2 additions & 1 deletion cmd/imagedigestexporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

var (
images = flag.String("images", "", "List of images resources built by task in json format")
terminationMessagePath = flag.String("terminationMessagePath", "", "Location of file containing termination message")
)

/* The input of this go program will be a JSON string with all the output PipelineResources of type
Expand Down Expand Up @@ -66,7 +67,7 @@ func main() {
log.Fatalf("Unexpected error converting images to json %v: %v", output, err)
}
log.Printf("Image digest exporter output: %s ", string(imagesJSON))
f, err := os.OpenFile("/workspace/builder/termination-log", os.O_WRONLY|os.O_CREATE, 0666)
f, err := os.OpenFile(*terminationMessagePath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Fatalf("Unexpected error converting images to json %v: %v", output, err)
}
Expand Down
4 changes: 3 additions & 1 deletion examples/taskruns/task-multiple-output-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ spec:
- name: url
value: https://github.com/GoogleContainerTools/skaffold
---
#Builds an image via kaniko and pushes it to registry.
# This task is currently hardcoding a index.json file instead of
# building an image since kaniko and other easy to use tools don't
# support exporting this file yet
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
Expand Down
10 changes: 6 additions & 4 deletions examples/taskruns/task-output-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ spec:
- name: url
value: https://github.com/GoogleContainerTools/skaffold
---
#Builds an image via kaniko and pushes it to registry.
# This task is currently hardcoding a index.json file instead of
# building an image since kaniko and other easy to use tools don't
# support exporting this file yet
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
name: build-push-kaniko
name: build-push-kaniko-output-image
spec:
inputs:
resources:
Expand Down Expand Up @@ -85,10 +87,10 @@ spec:
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
name: build-push-run
name: build-push-run-output-image
spec:
taskRef:
name: build-push-kaniko
name: build-push-kaniko-output-image
inputs:
resources:
- name: sourcerepo
Expand Down
5 changes: 4 additions & 1 deletion pkg/reconciler/v1alpha1/taskrun/resources/image_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
corev1 "k8s.io/api/core/v1"
)

const TerminationMessagePath = "/builder/home/image-outputs/termination-log"

var (
imageDigestExporterImage = flag.String("imagedigest-exporter-image", "override-with-imagedigest-exporter-image:latest", "The container image containing our image digest exporter binary.")
)
Expand Down Expand Up @@ -102,8 +104,9 @@ func imageDigestExporterContainer(stepName string, imagesJSON []byte) corev1.Con
Command: []string{"/ko-app/imagedigestexporter"},
Args: []string{
"-images", string(imagesJSON),
"-terminationMessagePath", TerminationMessagePath,
},
TerminationMessagePath: "/workspace/builder/termination-log",
TerminationMessagePath: TerminationMessagePath,
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
}
}
Expand Down
33 changes: 18 additions & 15 deletions pkg/reconciler/v1alpha1/taskrun/resources/image_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ func TestAddOutputImageDigestExporter(t *testing.T) {
Name: "step1",
},
{
Name: "image-digest-exporter-step1-9l9zj",
Image: "override-with-imagedigest-exporter-image:latest",
Command: []string{"/ko-app/imagedigestexporter"},
Args: []string{"-images", fmt.Sprintf("[{\"name\":\"source-image-1\",\"type\":\"image\",\"url\":\"gcr.io/some-image-1\",\"digest\":\"\",\"OutputImageDir\":\"%s\"}]", currentDir)},
TerminationMessagePath: "/workspace/builder/termination-log",
Name: "image-digest-exporter-step1-9l9zj",
Image: "override-with-imagedigest-exporter-image:latest",
Command: []string{"/ko-app/imagedigestexporter"},
Args: []string{"-images", fmt.Sprintf("[{\"name\":\"source-image-1\",\"type\":\"image\",\"url\":\"gcr.io/some-image-1\",\"digest\":\"\",\"OutputImageDir\":\"%s\"}]", currentDir),
"-terminationMessagePath", "/builder/home/image-outputs/termination-log"},
TerminationMessagePath: TerminationMessagePath,
TerminationMessagePolicy: "FallbackToLogsOnError",
}},
}, {
Expand Down Expand Up @@ -153,20 +154,22 @@ func TestAddOutputImageDigestExporter(t *testing.T) {
Name: "step1",
},
{
Name: "image-digest-exporter-step1-9l9zj",
Image: "override-with-imagedigest-exporter-image:latest",
Command: []string{"/ko-app/imagedigestexporter"},
Args: []string{"-images", fmt.Sprintf("[{\"name\":\"source-image-1\",\"type\":\"image\",\"url\":\"gcr.io/some-image-1\",\"digest\":\"\",\"OutputImageDir\":\"%s\"}]", currentDir)},
TerminationMessagePath: "/workspace/builder/termination-log",
Name: "image-digest-exporter-step1-9l9zj",
Image: "override-with-imagedigest-exporter-image:latest",
Command: []string{"/ko-app/imagedigestexporter"},
Args: []string{"-images", fmt.Sprintf("[{\"name\":\"source-image-1\",\"type\":\"image\",\"url\":\"gcr.io/some-image-1\",\"digest\":\"\",\"OutputImageDir\":\"%s\"}]", currentDir),
"-terminationMessagePath", "/builder/home/image-outputs/termination-log"},
TerminationMessagePath: TerminationMessagePath,
TerminationMessagePolicy: "FallbackToLogsOnError",
}, {
Name: "step2",
}, {
Name: "image-digest-exporter-step2-mz4c7",
Image: "override-with-imagedigest-exporter-image:latest",
Command: []string{"/ko-app/imagedigestexporter"},
Args: []string{"-images", fmt.Sprintf("[{\"name\":\"source-image-1\",\"type\":\"image\",\"url\":\"gcr.io/some-image-1\",\"digest\":\"\",\"OutputImageDir\":\"%s\"}]", currentDir)},
TerminationMessagePath: "/workspace/builder/termination-log",
Name: "image-digest-exporter-step2-mz4c7",
Image: "override-with-imagedigest-exporter-image:latest",
Command: []string{"/ko-app/imagedigestexporter"},
Args: []string{"-images", fmt.Sprintf("[{\"name\":\"source-image-1\",\"type\":\"image\",\"url\":\"gcr.io/some-image-1\",\"digest\":\"\",\"OutputImageDir\":\"%s\"}]", currentDir),
"-terminationMessagePath", "/builder/home/image-outputs/termination-log"},
TerminationMessagePath: TerminationMessagePath,
TerminationMessagePolicy: "FallbackToLogsOnError",
},
},
Expand Down
9 changes: 6 additions & 3 deletions pkg/reconciler/v1alpha1/taskrun/resources/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ import (
"github.com/tektoncd/pipeline/pkg/reconciler/v1alpha1/taskrun/entrypoint"
)

const workspaceDir = "/workspace"
const (
workspaceDir = "/workspace"
homeDir = "/builder/home"
)

// These are effectively const, but Go doesn't have such an annotation.
var (
Expand All @@ -62,14 +65,14 @@ var (
// These are injected into all of the source/step containers.
implicitEnvVars = []corev1.EnvVar{{
Name: "HOME",
Value: "/builder/home",
Value: homeDir,
}}
implicitVolumeMounts = []corev1.VolumeMount{{
Name: "workspace",
MountPath: workspaceDir,
}, {
Name: "home",
MountPath: "/builder/home",
MountPath: homeDir,
}}
implicitVolumes = []corev1.Volume{{
Name: "workspace",
Expand Down
52 changes: 26 additions & 26 deletions pkg/reconciler/v1alpha1/taskrun/resources/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,32 +395,6 @@ func TestMakePod(t *testing.T) {
}
}

func TestMakeWorkingDirScript(t *testing.T) {
for _, c := range []struct {
desc string
workingDirs map[string]bool
want string
}{{
desc: "default",
workingDirs: map[string]bool{"/workspace": true},
want: "",
}, {
desc: "simple",
workingDirs: map[string]bool{"/workspace/foo": true, "/workspace/bar": true, "/baz": true},
want: "mkdir -p /workspace/bar /workspace/foo",
}, {
desc: "empty",
workingDirs: map[string]bool{"/workspace": true, "": true, "/baz": true, "/workspacedir": true},
want: "",
}} {
t.Run(c.desc, func(t *testing.T) {
if script := makeWorkingDirScript(c.workingDirs); script != c.want {
t.Errorf("Expected `%v`, got `%v`", c.want, script)
}
})
}
}

func TestAddReadyAnnotation(t *testing.T) {
type testcase struct {
desc string
Expand Down Expand Up @@ -646,3 +620,29 @@ func TestInitOutputResourcesDefaultDir(t *testing.T) {
})
}
}

func TestMakeWorkingDirScript(t *testing.T) {
for _, c := range []struct {
desc string
workingDirs map[string]bool
want string
}{{
desc: "default",
workingDirs: map[string]bool{"/workspace": true},
want: "",
}, {
desc: "simple",
workingDirs: map[string]bool{"/workspace/foo": true, "/workspace/bar": true, "/baz": true},
want: "mkdir -p /workspace/bar /workspace/foo",
}, {
desc: "empty",
workingDirs: map[string]bool{"/workspace": true, "": true, "/baz": true, "/workspacedir": true},
want: "",
}} {
t.Run(c.desc, func(t *testing.T) {
if script := makeWorkingDirScript(c.workingDirs); script != c.want {
t.Errorf("Expected `%v`, got `%v`", c.want, script)
}
})
}
}
10 changes: 6 additions & 4 deletions pkg/reconciler/v1alpha1/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,8 @@ func TestReconcile(t *testing.T) {
tb.PodContainer("step-image-digest-exporter-mycontainer-9l9zj", "override-with-imagedigest-exporter-image:latest",
tb.Command("/builder/tools/entrypoint"),
tb.Args("-wait_file", "/builder/tools/1", "-post_file", "/builder/tools/2", "-entrypoint", "/ko-app/imagedigestexporter", "--",
"-images", "[{\"name\":\"image-resource\",\"type\":\"image\",\"url\":\"gcr.io/kristoff/sven\",\"digest\":\"\",\"OutputImageDir\":\"\"}]"),
"-images", "[{\"name\":\"image-resource\",\"type\":\"image\",\"url\":\"gcr.io/kristoff/sven\",\"digest\":\"\",\"OutputImageDir\":\"\"}]",
"-terminationMessagePath", "/builder/home/image-outputs/termination-log"),
tb.WorkingDir(workspaceDir),
tb.EnvVar("HOME", "/builder/home"),
tb.VolumeMount("tools", "/builder/tools"),
Expand All @@ -530,7 +531,7 @@ func TestReconcile(t *testing.T) {
tb.Memory("0"),
tb.EphemeralStorage("0"),
)),
tb.TerminationMessagePath("/workspace/builder/termination-log"),
tb.TerminationMessagePath("/builder/home/image-outputs/termination-log"),
tb.TerminationMessagePolicy(corev1.TerminationMessageFallbackToLogsOnError),
),
tb.PodContainer("step-myothercontainer", "myotherimage",
Expand All @@ -551,7 +552,8 @@ func TestReconcile(t *testing.T) {
tb.PodContainer("step-image-digest-exporter-myothercontainer-mz4c7", "override-with-imagedigest-exporter-image:latest",
tb.Command(entrypointLocation),
tb.Args("-wait_file", "/builder/tools/3", "-post_file", "/builder/tools/4", "-entrypoint", "/ko-app/imagedigestexporter", "--",
"-images", "[{\"name\":\"image-resource\",\"type\":\"image\",\"url\":\"gcr.io/kristoff/sven\",\"digest\":\"\",\"OutputImageDir\":\"\"}]"),
"-images", "[{\"name\":\"image-resource\",\"type\":\"image\",\"url\":\"gcr.io/kristoff/sven\",\"digest\":\"\",\"OutputImageDir\":\"\"}]",
"-terminationMessagePath", "/builder/home/image-outputs/termination-log"),
tb.WorkingDir(workspaceDir),
tb.EnvVar("HOME", "/builder/home"),
tb.VolumeMount("tools", "/builder/tools"),
Expand All @@ -562,7 +564,7 @@ func TestReconcile(t *testing.T) {
tb.Memory("0"),
tb.EphemeralStorage("0"),
)),
tb.TerminationMessagePath("/workspace/builder/termination-log"),
tb.TerminationMessagePath("/builder/home/image-outputs/termination-log"),
tb.TerminationMessagePolicy(corev1.TerminationMessageFallbackToLogsOnError),
),
),
Expand Down

0 comments on commit fa54c9a

Please sign in to comment.