This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
/
reconciler.go
197 lines (161 loc) · 6.13 KB
/
reconciler.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
/*
Copyright 2019 Google LLC
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 reconciler
import (
"context"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
"go.uber.org/zap"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection/clients/dynamicclient"
"knative.dev/pkg/logging"
"knative.dev/pkg/system"
servingclientset "knative.dev/serving/pkg/client/clientset/versioned"
servingclient "knative.dev/serving/pkg/client/injection/client"
clientset "github.com/google/knative-gcp/pkg/client/clientset/versioned"
runScheme "github.com/google/knative-gcp/pkg/client/clientset/versioned/scheme"
runclient "github.com/google/knative-gcp/pkg/client/injection/client"
)
const (
// DefaultResyncPeriod sets the period between reconciliations in case nothing we are watching within the
// cluster changed. This is needed because an external resource (e.g., Pub/Sub subscription, GCS notification, etc.)
// could have been deleted outside the cluster, and we want to properly update our K8s object statuses.
DefaultResyncPeriod = 5 * time.Minute
)
// Options defines the common reconciler options.
// We define this to reduce the boilerplate argument list when
// creating our controllers.
type Options struct {
KubeClientSet kubernetes.Interface
DynamicClientSet dynamic.Interface
RunClientSet clientset.Interface
Recorder record.EventRecorder
StatsReporter StatsReporter
ConfigMapWatcher configmap.Watcher
Logger *zap.SugaredLogger
ResyncPeriod time.Duration
StopChannel <-chan struct{}
}
// This is mutable for testing.
var resetPeriod = 30 * time.Second
func NewOptionsOrDie(cfg *rest.Config, logger *zap.SugaredLogger, stopCh <-chan struct{}) Options {
kubeClient := kubernetes.NewForConfigOrDie(cfg)
dynamicClient := dynamic.NewForConfigOrDie(cfg)
runClient := clientset.NewForConfigOrDie(cfg)
configMapWatcher := configmap.NewInformedWatcher(kubeClient, system.Namespace())
return Options{
KubeClientSet: kubeClient,
DynamicClientSet: dynamicClient,
ConfigMapWatcher: configMapWatcher,
RunClientSet: runClient,
Logger: logger,
ResyncPeriod: 10 * time.Hour, // Based on controller-runtime default.
StopChannel: stopCh,
}
}
// GetTrackerLease returns a multiple of the resync period to use as the
// duration for tracker leases. This attempts to ensure that resyncs happen to
// refresh leases frequently enough that we don't miss updates to tracked
// objects.
func (o Options) GetTrackerLease() time.Duration {
return o.ResyncPeriod * 3
}
// Base implements the core controller logic, given a Reconciler.
type Base struct {
// KubeClientSet allows us to talk to the k8s for core APIs
KubeClientSet kubernetes.Interface
// DynamicClientSet allows us to configure pluggable Build objects
DynamicClientSet dynamic.Interface
// RunClientSet is the client for cloud run events.
RunClientSet clientset.Interface
// ServingClientSet is the client for Knative Serving
ServingClientSet servingclientset.Interface
// ConfigMapWatcher allows us to watch for ConfigMap changes.
ConfigMapWatcher configmap.Watcher
// Recorder is an event recorder for recording Event resources to the
// Kubernetes API.
Recorder record.EventRecorder
// StatsReporter reports reconciler's metrics.
StatsReporter StatsReporter
// Sugared logger is easier to use but is not as performant as the
// raw logger. In performance critical paths, call logger.Desugar()
// and use the returned raw logger instead. In addition to the
// performance benefits, raw logger also preserves type-safety at
// the expense of slightly greater verbosity.
Logger *zap.SugaredLogger
}
const (
ControllerType = "events.cloud.google.com/controller"
)
// NewBase instantiates a new instance of Base implementing
// the common & boilerplate code between our reconcilers.
func NewBase(ctx context.Context, controllerAgentName string, cmw configmap.Watcher) *Base {
// Enrich the logs with controller name
logger := logging.FromContext(ctx).
Named(controllerAgentName).
With(zap.String(ControllerType, controllerAgentName))
kubeClient := kubeclient.Get(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(
&typedcorev1.EventSinkImpl{Interface: kubeClient.CoreV1().Events("")}),
}
recorder = eventBroadcaster.NewRecorder(
scheme.Scheme, corev1.EventSource{Component: controllerAgentName})
go func() {
<-ctx.Done()
for _, w := range watches {
w.Stop()
}
}()
}
statsReporter := GetStatsReporter(ctx)
if statsReporter == nil {
logger.Debug("Creating stats reporter")
var err error
statsReporter, err = NewStatsReporter(controllerAgentName)
if err != nil {
logger.Fatal(err)
}
}
base := &Base{
KubeClientSet: kubeClient,
RunClientSet: runclient.Get(ctx),
ServingClientSet: servingclient.Get(ctx),
DynamicClientSet: dynamicclient.Get(ctx),
ConfigMapWatcher: cmw,
Recorder: recorder,
StatsReporter: statsReporter,
Logger: logger,
}
return base
}
func init() {
// Add run types to the default Kubernetes Scheme so Events can be
// logged for run types.
_ = runScheme.AddToScheme(scheme.Scheme)
}