-
Notifications
You must be signed in to change notification settings - Fork 53
/
utils.go
49 lines (41 loc) · 1.34 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package uninstall
import (
"context"
"fmt"
"time"
"go.uber.org/zap/zapcore"
capabilityv1 "github.com/rancher/opni/pkg/apis/capability/v1"
"github.com/rancher/opni/pkg/task"
)
type TimestampedMetadata struct {
capabilityv1.DefaultUninstallOptions `json:",inline,omitempty"`
DeletionTimestamp time.Time `json:"deletionTimestamp,omitempty"`
}
type DefaultPendingHandler struct{}
func (DefaultPendingHandler) OnTaskPending(ctx context.Context, ti task.ActiveTask) error {
var md TimestampedMetadata
ti.LoadTaskMetadata(&md)
if md.DeleteStoredData {
ti.AddLogEntry(zapcore.WarnLevel, "Stored data will be deleted")
} else {
ti.AddLogEntry(zapcore.InfoLevel, "Stored data will not be deleted")
}
if md.InitialDelay > 0 {
endTime := md.DeletionTimestamp.Add(time.Duration(md.InitialDelay))
now := time.Now()
// sleep until endTime or context is cancelled
if endTime.After(now) {
ti.AddLogEntry(zapcore.InfoLevel, fmt.Sprintf("Delaying uninstall until %s (%s from now)", endTime.Format(time.Stamp), endTime.Sub(now).Round(time.Second)))
timer := time.NewTimer(endTime.Sub(now))
defer timer.Stop()
select {
case <-ctx.Done():
ti.AddLogEntry(zapcore.InfoLevel, "Uninstall canceled during delay period; no changes will be made")
return ctx.Err()
case <-timer.C:
return nil
}
}
}
return nil
}