-
Notifications
You must be signed in to change notification settings - Fork 2
/
pod_subscription.go
84 lines (75 loc) · 2.46 KB
/
pod_subscription.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
package subscription
import (
"context"
"fmt"
"github.com/opentracing/opentracing-go"
log "github.com/siruspen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
)
type PodSubscription struct {
watcherInterface watch.Interface
ClientSet kubernetes.Interface
Ctx context.Context
Completion chan bool
// This specific reference to ConfigMapSubscription is for updating pod annotations
ConfigMapSubscriptRef *ConfigMapSubscription
}
func (p *PodSubscription) applyConfigMapChanges(pod *v1.Pod, event watch.EventType) {
if p.ConfigMapSubscriptRef != nil {
if p.ConfigMapSubscriptRef.PlatformConfig != nil {
updatedPod := pod.DeepCopy()
if updatedPod.Annotations == nil {
updatedPod.Annotations = make(map[string]string)
}
// Loop through and apply
for _, annotation := range p.ConfigMapSubscriptRef.PlatformConfig.Annotations {
updatedPod.Annotations[annotation.Name] = annotation.Value
}
// Update the pod
_, err := p.ClientSet.CoreV1().Pods(pod.Namespace).Update(p.Ctx, updatedPod, metav1.UpdateOptions{})
if err != nil {
log.Error(err)
}
}
}
}
func (p *PodSubscription) Reconcile(object runtime.Object, event watch.EventType) {
rootSpan := opentracing.GlobalTracer().StartSpan("podSubscriptionReoncile")
defer rootSpan.Finish()
pod := object.(*v1.Pod)
log.WithFields(log.Fields{
"namespace": pod.Namespace,
}).Info(fmt.Sprintf("PodSubscription event type %s for %s", event, pod.Name))
switch event {
case watch.Added:
watchEventAdd := opentracing.GlobalTracer().StartSpan(
"watchEventAdd", opentracing.ChildOf(rootSpan.Context()),
)
defer watchEventAdd.Finish()
// Fetch the required PlatformConfig annotations
p.applyConfigMapChanges(pod, event)
case watch.Deleted:
case watch.Modified:
watchEventModified := opentracing.GlobalTracer().StartSpan(
"watchEventModified", opentracing.ChildOf(rootSpan.Context()),
)
defer watchEventModified.Finish()
p.applyConfigMapChanges(pod, event)
}
}
func (p *PodSubscription) Subscribe() (watch.Interface, error) {
var err error
p.watcherInterface, err = p.ClientSet.CoreV1().Pods("").Watch(p.Ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
log.Info("Started watch stream for PodSubscription")
return p.watcherInterface, nil
}
func (p *PodSubscription) IsComplete() <-chan bool {
return p.Completion
}