Created
August 11, 2021 00:31
-
-
Save aaronlehmann/64054c9a2cff0d27e200cc107bba3d69 to your computer and use it in GitHub Desktop.
Symlink cache issue repro (take 2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"os" | |
_ "github.com/moby/buildkit/client/connhelper/dockercontainer" | |
"github.com/moby/buildkit/client" | |
"github.com/moby/buildkit/client/llb" | |
"github.com/moby/buildkit/util/appdefaults" | |
"github.com/moby/buildkit/util/progress/progresswriter" | |
"golang.org/x/sync/errgroup" | |
) | |
func main() { | |
alpine := llb.Image("alpine") | |
st := alpine.Run( | |
llb.Shlex(`sh -c "mkdir -p /mnt/data/d1 && ln -s /mnt/data /data"`), | |
).Root() | |
st1 := alpine.Run( | |
llb.Shlex(`sh -c "mkdir -p /data /mnt && ln -s /data /mnt/data"`), | |
).Root() | |
st2 := st1.File(llb.Copy(st, "/mnt/data/d1", "/mnt/data/d1")) | |
st3 := st2.Run( | |
llb.Shlex(`sh -c "echo abc > /data/d1/foo"`), | |
).Root() | |
fmt.Println("Copying from st1 with a single file at /data/d1/foo:") | |
if err := copyAndList(st3); err != nil { | |
panic(err) | |
} | |
fmt.Println("Copying from stock alpine image - copy should not be cached!:") | |
if err := copyAndList(llb.Image("alpine")); err != nil { | |
panic(err) | |
} | |
} | |
func copyAndList(st llb.State) error { | |
copied := llb.Scratch().File( | |
llb.Copy(st, "/mnt/data/*", "/", &llb.CopyInfo{AllowWildcard: true, FollowSymlinks: true}), | |
) | |
findSt := llb.Image("alpine").Run( | |
llb.Shlex(`sh -c "apk add -U findutils"`), | |
).Root().Run( | |
llb.Shlex(`sh -c "find . -ls"`), | |
llb.AddMount("/d", copied), | |
llb.Dir("/d"), | |
llb.IgnoreCache, | |
) | |
ctx := context.Background() | |
def, err := findSt.Marshal(ctx, llb.LinuxAmd64) | |
if err != nil { | |
return err | |
} | |
addr := os.Getenv("BUILDKIT_HOST") | |
if addr == "" { | |
addr = appdefaults.Address | |
} | |
c, err := client.New(ctx, addr, client.WithFailFast()) | |
if err != nil { | |
return err | |
} | |
pw, err := progresswriter.NewPrinter(context.TODO(), os.Stderr, "plain") | |
if err != nil { | |
return err | |
} | |
mw := progresswriter.NewMultiWriter(pw) | |
eg, ctx := errgroup.WithContext(ctx) | |
cwd, err := os.Getwd() | |
if err != nil { | |
return err | |
} | |
eg.Go(func() error { | |
solveOpt := client.SolveOpt{ | |
Exports: []client.ExportEntry{{ | |
Type: client.ExporterLocal, | |
OutputDir: cwd, | |
}}, | |
} | |
_, err := c.Solve(ctx, def, solveOpt, progresswriter.ResetTime(mw.WithPrefix("", false)).Status()) | |
return err | |
}) | |
eg.Go(func() error { | |
<-pw.Done() | |
return pw.Err() | |
}) | |
return eg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment