-
Notifications
You must be signed in to change notification settings - Fork 25
/
controller.go
151 lines (130 loc) · 4.42 KB
/
controller.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
/*
Copyright 2020 VMware, Inc.
SPDX-License-Identifier: Apache-2.0
*/
// Code generated by injection-gen. DO NOT EDIT.
package vspheresource
import (
context "context"
fmt "fmt"
reflect "reflect"
strings "strings"
versionedscheme "github.com/vmware-tanzu/sources-for-knative/pkg/client/clientset/versioned/scheme"
client "github.com/vmware-tanzu/sources-for-knative/pkg/client/injection/client"
vspheresource "github.com/vmware-tanzu/sources-for-knative/pkg/client/injection/informers/sources/v1alpha1/vspheresource"
zap "go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
labels "k8s.io/apimachinery/pkg/labels"
types "k8s.io/apimachinery/pkg/types"
watch "k8s.io/apimachinery/pkg/watch"
scheme "k8s.io/client-go/kubernetes/scheme"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
record "k8s.io/client-go/tools/record"
kubeclient "knative.dev/pkg/client/injection/kube/client"
controller "knative.dev/pkg/controller"
logging "knative.dev/pkg/logging"
logkey "knative.dev/pkg/logging/logkey"
reconciler "knative.dev/pkg/reconciler"
)
const (
defaultControllerAgentName = "vspheresource-controller"
defaultFinalizerName = "vspheresources.sources.tanzu.vmware.com"
)
// NewImpl returns a controller.Impl that handles queuing and feeding work from
// the queue through an implementation of controller.Reconciler, delegating to
// the provided Interface and optional Finalizer methods. OptionsFn is used to return
// controller.ControllerOptions to be used by the internal reconciler.
func NewImpl(ctx context.Context, r Interface, optionsFns ...controller.OptionsFn) *controller.Impl {
logger := logging.FromContext(ctx)
// Check the options function input. It should be 0 or 1.
if len(optionsFns) > 1 {
logger.Fatal("Up to one options function is supported, found: ", len(optionsFns))
}
vspheresourceInformer := vspheresource.Get(ctx)
lister := vspheresourceInformer.Lister()
var promoteFilterFunc func(obj interface{}) bool
rec := &reconcilerImpl{
LeaderAwareFuncs: reconciler.LeaderAwareFuncs{
PromoteFunc: func(bkt reconciler.Bucket, enq func(reconciler.Bucket, types.NamespacedName)) error {
all, err := lister.List(labels.Everything())
if err != nil {
return err
}
for _, elt := range all {
if promoteFilterFunc != nil {
if ok := promoteFilterFunc(elt); !ok {
continue
}
}
enq(bkt, types.NamespacedName{
Namespace: elt.GetNamespace(),
Name: elt.GetName(),
})
}
return nil
},
},
Client: client.Get(ctx),
Lister: lister,
reconciler: r,
finalizerName: defaultFinalizerName,
}
ctrType := reflect.TypeOf(r).Elem()
ctrTypeName := fmt.Sprintf("%s.%s", ctrType.PkgPath(), ctrType.Name())
ctrTypeName = strings.ReplaceAll(ctrTypeName, "/", ".")
logger = logger.With(
zap.String(logkey.ControllerType, ctrTypeName),
zap.String(logkey.Kind, "sources.tanzu.vmware.com.VSphereSource"),
)
impl := controller.NewContext(ctx, rec, controller.ControllerOptions{WorkQueueName: ctrTypeName, Logger: logger})
agentName := defaultControllerAgentName
// Pass impl to the options. Save any optional results.
for _, fn := range optionsFns {
opts := fn(impl)
if opts.ConfigStore != nil {
rec.configStore = opts.ConfigStore
}
if opts.FinalizerName != "" {
rec.finalizerName = opts.FinalizerName
}
if opts.AgentName != "" {
agentName = opts.AgentName
}
if opts.SkipStatusUpdates {
rec.skipStatusUpdates = true
}
if opts.DemoteFunc != nil {
rec.DemoteFunc = opts.DemoteFunc
}
if opts.PromoteFilterFunc != nil {
promoteFilterFunc = opts.PromoteFilterFunc
}
}
rec.Recorder = createRecorder(ctx, agentName)
return impl
}
func createRecorder(ctx context.Context, agentName string) record.EventRecorder {
logger := logging.FromContext(ctx)
recorder := controller.GetEventRecorder(ctx)
if recorder == nil {
// Create event broadcaster
logger.Debug("Creating event broadcaster")
eventBroadcaster := record.NewBroadcaster()
watches := []watch.Interface{
eventBroadcaster.StartLogging(logger.Named("event-broadcaster").Infof),
eventBroadcaster.StartRecordingToSink(
&v1.EventSinkImpl{Interface: kubeclient.Get(ctx).CoreV1().Events("")}),
}
recorder = eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: agentName})
go func() {
<-ctx.Done()
for _, w := range watches {
w.Stop()
}
}()
}
return recorder
}
func init() {
versionedscheme.AddToScheme(scheme.Scheme)
}