From 52dc295a42c5dc61661864e0d1bb9f3fc7524149 Mon Sep 17 00:00:00 2001 From: Andreas Bergmeier Date: Mon, 22 Aug 2022 15:10:59 +0200 Subject: [PATCH] Add dockerimage.BuildFromContainerfile --- CHANGELOG.md | 20 ++++++++++++++----- pkg/dockerimage/build.go | 17 ++++++++++++++++ pkg/dockerimage/exec.go | 42 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 pkg/dockerimage/build.go create mode 100644 pkg/dockerimage/exec.go diff --git a/CHANGELOG.md b/CHANGELOG.md index a2e7bf3..99efb1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,16 @@ +# Changelog -# v0.0.4 -- Add initial `gke` package. -- Add function `AddAuthForCluster` for having local config. -- Add function `StreamContainerLog` for delegating a containers remote log to local files. -- Add new type `PodRun` for executing a Pod for testing. +## v0.0.5 - Migrate `dockerimage` to _sherlock-mavrodi_ + +- Add function `dockerimage.BuildFromContainerfile` for building Container Image. + +**Bugfixes** +- Correct docs of `StreamContainerLog` + + +--- +## v0.0.4 - Migrate `gke` package to _sherlock-mavrodi_ + +- Add function `gke.AddAuthForCluster` for having local config. +- Add function `gke.StreamContainerLog` for delegating a containers remote log to local files. +- Add new type `gke.test.PodRun` for executing a Pod for testing. diff --git a/pkg/dockerimage/build.go b/pkg/dockerimage/build.go new file mode 100644 index 0000000..35236e3 --- /dev/null +++ b/pkg/dockerimage/build.go @@ -0,0 +1,17 @@ +package dockerimage + +import "os" + +// BuildFromContainerfile builds a Container image from a Containerfile. +// Invokes docker via CLI since Docker's dependencies and API are beyond brittle. +func BuildFromContainerfile(path string, tag string) error { + + dir, _ := os.Getwd() + os.Chdir(path) + defer os.Chdir(dir) + // Normally you would want to use a Golang API here. + // Since the Docker API is a very thin shim, badly documented + // and talking to Dockerd or Buildkit has a lot of quirks + // just call the binary here and be done with it. + return executeDockerCommand("build", "-f", "Containerfile", ".", "-t", tag) +} diff --git a/pkg/dockerimage/exec.go b/pkg/dockerimage/exec.go new file mode 100644 index 0000000..5a7a4f8 --- /dev/null +++ b/pkg/dockerimage/exec.go @@ -0,0 +1,42 @@ +package dockerimage + +import ( + "io" + "os" + "os/exec" + "sync" +) + +// executeDockerCommandWithStdout calls docker CLI with `arg`. +// Ensures that output is written to provided `io.Writer`. +func executeDockerCommandWithStdout(stdout io.Writer, arg ...string) error { + cmd := exec.Command("docker", arg...) + errReader, err := cmd.StderrPipe() + if err != nil { + return err + } + defer errReader.Close() + outReader, err := cmd.StdoutPipe() + if err != nil { + return err + } + defer outReader.Close() + wg := sync.WaitGroup{} + wg.Add(2) + go func() { + defer wg.Done() + io.Copy(os.Stderr, errReader) + }() + go func() { + defer wg.Done() + io.Copy(stdout, outReader) + }() + err = cmd.Run() + wg.Wait() + return err +} + +// executeDockerCommand call docker CLI with `arg`. +func executeDockerCommand(arg ...string) error { + return executeDockerCommandWithStdout(os.Stdout, arg...) +}