-
Notifications
You must be signed in to change notification settings - Fork 217
/
binding_context.go
187 lines (160 loc) · 4.68 KB
/
binding_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package bindingcontext
import (
"encoding/json"
"github.com/deckhouse/deckhouse/pkg/log"
v1 "k8s.io/api/admission/v1"
apixv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
htypes "github.com/flant/shell-operator/pkg/hook/types"
kemtypes "github.com/flant/shell-operator/pkg/kube_events_manager/types"
)
// BindingContext contains information about event for hook
type BindingContext struct {
Metadata struct {
Version string
BindingType htypes.BindingType
JqFilter string
IncludeSnapshots []string
IncludeAllSnapshots bool
Group string
}
// name of a binding or a group or kubeEventType if binding has no 'name' field
Binding string
// additional fields for 'kubernetes' binding
Type kemtypes.KubeEventType
WatchEvent kemtypes.WatchEventType
Objects []kemtypes.ObjectAndFilterResult
Snapshots map[string][]kemtypes.ObjectAndFilterResult
AdmissionReview *v1.AdmissionReview
ConversionReview *apixv1.ConversionReview
FromVersion string
ToVersion string
}
func (bc BindingContext) IsSynchronization() bool {
return bc.Metadata.BindingType == htypes.OnKubernetesEvent && bc.Type == kemtypes.TypeSynchronization
}
func (bc BindingContext) MarshalJSON() ([]byte, error) {
return json.Marshal(bc.Map())
}
func (bc BindingContext) Map() map[string]interface{} {
switch bc.Metadata.Version {
case "v0":
return bc.MapV0()
case "v1":
return bc.MapV1()
default:
log.Errorf("Possible bug!!! Call Map for BindingContext without version.")
return make(map[string]interface{})
}
}
func (bc BindingContext) MapV1() map[string]interface{} {
res := make(map[string]interface{})
res["binding"] = bc.Binding
if bc.Metadata.BindingType == htypes.OnStartup {
return res
}
// Set "snapshots" field if needed.
if len(bc.Metadata.IncludeSnapshots) > 0 || bc.Metadata.IncludeAllSnapshots {
if len(bc.Snapshots) > 0 {
res["snapshots"] = bc.Snapshots
} else {
res["snapshots"] = map[string]string{}
}
}
// Handle admission and conversion before grouping.
if bc.Metadata.BindingType == htypes.KubernetesValidating {
res["type"] = "Validating"
res["review"] = bc.AdmissionReview
return res
}
if bc.Metadata.BindingType == htypes.KubernetesMutating {
res["type"] = "Mutating"
res["review"] = bc.AdmissionReview
return res
}
if bc.Metadata.BindingType == htypes.KubernetesConversion {
res["type"] = "Conversion"
res["fromVersion"] = bc.FromVersion
res["toVersion"] = bc.ToVersion
res["review"] = bc.ConversionReview
return res
}
// Group is always has "type: Group", even for Synchronization.
if bc.Metadata.Group != "" {
res["type"] = "Group"
res["groupName"] = bc.Metadata.Group
return res
}
if bc.Metadata.BindingType == htypes.Schedule {
res["type"] = "Schedule"
return res
}
// A short way for addon-operator's hooks.
if bc.Metadata.BindingType != htypes.OnKubernetesEvent || bc.Type == "" {
return res
}
// So, this BindingContext is for "kubernetes" binding.
res["type"] = bc.Type
// omitempty for watchEvent
if bc.WatchEvent != "" {
res["watchEvent"] = string(bc.WatchEvent)
}
switch bc.Type {
case kemtypes.TypeSynchronization:
if len(bc.Objects) == 0 {
res["objects"] = make([]string, 0)
} else {
res["objects"] = bc.Objects
}
case kemtypes.TypeEvent:
if len(bc.Objects) == 0 {
res["object"] = nil
if bc.Metadata.JqFilter != "" {
res["filterResult"] = ""
}
} else {
// Copy object and filterResult from the first item.
obj := bc.Objects[0]
objMap := obj.Map()
for k, v := range objMap {
res[k] = v
}
}
}
return res
}
func (bc BindingContext) MapV0() map[string]interface{} {
res := make(map[string]interface{})
res["binding"] = bc.Binding
if bc.Metadata.BindingType != htypes.OnKubernetesEvent {
return res
}
eventV0 := ""
switch bc.WatchEvent {
case kemtypes.WatchEventAdded:
eventV0 = "add"
case kemtypes.WatchEventModified:
eventV0 = "update"
case kemtypes.WatchEventDeleted:
eventV0 = "delete"
}
res["resourceEvent"] = eventV0
if len(bc.Objects) > 0 {
res["resourceNamespace"] = bc.Objects[0].Object.GetNamespace()
res["resourceKind"] = bc.Objects[0].Object.GetKind()
res["resourceName"] = bc.Objects[0].Object.GetName()
}
return res
}
type BindingContextList []map[string]interface{}
func ConvertBindingContextList(version string, contexts []BindingContext) BindingContextList {
res := make([]map[string]interface{}, len(contexts))
for i, context := range contexts {
context.Metadata.Version = version
res[i] = context.Map()
}
return res
}
func (b BindingContextList) Json() ([]byte, error) {
data, err := json.MarshalIndent(b, "", " ")
return data, err
}