/
context.go
131 lines (107 loc) · 3.61 KB
/
context.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package workflow
import (
"go.mongodb.org/atlas/mongodbatlas"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/atlas"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/watch"
)
// Context is a container for some information that is needed on all levels of function calls during reconciliation.
// It's mutable by design.
// Note, that it's completely different from the Go Context
type Context struct {
// Log is the root logger used in the reconciliation. Used just for convenience to avoid passing log to each
// method.
// Is not supposed to be mutated!
Log *zap.SugaredLogger
// Client is a mongodb atlas client used to make v1.0 API calls
Client mongodbatlas.Client
// Connection is an object encapsulating information about connecting to Atlas using API
Connection atlas.Connection
status Status
// This is the condition happened the last (most of all it contains the most important information that needs
// to be logged)
lastCondition *status.Condition
// lastConditionWarn indicates if the last "terminal" condition was expected (for example wait for some resource)
// or unexpected (any errors)
lastConditionWarn bool
// A list of sub-resources to add to a resource watcher after the Reconcile loop
resourcesToWatch []watch.WatchedObject
}
func NewContext(log *zap.SugaredLogger, conditions []status.Condition) *Context {
return &Context{
status: NewStatus(conditions),
Log: log,
}
}
func (c Context) Conditions() []status.Condition {
return c.status.conditions
}
func (c Context) GetCondition(conditionType status.ConditionType) (condition status.Condition, found bool) {
return c.status.GetCondition(conditionType)
}
func (c Context) StatusOptions() []status.Option {
return c.status.options
}
func (c Context) LastCondition() *status.Condition {
return c.lastCondition
}
func (c Context) LastConditionWarn() bool {
return c.lastConditionWarn
}
func (c *Context) EnsureStatusOption(option status.Option) *Context {
c.status.EnsureOption(option)
return c
}
func (c *Context) EnsureCondition(condition status.Condition) *Context {
c.status.EnsureCondition(condition)
c.lastCondition = &condition
return c
}
func (c *Context) SetConditionFromResult(conditionType status.ConditionType, result Result) *Context {
condition := status.Condition{
Type: conditionType,
Status: corev1.ConditionFalse,
Reason: string(result.reason),
Message: result.message,
}
if result.IsOk() {
condition.Status = corev1.ConditionTrue
}
c.EnsureCondition(condition)
c.lastConditionWarn = result.warning
return c
}
func (c *Context) SetConditionFalse(conditionType status.ConditionType) *Context {
c.EnsureCondition(status.Condition{
Type: conditionType,
Status: corev1.ConditionFalse,
})
return c
}
func (c *Context) SetConditionFalseMsg(conditionType status.ConditionType, msg string) *Context {
c.EnsureCondition(status.Condition{
Type: conditionType,
Status: corev1.ConditionFalse,
Message: msg,
})
return c
}
func (c *Context) SetConditionTrue(conditionType status.ConditionType) *Context {
c.EnsureCondition(status.Condition{
Type: conditionType,
Status: corev1.ConditionTrue,
})
return c
}
func (c *Context) UnsetCondition(conditionType status.ConditionType) *Context {
c.status.RemoveCondition(conditionType)
return c
}
func (c *Context) AddResourcesToWatch(resources ...watch.WatchedObject) {
c.resourcesToWatch = append(c.resourcesToWatch, resources...)
}
func (c *Context) ListResourcesToWatch() []watch.WatchedObject {
return c.resourcesToWatch
}