Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If two artifacts provide the same file, re-link or re-copy that file from the artifact left after the other was undeployed/uninstalled. #3455

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/smartlink/smartlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func Link(src, dest string) error {
return nil
}

if destDir := filepath.Dir(dest); !fileutils.DirExists(destDir) {
if err := os.MkdirAll(destDir, 0755); err != nil {
return errs.Wrap(err, "could not create directory %s", destDir)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You could simplify this with fileutils.MkdirUnlessExists().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I also found a case where we can use fileutils.Mkdir() instead of os.Mkdir() with a magic number.


// Multiple artifacts can supply the same file. We do not have a better solution for this at the moment other than
// favouring the first one encountered.
if fileutils.TargetExists(dest) {
Expand Down
78 changes: 67 additions & 11 deletions pkg/runtime/depot.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ type depotConfig struct {
}

type deployment struct {
Type deploymentType `json:"type"`
Path string `json:"path"`
Files []string `json:"files"`
Type deploymentType `json:"type"`
Path string `json:"path"`
Files []string `json:"files"`
RelativeSrc string `json:"relativeSrc"`
}

type deploymentType string
Expand Down Expand Up @@ -187,9 +188,10 @@ func (d *depot) DeployViaLink(id strfmt.UUID, relativeSrc, absoluteDest string)
d.config.Deployments[id] = []deployment{}
}
d.config.Deployments[id] = append(d.config.Deployments[id], deployment{
Type: deploymentTypeLink,
Path: absoluteDest,
Files: files.RelativePaths(),
Type: deploymentTypeLink,
Path: absoluteDest,
Files: files.RelativePaths(),
RelativeSrc: relativeSrc,
})

return nil
Expand Down Expand Up @@ -243,9 +245,10 @@ func (d *depot) DeployViaCopy(id strfmt.UUID, relativeSrc, absoluteDest string)
d.config.Deployments[id] = []deployment{}
}
d.config.Deployments[id] = append(d.config.Deployments[id], deployment{
Type: deploymentTypeCopy,
Path: absoluteDest,
Files: files.RelativePaths(),
Type: deploymentTypeCopy,
Path: absoluteDest,
Files: files.RelativePaths(),
RelativeSrc: relativeSrc,
})

return nil
Expand All @@ -270,16 +273,35 @@ func (d *depot) Undeploy(id strfmt.UUID, relativeSrc, path string) error {
if !ok {
return errs.New("deployment for %s not found in depot", id)
}
deploy := sliceutils.Filter(deployments, func(d deployment) bool { return d.Path == path })
if len(deploy) != 1 {
deployments = sliceutils.Filter(deployments, func(d deployment) bool { return d.Path == path })
if len(deployments) != 1 {
return errs.New("no deployment found for %s in depot", path)
}
deploy := deployments[0]

// Perform uninstall based on deployment type
if err := smartlink.UnlinkContents(filepath.Join(d.Path(id), relativeSrc), path); err != nil {
return errs.Wrap(err, "failed to unlink artifact")
}

// Re-link or re-copy any files provided by other artifacts.
redeploys, err := d.getSharedFilesToRedeploy(id, deploy, path)
if err != nil {
return errs.Wrap(err, "failed to get shared files")
}
for sharedFile, relinkSrc := range redeploys {
switch deploy.Type {
case deploymentTypeLink:
if err := smartlink.Link(relinkSrc, sharedFile); err != nil {
return errs.Wrap(err, "failed to relink file")
}
case deploymentTypeCopy:
if err := fileutils.CopyFile(relinkSrc, sharedFile); err != nil {
return errs.Wrap(err, "failed to re-copy file")
}
}
}

// Write changes to config
d.config.Deployments[id] = sliceutils.Filter(d.config.Deployments[id], func(d deployment) bool { return d.Path != path })

Expand All @@ -300,6 +322,40 @@ func (d *depot) validateVolume(absoluteDest string) error {
return nil
}

// getSharedFilesToRedeploy returns a map of deployed files to re-link to (or re-copy from) another
// artifact that provides those files. The key is the deployed file path and the value is the
// source path from another artifact.
func (d *depot) getSharedFilesToRedeploy(id strfmt.UUID, deploy deployment, path string) (map[string]string, error) {
// Map of deployed paths to other sources that provides those paths.
redeploy := make(map[string]string, 0)

// For each file deployed by this artifact, find another artifact (if any) that deploys its own copy.
for _, relativeDeployedFile := range deploy.Files {
deployedFile := filepath.Join(path, relativeDeployedFile)
for artifactId, artifactDeployments := range d.config.Deployments {
if artifactId == id {
continue
}

for _, deployment := range artifactDeployments {
for _, fileToDeploy := range deployment.Files {
if relativeDeployedFile == fileToDeploy {
// We'll want to redeploy this from other artifact's copy after undeploying the currently deployed version.
newSrc := filepath.Join(d.Path(artifactId), deployment.RelativeSrc, relativeDeployedFile)
logging.Debug("More than one artifact provides '%s'", relativeDeployedFile)
logging.Debug("Will redeploy '%s' to '%s'", newSrc, deployedFile)
redeploy[deployedFile] = newSrc
goto nextDeployedFile
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Would you mind using an anonymous function or a var to break out of the outer loop?

This might be my own personal pet peeve, but I find the goto statement to be an eyesore. I've never encountered a scenario where this problem cannot be solved in a (in my opinion) cleaner way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three consecutive

if fileFound {
  break
}

looked hideous, so I tried an anonymous function. Hopefully that passes your filter :)

}
}
}
}
nextDeployedFile:
}

return redeploy, nil
}

// Save will write config changes to disk (ie. links between depot artifacts and runtimes that use it).
// It will also delete any stale artifacts which are not used by any runtime.
func (d *depot) Save() error {
Expand Down
Loading