forked from knative/eventing
/
ref.go
155 lines (128 loc) · 3.98 KB
/
ref.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
/*
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 apiserver
import (
"context"
"reflect"
cloudevents "github.com/cloudevents/sdk-go"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/cache"
"knative.dev/eventing/pkg/adapter/apiserver/events"
"knative.dev/pkg/source"
)
type ref struct {
ce cloudevents.Client
source string
eventType string
logger *zap.SugaredLogger
controlledGVRs []schema.GroupVersionResource
reporter source.StatsReporter
namespace string
name string
}
var _ cache.Store = (*ref)(nil)
// TODO: I think asController is not the feature we want. I think we want to be
// able to set the controller as a filter to the watch. Not emit all owners of
// the resource. Fix this. It has to be an api change on the CRD.
func (a *ref) asController(obj interface{}) bool {
if len(a.controlledGVRs) > 0 {
object := obj.(*unstructured.Unstructured)
gvk := object.GroupVersionKind()
// TODO: pass down the resource and the kind so we do not have to guess.
gvr, _ := meta.UnsafeGuessKindToResource(gvk)
for _, gvrc := range a.controlledGVRs {
if reflect.DeepEqual(gvr, gvrc) {
return true
}
}
}
return false
}
// Implements cache.Store
func (a *ref) Add(obj interface{}) error {
event, err := events.MakeAddRefEvent(a.source, a.asController(obj), obj)
if err != nil {
a.logger.Info("event creation failed", zap.Error(err))
return err
}
return a.sendEvent(context.Background(), event)
}
// Implements cache.Store
func (a *ref) Update(obj interface{}) error {
event, err := events.MakeUpdateRefEvent(a.source, a.asController(obj), obj)
if err != nil {
a.logger.Info("event creation failed", zap.Error(err))
return err
}
return a.sendEvent(context.Background(), event)
}
// Implements cache.Store
func (a *ref) Delete(obj interface{}) error {
event, err := events.MakeDeleteRefEvent(a.source, a.asController(obj), obj)
if err != nil {
a.logger.Info("event creation failed", zap.Error(err))
return err
}
return a.sendEvent(context.Background(), event)
}
func (a *ref) addControllerWatch(gvr schema.GroupVersionResource) {
if a.controlledGVRs == nil {
a.controlledGVRs = []schema.GroupVersionResource{gvr}
return
}
a.controlledGVRs = append(a.controlledGVRs, gvr)
}
func (a *ref) sendEvent(ctx context.Context, event *cloudevents.Event) error {
reportArgs := &source.ReportArgs{
Namespace: a.namespace,
EventSource: event.Source(),
EventType: event.Type(),
Name: a.name,
ResourceGroup: resourceGroup,
}
rctx, _, err := a.ce.Send(ctx, *event)
if err != nil {
a.logger.Info("failed to send a ref based event ", zap.Error(err))
}
rtctx := cloudevents.HTTPTransportContextFrom(rctx)
a.reporter.ReportEventCount(reportArgs, rtctx.StatusCode)
return err
}
// Stub cache.Store impl
// Implements cache.Store
func (a *ref) List() []interface{} {
return nil
}
// Implements cache.Store
func (a *ref) ListKeys() []string {
return nil
}
// Implements cache.Store
func (a *ref) Get(obj interface{}) (item interface{}, exists bool, err error) {
return nil, false, nil
}
// Implements cache.Store
func (a *ref) GetByKey(key string) (item interface{}, exists bool, err error) {
return nil, false, nil
}
// Implements cache.Store
func (a *ref) Replace([]interface{}, string) error {
return nil
}
// Implements cache.Store
func (a *ref) Resync() error {
return nil
}