-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathtf_controller_runner.go
568 lines (508 loc) · 17.5 KB
/
tf_controller_runner.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package controllers
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/fluxcd/pkg/runtime/logger"
"google.golang.org/grpc"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
controllerruntime "sigs.k8s.io/controller-runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
infrav1 "github.com/flux-iac/tofu-controller/api/v1alpha2"
"github.com/flux-iac/tofu-controller/mtls"
"github.com/flux-iac/tofu-controller/runner"
)
func getRunnerPodObjectKey(terraform infrav1.Terraform) types.NamespacedName {
return types.NamespacedName{Namespace: terraform.Namespace, Name: fmt.Sprintf("%s-tf-runner", terraform.Name)}
}
func getRunnerPodImage(image string) string {
runnerPodImage := image
if runnerPodImage == "" {
runnerPodImage = os.Getenv("RUNNER_POD_IMAGE")
}
if runnerPodImage == "" {
runnerPodImage = "ghcr.io/flux-iac/tf-runner:latest"
}
return runnerPodImage
}
func runnerPodTemplate(terraform infrav1.Terraform, secretName string, revision string) (v1.Pod, error) {
podNamespace := terraform.Namespace
podName := fmt.Sprintf("%s-tf-runner", terraform.Name)
podInstance, err := runnerPodInstance(revision)
if err != nil {
return v1.Pod{}, err
}
runnerPodTemplate := v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: podNamespace,
Name: podName,
Labels: map[string]string{
"app.kubernetes.io/created-by": "tf-controller",
"app.kubernetes.io/name": "tf-runner",
"app.kubernetes.io/instance": podInstance,
infrav1.RunnerLabel: terraform.Namespace,
"tf.weave.works/tls-secret-name": secretName,
},
Annotations: terraform.Spec.RunnerPodTemplate.Metadata.Annotations,
},
}
// add runner pod custom labels
if len(terraform.Spec.RunnerPodTemplate.Metadata.Labels) != 0 {
for k, v := range terraform.Spec.RunnerPodTemplate.Metadata.Labels {
runnerPodTemplate.Labels[k] = v
}
}
return runnerPodTemplate, nil
}
func (r *TerraformReconciler) LookupOrCreateRunner(ctx context.Context, terraform infrav1.Terraform, revision string) (runner.RunnerClient, func() error, error) {
log := ctrl.LoggerFrom(ctx)
traceLog := log.V(logger.TraceLevel).WithValues("function", "TerraformReconciler.lookupOrCreateRunner_000")
// we have to make sure that the secret is valid before we can create the runner.
traceLog.Info("Validate the secret used for the Terraform resource")
secret, err := r.reconcileRunnerSecret(ctx, &terraform)
traceLog.Info("Check for an error")
if err != nil {
traceLog.Error(err, "Hit an error")
return nil, nil, err
}
var hostname string
traceLog.Info("Check if we're running a local Runner")
if os.Getenv("INSECURE_LOCAL_RUNNER") == "1" {
traceLog.Info("Local Runner, set hostname")
hostname = "localhost"
} else {
traceLog.Info("Get Runner pod IP")
podIP, err := r.reconcileRunnerPod(ctx, terraform, secret, revision)
traceLog.Info("Check for an error")
if err != nil {
traceLog.Error(err, "Hit an error")
return nil, nil, err
}
traceLog.Info("Get pod coordinates", "pod-ip", podIP, "pod-hostname", terraform.Name)
if r.UsePodSubdomainResolution {
hostname = terraform.GetRunnerHostname(terraform.Name, r.ClusterDomain)
} else {
hostname = terraform.GetRunnerHostname(podIP, r.ClusterDomain)
}
}
traceLog.Info("Pod hostname set", "hostname", hostname)
traceLog.Info("Create a new context for the runner connection")
dialCtx, dialCancel := context.WithTimeout(ctx, 30*time.Second)
traceLog.Info("Defer dialCancel")
defer dialCancel()
traceLog.Info("Get the Runner connection")
conn, err := r.getRunnerConnection(dialCtx, secret, hostname, r.RunnerGRPCPort)
traceLog.Info("Check for an error")
if err != nil {
traceLog.Error(err, "Hit an error")
return nil, nil, err
}
traceLog.Info("Create a close connection function")
connClose := func() error { return conn.Close() }
traceLog.Info("Create a new Runner client")
runnerClient := runner.NewRunnerClient(conn)
traceLog.Info("Return the client and close connection function")
return runnerClient, connClose, nil
}
func (r *TerraformReconciler) getRunnerConnection(ctx context.Context, tlsSecret *v1.Secret, hostname string, port int) (*grpc.ClientConn, error) {
log := ctrl.LoggerFrom(ctx)
traceLog := log.V(logger.TraceLevel).WithValues("function", "TerraformReconciler.getRunnerConnection")
addr := fmt.Sprintf("%s:%d", hostname, port)
traceLog.Info("Set address for target", "addr", addr)
traceLog.Info("Get GRPC Credentials")
credentials, err := mtls.GetGRPCClientCredentials(tlsSecret)
traceLog.Info("Check for an error")
if err != nil {
traceLog.Error(err, "Hit an error")
return nil, err
}
const retryPolicy = `{
"methodConfig": [{
"name": [{"service": "runner.Runner"}],
"waitForReady": true,
"retryPolicy": {
"MaxAttempts": 4,
"InitialBackoff": ".01s",
"MaxBackoff": ".01s",
"BackoffMultiplier": 1.0,
"RetryableStatusCodes": [ "UNAVAILABLE" ]
}
}]}`
traceLog.Info("Return dial context")
return grpc.DialContext(ctx, addr,
grpc.WithTransportCredentials(credentials),
grpc.WithBlock(),
grpc.WithDefaultServiceConfig(retryPolicy),
)
}
func (r *TerraformReconciler) runnerPodSpec(terraform infrav1.Terraform, tlsSecretName string) v1.PodSpec {
serviceAccountName := terraform.Spec.ServiceAccountName
if serviceAccountName == "" {
serviceAccountName = "tf-runner"
}
gracefulTermPeriod := terraform.Spec.RunnerTerminationGracePeriodSeconds
envvars := []v1.EnvVar{}
envvarsMap := map[string]v1.EnvVar{
"POD_NAME": {
Name: "POD_NAME",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
},
"POD_NAMESPACE": {
Name: "POD_NAMESPACE",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
}
for _, envName := range []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"} {
if envValue := os.Getenv(envName); envValue != "" {
envvarsMap[envName] = v1.EnvVar{
Name: envName,
Value: envValue,
}
}
}
for _, env := range terraform.Spec.RunnerPodTemplate.Spec.Env {
envvarsMap[env.Name] = env
}
for _, env := range envvarsMap {
envvars = append(envvars, env)
}
vFalse := false
vTrue := true
vUser := int64(65532)
podVolumes := []v1.Volume{
{
Name: "temp",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
{
Name: "home",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
}
if len(terraform.Spec.RunnerPodTemplate.Spec.Volumes) != 0 {
podVolumes = append(podVolumes, terraform.Spec.RunnerPodTemplate.Spec.Volumes...)
}
podVolumeMounts := []v1.VolumeMount{
{
Name: "temp",
MountPath: "/tmp",
},
{
Name: "home",
MountPath: "/home/runner",
},
}
if len(terraform.Spec.RunnerPodTemplate.Spec.VolumeMounts) != 0 {
podVolumeMounts = append(podVolumeMounts, terraform.Spec.RunnerPodTemplate.Spec.VolumeMounts...)
}
securityContext := &v1.SecurityContext{
Capabilities: &v1.Capabilities{
Drop: []v1.Capability{"ALL"},
},
AllowPrivilegeEscalation: &vFalse,
RunAsNonRoot: &vTrue,
RunAsUser: &vUser,
SeccompProfile: &v1.SeccompProfile{
Type: v1.SeccompProfileTypeRuntimeDefault,
},
ReadOnlyRootFilesystem: &vTrue,
}
if terraform.Spec.RunnerPodTemplate.Spec.SecurityContext != nil {
securityContext = terraform.Spec.RunnerPodTemplate.Spec.SecurityContext
}
resources := v1.ResourceRequirements{}
if terraform.Spec.RunnerPodTemplate.Spec.Resources != nil {
resources = *terraform.Spec.RunnerPodTemplate.Spec.Resources
}
podSpec := v1.PodSpec{
TerminationGracePeriodSeconds: gracefulTermPeriod,
InitContainers: terraform.Spec.RunnerPodTemplate.Spec.InitContainers,
Containers: []v1.Container{
{
Name: "tf-runner",
Args: []string{
"--grpc-port", fmt.Sprintf("%d", r.RunnerGRPCPort),
"--tls-secret-name", tlsSecretName,
"--grpc-max-message-size", fmt.Sprintf("%d", r.RunnerGRPCMaxMessageSize),
},
Image: getRunnerPodImage(terraform.Spec.RunnerPodTemplate.Spec.Image),
ImagePullPolicy: v1.PullIfNotPresent,
Ports: []v1.ContainerPort{
{
Name: "grpc",
ContainerPort: int32(r.RunnerGRPCPort),
},
},
Env: envvars,
EnvFrom: terraform.Spec.RunnerPodTemplate.Spec.EnvFrom,
SecurityContext: securityContext,
VolumeMounts: podVolumeMounts,
Resources: resources,
},
},
Volumes: podVolumes,
ServiceAccountName: serviceAccountName,
NodeSelector: terraform.Spec.RunnerPodTemplate.Spec.NodeSelector,
Affinity: terraform.Spec.RunnerPodTemplate.Spec.Affinity,
Tolerations: terraform.Spec.RunnerPodTemplate.Spec.Tolerations,
HostAliases: terraform.Spec.RunnerPodTemplate.Spec.HostAliases,
PriorityClassName: terraform.Spec.RunnerPodTemplate.Spec.PriorityClassName,
}
if r.UsePodSubdomainResolution {
podSpec.Hostname = terraform.Name
podSpec.Subdomain = "tf-runner"
}
return podSpec
}
func (r *TerraformReconciler) reconcileRunnerPod(ctx context.Context, terraform infrav1.Terraform, tlsSecret *v1.Secret, revision string) (string, error) {
log := controllerruntime.LoggerFrom(ctx)
traceLog := log.V(logger.TraceLevel).WithValues("function", "TerraformReconciler.reconcileRunnerPod")
traceLog.Info("Begin reconcile of the runner pod")
type state string
const (
stateUnknown state = "unknown"
stateRunning state = "running"
stateNotFound state = "not-found"
stateMustBeDeleted state = "must-be-deleted"
stateTerminating state = "terminating"
)
const interval = time.Second * 15
traceLog.Info("Set interval", "interval", interval)
timeout := r.RunnerCreationTimeout // default is 120 seconds
traceLog.Info("Set timeout", "timeout", timeout)
tlsSecretName := tlsSecret.Name
traceLog.Info("Set tlsSecretName", "tlsSecretName", tlsSecretName)
traceLog.Info("Setup create new pod function")
createNewPod := func() error {
runnerPodTemplate, err := runnerPodTemplate(terraform, tlsSecretName, revision)
if err != nil {
return err
}
newRunnerPod := *runnerPodTemplate.DeepCopy()
newRunnerPod.Spec = r.runnerPodSpec(terraform, tlsSecretName)
if err := r.Create(ctx, &newRunnerPod); err != nil {
return err
}
return nil
}
traceLog.Info("Setup wait for pod to be terminated function")
waitForPodToBeTerminated := func() error {
runnerPodTemplate, err := runnerPodTemplate(terraform, tlsSecretName, revision)
if err != nil {
return err
}
runnerPod := *runnerPodTemplate.DeepCopy()
runnerPodKey := client.ObjectKeyFromObject(&runnerPod)
err = wait.PollUntilContextTimeout(ctx, interval, timeout, true, func(ctx context.Context) (bool, error) {
err := r.Get(ctx, runnerPodKey, &runnerPod)
if err != nil && errors.IsNotFound(err) {
return true, nil
}
return false, nil
})
if err != nil {
return err
}
return nil
}
podState := stateUnknown
traceLog.Info("Set pod state", "pod-state", podState)
runnerPodTemplate, err := runnerPodTemplate(terraform, tlsSecretName, revision)
if err != nil {
return "", err
}
runnerPod := *runnerPodTemplate.DeepCopy()
runnerPodKey := client.ObjectKeyFromObject(&runnerPod)
traceLog.Info("Get pod state")
err = r.Get(ctx, runnerPodKey, &runnerPod)
traceLog.Info("Check for an error")
gracefulTermPeriod := *terraform.Spec.RunnerTerminationGracePeriodSeconds
if err != nil && errors.IsNotFound(err) {
podState = stateNotFound
} else if err != nil {
traceLog.Error(err, "Error getting the Runner Pod", "runner-pod-key", runnerPodKey)
} else if err == nil {
label, found := runnerPod.Labels["tf.weave.works/tls-secret-name"]
traceLog.Info("Set label and found", "label", label, "found", found)
if !found {
// this is the pod created by something else but with the same name
podState = stateMustBeDeleted
gracefulTermPeriod = int64(1) // force kill = 1 second
} else if label != tlsSecretName {
// this is the old pod, created by the previous instance of the controller
podState = stateMustBeDeleted
gracefulTermPeriod = *terraform.Spec.RunnerTerminationGracePeriodSeconds // honor the value from the spec
} else if runnerPod.DeletionTimestamp != nil {
podState = stateTerminating
} else if runnerPod.Status.Phase == v1.PodRunning {
podState = stateRunning
}
}
traceLog.Info("Updated Pod State", "pod-state", podState)
log.Info("show runner pod state: ", "name", terraform.Name, "state", podState)
traceLog.Info("Switch on Pod State")
switch podState {
case stateNotFound:
// create new pod
traceLog.Info("Create a new pod")
err := createNewPod()
traceLog.Info("Check for an error")
if err != nil {
traceLog.Error(err, "Hit an error")
return "", err
}
case stateMustBeDeleted:
// delete old pod
traceLog.Info("Pod must be deleted, attempt deletion")
if err := r.Delete(ctx, &runnerPod,
client.GracePeriodSeconds(gracefulTermPeriod),
client.PropagationPolicy(metav1.DeletePropagationForeground),
); err != nil {
traceLog.Error(err, "Hit an error")
return "", err
}
// wait for pod to be terminated
traceLog.Info("Wait for pod to be terminated and check for an error")
if err := waitForPodToBeTerminated(); err != nil {
traceLog.Error(err, "Hit an error")
return "", fmt.Errorf("failed to wait for the old pod termination: %v", err)
}
// create new pod
traceLog.Info("Create a new pod and check for an error")
if err := createNewPod(); err != nil {
traceLog.Error(err, "Hit an error")
return "", err
}
case stateTerminating:
// wait for pod to be terminated
traceLog.Info("Check for an error")
if err := waitForPodToBeTerminated(); err != nil {
traceLog.Error(err, "Hit an error")
return "", fmt.Errorf("failed to wait for the old pod termination: %v", err)
}
// create new pod
traceLog.Info("Create a new pod")
err := createNewPod()
traceLog.Info("Check for an error")
if err != nil {
traceLog.Error(err, "Hit an error")
return "", err
}
case stateRunning:
// do nothing
traceLog.Info("Pod is running, do nothing")
}
// wait for pod ip
watcher, err := r.Clientset.CoreV1().Pods(runnerPodKey.Namespace).Watch(ctx, metav1.SingleObject(metav1.ObjectMeta{
Name: runnerPodKey.Name,
Namespace: runnerPodKey.Namespace,
}))
if err != nil {
return "", fmt.Errorf("failed to create a watch on the pod: %w", err)
}
defer watcher.Stop()
// set a timeout for the watch
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
traceLog.Info("Wait for pod to receive an IP and check for an error")
for {
select {
case event, ok := <-watcher.ResultChan():
if !ok {
return "", fmt.Errorf("watch channel closed")
}
switch event.Type {
case watch.Added, watch.Modified:
runnerPod, ok := event.Object.(*corev1.Pod)
if !ok {
return "", fmt.Errorf("failed to cast object to pod: %v", event.Object)
}
traceLog.Info("Check if the pod has an IP")
if runnerPod.Status.PodIP != "" {
traceLog.Info("Success, pod has an IP")
return runnerPod.Status.PodIP, nil
}
traceLog.Info("Pod does not have an IP yet")
}
case <-ctx.Done():
traceLog.Info("Failed to get the pod, force kill the pod")
traceLog.Error(err, "Error getting the Pod")
if err := r.Delete(ctx, &runnerPod,
client.GracePeriodSeconds(1), // force kill = 1 second
client.PropagationPolicy(metav1.DeletePropagationForeground),
); err != nil {
traceLog.Error(err, "Hit an error")
return "", fmt.Errorf("failed to obtain pod ip and delete runner pod: %w", err)
}
return "", fmt.Errorf("failed to create and obtain pod ip")
}
}
}
// reconcileRunnerSecret reconciles the runner secret used for mTLS
//
// It should create the secret if it doesn't exist and then verify that the cert is valid
// if the cert is not present in the secret or is invalid, it will generate a new cert and
// write it to the secret. One secret per namespace is created in order to sidestep the need
// for specifying a pod ip in the certificate SAN field.
func (r *TerraformReconciler) reconcileRunnerSecret(ctx context.Context, terraform *infrav1.Terraform) (*v1.Secret, error) {
log := controllerruntime.LoggerFrom(ctx)
log.Info("trigger namespace tls secret generation")
trigger := mtls.Trigger{
Namespace: terraform.Namespace,
Ready: make(chan *mtls.TriggerResult),
}
r.CertRotator.TriggerNamespaceTLSGeneration <- trigger
result := <-trigger.Ready
if result.Err != nil {
return nil, result.Err
}
// Check if the secret already exists
secret := &v1.Secret{}
if err := r.Client.Get(ctx, types.NamespacedName{Name: result.Secret.Name, Namespace: terraform.Namespace}, secret); err != nil {
if errors.IsNotFound(err) {
// If secret does not exist, create it
result.Secret.SetResourceVersion("")
result.Secret.SetUID("")
result.Secret.SetGeneration(0)
if err := r.Client.Create(ctx, result.Secret); err != nil {
return nil, err
}
} else {
// For any other type of error, return it
return nil, err
}
}
return result.Secret, nil
}
func runnerPodInstance(revision string) (string, error) {
parts := strings.Split(revision, ":")
if len(parts) < 2 {
return "", fmt.Errorf("invalid revision: %s", revision)
}
gitSHA := parts[1]
if len(gitSHA) < 8 {
return "", fmt.Errorf("invalid git sha: %s", gitSHA)
}
return fmt.Sprintf("tf-runner-%s", gitSHA[0:8]), nil
}