Skip to content

Instantly share code, notes, and snippets.

@smothiki
Created May 27, 2021 22:35
Show Gist options
  • Save smothiki/4b27129d32d4e34f66fee225f16dad6a to your computer and use it in GitHub Desktop.
Save smothiki/4b27129d32d4e34f66fee225f16dad6a to your computer and use it in GitHub Desktop.
imagehandler.go
kanikoCmdStr := fmt.Sprintf("executor --context %s --dockerfile %s --skip-tls-verify --destination %s",
tmpDir, dockerBuildFile, dockerImageTag)
package imagehandler
import (
"fmt"
"os"
)
const (
DOCKER = "docker"
BUILDAH = "buildah"
BUILDER = "builder"
dockersock = "/var/run/docker.sock"
)
func ImageCmdFactory() ImageCmdHandler {
if !DockerSockExists() {
if os.Getenv(BUILDER) == "IMG" {
return Docker{
tool: "img",
debug: true,
}
}
insecureRegistry := os.Getenv("insecure_registry") == "true"
privileged := os.Getenv("storagedriver") != "vfs"
return Buildah{
InsecureRegistry: insecureRegistry,
Privileged: privileged,
}
}
return Docker{
tool: "docker",
}
}
func DockerSockExists() bool {
_, err := os.Stat(dockersock)
if os.IsNotExist(err) {
return false
}
return true
}
type ImageCmdHandler interface {
BuildCmd(dockerBuildFile string, buildUUID string, contextDir string) string
TagCmd(imagename, tagname string) string
PushCmd(imagename string) string
}
type Docker struct {
tool string
debug bool
}
func (b Docker) BuildCmd(dockerBuildFile string, buildUUID string, contextDir string) string {
command := ""
if !b.debug {
command = fmt.Sprintf("%s build --network=host -t %s %s -f %s", b.tool,
buildUUID, contextDir, dockerBuildFile)
} else {
command = fmt.Sprintf("%s build -t %s %s -f %s", b.tool,
buildUUID, contextDir, dockerBuildFile)
}
if b.debug {
return command + " --debug"
}
return command
}
func (b Docker) TagCmd(imagename string, tagname string) string {
command := fmt.Sprintf("%s tag %s %s", b.tool, imagename, tagname)
if b.debug {
return command + " --debug"
}
return command
}
func (b Docker) PushCmd(imagename string) string {
command := fmt.Sprintf("%s push %s", b.tool, imagename)
if b.debug {
return command + " --debug"
}
return command
}
type Buildah struct {
InsecureRegistry bool
Privileged bool
}
func (b Buildah) BuildCmd(dockerBuildFile string, buildUUID string, contextDir string) string {
buildah := BUILDAH + " "
if !b.Privileged {
buildah = buildah + "--storage-driver vfs "
}
return fmt.Sprintf("%s bud --isolation chroot --format=docker -f %s -t %s %s",
buildah, dockerBuildFile, buildUUID, contextDir)
}
func (b Buildah) TagCmd(imagename string, tagname string) string {
buildah := BUILDAH + " "
if !b.Privileged {
buildah = buildah + "--storage-driver vfs "
}
return fmt.Sprintf("%s tag %s %s", buildah, imagename, tagname)
}
func (b Buildah) PushCmd(imagename string) string {
buildah := BUILDAH + " "
if !b.Privileged {
buildah = buildah + "--storage-driver vfs "
}
buildahpushstr := fmt.Sprintf("%s push ", buildah)
if b.InsecureRegistry {
buildahpushstr = buildahpushstr + "--tls-verify=false"
}
return fmt.Sprintf("%s %s", buildahpushstr, imagename)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment