-
Notifications
You must be signed in to change notification settings - Fork 341
/
job.go
280 lines (242 loc) · 8.78 KB
/
job.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
/*
Copyright 2018 Heptio Inc.
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 job
import (
"context"
"crypto/tls"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
"github.com/vmware-tanzu/sonobuoy/pkg/plugin"
"github.com/vmware-tanzu/sonobuoy/pkg/plugin/driver"
"github.com/vmware-tanzu/sonobuoy/pkg/plugin/driver/utils"
"github.com/vmware-tanzu/sonobuoy/pkg/plugin/manifest"
sonotime "github.com/vmware-tanzu/sonobuoy/pkg/time"
)
const (
// pollingInterval is the time between polls when monitoring the job status.
pollingInterval = 10 * time.Second
)
// Plugin is a plugin driver that dispatches a single pod to the given
// kubernetes cluster.
type Plugin struct {
driver.Base
}
// Ensure Plugin implements plugin.Interface
var _ plugin.Interface = &Plugin{}
// NewPlugin creates a new DaemonSet plugin from the given Plugin Definition
// and sonobuoy aggregator address.
func NewPlugin(dfn manifest.Manifest, namespace, sonobuoyImage, imagePullPolicy, imagePullSecrets string, customAnnotations map[string]string) *Plugin {
return &Plugin{
driver.Base{
Definition: dfn,
SessionID: utils.GetSessionID(),
Namespace: namespace,
SonobuoyImage: sonobuoyImage,
ImagePullPolicy: imagePullPolicy,
ImagePullSecrets: imagePullSecrets,
CustomAnnotations: customAnnotations,
CleanedUp: false, // be explicit
},
}
}
// ExpectedResults returns the list of results expected for this plugin. Since
// a Job only launches one pod, only one result type is expected.
func (p *Plugin) ExpectedResults(nodes []v1.Node) []plugin.ExpectedResult {
return []plugin.ExpectedResult{
{ResultType: p.GetName(), NodeName: plugin.GlobalResult},
}
}
func (p *Plugin) createPodDefinition(hostname string, cert *tls.Certificate, ownerPod *v1.Pod, progressPort, resultDir string) v1.Pod {
pod := v1.Pod{}
annotations := map[string]string{
"sonobuoy-driver": p.GetDriver(),
"sonobuoy-plugin": p.GetName(),
}
for k, v := range p.CustomAnnotations {
annotations[k] = v
}
labels := map[string]string{
"component": "sonobuoy",
"sonobuoy-component": "plugin",
"sonobuoy-plugin": p.GetName(),
"sonobuoy-run": p.SessionID,
"tier": "analysis",
}
pod.ObjectMeta = metav1.ObjectMeta{
Name: fmt.Sprintf("sonobuoy-%s-job-%s", p.GetName(), p.SessionID),
Namespace: p.Namespace,
Labels: labels,
Annotations: annotations,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "v1",
Kind: "Pod",
Name: ownerPod.GetName(),
UID: ownerPod.GetUID(),
},
},
}
var podSpec v1.PodSpec
if p.Definition.PodSpec != nil {
podSpec = p.Definition.PodSpec.PodSpec
} else {
podSpec = driver.DefaultPodSpec(p.GetDriver())
}
podSpec.Containers = append(podSpec.Containers,
p.Definition.Spec.Container,
p.CreateWorkerContainerDefintion(hostname, cert, []string{"/sonobuoy"}, []string{"worker", "global", "--level=trace", "-v=6", "--logtostderr"}, progressPort, resultDir, p.Definition.Spec.Container.Env),
)
if len(p.ImagePullSecrets) > 0 {
podSpec.ImagePullSecrets = append(podSpec.ImagePullSecrets, v1.LocalObjectReference{
Name: p.ImagePullSecrets,
})
}
podSpec.Volumes = append(podSpec.Volumes, v1.Volume{
Name: "results",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
})
for _, v := range p.Definition.ExtraVolumes {
podSpec.Volumes = append(podSpec.Volumes, v.Volume)
}
pod.Spec = podSpec
return pod
}
// Run dispatches worker pods according to the Job's configuration.
func (p *Plugin) Run(kubeclient kubernetes.Interface, hostname string, cert *tls.Certificate, ownerPod *v1.Pod, progressPort, resultDir string) error {
job := p.createPodDefinition(fmt.Sprintf("https://%s", hostname), cert, ownerPod, progressPort, resultDir)
secret, err := p.MakeTLSSecret(cert, ownerPod)
if err != nil {
return errors.Wrapf(err, "couldn't make secret for Job plugin %v", p.GetName())
}
if _, err := kubeclient.CoreV1().Secrets(p.Namespace).Create(context.TODO(), secret, metav1.CreateOptions{}); err != nil {
return errors.Wrapf(err, "couldn't create TLS secret for job plugin %v", p.GetName())
}
if _, err := kubeclient.CoreV1().Pods(p.Namespace).Create(context.TODO(), &job, metav1.CreateOptions{}); err != nil {
return errors.Wrapf(err, "could not create Job resource for Job plugin %v", p.GetName())
}
return nil
}
// Monitor adheres to plugin.Interface by ensuring the pod created by the job
// doesn't have any unrecoverable failures. It closes the results channel when
// it is done.
func (p *Plugin) Monitor(ctx context.Context, kubeclient kubernetes.Interface, _ []v1.Node, resultsCh chan<- *plugin.Result) {
defer close(resultsCh)
for {
// Sleep between each poll, which should give the Job
// enough time to create a Pod.
// TODO: maybe use a watcher instead of polling.
select {
case <-ctx.Done():
switch {
case ctx.Err() == context.DeadlineExceeded:
logrus.Errorf("Timeout waiting for plugin %v. Try checking the pod logs and other data in the results tarball for more information.", p.GetName())
resultsCh <- utils.MakeErrorResult(
p.GetName(),
map[string]interface{}{"error": plugin.TimeoutErrMsg},
plugin.GlobalResult,
)
case ctx.Err() == context.Canceled:
// Do nothing, just stop.
case ctx.Err() != nil:
logrus.Errorf("Error seen while monitoring plugin %v: %v", p.GetName(), ctx.Err().Error())
resultsCh <- utils.MakeErrorResult(
p.GetName(),
map[string]interface{}{"error": ctx.Err().Error()},
plugin.GlobalResult,
)
}
return
case <-sonotime.After(pollingInterval):
}
done, errResult := p.monitorOnce(kubeclient, nil)
if errResult != nil {
resultsCh <- errResult
}
if done {
return
}
}
}
func (p *Plugin) monitorOnce(kubeclient kubernetes.Interface, _ []v1.Node) (done bool, errResult *plugin.Result) {
// If we've cleaned up after ourselves, stop monitoring
if p.CleanedUp {
return true, nil
}
// Make sure there's a pod; dont fail the pod if there are issues querying the API server.
pod, err := p.findPod(kubeclient)
if err != nil {
errlog.LogError(errors.Wrapf(err, "could not find pod created by plugin %v, will retry", p.GetName()))
return false, nil
}
// Make sure the pod isn't failing
if isFailing, reason := utils.IsPodFailing(pod); isFailing {
return true, utils.MakeErrorResult(p.GetName(), map[string]interface{}{
"error": reason,
"pod": pod,
}, plugin.GlobalResult)
}
return false, nil
}
// Cleanup cleans up the k8s Job and ConfigMap created by this plugin instance
func (p *Plugin) Cleanup(kubeclient kubernetes.Interface) {
p.CleanedUp = true
gracePeriod := int64(plugin.GracefulShutdownPeriod)
deletionPolicy := metav1.DeletePropagationBackground
listOptions := metav1.ListOptions{
LabelSelector: "sonobuoy-run=" + p.GetSessionID(),
}
deleteOptions := metav1.DeleteOptions{
GracePeriodSeconds: &gracePeriod,
PropagationPolicy: &deletionPolicy,
}
// Delete the Pod created by the job manually (just deleting the Job
// doesn't kill the pod, it still lets it finish.)
// TODO: for now we're not actually creating a Job at all, just a
// single Pod, to get the restart semantics we want. But later if we
// want to make this a real Job, we still need to delete pods manually
// after deleting the job.
err := kubeclient.CoreV1().Pods(p.Namespace).DeleteCollection(
context.TODO(),
deleteOptions,
listOptions,
)
if err != nil {
errlog.LogError(errors.Wrapf(err, "error deleting pods for Job-%v", p.GetSessionID()))
}
}
func (p *Plugin) listOptions() metav1.ListOptions {
return metav1.ListOptions{
LabelSelector: "sonobuoy-run=" + p.GetSessionID(),
}
}
// findPod finds the pod created by this plugin, using a kubernetes label
// search. If no pod is found, or if multiple pods are found, returns an
// error.
func (p *Plugin) findPod(kubeclient kubernetes.Interface) (*v1.Pod, error) {
pods, err := kubeclient.CoreV1().Pods(p.Namespace).List(context.TODO(), p.listOptions())
if err != nil {
return nil, errors.WithStack(err)
}
if len(pods.Items) != 1 {
return nil, errors.Errorf("no pods were created by plugin %v", p.GetName())
}
return &pods.Items[0], nil
}