Files
kind/pkg/build/base_image.go

128 lines
3.4 KiB
Go
Raw Normal View History

2018-07-23 10:06:37 -07:00
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package build implements functionality to build the kind images
// TODO(bentheelder): and k8s
package build
import (
"io/ioutil"
"os"
"path/filepath"
2018-08-29 16:01:49 -07:00
log "github.com/sirupsen/logrus"
2018-08-06 12:35:02 -07:00
"sigs.k8s.io/kind/pkg/build/sources"
"sigs.k8s.io/kind/pkg/exec"
2018-07-23 10:06:37 -07:00
)
2018-08-22 11:04:51 -07:00
// BaseImageBuildContext is used to build the kind node base image, and contains
2018-07-23 10:06:37 -07:00
// build configuration
2018-08-22 11:04:51 -07:00
type BaseImageBuildContext struct {
2018-07-23 10:06:37 -07:00
SourceDir string
ImageTag string
GoCmd string
Arch string
}
2018-08-22 11:04:51 -07:00
// NewBaseImageBuildContext creates a new BaseImageBuildContext with
2018-07-23 10:06:37 -07:00
// default configuration
func NewBaseImageBuildContext(imageName string) *BaseImageBuildContext {
2018-08-22 11:04:51 -07:00
return &BaseImageBuildContext{
ImageTag: imageName,
2018-07-23 10:06:37 -07:00
GoCmd: "go",
Arch: "amd64",
}
}
// Build builds the cluster node image, the sourcedir must be set on
// the NodeImageBuildContext
2018-08-22 11:04:51 -07:00
func (c *BaseImageBuildContext) Build() (err error) {
2018-07-23 10:06:37 -07:00
// create tempdir to build in
2018-08-22 14:00:14 -07:00
tmpDir, err := ioutil.TempDir("", "kind-base-image")
2018-07-23 10:06:37 -07:00
if err != nil {
return err
}
2018-08-06 12:35:02 -07:00
defer os.RemoveAll(tmpDir)
2018-07-23 10:06:37 -07:00
// populate with image sources
2018-08-06 12:35:02 -07:00
// if SourceDir is unset, use the baked in sources
buildDir := tmpDir
if c.SourceDir == "" {
// populate with image sources
2018-08-22 11:04:51 -07:00
err = sources.RestoreAssets(buildDir, "images/base")
2018-08-06 12:35:02 -07:00
if err != nil {
return err
}
2018-08-22 11:04:51 -07:00
buildDir = filepath.Join(buildDir, "images", "base")
2018-08-06 12:35:02 -07:00
} else {
err = copyDir(c.SourceDir, buildDir)
if err != nil {
2018-08-29 16:01:49 -07:00
log.Errorf("failed to copy sources to build dir %v", err)
2018-08-06 12:35:02 -07:00
return err
}
2018-07-23 10:06:37 -07:00
}
2018-08-29 16:01:49 -07:00
log.Infof("Building base image in: %s", buildDir)
2018-07-23 10:06:37 -07:00
2018-08-06 17:05:46 -07:00
// build the entrypoint binary first
if err := c.buildEntrypoint(buildDir); err != nil {
return err
}
// then the actual docker image
return c.buildImage(buildDir)
}
// builds the entrypoint binary
2018-08-22 11:04:51 -07:00
func (c *BaseImageBuildContext) buildEntrypoint(dir string) error {
2018-07-23 10:06:37 -07:00
// NOTE: this binary only uses the go1 stdlib, and is a single file
2018-08-06 17:05:46 -07:00
entrypointSrc := filepath.Join(dir, "entrypoint", "main.go")
entrypointDest := filepath.Join(dir, "entrypoint", "entrypoint")
2018-07-23 10:06:37 -07:00
cmd := exec.Command(c.GoCmd, "build", "-o", entrypointDest, entrypointSrc)
// TODO(bentheelder): we may need to map between docker image arch and GOARCH
cmd.Env = []string{"GOOS=linux", "GOARCH=" + c.Arch}
2018-08-06 17:05:46 -07:00
// actually build
2018-08-29 16:01:49 -07:00
log.Info("Building entrypoint binary ...")
2018-08-06 17:05:46 -07:00
cmd.Debug = true
cmd.InheritOutput = true
if err := cmd.Run(); err != nil {
2018-08-29 16:01:49 -07:00
log.Errorf("Entrypoint build Failed! %v", err)
2018-07-23 10:06:37 -07:00
return err
}
2018-08-29 16:01:49 -07:00
log.Info("Entrypoint build completed.")
2018-08-06 17:05:46 -07:00
return nil
}
2018-07-23 10:06:37 -07:00
2018-08-22 11:04:51 -07:00
func (c *BaseImageBuildContext) buildImage(dir string) error {
2018-07-23 10:06:37 -07:00
// build the image, tagged as tagImageAs, using the our tempdir as the context
2018-08-06 17:05:46 -07:00
cmd := exec.Command("docker", "build", "-t", c.ImageTag, dir)
cmd.Debug = true
cmd.InheritOutput = true
2018-08-29 16:01:49 -07:00
log.Info("Starting Docker build ...")
2018-08-06 17:05:46 -07:00
err := cmd.Run()
2018-07-23 10:06:37 -07:00
if err != nil {
2018-08-29 16:01:49 -07:00
log.Errorf("Docker build Failed! %v", err)
2018-07-23 10:06:37 -07:00
return err
}
2018-08-29 16:01:49 -07:00
log.Info("Docker build completed.")
2018-07-23 10:06:37 -07:00
return nil
}