|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/linuxkit/linuxkit/src/cmd/linuxkit/pkglib" |
| 11 | + "github.com/linuxkit/linuxkit/src/cmd/linuxkit/spec" |
| 12 | + buildkitClient "github.com/moby/buildkit/client" |
| 13 | + imagespec "github.com/opencontainers/image-spec/specs-go/v1" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + defaultPkgBuildYML = "build.yml" |
| 18 | + defaultPkgCommit = "HEAD" |
| 19 | + defaultPkgTag = "{{.Hash}}" |
| 20 | + defaultBuilderImage = "moby/buildkit:v0.12.3" |
| 21 | +) |
| 22 | + |
| 23 | +// heavily inspired by src/cmd/linuxkit/pkglib/dockerdryrun.go |
| 24 | +type buildArgsDockerRunner struct { |
| 25 | + buildArgs map[string]string |
| 26 | +} |
| 27 | + |
| 28 | +func (dr *buildArgsDockerRunner) Builder( |
| 29 | + ctx context.Context, |
| 30 | + dockerContext, builderImage, builderConfigPath, platform string, |
| 31 | + restart bool, |
| 32 | +) (*buildkitClient.Client, error) { |
| 33 | + return nil, nil |
| 34 | +} |
| 35 | + |
| 36 | +func (dr *buildArgsDockerRunner) Pull(img string) (bool, error) { |
| 37 | + return false, errors.New("not implemented") |
| 38 | +} |
| 39 | + |
| 40 | +func (dr *buildArgsDockerRunner) Tag(ref, tag string) error { |
| 41 | + return errors.New("not implemented") |
| 42 | +} |
| 43 | + |
| 44 | +func (dr *buildArgsDockerRunner) Build( |
| 45 | + ctx context.Context, |
| 46 | + tag, pkg, dockerContext, builderImage, builderConfigPath, platform string, |
| 47 | + restart, preCacheImages bool, |
| 48 | + c spec.CacheProvider, |
| 49 | + r io.Reader, |
| 50 | + stdout io.Writer, |
| 51 | + sbomScan bool, |
| 52 | + sbomScannerImage, platformType string, |
| 53 | + imageBuildOpts spec.ImageBuildOptions, |
| 54 | +) error { |
| 55 | + for k, v := range imageBuildOpts.BuildArgs { |
| 56 | + dr.buildArgs[k] = *v |
| 57 | + } |
| 58 | + |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func (dr *buildArgsDockerRunner) Save(tgt string, refs ...string) error { |
| 63 | + return errors.New("not implemented") |
| 64 | +} |
| 65 | + |
| 66 | +func (dr *buildArgsDockerRunner) Load(src io.Reader) error { |
| 67 | + return errors.New("not implemented") |
| 68 | +} |
| 69 | + |
| 70 | +func (dr *buildArgsDockerRunner) ContextSupportCheck() error { |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func lktBuildArgs(ymlPath string) map[string]string { |
| 75 | + |
| 76 | + ymlBuildFile := filepath.Base(ymlPath) |
| 77 | + |
| 78 | + pkglibConfig := pkglib.PkglibConfig{ |
| 79 | + BuildYML: ymlBuildFile, |
| 80 | + HashCommit: defaultPkgCommit, |
| 81 | + Dev: false, |
| 82 | + Tag: defaultPkgTag, |
| 83 | + } |
| 84 | + pkgs, err := pkglib.NewFromConfig(pkglibConfig, filepath.Dir(ymlPath)) |
| 85 | + if err != nil { |
| 86 | + panic(err) |
| 87 | + // silently ignore that this is not a linuxkit package |
| 88 | + } |
| 89 | + |
| 90 | + var opts []pkglib.BuildOpt |
| 91 | + |
| 92 | + badr := &buildArgsDockerRunner{ |
| 93 | + buildArgs: map[string]string{}, |
| 94 | + } |
| 95 | + |
| 96 | + opts = append(opts, pkglib.WithBuildDocker(badr)) |
| 97 | + opts = append(opts, pkglib.WithDryRun()) |
| 98 | + tmpDir := os.TempDir() |
| 99 | + defer os.RemoveAll(tmpDir) |
| 100 | + opts = append(opts, pkglib.WithBuildCacheDir(tmpDir)) |
| 101 | + opts = append(opts, pkglib.WithBuildSbomScanner("")) // but why? |
| 102 | + opts = append(opts, pkglib.WithBuildBuilderImage(defaultBuilderImage)) |
| 103 | + opts = append(opts, pkglib.WithBuildBuilderRestart(false)) |
| 104 | + opts = append(opts, pkglib.WithProgress("auto")) |
| 105 | + |
| 106 | + for _, pkg := range pkgs { |
| 107 | + plats := []imagespec.Platform{{OS: TARGETOS, Architecture: TARGETARCH}} |
| 108 | + |
| 109 | + var ( |
| 110 | + pkgPlats = make([]imagespec.Platform, len(plats)) |
| 111 | + ) |
| 112 | + copy(pkgPlats, plats) |
| 113 | + // unless overridden, platforms are specific to a package, so this needs to be inside the for loop |
| 114 | + |
| 115 | + // if there are no platforms to build for, do nothing. |
| 116 | + // note that this is *not* an error; we simply skip it |
| 117 | + if len(pkgPlats) == 0 { |
| 118 | + // fmt.Printf("Skipping %s with no architectures to build\n", p.Tag()) |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + opts = append(opts, pkglib.WithBuildPlatforms(pkgPlats...)) |
| 123 | + |
| 124 | + pkg.ProcessBuildArgs() |
| 125 | + |
| 126 | + err := pkg.Build(opts...) |
| 127 | + if err != nil { |
| 128 | + panic(err) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return badr.buildArgs |
| 133 | +} |
0 commit comments