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

Refactor lazy sync code #340

Merged
merged 5 commits into from Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 4 additions & 9 deletions pkg/vendir/cmd/sync.go
Expand Up @@ -36,13 +36,6 @@ type SyncOptions struct {
AllowAllSymlinkDestinations bool
}

func (o *SyncOptions) LockFileExists() bool {
if _, err := os.Stat(o.LockFile); err != nil {
return false
}
return true
}

func NewSyncOptions(ui ui.UI) *SyncOptions {
return &SyncOptions{ui: ui}
}
Expand Down Expand Up @@ -162,9 +155,9 @@ func (o *SyncOptions) Run() error {
newLockConfig.Directories = append(newLockConfig.Directories, dirLockConf)
}

// Update only selected directories in lock file
// Update only selected directories in lock file, if it exists
if len(dirs) > 0 {
if o.LockFileExists() {
if _, err := os.Stat(o.LockFile); err == nil {
existingLockConfig, err := ctlconf.NewLockConfigFromFile(o.LockFile)
if err != nil {
return err
Expand All @@ -175,6 +168,8 @@ func (o *SyncOptions) Run() error {
}

newLockConfig = existingLockConfig
} else if !os.IsNotExist(err) {
return err
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/vendir/config/directory.go
Expand Up @@ -112,7 +112,7 @@ type DirectoryContentsImage struct {

// Secret may include one or more keys: username, password, token.
// By default anonymous access is used for authentication.
// TODO support docker config formated secret
// TODO support docker config formatted secret
// +optional
SecretRef *DirectoryContentsLocalRef `json:"secretRef,omitempty"`

Expand All @@ -130,7 +130,7 @@ type DirectoryContentsImgpkgBundle struct {

// Secret may include one or more keys: username, password, token.
// By default anonymous access is used for authentication.
// TODO support docker config formated secret
// TODO support docker config formatted secret
// +optional
SecretRef *DirectoryContentsLocalRef `json:"secretRef,omitempty"`

Expand Down
11 changes: 0 additions & 11 deletions pkg/vendir/config/lock_config.go
Expand Up @@ -28,16 +28,6 @@ func NewLockConfig() LockConfig {
}
}

func LockFileExists(path string) bool {
if _, err := os.Stat(path); err != nil {
if errors.Is(err, fs.ErrNotExist) {
return false
}
panic(fmt.Errorf("can not stat file '%v': %v", path, err))
}
return true
}

func NewLockConfigFromFile(path string) (LockConfig, error) {
bs, err := os.ReadFile(path)
if err != nil {
Expand Down Expand Up @@ -155,7 +145,6 @@ func (c *LockConfig) Merge(other LockConfig) error {
}

func (c *LockConfig) ReplaceContents(path string, replaceCon LockDirectoryContents) bool {

for i, dir := range c.Directories {
for j, con := range dir.Contents {
if filepath.Join(dir.Path, con.Path) != path {
Expand Down
22 changes: 5 additions & 17 deletions pkg/vendir/directory/directory.go
Expand Up @@ -85,9 +85,10 @@ func (d *Directory) Sync(syncOpts SyncOpts) (ctlconf.LockDirectory, error) {

// error is safe to ignore, since it indicates that no lock file entry for the given path exists
oldLockContents, _ := d.lockDirectory.FindContents(contents.Path)
skipFetching, lazySyncAddConfigDigest := d.handleLazySync(oldLockContents.ConfigDigest, configDigest, syncOpts.Lazy, contents.Lazy)

if skipFetching {
// if lazy sync is enabled in both config and sync options and the config was not changed since the last sync,
// skip fetching
if syncOpts.Lazy && contents.Lazy && oldLockContents.ConfigDigest == configDigest {
d.ui.PrintLinef("Skipping fetch: %s + %s (flagged as lazy, config has not changed since last sync)", d.opts.Path, contents.Path)
lockConfig.Contents = append(lockConfig.Contents, oldLockContents)
// copy previously fetched contents to staging dir
Expand Down Expand Up @@ -252,7 +253,8 @@ func (d *Directory) Sync(syncOpts SyncOpts) (ctlconf.LockDirectory, error) {
return lockConfig, fmt.Errorf("chmod on '%s': %s", stagingDstPath, err)
}

if lazySyncAddConfigDigest {
// config digest is always added if lazy syncing is enabled in the config
if contents.Lazy {
lockDirContents.ConfigDigest = configDigest
}

Expand Down Expand Up @@ -293,17 +295,3 @@ func maybeChmod(path string, potentialPerms ...*os.FileMode) error {

return nil
}

func (d *Directory) handleLazySync(oldConfigDigest string, newConfigDigest string, fetchLazyGlobalOverride bool, fetchLazy bool) (bool, bool) {
skipFetching := false
addConfigDigest := false
// if lazy sync is enabled and config remains unchanged, skip fetching
if fetchLazyGlobalOverride && fetchLazy && oldConfigDigest == newConfigDigest {
skipFetching = true
}
// config digest is always added if lazy syncing is enabled locally and globally
if fetchLazy {
addConfigDigest = true
}
return skipFetching, addConfigDigest
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.