-
Notifications
You must be signed in to change notification settings - Fork 575
/
sinkbinding_lifecycle.go
120 lines (101 loc) · 3.46 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
/*
Copyright 2019 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 v1alpha1
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/eventing/pkg/logging"
"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/tracker"
)
var sbCondSet = apis.NewLivingConditionSet()
// GetGroupVersionKind returns the GroupVersionKind.
func (s *SinkBinding) GetGroupVersionKind() schema.GroupVersionKind {
return SchemeGroupVersion.WithKind("SinkBinding")
}
// GetUntypedSpec implements apis.HasSpec
func (c *SinkBinding) GetUntypedSpec() interface{} {
return c.Spec
}
// GetSubject implements psbinding.Bindable
func (fb *SinkBinding) GetSubject() tracker.Reference {
return fb.Spec.Subject
}
// GetBindingStatus implements psbinding.Bindable
func (fb *SinkBinding) GetBindingStatus() duck.BindableStatus {
return &fb.Status
}
// SetObservedGeneration implements psbinding.BindableStatus
func (fbs *SinkBindingStatus) SetObservedGeneration(gen int64) {
fbs.ObservedGeneration = gen
}
// InitializeConditions populates the SinkBindingStatus's conditions field
// with all of its conditions configured to Unknown.
func (fbs *SinkBindingStatus) InitializeConditions() {
sbCondSet.Manage(fbs).InitializeConditions()
}
// MarkBindingUnavailable marks the SinkBinding's Ready condition to False with
// the provided reason and message.
func (fbs *SinkBindingStatus) MarkBindingUnavailable(reason, message string) {
sbCondSet.Manage(fbs).MarkFalse(SinkBindingConditionReady, reason, message)
}
// MarkBindingAvailable marks the SinkBinding's Ready condition to True.
func (fbs *SinkBindingStatus) MarkBindingAvailable() {
sbCondSet.Manage(fbs).MarkTrue(SinkBindingConditionReady)
}
// Do implements psbinding.Bindable
func (fb *SinkBinding) Do(ctx context.Context, ps *duckv1.WithPod) {
// First undo so that we can just unconditionally append below.
fb.Undo(ctx, ps)
uri := GetSinkURI(ctx)
if uri == nil {
logging.FromContext(ctx).Error(fmt.Sprintf("No sink URI associated with context for %+v", fb))
return
}
spec := ps.Spec.Template.Spec
for i := range spec.InitContainers {
spec.InitContainers[i].Env = append(spec.InitContainers[i].Env, corev1.EnvVar{
Name: "K_SINK",
Value: uri.String(),
})
}
for i := range spec.Containers {
spec.Containers[i].Env = append(spec.Containers[i].Env, corev1.EnvVar{
Name: "K_SINK",
Value: uri.String(),
})
}
}
func (fb *SinkBinding) Undo(ctx context.Context, ps *duckv1.WithPod) {
spec := ps.Spec.Template.Spec
for i, c := range spec.InitContainers {
for j, ev := range c.Env {
if ev.Name == "K_SINK" {
spec.InitContainers[i].Env = append(spec.InitContainers[i].Env[:j], spec.InitContainers[i].Env[j+1:]...)
break
}
}
}
for i, c := range spec.Containers {
for j, ev := range c.Env {
if ev.Name == "K_SINK" {
spec.Containers[i].Env = append(spec.Containers[i].Env[:j], spec.Containers[i].Env[j+1:]...)
break
}
}
}
}