From f6b9dc1717dd9bb77ec3b74d7fd0b5cfdae17f17 Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Mon, 10 Jan 2022 21:18:27 +0200 Subject: [PATCH] replace Build many args with opts struct Signed-off-by: Avi Deitcher --- src/cmd/linuxkit/build.go | 2 +- src/cmd/linuxkit/moby/build.go | 22 +++++++++++----------- src/cmd/linuxkit/moby/image.go | 8 ++++---- src/cmd/linuxkit/moby/linuxkit.go | 2 +- src/cmd/linuxkit/moby/opts.go | 11 +++++++++++ 5 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 src/cmd/linuxkit/moby/opts.go diff --git a/src/cmd/linuxkit/build.go b/src/cmd/linuxkit/build.go index 06b9e6440..88f3573ba 100644 --- a/src/cmd/linuxkit/build.go +++ b/src/cmd/linuxkit/build.go @@ -204,7 +204,7 @@ func build(args []string) { if moby.Streamable(buildFormats[0]) { tp = buildFormats[0] } - err = moby.Build(m, w, *buildPull, tp, *buildDecompressKernel, cacheDir, *buildDocker, *buildArch) + err = moby.Build(m, w, moby.BuildOpts{Pull: *buildPull, BuilderType: tp, DecompressKernel: *buildDecompressKernel, CacheDir: cacheDir, DockerCache: *buildDocker, Arch: *buildArch}) if err != nil { log.Fatalf("%v", err) } diff --git a/src/cmd/linuxkit/moby/build.go b/src/cmd/linuxkit/moby/build.go index 4c21b50d6..af7f3de7e 100644 --- a/src/cmd/linuxkit/moby/build.go +++ b/src/cmd/linuxkit/moby/build.go @@ -83,14 +83,14 @@ func OutputTypes() []string { return ts } -func outputImage(image *Image, section string, prefix string, m Moby, idMap map[string]uint32, dupMap map[string]string, pull bool, iw *tar.Writer, cacheDir string, dockerCache bool, arch string) error { +func outputImage(image *Image, section string, prefix string, m Moby, idMap map[string]uint32, dupMap map[string]string, iw *tar.Writer, opts BuildOpts) error { log.Infof(" Create OCI config for %s", image.Image) imageName := util.ReferenceExpand(image.Image) ref, err := reference.Parse(imageName) if err != nil { return fmt.Errorf("could not resolve references for image %s: %v", image.Image, err) } - src, err := imagePull(&ref, pull, cacheDir, dockerCache, arch) + src, err := imagePull(&ref, opts.Pull, opts.CacheDir, opts.DockerCache, opts.Arch) if err != nil { return fmt.Errorf("Could not pull image %s: %v", image.Image, err) } @@ -108,7 +108,7 @@ func outputImage(image *Image, section string, prefix string, m Moby, idMap map[ } path := path.Join("containers", section, prefix+image.Name) readonly := oci.Root.Readonly - err = ImageBundle(path, image.ref, config, runtime, iw, pull, readonly, dupMap, cacheDir, dockerCache, arch) + err = ImageBundle(path, image.ref, config, runtime, iw, readonly, dupMap, opts) if err != nil { return fmt.Errorf("Failed to extract root filesystem for %s: %v", image.Image, err) } @@ -116,7 +116,7 @@ func outputImage(image *Image, section string, prefix string, m Moby, idMap map[ } // Build performs the actual build process -func Build(m Moby, w io.Writer, pull bool, tp string, decompressKernel bool, cacheDir string, dockerCache bool, arch string) error { +func Build(m Moby, w io.Writer, opts BuildOpts) error { if MobyDir == "" { MobyDir = defaultMobyConfigDir() } @@ -129,7 +129,7 @@ func Build(m Moby, w io.Writer, pull bool, tp string, decompressKernel bool, cac iw := tar.NewWriter(w) // add additions - addition := additions[tp] + addition := additions[opts.BuilderType] // allocate each container a uid, gid that can be referenced by name idMap := map[string]uint32{} @@ -153,8 +153,8 @@ func Build(m Moby, w io.Writer, pull bool, tp string, decompressKernel bool, cac if m.Kernel.ref != nil { // get kernel and initrd tarball and ucode cpio archive from container log.Infof("Extract kernel image: %s", m.Kernel.ref) - kf := newKernelFilter(iw, m.Kernel.Cmdline, m.Kernel.Binary, m.Kernel.Tar, m.Kernel.UCode, decompressKernel) - err := ImageTar(m.Kernel.ref, "", kf, pull, "", cacheDir, dockerCache, arch) + kf := newKernelFilter(iw, m.Kernel.Cmdline, m.Kernel.Binary, m.Kernel.Tar, m.Kernel.UCode, opts.DecompressKernel) + err := ImageTar(m.Kernel.ref, "", kf, "", opts) if err != nil { return fmt.Errorf("Failed to extract kernel image and tarball: %v", err) } @@ -170,7 +170,7 @@ func Build(m Moby, w io.Writer, pull bool, tp string, decompressKernel bool, cac } for _, ii := range m.initRefs { log.Infof("Process init image: %s", ii) - err := ImageTar(ii, "", iw, pull, resolvconfSymlink, cacheDir, dockerCache, arch) + err := ImageTar(ii, "", iw, resolvconfSymlink, opts) if err != nil { return fmt.Errorf("Failed to build init tarball from %s: %v", ii, err) } @@ -181,7 +181,7 @@ func Build(m Moby, w io.Writer, pull bool, tp string, decompressKernel bool, cac } for i, image := range m.Onboot { so := fmt.Sprintf("%03d", i) - if err := outputImage(image, "onboot", so+"-", m, idMap, dupMap, pull, iw, cacheDir, dockerCache, arch); err != nil { + if err := outputImage(image, "onboot", so+"-", m, idMap, dupMap, iw, opts); err != nil { return err } } @@ -191,7 +191,7 @@ func Build(m Moby, w io.Writer, pull bool, tp string, decompressKernel bool, cac } for i, image := range m.Onshutdown { so := fmt.Sprintf("%03d", i) - if err := outputImage(image, "onshutdown", so+"-", m, idMap, dupMap, pull, iw, cacheDir, dockerCache, arch); err != nil { + if err := outputImage(image, "onshutdown", so+"-", m, idMap, dupMap, iw, opts); err != nil { return err } } @@ -200,7 +200,7 @@ func Build(m Moby, w io.Writer, pull bool, tp string, decompressKernel bool, cac log.Infof("Add service containers:") } for _, image := range m.Services { - if err := outputImage(image, "services", "", m, idMap, dupMap, pull, iw, cacheDir, dockerCache, arch); err != nil { + if err := outputImage(image, "services", "", m, idMap, dupMap, iw, opts); err != nil { return err } } diff --git a/src/cmd/linuxkit/moby/image.go b/src/cmd/linuxkit/moby/image.go index cc676b384..7baefe0ec 100644 --- a/src/cmd/linuxkit/moby/image.go +++ b/src/cmd/linuxkit/moby/image.go @@ -172,7 +172,7 @@ func tarPrefix(path string, tw tarWriter) error { } // ImageTar takes a Docker image and outputs it to a tar stream -func ImageTar(ref *reference.Spec, prefix string, tw tarWriter, pull bool, resolv, cacheDir string, dockerCache bool, architecture string) (e error) { +func ImageTar(ref *reference.Spec, prefix string, tw tarWriter, resolv string, opts BuildOpts) (e error) { log.Debugf("image tar: %s %s", ref, prefix) if prefix != "" && prefix[len(prefix)-1] != '/' { return fmt.Errorf("prefix does not end with /: %s", prefix) @@ -185,7 +185,7 @@ func ImageTar(ref *reference.Spec, prefix string, tw tarWriter, pull bool, resol // pullImage first checks in the cache, then pulls the image. // If pull==true, then it always tries to pull from registry. - src, err := imagePull(ref, pull, cacheDir, dockerCache, architecture) + src, err := imagePull(ref, opts.Pull, opts.CacheDir, opts.DockerCache, opts.Arch) if err != nil { return fmt.Errorf("Could not pull image %s: %v", ref, err) } @@ -318,7 +318,7 @@ func ImageTar(ref *reference.Spec, prefix string, tw tarWriter, pull bool, resol } // ImageBundle produces an OCI bundle at the given path in a tarball, given an image and a config.json -func ImageBundle(prefix string, ref *reference.Spec, config []byte, runtime Runtime, tw tarWriter, pull bool, readonly bool, dupMap map[string]string, cacheDir string, dockerCache bool, architecture string) error { // nolint: lll +func ImageBundle(prefix string, ref *reference.Spec, config []byte, runtime Runtime, tw tarWriter, readonly bool, dupMap map[string]string, opts BuildOpts) error { // nolint: lll // if read only, just unpack in rootfs/ but otherwise set up for overlay rootExtract := "rootfs" if !readonly { @@ -329,7 +329,7 @@ func ImageBundle(prefix string, ref *reference.Spec, config []byte, runtime Runt root := path.Join(prefix, rootExtract) var foundElsewhere = dupMap[ref.String()] != "" if !foundElsewhere { - if err := ImageTar(ref, root+"/", tw, pull, "", cacheDir, dockerCache, architecture); err != nil { + if err := ImageTar(ref, root+"/", tw, "", opts); err != nil { return err } dupMap[ref.String()] = root diff --git a/src/cmd/linuxkit/moby/linuxkit.go b/src/cmd/linuxkit/moby/linuxkit.go index 93bfdd3f3..175c7bb25 100644 --- a/src/cmd/linuxkit/moby/linuxkit.go +++ b/src/cmd/linuxkit/moby/linuxkit.go @@ -57,7 +57,7 @@ func ensureLinuxkitImage(name, cache string) error { return err } defer os.Remove(tf.Name()) - if err := Build(m, tf, false, "", false, cache, true, arch); err != nil { + if err := Build(m, tf, BuildOpts{Pull: false, BuilderType: "", DecompressKernel: false, CacheDir: cache, DockerCache: true, Arch: arch}); err != nil { return err } if err := tf.Close(); err != nil { diff --git a/src/cmd/linuxkit/moby/opts.go b/src/cmd/linuxkit/moby/opts.go new file mode 100644 index 000000000..6f561689b --- /dev/null +++ b/src/cmd/linuxkit/moby/opts.go @@ -0,0 +1,11 @@ +package moby + +// BuildOpts options that control the linuxkit build process +type BuildOpts struct { + Pull bool + BuilderType string + DecompressKernel bool + CacheDir string + DockerCache bool + Arch string +}