feat: Multiple postinstall symlinks pointing to the same thing#495
Conversation
spec.go
Outdated
| type SymlinkTarget struct { | ||
| // Path is the path where the symlink should be placed | ||
| Path string `yaml:"path" json:"path" jsonschema:"required"` | ||
| Path string `yaml:"path" json:"path" jsonschema:"oneof_required=path"` |
There was a problem hiding this comment.
We should mark this is deprecated and have people use paths instead.
We can also make it so when we load the spec we migrate Path to Paths then there's only one field to deal with.
There was a problem hiding this comment.
How do we mark it as deprecated?
There was a problem hiding this comment.
There was a problem hiding this comment.
done, but I'm going to add tests for it.
|
@cpuguy83 This should be ready for another round of review. |
load.go
Outdated
|
|
||
| var errs []error | ||
| if err := i.Post.validate(); err != nil { | ||
| errs = append(errs, errors.Wrap(err, "postinstall")) |
load.go
Outdated
|
|
||
| // By this point, both the oldpath and the SymlinkTarget are | ||
| // well-formed. We still need to make sure each 'newpath' is unique. | ||
| if cfg.Paths == nil { |
There was a problem hiding this comment.
Validations should not be modifying anything.
We can also iterate on Paths without having to have it be non-nil.
load.go
Outdated
| cfg.Paths = []string{} | ||
| } | ||
|
|
||
| newpaths := slices.Clone(cfg.Paths) |
There was a problem hiding this comment.
Why the clone? Should not need to modify anything here.
If we want to make sure that cfg.Path is not also somewhere in cfg.Paths you can check that directly.
for _, link := range symlinks {
if link.Path == cfg.Path {
// error
}
}There was a problem hiding this comment.
I could have sworn I put this in fillDefaults. I need to move this code there. The clone from moving the old code around. I'll take another pass
There was a problem hiding this comment.
Now I remember. Clone so we don't modify the original, but stick the cfg.Path field on the end so we can just loop through. It's weird, I'll change it.
There was a problem hiding this comment.
Why the clone? Should not need to modify anything here. If we want to make sure that
cfg.Pathis not also somewhere incfg.Pathsyou can check that directly.for _, link := range symlinks { if link.Path == cfg.Path { // error } }
And the goal is to make sure the there are no duplicate oldpaths in the whole map[string]SymlinkTarget. The desired behavior is unclear (follow the symlink that has already been created? Or overwrite the existing symlink?) Map iteration is not reliably ordered (and we sort the keys to avoid cache misses), so the user is unlikely to get what they want anyway. Better to just not allow it.
There was a problem hiding this comment.
done. This exhibits the desired behavior, doesn't modify the spec, is tested properly, and only allocates the map when needed.
load.go
Outdated
|
|
||
| func validateSymlinks(symlinks map[string]SymlinkTarget) error { | ||
| var errs []error | ||
| dests := make(map[string]string, len(symlinks)<<1) |
There was a problem hiding this comment.
Maybe lets call this seen and only allocate the map if its needed.
e4a8974 to
cf1cf76
Compare
frontend/windows/handle_container.go
Outdated
| s = s.File(llb.Mkdir(path.Dir(dstPath), 0755, llb.WithParents(true))) | ||
| s = s.File(llb.Copy(s, srcPath, dstPath)) | ||
|
|
||
| for oldpath, newpaths := range post.Symlinks { |
There was a problem hiding this comment.
I think we still need to sort this.
helpers.go
Outdated
| for src, tgt := range post.Symlinks { | ||
| fmt.Fprintf(buf, "mkdir -p %q\n", filepath.Join(rootfsPath, filepath.Dir(tgt.Path))) | ||
| fmt.Fprintf(buf, "ln -s %q %q\n", src, filepath.Join(rootfsPath, tgt.Path)) | ||
| for oldpath, newpaths := range post.Symlinks { |
load_test.go
Outdated
| Post: &PostInstall{ | ||
| Symlinks: map[string]SymlinkTarget{ | ||
| "oldpath": { | ||
| Path: "/newpath3", |
There was a problem hiding this comment.
I don't think we need to test this since its not a valid case.
There was a problem hiding this comment.
Good catch. Fixed it so the test code will reject invalid tests.
target.go
Outdated
| cfg.Path = "" | ||
| } | ||
|
|
||
| sort.Strings(cfg.Paths) |
There was a problem hiding this comment.
I don't think this should be sorted since this should be left to the user.
a21b267 to
6fd5303
Compare
Allow for multiple links to point to the same target. Signed-off-by: Peter Engelbert <pmengelbert@gmail.com>
Signed-off-by: Peter Engelbert <pmengelbert@gmail.com>
Upon loading the spec, if `Path` is nonempty, move it to `Paths`. This commit also reworks the tests. Signed-off-by: Peter Engelbert <pmengelbert@gmail.com>
Signed-off-by: Peter Engelbert <pmengelbert@gmail.com>
Signed-off-by: Peter Engelbert <pmengelbert@gmail.com>
* no longer modifies anything in `.validate()` * only allocates map when it's actually needed * stipulates in the test that `.Paths` should end up sorted (this is for llb caching) * s/postinstall/post Signed-off-by: Peter Engelbert <pmengelbert@gmail.com>
Some of the `fillDefaults` tests were testing invalid situations. To prevent this, validation is run on the test ins and outs before calling `fillDefaults`. The invalid tests have been removed. This commit also removes the sorting from `fillDefaults`, this will be left to the user instead. Signed-off-by: Peter Engelbert <pmengelbert@gmail.com>
Instead of sorting the list of symlinks, leave it for the user to do. This commit removes the sorting from `fillDefaults` and implements it in the handlers. The map keys of the symlinks are now sorted as well. Signed-off-by: Peter Engelbert <pmengelbert@gmail.com>
6fd5303 to
7a63ab6
Compare
|
This has been rebased and is ready for another round of review @cpuguy83 |
Signed-off-by: Peter Engelbert <pmengelbert@gmail.com>
What this PR does / why we need it:
Currently, you can't create multiple symlink
newpaths that point to the sameoldpath. This is due to the fact that map keys have to be unique.For more information, see #489
This should be merged before #494