-
Notifications
You must be signed in to change notification settings - Fork 37
/
context_impl.go
108 lines (90 loc) · 2.67 KB
/
context_impl.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package context
import (
"github.com/Apicurio/apicurio-registry-operator/controllers/client"
c "github.com/Apicurio/apicurio-registry-operator/controllers/common"
"github.com/Apicurio/apicurio-registry-operator/controllers/svc/env"
"github.com/Apicurio/apicurio-registry-operator/controllers/svc/resources"
"go.uber.org/zap"
"time"
)
var _ LoopContext = &loopContext{}
// A long-lived singleton container for shared, data only, 0 dependencies, components
type loopContext struct {
appName c.Name
appNamespace c.Namespace
log *zap.Logger
requeue bool
requeueDelay time.Duration
resourceCache resources.ResourceCache
envCache env.EnvCache
attempts int
clients *client.Clients
testing *c.TestSupport
features *c.SupportedFeatures
}
// Create a new context when the operator is deployed, provide mostly static data
func NewLoopContext(appName c.Name, appNamespace c.Namespace, log *zap.Logger, clients *client.Clients, testing *c.TestSupport, features *c.SupportedFeatures) LoopContext {
this := &loopContext{
appName: appName,
appNamespace: appNamespace,
requeue: false,
requeueDelay: 0,
clients: clients,
testing: testing,
log: log,
features: features,
}
this.resourceCache = resources.NewResourceCache()
this.envCache = env.NewEnvCache(log)
return this
}
func (this *loopContext) GetLog() *zap.Logger {
return this.log
}
func (this *loopContext) GetAppName() c.Name {
return this.appName
}
func (this *loopContext) GetAppNamespace() c.Namespace {
return this.appNamespace
}
func (this *loopContext) SetRequeueNow() {
this.SetRequeueDelaySec(0)
}
func (this *loopContext) SetRequeueDelaySoon() {
this.SetRequeueDelaySec(5)
}
func (this *loopContext) SetRequeueDelaySec(delay uint) {
d := time.Duration(delay) * time.Second
if this.requeue == false || d < this.requeueDelay {
this.requeueDelay = d
this.requeue = true
}
}
func (this *loopContext) GetAndResetRequeue() (bool, time.Duration) {
defer func() {
this.requeue = false
this.requeueDelay = 0
}()
return this.requeue, this.requeueDelay
}
func (this *loopContext) GetClients() *client.Clients {
return this.clients
}
func (this *loopContext) GetResourceCache() resources.ResourceCache {
return this.resourceCache
}
func (this *loopContext) GetEnvCache() env.EnvCache {
return this.envCache
}
func (this *loopContext) SetAttempts(attempts int) {
this.attempts = attempts
}
func (this *loopContext) GetAttempts() int {
return this.attempts
}
func (this *loopContext) GetTestingSupport() *c.TestSupport {
return this.testing
}
func (this *loopContext) GetSupportedFeatures() *c.SupportedFeatures {
return this.features
}