-
Notifications
You must be signed in to change notification settings - Fork 586
/
sinkbinding_lifecycle.go
325 lines (282 loc) · 11 KB
/
sinkbinding_lifecycle.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1
import (
"context"
"encoding/json"
"fmt"
"strings"
"go.uber.org/zap"
corev1listers "k8s.io/client-go/listers/core/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/logging"
"knative.dev/pkg/tracker"
"knative.dev/eventing/pkg/eventingtls"
)
const (
oidcTokenVolumeName = "oidc-token"
)
var sbCondSet = apis.NewLivingConditionSet(
SinkBindingConditionSinkProvided,
SinkBindingConditionOIDCIdentityCreated,
SinkBindingConditionOIDCTokenSecretCreated,
)
// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface.
func (*SinkBinding) GetConditionSet() apis.ConditionSet {
return sbCondSet
}
// GetGroupVersionKind returns the GroupVersionKind.
func (*SinkBinding) GetGroupVersionKind() schema.GroupVersionKind {
return SchemeGroupVersion.WithKind("SinkBinding")
}
// GetUntypedSpec implements apis.HasSpec
func (s *SinkBinding) GetUntypedSpec() interface{} {
return s.Spec
}
// GetSubject implements psbinding.Bindable
func (sb *SinkBinding) GetSubject() tracker.Reference {
return sb.Spec.Subject
}
// GetBindingStatus implements psbinding.Bindable
func (sb *SinkBinding) GetBindingStatus() duck.BindableStatus {
return &sb.Status
}
// SetObservedGeneration implements psbinding.BindableStatus
func (sbs *SinkBindingStatus) SetObservedGeneration(gen int64) {
sbs.ObservedGeneration = gen
}
// InitializeConditions populates the SinkBindingStatus's conditions field
// with all of its conditions configured to Unknown.
func (sbs *SinkBindingStatus) InitializeConditions() {
sbCondSet.Manage(sbs).InitializeConditions()
}
// MarkBindingUnavailable marks the SinkBinding's Ready condition to False with
// the provided reason and message.
func (sbs *SinkBindingStatus) MarkBindingUnavailable(reason, message string) {
sbCondSet.Manage(sbs).MarkFalse(SinkBindingConditionReady, reason, message)
}
// MarkBindingAvailable marks the SinkBinding's Ready condition to True.
func (sbs *SinkBindingStatus) MarkBindingAvailable() {
sbCondSet.Manage(sbs).MarkTrue(SinkBindingConditionReady)
}
// MarkSink sets the condition that the source has a sink configured.
func (sbs *SinkBindingStatus) MarkSink(addr *duckv1.Addressable) {
if addr != nil {
sbs.SinkURI = addr.URL
sbs.SinkCACerts = addr.CACerts
sbs.SinkAudience = addr.Audience
sbCondSet.Manage(sbs).MarkTrue(SinkBindingConditionSinkProvided)
} else {
sbCondSet.Manage(sbs).MarkFalse(SinkBindingConditionSinkProvided, "SinkEmpty", "Sink has resolved to empty.%s", "")
}
}
func (sbs *SinkBindingStatus) MarkOIDCIdentityCreatedSucceeded() {
sbCondSet.Manage(sbs).MarkTrue(SinkBindingConditionOIDCIdentityCreated)
}
func (sbs *SinkBindingStatus) MarkOIDCIdentityCreatedSucceededWithReason(reason, messageFormat string, messageA ...interface{}) {
sbCondSet.Manage(sbs).MarkTrueWithReason(SinkBindingConditionOIDCIdentityCreated, reason, messageFormat, messageA...)
}
func (sbs *SinkBindingStatus) MarkOIDCIdentityCreatedFailed(reason, messageFormat string, messageA ...interface{}) {
sbCondSet.Manage(sbs).MarkFalse(SinkBindingConditionOIDCIdentityCreated, reason, messageFormat, messageA...)
}
func (sbs *SinkBindingStatus) MarkOIDCIdentityCreatedUnknown(reason, messageFormat string, messageA ...interface{}) {
sbCondSet.Manage(sbs).MarkUnknown(SinkBindingConditionOIDCIdentityCreated, reason, messageFormat, messageA...)
}
func (sbs *SinkBindingStatus) MarkOIDCTokenSecretCreatedSuccceeded() {
sbCondSet.Manage(sbs).MarkTrue(SinkBindingConditionOIDCTokenSecretCreated)
}
func (sbs *SinkBindingStatus) MarkOIDCTokenSecretCreatedSuccceededWithReason(reason, messageFormat string, messageA ...interface{}) {
sbCondSet.Manage(sbs).MarkTrueWithReason(SinkBindingConditionOIDCTokenSecretCreated, reason, messageFormat, messageA...)
}
func (sbs *SinkBindingStatus) MarkOIDCTokenSecretCreatedFailed(reason, messageFormat string, messageA ...interface{}) {
sbCondSet.Manage(sbs).MarkFalse(SinkBindingConditionOIDCTokenSecretCreated, reason, messageFormat, messageA...)
}
func (sbs *SinkBindingStatus) MarkOIDCTokenSecretCreatedUnknown(reason, messageFormat string, messageA ...interface{}) {
sbCondSet.Manage(sbs).MarkUnknown(SinkBindingConditionOIDCTokenSecretCreated, reason, messageFormat, messageA...)
}
// Do implements psbinding.Bindable
func (sb *SinkBinding) Do(ctx context.Context, ps *duckv1.WithPod) {
// First undo so that we can just unconditionally append below.
sb.Undo(ctx, ps)
resolver := GetURIResolver(ctx)
if resolver == nil {
logging.FromContext(ctx).Errorf("No Resolver associated with context for sink: %+v", sb)
return
}
addr, err := resolver.AddressableFromDestinationV1(ctx, sb.Spec.Sink, sb)
if err != nil {
logging.FromContext(ctx).Errorw("URI could not be extracted from destination: ", zap.Error(err))
return
}
sb.Status.MarkSink(addr)
var ceOverrides string
if sb.Spec.CloudEventOverrides != nil {
if co, err := json.Marshal(sb.Spec.SourceSpec.CloudEventOverrides); err != nil {
logging.FromContext(ctx).Errorw(fmt.Sprintf("Failed to marshal CloudEventOverrides into JSON for %+v", sb), zap.Error(err))
} else if len(co) > 0 {
ceOverrides = string(co)
}
}
for i := range ps.Spec.Template.Spec.InitContainers {
ps.Spec.Template.Spec.InitContainers[i].Env = append(ps.Spec.Template.Spec.InitContainers[i].Env, corev1.EnvVar{
Name: "K_SINK",
Value: addr.URL.String(),
})
if addr.CACerts != nil {
ps.Spec.Template.Spec.InitContainers[i].Env = append(ps.Spec.Template.Spec.InitContainers[i].Env, corev1.EnvVar{
Name: "K_CA_CERTS",
Value: *addr.CACerts,
})
}
ps.Spec.Template.Spec.InitContainers[i].Env = append(ps.Spec.Template.Spec.InitContainers[i].Env, corev1.EnvVar{
Name: "K_CE_OVERRIDES",
Value: ceOverrides,
})
}
for i := range ps.Spec.Template.Spec.Containers {
ps.Spec.Template.Spec.Containers[i].Env = append(ps.Spec.Template.Spec.Containers[i].Env, corev1.EnvVar{
Name: "K_SINK",
Value: addr.URL.String(),
})
if addr.CACerts != nil {
ps.Spec.Template.Spec.Containers[i].Env = append(ps.Spec.Template.Spec.Containers[i].Env, corev1.EnvVar{
Name: "K_CA_CERTS",
Value: *addr.CACerts,
})
}
ps.Spec.Template.Spec.Containers[i].Env = append(ps.Spec.Template.Spec.Containers[i].Env, corev1.EnvVar{
Name: "K_CE_OVERRIDES",
Value: ceOverrides,
})
}
pss, err := eventingtls.AddTrustBundleVolumes(GetTrustBundleConfigMapLister(ctx), sb, &ps.Spec.Template.Spec)
if err != nil {
logging.FromContext(ctx).Errorw("Failed to add trust bundle volumes %s/%s: %+v", zap.Error(err))
return
}
ps.Spec.Template.Spec = *pss
if sb.Status.OIDCTokenSecretName != nil {
ps.Spec.Template.Spec.Volumes = append(ps.Spec.Template.Spec.Volumes, corev1.Volume{
Name: oidcTokenVolumeName,
VolumeSource: corev1.VolumeSource{
Projected: &corev1.ProjectedVolumeSource{
Sources: []corev1.VolumeProjection{
{
Secret: &corev1.SecretProjection{
LocalObjectReference: corev1.LocalObjectReference{
Name: *sb.Status.OIDCTokenSecretName,
},
},
},
},
},
},
})
for i := range ps.Spec.Template.Spec.Containers {
ps.Spec.Template.Spec.Containers[i].VolumeMounts = append(ps.Spec.Template.Spec.Containers[i].VolumeMounts, corev1.VolumeMount{
Name: oidcTokenVolumeName,
MountPath: "/oidc",
})
}
for i := range ps.Spec.Template.Spec.InitContainers {
ps.Spec.Template.Spec.InitContainers[i].VolumeMounts = append(ps.Spec.Template.Spec.InitContainers[i].VolumeMounts, corev1.VolumeMount{
Name: oidcTokenVolumeName,
MountPath: "/oidc",
})
}
}
}
func (sb *SinkBinding) Undo(ctx context.Context, ps *duckv1.WithPod) {
for i, c := range ps.Spec.Template.Spec.InitContainers {
if len(c.Env) > 0 {
env := make([]corev1.EnvVar, 0, len(ps.Spec.Template.Spec.InitContainers[i].Env))
for j, ev := range c.Env {
switch ev.Name {
case "K_SINK", "K_CE_OVERRIDES", "K_CA_CERTS":
continue
default:
env = append(env, ps.Spec.Template.Spec.InitContainers[i].Env[j])
}
}
ps.Spec.Template.Spec.InitContainers[i].Env = env
}
if len(ps.Spec.Template.Spec.InitContainers[i].VolumeMounts) > 0 {
volumeMounts := make([]corev1.VolumeMount, 0, len(ps.Spec.Template.Spec.InitContainers[i].VolumeMounts))
for j, vol := range c.VolumeMounts {
if vol.Name == oidcTokenVolumeName {
continue
}
if strings.HasPrefix(vol.Name, eventingtls.TrustBundleVolumeNamePrefix) {
continue
}
volumeMounts = append(volumeMounts, ps.Spec.Template.Spec.InitContainers[i].VolumeMounts[j])
}
ps.Spec.Template.Spec.InitContainers[i].VolumeMounts = volumeMounts
}
}
for i, c := range ps.Spec.Template.Spec.Containers {
if len(c.Env) > 0 {
env := make([]corev1.EnvVar, 0, len(ps.Spec.Template.Spec.Containers[i].Env))
for j, ev := range c.Env {
switch ev.Name {
case "K_SINK", "K_CE_OVERRIDES", "K_CA_CERTS":
continue
default:
env = append(env, ps.Spec.Template.Spec.Containers[i].Env[j])
}
}
ps.Spec.Template.Spec.Containers[i].Env = env
}
if len(ps.Spec.Template.Spec.Containers[i].VolumeMounts) > 0 {
volumeMounts := make([]corev1.VolumeMount, 0, len(ps.Spec.Template.Spec.Containers[i].VolumeMounts))
for j, vol := range c.VolumeMounts {
if vol.Name == oidcTokenVolumeName {
continue
}
if strings.HasPrefix(vol.Name, eventingtls.TrustBundleVolumeNamePrefix) {
continue
}
volumeMounts = append(volumeMounts, ps.Spec.Template.Spec.Containers[i].VolumeMounts[j])
}
ps.Spec.Template.Spec.Containers[i].VolumeMounts = volumeMounts
}
}
if len(ps.Spec.Template.Spec.Volumes) > 0 {
volumes := make([]corev1.Volume, 0, len(ps.Spec.Template.Spec.Volumes))
for i, vol := range ps.Spec.Template.Spec.Volumes {
if vol.Name == oidcTokenVolumeName {
continue
}
if strings.HasPrefix(vol.Name, eventingtls.TrustBundleVolumeNamePrefix) {
continue
}
volumes = append(volumes, ps.Spec.Template.Spec.Volumes[i])
}
ps.Spec.Template.Spec.Volumes = volumes
}
}
type configMapListerKey struct{}
func WithTrustBundleConfigMapLister(ctx context.Context, lister corev1listers.ConfigMapLister) context.Context {
return context.WithValue(ctx, configMapListerKey{}, lister)
}
func GetTrustBundleConfigMapLister(ctx context.Context) corev1listers.ConfigMapLister {
value := ctx.Value(configMapListerKey{})
if value == nil {
panic("No ConfigMapLister found in context.")
}
return value.(corev1listers.ConfigMapLister)
}