|
| 1 | +package phase |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + gos "os" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + retry "github.com/avast/retry-go" |
| 11 | + "github.com/k0sproject/k0sctl/analytics" |
| 12 | + "github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1" |
| 13 | + "github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster" |
| 14 | + "github.com/k0sproject/rig/exec" |
| 15 | + log "github.com/sirupsen/logrus" |
| 16 | +) |
| 17 | + |
| 18 | +// Lock acquires an exclusive k0sctl lock on hosts |
| 19 | +type Lock struct { |
| 20 | + GenericPhase |
| 21 | + cfs []func() |
| 22 | + instanceID string |
| 23 | + m sync.Mutex |
| 24 | + wg sync.WaitGroup |
| 25 | +} |
| 26 | + |
| 27 | +// Prepare the phase |
| 28 | +func (p *Lock) Prepare(c *v1beta1.Cluster) error { |
| 29 | + p.Config = c |
| 30 | + mid, _ := analytics.MachineID() |
| 31 | + p.instanceID = fmt.Sprintf("%s-%d", mid, gos.Getpid()) |
| 32 | + return nil |
| 33 | +} |
| 34 | + |
| 35 | +// Title for the phase |
| 36 | +func (p *Lock) Title() string { |
| 37 | + return "Acquire exclusive host lock" |
| 38 | +} |
| 39 | + |
| 40 | +func (p *Lock) Cancel() { |
| 41 | + p.m.Lock() |
| 42 | + defer p.m.Unlock() |
| 43 | + for _, f := range p.cfs { |
| 44 | + f() |
| 45 | + } |
| 46 | + p.wg.Wait() |
| 47 | +} |
| 48 | + |
| 49 | +// Run the phase |
| 50 | +func (p *Lock) Run() error { |
| 51 | + if err := p.Config.Spec.Hosts.ParallelEach(p.startLock); err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + return p.Config.Spec.Hosts.ParallelEach(p.startTicker) |
| 55 | +} |
| 56 | + |
| 57 | +func (p *Lock) startTicker(h *cluster.Host) error { |
| 58 | + p.wg.Add(1) |
| 59 | + lfp := h.Configurer.K0sctlLockFilePath(h) |
| 60 | + ticker := time.NewTicker(10 * time.Second) |
| 61 | + ctx, cancel := context.WithCancel(context.Background()) |
| 62 | + p.m.Lock() |
| 63 | + p.cfs = append(p.cfs, cancel) |
| 64 | + p.m.Unlock() |
| 65 | + |
| 66 | + go func() { |
| 67 | + log.Debugf("%s: started periodic update of lock file %s timestamp", h, lfp) |
| 68 | + for { |
| 69 | + select { |
| 70 | + case <-ticker.C: |
| 71 | + if err := h.Configurer.Touch(h, lfp, time.Now(), exec.Sudo(h)); err != nil { |
| 72 | + log.Warnf("%s: failed to touch lock file: %s", h, err) |
| 73 | + } |
| 74 | + case <-ctx.Done(): |
| 75 | + log.Debugf("%s: stopped lock cycle, removing file", h) |
| 76 | + if err := h.Configurer.DeleteFile(h, lfp); err != nil { |
| 77 | + log.Warnf("%s: failed to remove host lock file: %s", h, err) |
| 78 | + } |
| 79 | + p.wg.Done() |
| 80 | + return |
| 81 | + } |
| 82 | + } |
| 83 | + }() |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func (p *Lock) startLock(h *cluster.Host) error { |
| 89 | + return retry.Do( |
| 90 | + func() error { |
| 91 | + return p.tryLock(h) |
| 92 | + }, |
| 93 | + retry.OnRetry( |
| 94 | + func(n uint, err error) { |
| 95 | + log.Errorf("%s: attempt %d of %d.. trying to obtain a lock on host: %s", h, n+1, retries, err.Error()) |
| 96 | + }, |
| 97 | + ), |
| 98 | + retry.DelayType(retry.CombineDelay(retry.FixedDelay, retry.RandomDelay)), |
| 99 | + retry.MaxJitter(time.Second*2), |
| 100 | + retry.Delay(time.Second*3), |
| 101 | + retry.Attempts(5), |
| 102 | + retry.LastErrorOnly(true), |
| 103 | + ) |
| 104 | +} |
| 105 | + |
| 106 | +func (p *Lock) tryLock(h *cluster.Host) error { |
| 107 | + lfp := h.Configurer.K0sctlLockFilePath(h) |
| 108 | + |
| 109 | + if err := h.Configurer.UpsertFile(h, lfp, p.instanceID); err != nil { |
| 110 | + stat, err := h.Configurer.Stat(h, lfp, exec.Sudo(h)) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("lock file disappeared: %w", err) |
| 113 | + } |
| 114 | + content, err := h.Configurer.ReadFile(h, lfp) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("failed to read lock file: %w", err) |
| 117 | + } |
| 118 | + if content != p.instanceID { |
| 119 | + if time.Since(stat.ModTime()) < 30*time.Second { |
| 120 | + return fmt.Errorf("another instance of k0sctl is currently operating on the host") |
| 121 | + } |
| 122 | + _ = h.Configurer.DeleteFile(h, lfp) |
| 123 | + return fmt.Errorf("removed existing expired lock file") |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
0 commit comments