-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.go
More file actions
56 lines (47 loc) · 1.47 KB
/
controller.go
File metadata and controls
56 lines (47 loc) · 1.47 KB
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
package main
import (
"context"
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
// reconcilePod reconciles Pods
type reconcilePod struct {
client client.Client
}
func (r *reconcilePod) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log := log.FromContext(ctx)
rs := &corev1.Pod{}
err := r.client.Get(ctx, request.NamespacedName, rs)
if apierrors.IsNotFound(err) {
log.Error(nil, "Could not find Pod")
return reconcile.Result{}, nil
}
if err != nil {
return reconcile.Result{Requeue: true}, fmt.Errorf("could not fetch Pod: %+w", err)
}
annotationKey, err := GetConfigAnnotation()
if err != nil {
return reconcile.Result{Requeue: true}, fmt.Errorf("could not fetch Configmap: %+w", err)
}
if rs.Annotations == nil {
rs.Annotations = map[string]string{}
}
if v, ok := rs.Annotations[annotationKey]; ok {
if strings.EqualFold(v, "true") {
log.Info("Skipping pod, already annotated", "name", rs.Name, "namespace", rs.Namespace)
return reconcile.Result{}, nil
}
}
rs.Annotations[annotationKey] = "true"
err = r.client.Update(ctx, rs)
log.Info("Annotating", "name", rs.Name, "namespace", rs.Namespace)
if err != nil {
return reconcile.Result{Requeue: true}, fmt.Errorf("could not write Pod: %+w", err)
}
return reconcile.Result{}, nil
}