From 379617ca0da1f6a76319f06990bf821eb6f4020a Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Tue, 7 May 2024 19:31:11 +0300 Subject: [PATCH] move moby components that do not have runtime dependencies to own directory Signed-off-by: Avi Deitcher --- src/cmd/linuxkit/build.go | 21 +++--- src/cmd/linuxkit/moby/apk_tarwriter.go | 2 +- src/cmd/linuxkit/moby/{ => build}/build.go | 68 +++++++++++-------- .../linuxkit/moby/{util.go => build/dir.go} | 2 +- src/cmd/linuxkit/moby/{ => build}/docker.go | 2 +- src/cmd/linuxkit/moby/{ => build}/image.go | 45 ++++++------ src/cmd/linuxkit/moby/{ => build}/images.go | 2 +- src/cmd/linuxkit/moby/{ => build}/images.yaml | 0 src/cmd/linuxkit/moby/{ => build}/linuxkit.go | 5 +- .../linuxkit/moby/{ => build}/mkimage.yaml | 0 src/cmd/linuxkit/moby/{ => build}/opts.go | 2 +- src/cmd/linuxkit/moby/{ => build}/output.go | 2 +- src/cmd/linuxkit/moby/{ => build}/sbom.go | 2 +- src/cmd/linuxkit/moby/config.go | 22 ++++-- src/cmd/linuxkit/moby/const.go | 9 +++ 15 files changed, 105 insertions(+), 79 deletions(-) rename src/cmd/linuxkit/moby/{ => build}/build.go (90%) rename src/cmd/linuxkit/moby/{util.go => build/dir.go} (97%) rename src/cmd/linuxkit/moby/{ => build}/docker.go (99%) rename src/cmd/linuxkit/moby/{ => build}/image.go (91%) rename src/cmd/linuxkit/moby/{ => build}/images.go (99%) rename src/cmd/linuxkit/moby/{ => build}/images.yaml (100%) rename src/cmd/linuxkit/moby/{ => build}/linuxkit.go (96%) rename src/cmd/linuxkit/moby/{ => build}/mkimage.yaml (100%) rename src/cmd/linuxkit/moby/{ => build}/opts.go (95%) rename src/cmd/linuxkit/moby/{ => build}/output.go (99%) rename src/cmd/linuxkit/moby/{ => build}/sbom.go (99%) create mode 100644 src/cmd/linuxkit/moby/const.go diff --git a/src/cmd/linuxkit/build.go b/src/cmd/linuxkit/build.go index 6217e8370..09317d33d 100644 --- a/src/cmd/linuxkit/build.go +++ b/src/cmd/linuxkit/build.go @@ -11,6 +11,7 @@ import ( "strings" "github.com/linuxkit/linuxkit/src/cmd/linuxkit/moby" + mobybuild "github.com/linuxkit/linuxkit/src/cmd/linuxkit/moby/build" "github.com/linuxkit/linuxkit/src/cmd/linuxkit/spec" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -52,7 +53,7 @@ func buildCmd() *cobra.Command { arch string cacheDir flagOverEnvVarOverDefaultString buildFormats formatList - outputTypes = moby.OutputTypes() + outputTypes = mobybuild.OutputTypes() noSbom bool sbomOutputFilename string inputTar string @@ -94,13 +95,13 @@ The generated image can be in one of multiple formats which can be run on variou if len(buildFormats) > 1 { for _, o := range buildFormats { - if moby.Streamable(o) { + if mobybuild.Streamable(o) { return fmt.Errorf("format type %s must be the only format specified", o) } } } - if len(buildFormats) == 1 && moby.Streamable(buildFormats[0]) { + if len(buildFormats) == 1 && mobybuild.Streamable(buildFormats[0]) { if outputFile == "" { outputFile = filepath.Join(dir, name+"."+buildFormats[0]) // stop the errors in the validation below @@ -108,7 +109,7 @@ The generated image can be in one of multiple formats which can be run on variou dir = "" } } else { - err := moby.ValidateFormats(buildFormats, cacheDir.String()) + err := mobybuild.ValidateFormats(buildFormats, cacheDir.String()) if err != nil { return fmt.Errorf("error parsing formats: %v", err) } @@ -129,7 +130,7 @@ The generated image can be in one of multiple formats which can be run on variou if dir != "" { return fmt.Errorf("the -output option cannot be specified with -dir") } - if !moby.Streamable(buildFormats[0]) { + if !mobybuild.Streamable(buildFormats[0]) { return fmt.Errorf("the -output option cannot be specified for build type %s as it cannot be streamed", buildFormats[0]) } if outputFile == "-" { @@ -225,17 +226,17 @@ The generated image can be in one of multiple formats which can be run on variou // this is a weird interface, but currently only streamable types can have additional files // need to split up the base tarball outputs from the secondary stages var tp string - if moby.Streamable(buildFormats[0]) { + if mobybuild.Streamable(buildFormats[0]) { tp = buildFormats[0] } - var sbomGenerator *moby.SbomGenerator + var sbomGenerator *mobybuild.SbomGenerator if !noSbom { - sbomGenerator, err = moby.NewSbomGenerator(sbomOutputFilename, sbomCurrentTime) + sbomGenerator, err = mobybuild.NewSbomGenerator(sbomOutputFilename, sbomCurrentTime) if err != nil { return fmt.Errorf("error creating sbom generator: %v", err) } } - err = moby.Build(m, w, moby.BuildOpts{Pull: pull, BuilderType: tp, DecompressKernel: decompressKernel, CacheDir: cacheDir.String(), DockerCache: docker, Arch: arch, SbomGenerator: sbomGenerator, InputTar: inputTar}) + err = mobybuild.Build(m, w, mobybuild.BuildOpts{Pull: pull, BuilderType: tp, DecompressKernel: decompressKernel, CacheDir: cacheDir.String(), DockerCache: docker, Arch: arch, SbomGenerator: sbomGenerator, InputTar: inputTar}) if err != nil { return fmt.Errorf("%v", err) } @@ -247,7 +248,7 @@ The generated image can be in one of multiple formats which can be run on variou } log.Infof("Create outputs:") - err = moby.Formats(filepath.Join(dir, name), image, buildFormats, size, arch, cacheDir.String()) + err = mobybuild.Formats(filepath.Join(dir, name), image, buildFormats, size, arch, cacheDir.String()) if err != nil { return fmt.Errorf("error writing outputs: %v", err) } diff --git a/src/cmd/linuxkit/moby/apk_tarwriter.go b/src/cmd/linuxkit/moby/apk_tarwriter.go index 4db8cf419..7d22edef8 100644 --- a/src/cmd/linuxkit/moby/apk_tarwriter.go +++ b/src/cmd/linuxkit/moby/apk_tarwriter.go @@ -19,7 +19,7 @@ type apkTarWriter struct { location string } -func newAPKTarWriter(w *tar.Writer, location string) *apkTarWriter { +func NewAPKTarWriter(w *tar.Writer, location string) *apkTarWriter { return &apkTarWriter{ Writer: w, location: location, diff --git a/src/cmd/linuxkit/moby/build.go b/src/cmd/linuxkit/moby/build/build.go similarity index 90% rename from src/cmd/linuxkit/moby/build.go rename to src/cmd/linuxkit/moby/build/build.go index 47c9904f9..10c8fde00 100644 --- a/src/cmd/linuxkit/moby/build.go +++ b/src/cmd/linuxkit/moby/build/build.go @@ -1,4 +1,4 @@ -package moby +package build import ( "archive/tar" @@ -18,6 +18,7 @@ import ( "github.com/containerd/containerd/reference" // drop-in 100% compatible replacement and 17% faster than compress/gzip. gzip "github.com/klauspost/pgzip" + "github.com/linuxkit/linuxkit/src/cmd/linuxkit/moby" "github.com/linuxkit/linuxkit/src/cmd/linuxkit/util" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" @@ -83,7 +84,7 @@ func OutputTypes() []string { return ts } -func outputImage(image *Image, section string, index int, prefix string, m Moby, idMap map[string]uint32, dupMap map[string]string, iw *tar.Writer, opts BuildOpts) error { +func outputImage(image *moby.Image, section string, index int, prefix string, m moby.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) @@ -98,7 +99,7 @@ func outputImage(image *Image, section string, index int, prefix string, m Moby, if err != nil { return fmt.Errorf("failed to retrieve config for %s: %v", image.Image, err) } - oci, runtime, err := ConfigToOCI(image, configRaw, idMap) + oci, runtime, err := moby.ConfigToOCI(image, configRaw, idMap) if err != nil { return fmt.Errorf("failed to create OCI spec for %s: %v", image.Image, err) } @@ -108,7 +109,7 @@ func outputImage(image *Image, section string, index int, prefix string, m Moby, } path := path.Join("containers", section, prefix+image.Name) readonly := oci.Root.Readonly - err = ImageBundle(path, fmt.Sprintf("%s[%d]", section, index), image.ref, config, runtime, iw, readonly, dupMap, opts) + err = ImageBundle(path, fmt.Sprintf("%s[%d]", section, index), 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) } @@ -117,7 +118,7 @@ func outputImage(image *Image, section string, index int, prefix string, m Moby, // Build performs the actual build process. The output is the filesystem // in a tar stream written to w. -func Build(m Moby, w io.Writer, opts BuildOpts) error { +func Build(m moby.Moby, w io.Writer, opts BuildOpts) error { if MobyDir == "" { MobyDir = defaultMobyConfigDir() } @@ -138,7 +139,7 @@ func Build(m Moby, w io.Writer, opts BuildOpts) error { } } var ( - oldConfig *Moby + oldConfig *moby.Moby in *os.File err error ) @@ -167,7 +168,7 @@ func Build(m Moby, w io.Writer, opts BuildOpts) error { if _, err := buf.ReadFrom(inputTarReader); err != nil { return fmt.Errorf("failed to read metadata file from input tar: %w", err) } - config, err := NewConfig(buf.Bytes(), nil) + config, err := moby.NewConfig(buf.Bytes(), nil) if err != nil { return fmt.Errorf("invalid config in existing tar file: %v", err) } @@ -202,18 +203,22 @@ func Build(m Moby, w io.Writer, opts BuildOpts) error { // deduplicate containers with the same image dupMap := map[string]string{} - if m.Kernel.ref != nil { + kernelRef := m.Kernel.Ref() + var oldKernelRef *reference.Spec + if oldConfig != nil { + oldKernelRef = oldConfig.Kernel.Ref() + } + if kernelRef != nil { // first check if the existing one had it - //if config != nil && len(oldConfig.initRefs) > index+1 && oldConfig.initRefs[index].String() == image { - if oldConfig != nil && oldConfig.Kernel.ref != nil && oldConfig.Kernel.ref.String() == m.Kernel.ref.String() { - if err := extractPackageFilesFromTar(in, iw, m.Kernel.ref.String(), "kernel"); err != nil { + if oldKernelRef != nil && oldKernelRef.String() == kernelRef.String() { + if err := extractPackageFilesFromTar(in, iw, kernelRef.String(), "kernel"); err != nil { return err } } else { // get kernel and initrd tarball and ucode cpio archive from container - log.Infof("Extract kernel image: %s", m.Kernel.ref) - kf := newKernelFilter(m.Kernel.ref, iw, m.Kernel.Cmdline, m.Kernel.Binary, m.Kernel.Tar, m.Kernel.UCode, opts.DecompressKernel) - err := ImageTar("kernel", m.Kernel.ref, "", kf, "", opts) + log.Infof("Extract kernel image: %s", m.Kernel.Ref()) + kf := newKernelFilter(kernelRef, iw, m.Kernel.Cmdline, m.Kernel.Binary, m.Kernel.Tar, m.Kernel.UCode, opts.DecompressKernel) + err := ImageTar("kernel", kernelRef, "", kf, "", opts) if err != nil { return fmt.Errorf("failed to extract kernel image and tarball: %v", err) } @@ -228,9 +233,14 @@ func Build(m Moby, w io.Writer, opts BuildOpts) error { if len(m.Init) != 0 { log.Infof("Add init containers:") } - apkTar := newAPKTarWriter(iw, "init") - for i, ii := range m.initRefs { - if oldConfig != nil && len(oldConfig.initRefs) > i && oldConfig.initRefs[i].String() == ii.String() { + apkTar := moby.NewAPKTarWriter(iw, "init") + initRefs := m.InitRefs() + var oldInitRefs []*reference.Spec + if oldConfig != nil { + oldInitRefs = oldConfig.InitRefs() + } + for i, ii := range initRefs { + if len(oldInitRefs) > i && oldInitRefs[i].String() == ii.String() { if err := extractPackageFilesFromTar(in, apkTar, ii.String(), fmt.Sprintf("init[%d]", i)); err != nil { return err } @@ -521,8 +531,8 @@ func tarAppend(ref *reference.Spec, iw *tar.Writer, tr *tar.Reader) error { if hdr.PAXRecords == nil { hdr.PAXRecords = make(map[string]string) } - hdr.PAXRecords[PaxRecordLinuxkitSource] = ref.String() - hdr.PAXRecords[PaxRecordLinuxkitLocation] = "kernel" + hdr.PAXRecords[moby.PaxRecordLinuxkitSource] = ref.String() + hdr.PAXRecords[moby.PaxRecordLinuxkitLocation] = "kernel" err = iw.WriteHeader(hdr) if err != nil { return err @@ -615,9 +625,9 @@ func gunzip(src *bytes.Buffer) (*bytes.Buffer, error) { } // this allows inserting metadata into a file in the image -func metadata(m Moby, md string) ([]byte, error) { +func metadata(m moby.Moby, md string) ([]byte, error) { // Make sure the Image strings are update to date with the refs - updateImages(&m) + moby.UpdateImages(&m) switch md { case "json": return json.MarshalIndent(m, "", " ") @@ -628,7 +638,7 @@ func metadata(m Moby, md string) ([]byte, error) { } } -func filesystem(m Moby, tw *tar.Writer, idMap map[string]uint32) error { +func filesystem(m moby.Moby, tw *tar.Writer, idMap map[string]uint32) error { // TODO also include the files added in other parts of the build var addedFiles = map[string]bool{} @@ -666,11 +676,11 @@ func filesystem(m Moby, tw *tar.Writer, idMap map[string]uint32) error { dirMode |= 0001 } - uid, err := idNumeric(f.UID, idMap) + uid, err := moby.IDNumeric(f.UID, idMap) if err != nil { return err } - gid, err := idNumeric(f.GID, idMap) + gid, err := moby.IDNumeric(f.GID, idMap) if err != nil { return err } @@ -740,8 +750,8 @@ func filesystem(m Moby, tw *tar.Writer, idMap map[string]uint32) error { Gid: int(gid), Format: tar.FormatPAX, PAXRecords: map[string]string{ - PaxRecordLinuxkitSource: "linuxkit.files", - PaxRecordLinuxkitLocation: fmt.Sprintf("files[%d]", filecount), + moby.PaxRecordLinuxkitSource: "linuxkit.files", + moby.PaxRecordLinuxkitLocation: fmt.Sprintf("files[%d]", filecount), }, } err := tw.WriteHeader(hdr) @@ -760,8 +770,8 @@ func filesystem(m Moby, tw *tar.Writer, idMap map[string]uint32) error { Gid: int(gid), Format: tar.FormatPAX, PAXRecords: map[string]string{ - PaxRecordLinuxkitSource: "linuxkit.files", - PaxRecordLinuxkitLocation: fmt.Sprintf("files[%d]", filecount), + moby.PaxRecordLinuxkitSource: "linuxkit.files", + moby.PaxRecordLinuxkitLocation: fmt.Sprintf("files[%d]", filecount), }, } if f.Directory { @@ -815,7 +825,7 @@ func extractPackageFilesFromTar(inTar *os.File, tw tarWriter, image, section str if hdr.PAXRecords == nil { continue } - if hdr.PAXRecords[PaxRecordLinuxkitSource] == image && hdr.PAXRecords[PaxRecordLinuxkitLocation] == section { + if hdr.PAXRecords[moby.PaxRecordLinuxkitSource] == image && hdr.PAXRecords[moby.PaxRecordLinuxkitLocation] == section { if err := tw.WriteHeader(hdr); err != nil { return fmt.Errorf("failed to write header: %w", err) } diff --git a/src/cmd/linuxkit/moby/util.go b/src/cmd/linuxkit/moby/build/dir.go similarity index 97% rename from src/cmd/linuxkit/moby/util.go rename to src/cmd/linuxkit/moby/build/dir.go index 3c8996029..db794c31b 100644 --- a/src/cmd/linuxkit/moby/util.go +++ b/src/cmd/linuxkit/moby/build/dir.go @@ -1,4 +1,4 @@ -package moby +package build import ( "path/filepath" diff --git a/src/cmd/linuxkit/moby/docker.go b/src/cmd/linuxkit/moby/build/docker.go similarity index 99% rename from src/cmd/linuxkit/moby/docker.go rename to src/cmd/linuxkit/moby/build/docker.go index 63a4ec806..9a3ec580d 100644 --- a/src/cmd/linuxkit/moby/docker.go +++ b/src/cmd/linuxkit/moby/build/docker.go @@ -1,4 +1,4 @@ -package moby +package build // We want to replace much of this with use of containerd tools // and also using the Docker API not shelling out diff --git a/src/cmd/linuxkit/moby/image.go b/src/cmd/linuxkit/moby/build/image.go similarity index 91% rename from src/cmd/linuxkit/moby/image.go rename to src/cmd/linuxkit/moby/build/image.go index bc5a12c6c..6715c0f4a 100644 --- a/src/cmd/linuxkit/moby/image.go +++ b/src/cmd/linuxkit/moby/build/image.go @@ -1,4 +1,4 @@ -package moby +package build import ( "archive/tar" @@ -10,18 +10,11 @@ import ( "strings" "github.com/containerd/containerd/reference" + "github.com/linuxkit/linuxkit/src/cmd/linuxkit/moby" "github.com/opencontainers/runtime-spec/specs-go" log "github.com/sirupsen/logrus" ) -const ( - // PaxRecordLinuxkitSource report the package source for a specific file - PaxRecordLinuxkitSource = "LINUXKIT.source" - // PaxRecordLinuxkitLocation report the location of the file in the linuxkit.yaml - // that led to this file being in this location - PaxRecordLinuxkitLocation = "LINUXKIT.location" -) - type tarWriter interface { Close() error Flush() error @@ -170,8 +163,8 @@ func tarPrefix(path, location string, ref *reference.Spec, tw tarWriter) error { Typeflag: tar.TypeDir, Format: tar.FormatPAX, PAXRecords: map[string]string{ - PaxRecordLinuxkitSource: ref.String(), - PaxRecordLinuxkitLocation: location, + moby.PaxRecordLinuxkitSource: ref.String(), + moby.PaxRecordLinuxkitLocation: location, }, } if err := tw.WriteHeader(hdr); err != nil { @@ -232,8 +225,8 @@ func ImageTar(location string, ref *reference.Spec, prefix string, tw tarWriter, if hdr.PAXRecords == nil { hdr.PAXRecords = make(map[string]string) } - hdr.PAXRecords[PaxRecordLinuxkitSource] = ref.String() - hdr.PAXRecords[PaxRecordLinuxkitLocation] = location + hdr.PAXRecords[moby.PaxRecordLinuxkitSource] = ref.String() + hdr.PAXRecords[moby.PaxRecordLinuxkitLocation] = location if exclude[hdr.Name] { log.Debugf("image tar: %s %s exclude %s", ref, prefix, hdr.Name) _, err = io.Copy(io.Discard, tr) @@ -310,8 +303,8 @@ func ImageTar(location string, ref *reference.Spec, prefix string, tw tarWriter, if hdr.PAXRecords == nil { hdr.PAXRecords = make(map[string]string) } - hdr.PAXRecords[PaxRecordLinuxkitSource] = ref.String() - hdr.PAXRecords[PaxRecordLinuxkitLocation] = location + hdr.PAXRecords[moby.PaxRecordLinuxkitSource] = ref.String() + hdr.PAXRecords[moby.PaxRecordLinuxkitLocation] = location origName := hdr.Name hdr.Name = prefix + origName hdr.Format = tar.FormatPAX @@ -355,7 +348,7 @@ func ImageTar(location string, ref *reference.Spec, prefix string, tw tarWriter, } // ImageBundle produces an OCI bundle at the given path in a tarball, given an image and a config.json -func ImageBundle(prefix, location string, ref *reference.Spec, config []byte, runtime Runtime, tw tarWriter, readonly bool, dupMap map[string]string, opts BuildOpts) error { // nolint: lll +func ImageBundle(prefix, location string, ref *reference.Spec, config []byte, runtime moby.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 { @@ -384,8 +377,8 @@ func ImageBundle(prefix, location string, ref *reference.Spec, config []byte, ru ModTime: defaultModTime, Format: tar.FormatPAX, PAXRecords: map[string]string{ - PaxRecordLinuxkitSource: ref.String(), - PaxRecordLinuxkitLocation: location, + moby.PaxRecordLinuxkitSource: ref.String(), + moby.PaxRecordLinuxkitLocation: location, }, } if err := tw.WriteHeader(hdr); err != nil { @@ -406,8 +399,8 @@ func ImageBundle(prefix, location string, ref *reference.Spec, config []byte, ru ModTime: defaultModTime, Format: tar.FormatPAX, PAXRecords: map[string]string{ - PaxRecordLinuxkitSource: ref.String(), - PaxRecordLinuxkitLocation: location, + moby.PaxRecordLinuxkitSource: ref.String(), + moby.PaxRecordLinuxkitLocation: location, }, } if err := tw.WriteHeader(hdr); err != nil { @@ -421,8 +414,8 @@ func ImageBundle(prefix, location string, ref *reference.Spec, config []byte, ru ModTime: defaultModTime, Format: tar.FormatPAX, PAXRecords: map[string]string{ - PaxRecordLinuxkitSource: ref.String(), - PaxRecordLinuxkitLocation: location, + moby.PaxRecordLinuxkitSource: ref.String(), + moby.PaxRecordLinuxkitLocation: location, }, } if err := tw.WriteHeader(hdr); err != nil { @@ -445,8 +438,8 @@ func ImageBundle(prefix, location string, ref *reference.Spec, config []byte, ru ModTime: defaultModTime, Format: tar.FormatPAX, PAXRecords: map[string]string{ - PaxRecordLinuxkitSource: ref.String(), - PaxRecordLinuxkitLocation: location, + moby.PaxRecordLinuxkitSource: ref.String(), + moby.PaxRecordLinuxkitLocation: location, }, } if err := tw.WriteHeader(hdr); err != nil { @@ -476,8 +469,8 @@ func ImageBundle(prefix, location string, ref *reference.Spec, config []byte, ru ModTime: defaultModTime, Format: tar.FormatPAX, PAXRecords: map[string]string{ - PaxRecordLinuxkitSource: ref.String(), - PaxRecordLinuxkitLocation: location, + moby.PaxRecordLinuxkitSource: ref.String(), + moby.PaxRecordLinuxkitLocation: location, }, } if err := tw.WriteHeader(hdr); err != nil { diff --git a/src/cmd/linuxkit/moby/images.go b/src/cmd/linuxkit/moby/build/images.go similarity index 99% rename from src/cmd/linuxkit/moby/images.go rename to src/cmd/linuxkit/moby/build/images.go index 46a6fafe4..8b5a72da2 100644 --- a/src/cmd/linuxkit/moby/images.go +++ b/src/cmd/linuxkit/moby/build/images.go @@ -1,4 +1,4 @@ -package moby +package build import ( "github.com/containerd/containerd/reference" diff --git a/src/cmd/linuxkit/moby/images.yaml b/src/cmd/linuxkit/moby/build/images.yaml similarity index 100% rename from src/cmd/linuxkit/moby/images.yaml rename to src/cmd/linuxkit/moby/build/images.yaml diff --git a/src/cmd/linuxkit/moby/linuxkit.go b/src/cmd/linuxkit/moby/build/linuxkit.go similarity index 96% rename from src/cmd/linuxkit/moby/linuxkit.go rename to src/cmd/linuxkit/moby/build/linuxkit.go index 91fe42bf3..9fcf5991e 100644 --- a/src/cmd/linuxkit/moby/linuxkit.go +++ b/src/cmd/linuxkit/moby/build/linuxkit.go @@ -1,4 +1,4 @@ -package moby +package build import ( "crypto/sha256" @@ -13,6 +13,7 @@ import ( "path/filepath" "runtime" + "github.com/linuxkit/linuxkit/src/cmd/linuxkit/moby" log "github.com/sirupsen/logrus" ) @@ -43,7 +44,7 @@ func ensureLinuxkitImage(name, cache string) error { yaml := linuxkitYaml[name] - m, err := NewConfig([]byte(yaml), nil) + m, err := moby.NewConfig([]byte(yaml), nil) if err != nil { return err } diff --git a/src/cmd/linuxkit/moby/mkimage.yaml b/src/cmd/linuxkit/moby/build/mkimage.yaml similarity index 100% rename from src/cmd/linuxkit/moby/mkimage.yaml rename to src/cmd/linuxkit/moby/build/mkimage.yaml diff --git a/src/cmd/linuxkit/moby/opts.go b/src/cmd/linuxkit/moby/build/opts.go similarity index 95% rename from src/cmd/linuxkit/moby/opts.go rename to src/cmd/linuxkit/moby/build/opts.go index fb584052a..bdd7e5c61 100644 --- a/src/cmd/linuxkit/moby/opts.go +++ b/src/cmd/linuxkit/moby/build/opts.go @@ -1,4 +1,4 @@ -package moby +package build // BuildOpts options that control the linuxkit build process type BuildOpts struct { diff --git a/src/cmd/linuxkit/moby/output.go b/src/cmd/linuxkit/moby/build/output.go similarity index 99% rename from src/cmd/linuxkit/moby/output.go rename to src/cmd/linuxkit/moby/build/output.go index 2719e0266..fbacc8948 100644 --- a/src/cmd/linuxkit/moby/output.go +++ b/src/cmd/linuxkit/moby/build/output.go @@ -1,4 +1,4 @@ -package moby +package build import ( "archive/tar" diff --git a/src/cmd/linuxkit/moby/sbom.go b/src/cmd/linuxkit/moby/build/sbom.go similarity index 99% rename from src/cmd/linuxkit/moby/sbom.go rename to src/cmd/linuxkit/moby/build/sbom.go index 409fb9ca4..175ebdb7a 100644 --- a/src/cmd/linuxkit/moby/sbom.go +++ b/src/cmd/linuxkit/moby/build/sbom.go @@ -1,4 +1,4 @@ -package moby +package build import ( "archive/tar" diff --git a/src/cmd/linuxkit/moby/config.go b/src/cmd/linuxkit/moby/config.go index 5984f1cf2..5e7ac0029 100644 --- a/src/cmd/linuxkit/moby/config.go +++ b/src/cmd/linuxkit/moby/config.go @@ -31,6 +31,10 @@ type Moby struct { initRefs []*reference.Spec } +func (m Moby) InitRefs() []*reference.Spec { + return m.initRefs +} + // KernelConfig is the type of the config for a kernel type KernelConfig struct { Image string `yaml:"image" json:"image"` @@ -42,6 +46,10 @@ type KernelConfig struct { ref *reference.Spec } +func (k KernelConfig) Ref() *reference.Spec { + return k.ref +} + // File is the type of a file specification type File struct { Path string `yaml:"path" json:"path"` @@ -127,6 +135,10 @@ type ImageConfig struct { ref *reference.Spec } +func (i ImageConfig) Ref() *reference.Spec { + return i.ref +} + // Device specifies a device to be exposed to the container. type Device struct { Path string `yaml:"path" json:"path"` @@ -237,7 +249,7 @@ func extractReferences(m *Moby) error { return nil } -func updateImages(m *Moby) { +func UpdateImages(m *Moby) { if m.Kernel.ref != nil { m.Kernel.Image = m.Kernel.ref.String() } @@ -689,7 +701,7 @@ func getAllCapabilities() []string { var allCaps = getAllCapabilities() -func idNumeric(v interface{}, idMap map[string]uint32) (uint32, error) { +func IDNumeric(v interface{}, idMap map[string]uint32) (uint32, error) { switch id := v.(type) { case nil: return uint32(0), nil @@ -984,17 +996,17 @@ func ConfigToOCI(yaml *Image, config imagespec.ImageConfig, idMap map[string]uin uidIf := assignInterface(label.UID, yaml.UID) gidIf := assignInterface(label.GID, yaml.GID) agIf := assignInterfaceArray(label.AdditionalGids, yaml.AdditionalGids) - uid, err := idNumeric(uidIf, idMap) + uid, err := IDNumeric(uidIf, idMap) if err != nil { return oci, runtime, err } - gid, err := idNumeric(gidIf, idMap) + gid, err := IDNumeric(gidIf, idMap) if err != nil { return oci, runtime, err } var additionalGroups []uint32 for _, id := range agIf { - ag, err := idNumeric(id, idMap) + ag, err := IDNumeric(id, idMap) if err != nil { return oci, runtime, err } diff --git a/src/cmd/linuxkit/moby/const.go b/src/cmd/linuxkit/moby/const.go new file mode 100644 index 000000000..d2b786def --- /dev/null +++ b/src/cmd/linuxkit/moby/const.go @@ -0,0 +1,9 @@ +package moby + +const ( + // PaxRecordLinuxkitSource report the package source for a specific file + PaxRecordLinuxkitSource = "LINUXKIT.source" + // PaxRecordLinuxkitLocation report the location of the file in the linuxkit.yaml + // that led to this file being in this location + PaxRecordLinuxkitLocation = "LINUXKIT.location" +)