diff --git a/internal/myks/sync.go b/internal/myks/sync.go index e1d4159e..97696d45 100644 --- a/internal/myks/sync.go +++ b/internal/myks/sync.go @@ -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) @@ -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 + } } - // 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 { 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")) diff --git a/internal/myks/util.go b/internal/myks/util.go index 1ab21c5d..9c18d2a7 100644 --- a/internal/myks/util.go +++ b/internal/myks/util.go @@ -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 + } + return exists +} + func collectBySubpath(rootDir string, targetDir string, subpath string) []string { items := []string{} currentPath := rootDir