Skip to content

Commit 4c07889

Browse files
authored
Remove use of templates for plugin Jobs and DaemonSets (#834)
This change removes the use of templates for creating resources when running plugins and instead creates the objects directly to use with the Kubernetes API. This change should introduce no change in behaviour. It is just refactoring in preparation for adding support to customise pod options. Signed-off-by: Bridget McErlean <[email protected]>
1 parent 94702fb commit 4c07889

File tree

13 files changed

+387
-342
lines changed

13 files changed

+387
-342
lines changed

cmd/sonobuoy/app/gen_plugin_def.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222

2323
"github.com/heptio/sonobuoy/pkg/errlog"
24+
"github.com/heptio/sonobuoy/pkg/plugin"
2425
"github.com/heptio/sonobuoy/pkg/plugin/manifest"
2526
"github.com/pkg/errors"
2627
"github.com/spf13/cobra"
@@ -32,7 +33,6 @@ import (
3233
const (
3334
defaultPluginName = "plugin"
3435
defaultPluginDriver = "Job"
35-
defaultMountPath = "/tmp/results"
3636
defaultMountName = "results"
3737
defaultMountReadOnly = false
3838
)
@@ -107,7 +107,7 @@ func defaultManifest() manifest.Manifest {
107107
m.SonobuoyConfig.Driver = defaultPluginDriver
108108
m.Spec.VolumeMounts = []v1.VolumeMount{
109109
v1.VolumeMount{
110-
MountPath: defaultMountPath,
110+
MountPath: plugin.ResultsDir,
111111
Name: defaultMountName,
112112
ReadOnly: defaultMountReadOnly,
113113
},

pkg/client/gen.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ import (
3737
)
3838

3939
const (
40-
e2ePluginName = "e2e"
41-
systemdLogsName = "systemd-logs"
42-
pluginResultsDir = "/tmp/results"
40+
e2ePluginName = "e2e"
41+
systemdLogsName = "systemd-logs"
4342
)
4443

4544
// templateValues are used for direct template substitution for manifest generation.
@@ -257,7 +256,7 @@ func systemdLogsManifest(cfg *GenConfig) *manifest.Manifest {
257256
ImagePullPolicy: corev1.PullPolicy(cfg.ImagePullPolicy),
258257
Env: []corev1.EnvVar{
259258
{Name: "CHROOT_DIR", Value: "/node"},
260-
{Name: "RESULTS_DIR", Value: pluginResultsDir},
259+
{Name: "RESULTS_DIR", Value: plugin.ResultsDir},
261260
{Name: "NODE_NAME",
262261
ValueFrom: &corev1.EnvVarSource{
263262
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "spec.nodeName"},
@@ -271,7 +270,7 @@ func systemdLogsManifest(cfg *GenConfig) *manifest.Manifest {
271270
{
272271
ReadOnly: false,
273272
Name: "results",
274-
MountPath: pluginResultsDir,
273+
MountPath: plugin.ResultsDir,
275274
}, {
276275
ReadOnly: false,
277276
Name: "root",
@@ -306,7 +305,7 @@ func e2eManifest(cfg *GenConfig) *manifest.Manifest {
306305
{
307306
ReadOnly: false,
308307
Name: "results",
309-
MountPath: pluginResultsDir,
308+
MountPath: plugin.ResultsDir,
310309
},
311310
},
312311
},

pkg/plugin/constants.go

+3
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ package plugin
1919
const (
2020
// GracefulShutdownPeriod is how long plugins have to cleanly finish before they are terminated.
2121
GracefulShutdownPeriod = 60
22+
23+
// ResultsDir is the directory where results will be available in Sonobuoy plugin containers.
24+
ResultsDir = "/tmp/results"
2225
)

pkg/plugin/driver/base.go

+73-54
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ import (
2323
"encoding/pem"
2424
"fmt"
2525

26+
"github.com/heptio/sonobuoy/pkg/plugin"
2627
"github.com/heptio/sonobuoy/pkg/plugin/manifest"
2728
"github.com/pkg/errors"
2829
v1 "k8s.io/api/core/v1"
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30-
kuberuntime "k8s.io/apimachinery/pkg/runtime"
3131
)
3232

3333
// Base is the struct that stores state for plugin drivers and contains helper methods.
@@ -42,23 +42,6 @@ type Base struct {
4242
CustomAnnotations map[string]string
4343
}
4444

45-
// TemplateData is all the fields available to plugin driver templates.
46-
type TemplateData struct {
47-
PluginName string
48-
ResultType string
49-
SessionID string
50-
Namespace string
51-
SonobuoyImage string
52-
ImagePullPolicy string
53-
ImagePullSecrets string
54-
CustomAnnotations map[string]string
55-
ProducerContainer string
56-
MasterAddress string
57-
CACert string
58-
SecretName string
59-
ExtraVolumes []string
60-
}
61-
6245
// GetSessionID returns the session id associated with the plugin.
6346
func (b *Base) GetSessionID() string {
6447
return b.SessionID
@@ -94,42 +77,6 @@ func (b *Base) GetResultFiles() []string {
9477
return b.Definition.SonobuoyConfig.ResultFiles
9578
}
9679

97-
//GetTemplateData fills a TemplateData struct with the passed in and state variables.
98-
func (b *Base) GetTemplateData(masterAddress string, cert *tls.Certificate) (*TemplateData, error) {
99-
100-
container, err := kuberuntime.Encode(manifest.Encoder, &b.Definition.Spec)
101-
if err != nil {
102-
return nil, errors.Wrapf(err, "couldn't reserialize container for job %q", b.Definition.SonobuoyConfig.PluginName)
103-
}
104-
105-
volumes := make([]string, len(b.Definition.ExtraVolumes))
106-
for i, volume := range b.Definition.ExtraVolumes {
107-
enc, err := kuberuntime.Encode(manifest.Encoder, &volume)
108-
if err != nil {
109-
return nil, errors.Wrap(err, "couldn't serialize extra volume")
110-
}
111-
volumes[i] = string(enc)
112-
}
113-
114-
cacert := getCACertPEM(cert)
115-
116-
return &TemplateData{
117-
PluginName: b.Definition.SonobuoyConfig.PluginName,
118-
ResultType: b.Definition.SonobuoyConfig.ResultType,
119-
SessionID: b.SessionID,
120-
Namespace: b.Namespace,
121-
SonobuoyImage: b.SonobuoyImage,
122-
ImagePullPolicy: b.ImagePullPolicy,
123-
ImagePullSecrets: b.ImagePullSecrets,
124-
CustomAnnotations: b.CustomAnnotations,
125-
ProducerContainer: string(container),
126-
MasterAddress: masterAddress,
127-
CACert: cacert,
128-
SecretName: b.GetSecretName(),
129-
ExtraVolumes: volumes,
130-
}, nil
131-
}
132-
13380
// MakeTLSSecret makes a Kubernetes secret object for the given TLS certificate.
13481
func (b *Base) MakeTLSSecret(cert *tls.Certificate) (*v1.Secret, error) {
13582
rsaKey, ok := cert.PrivateKey.(*ecdsa.PrivateKey)
@@ -192,3 +139,75 @@ func getKeyPEM(key *ecdsa.PrivateKey) ([]byte, error) {
192139
Bytes: derKEY,
193140
}), nil
194141
}
142+
143+
func (b *Base) workerEnvironment(hostname string, cert *tls.Certificate) []v1.EnvVar {
144+
envVars := []v1.EnvVar{
145+
{
146+
Name: "NODE_NAME",
147+
ValueFrom: &v1.EnvVarSource{
148+
FieldRef: &v1.ObjectFieldSelector{
149+
FieldPath: "spec.nodeName",
150+
},
151+
},
152+
},
153+
{
154+
Name: "RESULTS_DIR",
155+
Value: plugin.ResultsDir,
156+
},
157+
{
158+
Name: "RESULT_TYPE",
159+
Value: b.GetResultType(),
160+
},
161+
{
162+
Name: "MASTER_URL",
163+
Value: hostname,
164+
},
165+
{
166+
Name: "CA_CERT",
167+
Value: getCACertPEM(cert),
168+
},
169+
{
170+
Name: "CLIENT_CERT",
171+
ValueFrom: &v1.EnvVarSource{
172+
SecretKeyRef: &v1.SecretKeySelector{
173+
LocalObjectReference: v1.LocalObjectReference{
174+
Name: b.GetSecretName(),
175+
},
176+
Key: "tls.crt",
177+
},
178+
},
179+
},
180+
{
181+
Name: "CLIENT_KEY",
182+
ValueFrom: &v1.EnvVarSource{
183+
SecretKeyRef: &v1.SecretKeySelector{
184+
LocalObjectReference: v1.LocalObjectReference{
185+
Name: b.GetSecretName(),
186+
},
187+
Key: "tls.key",
188+
},
189+
},
190+
},
191+
}
192+
return envVars
193+
}
194+
195+
// CreateWorkerContainerDefintion creates the container definition to run the Sonobuoy worker for a plugin.
196+
func (b *Base) CreateWorkerContainerDefintion(hostname string, cert *tls.Certificate, command, args []string) v1.Container {
197+
container := v1.Container{
198+
Name: "sonobuoy-worker",
199+
Image: b.SonobuoyImage,
200+
Command: command,
201+
Args: args,
202+
Env: b.workerEnvironment(hostname, cert),
203+
ImagePullPolicy: v1.PullPolicy(b.ImagePullPolicy),
204+
VolumeMounts: []v1.VolumeMount{
205+
{
206+
Name: "results",
207+
ReadOnly: false,
208+
MountPath: plugin.ResultsDir,
209+
},
210+
},
211+
}
212+
return container
213+
}

0 commit comments

Comments
 (0)