|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/containerd/platforms" |
| 8 | + "github.com/moby/buildkit/client/llb" |
| 9 | + "github.com/moby/buildkit/client/llb/sourceresolver" |
| 10 | + "github.com/moby/buildkit/frontend/dockerfile/dockerfile2llb" |
| 11 | + "github.com/moby/buildkit/frontend/dockerfile/parser" |
| 12 | + "github.com/moby/buildkit/frontend/dockerui" |
| 13 | + "github.com/moby/buildkit/solver/pb" |
| 14 | + "github.com/opencontainers/go-digest" |
| 15 | + ocispecs "github.com/opencontainers/image-spec/specs-go/v1" |
| 16 | + "github.com/pkg/errors" |
| 17 | + "log" |
| 18 | + "os" |
| 19 | + "strings" |
| 20 | +) |
| 21 | + |
| 22 | +func getDockerfileDeps(dockerfile string) string { |
| 23 | + ctx := context.Background() |
| 24 | + data := noErr2(os.ReadFile(dockerfile)) |
| 25 | + |
| 26 | + st, _, _, _, err := dockerfile2llb.Dockerfile2LLB(ctx, data, dockerfile2llb.ConvertOpt{ |
| 27 | + // building an image requires fetching the metadata for its parent |
| 28 | + // this fakes a parent so that this tool does not need to do network i/o |
| 29 | + MetaResolver: &testResolver{ |
| 30 | + // random digest value |
| 31 | + digest: "sha256:a1c7d58d98df3f9a67eda799200655b923ebc7a41cad1d9bb52723ae1c81ad17", |
| 32 | + dir: "/", |
| 33 | + platform: "linux/amd64", |
| 34 | + }, |
| 35 | + Config: dockerui.Config{ |
| 36 | + BuildArgs: map[string]string{"BASE_IMAGE": "fake-image"}, |
| 37 | + BuildPlatforms: []ocispecs.Platform{{OS: "linux", Architecture: "amd64"}}, |
| 38 | + }, |
| 39 | + Warn: func(rulename, description, url, fmtmsg string, location []parser.Range) { |
| 40 | + log.Printf(rulename, description, url, fmtmsg, location) |
| 41 | + }, |
| 42 | + }) |
| 43 | + noErr(err) |
| 44 | + |
| 45 | + definition := noErr2(st.Marshal(ctx)) |
| 46 | + return getOpSourceFollowPaths(definition) |
| 47 | +} |
| 48 | + |
| 49 | +func getOpSourceFollowPaths(definition *llb.Definition) string { |
| 50 | + // https://earthly.dev/blog/compiling-containers-dockerfiles-llvm-and-buildkit/ |
| 51 | + // https://stackoverflow.com/questions/73067660/what-exactly-is-the-frontend-and-backend-of-docker-buildkit |
| 52 | + |
| 53 | + ops := make([]llbOp, 0) |
| 54 | + for _, dt := range definition.Def { |
| 55 | + var op pb.Op |
| 56 | + if err := op.UnmarshalVT(dt); err != nil { |
| 57 | + panic("failed to parse op") |
| 58 | + } |
| 59 | + dgst := digest.FromBytes(dt) |
| 60 | + ent := llbOp{Op: &op, Digest: dgst, OpMetadata: definition.Metadata[dgst].ToPB()} |
| 61 | + ops = append(ops, ent) |
| 62 | + } |
| 63 | + |
| 64 | + for _, op := range ops { |
| 65 | + switch op := op.Op.Op.(type) { |
| 66 | + case *pb.Op_Source: |
| 67 | + if strings.HasPrefix(op.Source.Identifier, "docker-image://") { |
| 68 | + // no-op |
| 69 | + } else if strings.HasPrefix(op.Source.Identifier, "local://") { |
| 70 | + paths := op.Source.Attrs[pb.AttrFollowPaths] |
| 71 | + return paths |
| 72 | + } else { |
| 73 | + panic(fmt.Errorf("unexpected prefix %v", op.Source.Identifier)) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + return "" |
| 78 | +} |
| 79 | + |
| 80 | +// llbOp holds data for a single loaded LLB op |
| 81 | +type llbOp struct { |
| 82 | + Op *pb.Op |
| 83 | + Digest digest.Digest |
| 84 | + OpMetadata *pb.OpMetadata |
| 85 | +} |
| 86 | + |
| 87 | +// testResolver provides a fake parent image manifest for the build |
| 88 | +type testResolver struct { |
| 89 | + digest digest.Digest |
| 90 | + dir string |
| 91 | + platform string |
| 92 | +} |
| 93 | + |
| 94 | +func (r *testResolver) ResolveImageConfig(ctx context.Context, ref string, opt sourceresolver.Opt) (string, digest.Digest, []byte, error) { |
| 95 | + var img struct { |
| 96 | + Config struct { |
| 97 | + Env []string `json:"Env,omitempty"` |
| 98 | + WorkingDir string `json:"WorkingDir,omitempty"` |
| 99 | + User string `json:"User,omitempty"` |
| 100 | + } `json:"config,omitempty"` |
| 101 | + } |
| 102 | + |
| 103 | + img.Config.WorkingDir = r.dir |
| 104 | + |
| 105 | + if opt.Platform != nil { |
| 106 | + r.platform = platforms.Format(*opt.Platform) |
| 107 | + } |
| 108 | + |
| 109 | + dt, err := json.Marshal(img) |
| 110 | + if err != nil { |
| 111 | + return "", "", nil, errors.WithStack(err) |
| 112 | + } |
| 113 | + return ref, r.digest, dt, nil |
| 114 | +} |
0 commit comments