-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
kubernetes.go
281 lines (239 loc) · 8.5 KB
/
kubernetes.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
// kubernetes.go
//
// This source file is part of the FoundationDB open source project
//
// Copyright 2023 Apple Inc. and the FoundationDB project 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 main
import (
"context"
"encoding/json"
"os"
"path"
"strconv"
"github.com/apple/foundationdb/fdbkubernetesmonitor/api"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// CurrentConfigurationAnnotation is the annotation we use to store the
// latest configuration.
CurrentConfigurationAnnotation = "foundationdb.org/launcher-current-configuration"
// EnvironmentAnnotation is the annotation we use to store the environment
// variables.
EnvironmentAnnotation = "foundationdb.org/launcher-environment"
// OutdatedConfigMapAnnotation is the annotation we read to get notified of
// outdated configuration.
OutdatedConfigMapAnnotation = "foundationdb.org/outdated-config-map-seen"
// DelayShutdownAnnotation defines how long the FDB Kubernetes monitor process should sleep before shutting itself down.
// The FDB Kubernetes monitor will always shutdown all fdbserver processes, independent of this setting.
// The value of this annotation must be a duration like "60s".
DelayShutdownAnnotation = "foundationdb.org/delay-shutdown"
)
// PodClient is a wrapper around the pod API.
type PodClient struct {
// metadata is the latest metadata that was seen by the fdb-kubernetes-monitor for the according Pod.
metadata *metav1.PartialObjectMetadata
// TimestampFeed is a channel where the pod client will send updates with
// the values from OutdatedConfigMapAnnotation.
TimestampFeed chan int64
// Logger is the logger we use for this client.
Logger logr.Logger
// Adds the controller runtime client to the PodClient.
client.Client
}
// CreatePodClient creates a new client for working with the pod object.
func CreatePodClient(ctx context.Context, logger logr.Logger) (*PodClient, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
scheme := runtime.NewScheme()
err = clientgoscheme.AddToScheme(scheme)
if err != nil {
return nil, err
}
// Create the new client for writes. This client will also be used to setup the cache.
internalClient, err := client.NewWithWatch(config, client.Options{
Scheme: scheme,
})
if err != nil {
return nil, err
}
namespace := os.Getenv("FDB_POD_NAMESPACE")
podName := os.Getenv("FDB_POD_NAME")
internalCache, err := cache.New(config, cache.Options{
Scheme: scheme,
Mapper: internalClient.RESTMapper(),
Namespace: namespace,
SelectorsByObject: map[client.Object]cache.ObjectSelector{
&corev1.Pod{}: {
Field: fields.OneTermEqualSelector(metav1.ObjectNameField, podName),
},
},
})
if err != nil {
return nil, err
}
// Fetch the informer for the FoundationDBCluster resource
informer, err := internalCache.GetInformer(ctx, &corev1.Pod{})
if err != nil {
return nil, err
}
podClient := &PodClient{
metadata: nil,
TimestampFeed: make(chan int64, 10),
Logger: logger,
}
// Setup an event handler to make sue we get events for new clusters and directly reload.
_, err = informer.AddEventHandler(podClient)
if err != nil {
return nil, err
}
// Make sure the internal cache is started.
go func() {
_ = internalCache.Start(ctx)
}()
// This should be fairly quick as no informers are provided by default.
internalCache.WaitForCacheSync(ctx)
controllerClient, err := client.NewDelegatingClient(client.NewDelegatingClientInput{
CacheReader: internalCache,
Client: internalClient,
UncachedObjects: nil,
CacheUnstructured: false,
})
if err != nil {
return nil, err
}
podClient.Client = controllerClient
// Fetch the current metadata before returning the PodClient
currentMetadata := &metav1.PartialObjectMetadata{}
currentMetadata.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Pod"))
err = podClient.Client.Get(ctx, client.ObjectKey{Namespace: namespace, Name: podName}, currentMetadata)
if err != nil {
return nil, err
}
podClient.metadata = currentMetadata
return podClient, nil
}
// retrieveEnvironmentVariables extracts the environment variables we have for
// an argument into a map.
func retrieveEnvironmentVariables(monitor *Monitor, argument api.Argument, target map[string]string) {
if argument.Source != "" {
value, err := argument.LookupEnv(monitor.CustomEnvironment)
if err == nil {
target[argument.Source] = value
}
}
if argument.Values != nil {
for _, childArgument := range argument.Values {
retrieveEnvironmentVariables(monitor, childArgument, target)
}
}
}
// UpdateAnnotations updates annotations on the pod after loading new
// configuration.
func (podClient *PodClient) UpdateAnnotations(monitor *Monitor) error {
environment := make(map[string]string)
for _, argument := range monitor.ActiveConfiguration.Arguments {
retrieveEnvironmentVariables(monitor, argument, environment)
}
environment["BINARY_DIR"] = path.Dir(monitor.ActiveConfiguration.BinaryPath)
jsonEnvironment, err := json.Marshal(environment)
if err != nil {
return err
}
annotations := podClient.metadata.Annotations
if len(annotations) > 0 {
annotations = map[string]string{}
}
annotations[CurrentConfigurationAnnotation] = string(monitor.ActiveConfigurationBytes)
annotations[EnvironmentAnnotation] = string(jsonEnvironment)
return podClient.Patch(context.Background(), &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: podClient.metadata.Namespace,
Name: podClient.metadata.Name,
Annotations: annotations,
},
}, client.Apply, client.FieldOwner("fdb-kubernetes-monitor"), client.ForceOwnership)
}
// OnAdd is called when an object is added.
func (podClient *PodClient) OnAdd(obj interface{}) {
pod, ok := obj.(*corev1.Pod)
if !ok {
return
}
podClient.Logger.Info("Got event for OnAdd", "name", pod.Name, "namespace", pod.Namespace)
podClient.metadata = &metav1.PartialObjectMetadata{
TypeMeta: pod.TypeMeta,
ObjectMeta: pod.ObjectMeta,
}
}
// OnUpdate is called when an object is modified. Note that oldObj is the
// last known state of the object-- it is possible that several changes
// were combined together, so you can't use this to see every single
// change. OnUpdate is also called when a re-list happens, and it will
// get called even if nothing changed. This is useful for periodically
// evaluating or syncing something.
func (podClient *PodClient) OnUpdate(_, newObj interface{}) {
pod, ok := newObj.(*corev1.Pod)
if !ok {
return
}
podClient.Logger.Info("Got event for OnAdd", "name", pod.Name, "namespace", pod.Namespace, "generation", pod.Generation)
podClient.metadata = &metav1.PartialObjectMetadata{
TypeMeta: pod.TypeMeta,
ObjectMeta: pod.ObjectMeta,
}
if podClient.metadata == nil {
return
}
annotation := podClient.metadata.Annotations[OutdatedConfigMapAnnotation]
if annotation == "" {
return
}
timestamp, err := strconv.ParseInt(annotation, 10, 64)
if err != nil {
podClient.Logger.Error(err, "Error parsing annotation", "key", OutdatedConfigMapAnnotation, "rawAnnotation", annotation)
return
}
podClient.TimestampFeed <- timestamp
}
// OnDelete will get the final state of the item if it is known, otherwise
// it will get an object of type DeletedFinalStateUnknown. This can
// happen if the watch is closed and misses the delete event and we don't
// notice the deletion until the subsequent re-list.
func (podClient *PodClient) OnDelete(obj interface{}) {
pod, ok := obj.(*corev1.Pod)
if !ok {
return
}
podClient.Logger.Info("Got event for OnDelete", "name", pod.Name, "namespace", pod.Namespace)
podClient.metadata = &metav1.PartialObjectMetadata{
TypeMeta: pod.TypeMeta,
ObjectMeta: pod.ObjectMeta,
}
}