New ๐ŸŒŠ wave for @helm

Commit 758185ea

758185ea74d1Merged

Svyatoslav Dumchenko <[email protected]> ยท 4 years ago ยท 1 file ยท ~1 modified

Description

Fix flock usage

Details

Commit
758185ea74d1
Parent
a9e680663f21

pkg/plan/apply.gomodifiedView file

@@ -62,6 +62,18 @@
6262 func syncRepositories(repositories repoConfigs) (err error) {
6363 log.Trace("๐Ÿ—„ helm repository.yaml: ", helper.Helm.RepositoryConfig)
6464
65+ // we need to get a flock first
66+ lockPath := helper.Helm.RepositoryConfig + ".lock"
67+ fileLock := flock.New(lockPath)
68+ lockCtx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
69+ defer cancel()
70+ // We need to unlock in deferred mode in case of any other errors returned
71+ defer fileLock.Unlock() //nolint:errcheck // TODO: add error checking
72+ locked, err := fileLock.TryLockContext(lockCtx, time.Second)
73+ if err != nil && !locked {
74+ return fmt.Errorf("failed to get lock %s: %w", fileLock.Path(), err)
75+ }
76+
6577 var f *helmRepo.File
6678 // Create if not exits
6779 if !helper.IsExists(helper.Helm.RepositoryConfig) {
@@ -78,17 +90,6 @@
7890 }
7991 }
8092
81- // Flock
82- lockPath := helper.Helm.RepositoryConfig + ".lock"
83- fileLock := flock.New(lockPath)
84- lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
85- defer cancel()
86-
87- locked, err := fileLock.TryLockContext(lockCtx, time.Second)
88- if err != nil && !locked {
89- return fmt.Errorf("failed to get lock %s: %w", fileLock.Path(), err)
90- }
91-
9293 // We cannot parallel repositories installation as helm manages single repositories.yaml.
9394 // To prevent data race we need either make helm use futex or not parallel at all
9495 for i := range repositories {
@@ -103,7 +104,7 @@
103104 return fmt.Errorf("failed to write repositories file: %w", err)
104105 }
105106
106- // Unlock
107+ // If we haven't met any errors yet unlock the repository file. Deferred unlock will exit quickly after this.
107108 if err := fileLock.Unlock(); err != nil {
108109 return fmt.Errorf("failed to unlock %s: %w", fileLock.Path(), err)
109110 }