Skip to content

Commit

Permalink
cleanup image building a little
Browse files Browse the repository at this point in the history
this code is still gnarly
  • Loading branch information
BenTheElder committed May 10, 2022
1 parent f71bb1f commit 0b55550
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 52 deletions.
68 changes: 16 additions & 52 deletions pkg/build/nodeimage/buildcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/exec"
"sigs.k8s.io/kind/pkg/fs"
"sigs.k8s.io/kind/pkg/log"

"sigs.k8s.io/kind/pkg/build/nodeimage/internal/container/docker"
Expand Down Expand Up @@ -87,15 +86,10 @@ func (c *buildContext) buildImage(bits kube.Bits) error {
return err
}

c.logger.V(0).Info("Building in " + containerID)

// helper we will use to run "build steps"
execInBuild := func(command string, args ...string) error {
return exec.InheritOutput(cmder.Command(command, args...)).Run()
}
c.logger.V(0).Info("Building in container: " + containerID)

// make artifacts directory
if err = execInBuild("mkdir", "/kind/"); err != nil {
if err = cmder.Command("mkdir", "/kind/").Run(); err != nil {
c.logger.Errorf("Image build Failed! Failed to make directory %v", err)
return err
}
Expand All @@ -108,48 +102,40 @@ func (c *buildContext) buildImage(bits kube.Bits) error {
if err := exec.Command("docker", "cp", binary, containerID+":"+nodePath).Run(); err != nil {
return err
}
if err := execInBuild("chmod", "+x", nodePath); err != nil {
if err := cmder.Command("chmod", "+x", nodePath).Run(); err != nil {
return err
}
if err := execInBuild("chown", "root:root", nodePath); err != nil {
if err := cmder.Command("chown", "root:root", nodePath).Run(); err != nil {
return err
}
}

// write version
// TODO: support grabbing version from a binary instead
// TODO: support grabbing version from a binary instead?
// This may or may not be a good idea ...
if err := createFile(cmder, "/kind/version", bits.Version()); err != nil {
return err
}

dir, err := fs.TempDir("", "kind-build")
if err != nil {
return err
}
defer func() {
_ = os.RemoveAll(dir)
}()

// pre-pull images that were not part of the build
if _, err = c.prePullImages(bits, dir, containerID); err != nil {
// pre-pull images that were not part of the build and write CNI / storage
// manifests
if _, err = c.prePullImagesAndWriteManifests(bits, containerID); err != nil {
c.logger.Errorf("Image build Failed! Failed to pull Images: %v", err)
return err
}

// Save the image changes to a new image
cmd := exec.Command(
if err = exec.Command(
"docker", "commit",
// we need to put this back after changing it when running the image
"--change", `ENTRYPOINT [ "/usr/local/bin/entrypoint", "/sbin/init" ]`,
containerID, c.image,
)
exec.InheritOutput(cmd)
if err = cmd.Run(); err != nil {
).Run(); err != nil {
c.logger.Errorf("Image build Failed! Failed to save image: %v", err)
return err
}

c.logger.V(0).Info("Image build completed.")
c.logger.V(0).Infof("Image %q build completed.", c.image)
return nil
}

Expand All @@ -167,7 +153,7 @@ func (c *buildContext) getBuiltImages(bits kube.Bits) (sets.String, error) {
}

// must be run after kubernetes has been installed on the node
func (c *buildContext) prePullImages(bits kube.Bits, dir, containerID string) ([]string, error) {
func (c *buildContext) prePullImagesAndWriteManifests(bits kube.Bits, containerID string) ([]string, error) {
// first get the images we actually built
builtImages, err := c.getBuiltImages(bits)
if err != nil {
Expand All @@ -178,20 +164,9 @@ func (c *buildContext) prePullImages(bits kube.Bits, dir, containerID string) ([
// helpers to run things in the build container
cmder := docker.ContainerCmder(containerID)

// get the Kubernetes version we installed on the node
// we need this to ask kubeadm what images we need
rawVersion, err := exec.OutputLines(cmder.Command("cat", kubernetesVersionLocation))
if err != nil {
c.logger.Errorf("Image build Failed! Failed to get Kubernetes version: %v", err)
return nil, err
}
if len(rawVersion) != 1 {
c.logger.Errorf("Image build Failed! Failed to get Kubernetes version: %v", err)
return nil, errors.New("invalid kubernetes version file")
}

// parse version for comparison
ver, err := version.ParseSemantic(rawVersion[0])
rawVersion := bits.Version()
ver, err := version.ParseSemantic(rawVersion)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -224,7 +199,7 @@ func (c *buildContext) prePullImages(bits kube.Bits, dir, containerID string) ([

// gets the list of images required by kubeadm
requiredImages, err := exec.OutputLines(cmder.Command(
"kubeadm", "config", "images", "list", "--kubernetes-version", rawVersion[0],
"kubeadm", "config", "images", "list", "--kubernetes-version", rawVersion,
))
if err != nil {
return nil, err
Expand Down Expand Up @@ -269,13 +244,6 @@ func (c *buildContext) prePullImages(bits kube.Bits, dir, containerID string) ([
// all builds should install the default storage driver images currently
requiredImages = append(requiredImages, defaultStorageImages...)

// Create "images" subdir.
imagesDir := path.Join(dir, "bits", "images")
if err := os.MkdirAll(imagesDir, 0777); err != nil {
c.logger.Errorf("Image build Failed! Failed create local images dir: %v", err)
return nil, errors.Wrap(err, "failed to make images dir")
}

// setup image importer
importer := newContainerdImporter(cmder)
if err := importer.Prepare(); err != nil {
Expand Down Expand Up @@ -371,7 +339,3 @@ func (c *buildContext) createBuildContainer() (id string, err error) {
}
return id, nil
}

func dockerBuildOsAndArch(arch string) string {
return "linux/" + arch
}
4 changes: 4 additions & 0 deletions pkg/build/nodeimage/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ func findSandboxImage(config string) (string, error) {
}
return match[1], nil
}

func dockerBuildOsAndArch(arch string) string {
return "linux/" + arch
}

0 comments on commit 0b55550

Please sign in to comment.