Skip to content

Commit

Permalink
With the improved handling of lock files in vendir 0.38.0 we can simp…
Browse files Browse the repository at this point in the history
…lify our own cache logic and make it more granular to operate on vendir.yaml directory level
  • Loading branch information
fritzduchardt committed Dec 17, 2023
1 parent 68f28b4 commit 0a94f4f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
45 changes: 13 additions & 32 deletions internal/myks/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func (a *Application) doSync(vendirSecrets string) error {
vendirConfigFileRelativePath := filepath.Join("..", a.e.g.ServiceDirName, a.e.g.VendirConfigFileName)
vendirLockFileRelativePath := filepath.Join("..", a.e.g.ServiceDirName, a.e.g.VendirLockFileName)
vendirConfigFilePath := a.expandServicePath(a.e.g.VendirConfigFileName)
vendirLockFilePath := a.expandServicePath(a.e.g.VendirLockFileName)
vendirSyncFilePath := a.expandServicePath(a.e.g.VendirSyncFileName)
vendorDir := a.expandPath(a.e.g.VendorDirName)

Expand All @@ -99,49 +98,31 @@ func (a *Application) doSync(vendirSecrets string) error {
return err
}

// having hashes here is actually not necessary, since we only need the paths, but it's easier to just reuse the function
lockFileDirHashes, err := readLockFileDirHashes(vendirLockFilePath)
if err != nil {
log.Error().Err(err).Msg(a.Msg(syncStepName, "Unable to read Vendir Lock file: "+vendirLockFilePath))
return err
}

exist, err := isExist(vendorDir)
if err != nil {
log.Error().Err(err).Msg(a.Msg(syncStepName, "Unable to check if vendor dir exists"))
return err
// remove old content of vendor directory, since there might be leftovers in case of path changes
if !a.useCache && isExistsSloppy(vendorDir) {
if err := os.RemoveAll(vendorDir); err != nil {
return err
}
if err := createDirectory(vendorDir); err != nil {
log.Error().Err(err).Msg(a.Msg(syncStepName, "Unable to create vendor dir: "+vendorDir))
return err
}

Check warning on line 109 in internal/myks/sync.go

View check run for this annotation

Codecov / codecov/patch

internal/myks/sync.go#L103-L109

Added lines #L103 - L109 were not covered by tests
}

// TODO sync retry
// only sync vendir with directory flag, if the lock file matches the vendir config file and caching is enabled
if exist && a.useCache && checkLockFileMatch(vendirDirHashes, lockFileDirHashes) {
for dir, hash := range vendirDirHashes {
for dir, hash := range vendirDirHashes {
if isExistsSloppy(filepath.Join(vendorDir, dir)) && a.useCache {
if checkVersionMatch(dir, hash, syncFileDirHashes) {
log.Info().Str("vendir dir", dir).Msg(a.Msg(syncStepName, "Resource already synced"))
continue
}
if err := a.runVendirSync(vendorDir, vendirConfigFileRelativePath, vendirLockFileRelativePath, vendirSecrets, dir); err != nil {
log.Error().Err(err).Msg(a.Msg(syncStepName, "Vendir sync failed"))
return err
}
}
} else {
// remove old content of vendor directory, since there might be leftovers in case of path changes
if err := os.RemoveAll(vendorDir); err != nil {
return err
}

if err := createDirectory(vendorDir); err != nil {
log.Error().Err(err).Msg(a.Msg(syncStepName, "Unable to create vendor dir: "+vendorDir))
return err
}

if err := a.runVendirSync(vendorDir, vendirConfigFileRelativePath, vendirLockFileRelativePath, vendirSecrets, ""); err != nil {
if err := a.runVendirSync(vendorDir, vendirConfigFileRelativePath, vendirLockFileRelativePath, vendirSecrets, dir); err != nil {

Check warning on line 120 in internal/myks/sync.go

View check run for this annotation

Codecov / codecov/patch

internal/myks/sync.go#L120

Added line #L120 was not covered by tests
log.Error().Err(err).Msg(a.Msg(syncStepName, "Vendir sync failed"))
return err
}
}

// rewrite sync file
err = writeSyncFile(a.expandServicePath(a.e.g.VendirSyncFileName), vendirDirHashes)
if err != nil {
log.Error().Err(err).Msg(a.Msg(syncStepName, "Unable to write sync file"))
Expand Down
9 changes: 9 additions & 0 deletions internal/myks/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ func isExist(path string) (bool, error) {
return false, err
}

func isExistsSloppy(path string) bool {
exists, err := isExist(path)
if err != nil {
log.Error().Err(err).Msg("Unable to check if file exists")
return false
}

Check warning on line 329 in internal/myks/util.go

View check run for this annotation

Codecov / codecov/patch

internal/myks/util.go#L327-L329

Added lines #L327 - L329 were not covered by tests
return exists
}

func collectBySubpath(rootDir string, targetDir string, subpath string) []string {
items := []string{}
currentPath := rootDir
Expand Down

0 comments on commit 0a94f4f

Please sign in to comment.