-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod.go
178 lines (156 loc) · 4 KB
/
pod.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
package main
import (
"context"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
watch "k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
funk "github.com/thoas/go-funk"
)
var podPermittedStates = []ResourceState{ResourceReady, ResourceSucceeded, ResourceFailed}
// PodMatcher
type PodMatcher struct {
clientset kubernetes.Interface
description StateDescription
podstate map[string]ResourceState
watcher watch.Interface
done chan bool
}
// PodValidator
type PodValidator struct {
BaseValidator
}
func (p *PodValidator) Validate(ctx context.Context, description StateDescription) error {
err := p.BaseValidator.Validate(ctx, description)
if err != nil {
return err
}
for _, requiredState := range description.RequiredStates {
if !funk.Contains(podPermittedStates, requiredState) {
return ErrStateNotValidForResourceType(description, requiredState)
}
}
return nil
}
func NewPodValidator() Validator {
return &PodValidator{}
}
func NewPodMatcher(clientset kubernetes.Interface, description StateDescription) Matcher {
return &PodMatcher{
clientset: clientset,
description: description,
done: make(chan bool, 1),
podstate: make(map[string]ResourceState),
}
}
func (p *PodMatcher) Start(ctx context.Context) error {
options := metav1.ListOptions{
LabelSelector: p.description.LabelSelector,
}
logger := log.WithFields(log.Fields{
"namespace": p.description.Namespace,
"type": p.description.Type,
"labelselector": p.description.LabelSelector,
})
logger.Debug("fetching initial context")
pods, err := p.clientset.CoreV1().Pods(p.description.Namespace).List(options)
if err != nil {
return err
}
for _, pod := range pods.Items {
state := getPodResourceState(&pod)
p.podstate[pod.Name] = state
log.WithFields(log.Fields{
"podName": pod.Name,
"podState": state,
}).Debug("added to podstate")
}
logger.Debug("fetched context")
if match := MatchStateMap(p.podstate, p.description.RequiredStates); match {
logger.Debug("match: ", match)
return nil
}
p.watcher, err = p.clientset.CoreV1().Pods(p.description.Namespace).Watch(options)
if err != nil {
return err
}
log.Info("watching for updates")
for event := range p.watcher.ResultChan() {
ctxLogger := log.WithFields(log.Fields{
"eventType": event.Type,
})
switch event.Type {
case watch.Added:
pod := event.Object.(*v1.Pod)
state := getPodResourceState(pod)
p.podstate[pod.Name] = state
ctxLogger.WithFields(log.Fields{
"podName": pod.Name,
"podState": state,
}).Debug("added to pod state")
case watch.Modified:
pod := event.Object.(*v1.Pod)
state := getPodResourceState(pod)
p.podstate[pod.Name] = state
ctxLogger.WithFields(log.Fields{
"podName": pod.Name,
"podState": state,
}).Debug("updated pod state")
case watch.Deleted:
pod := event.Object.(*v1.Pod)
_, ok := p.podstate[pod.Name]
if ok {
delete(p.podstate, pod.Name)
ctxLogger.WithFields(log.Fields{
"podName": pod.Name,
}).Debug("removed from pod state")
}
case watch.Error:
// TODO: Do something with this error
return nil
}
if MatchStateMap(p.podstate, p.description.RequiredStates) {
log.Info("state description matched by cluster")
select {
case <-p.done:
default:
close(p.done)
}
break
}
}
return nil
}
func (p *PodMatcher) Done() <-chan bool {
return p.done
}
func (p *PodMatcher) Stop(ctx context.Context) error {
defer func() {
select {
case <-p.done:
default:
close(p.done)
}
}()
if p.watcher != nil {
p.watcher.Stop()
}
return nil
}
func getPodResourceState(pod *v1.Pod) ResourceState {
// check for ready
for _, condition := range pod.Status.Conditions {
if condition.Type == v1.PodReady && condition.Status == v1.ConditionTrue {
return ResourceReady
}
}
switch pod.Status.Phase {
case v1.PodSucceeded:
return ResourceSucceeded
case v1.PodFailed:
return ResourceFailed
default:
}
return resourceWaiting
}