-
Notifications
You must be signed in to change notification settings - Fork 341
/
gen_plugin_def.go
171 lines (141 loc) · 4.77 KB
/
gen_plugin_def.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
/*
Copyright the Sonobuoy contributors 2019
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 app
import (
"fmt"
"os"
"github.com/heptio/sonobuoy/pkg/client/results"
"github.com/heptio/sonobuoy/pkg/errlog"
"github.com/heptio/sonobuoy/pkg/plugin"
"github.com/heptio/sonobuoy/pkg/plugin/driver"
"github.com/heptio/sonobuoy/pkg/plugin/manifest"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
v1 "k8s.io/api/core/v1"
kuberuntime "k8s.io/apimachinery/pkg/runtime"
)
const (
defaultPluginName = "plugin"
defaultPluginDriver = "Job"
defaultMountName = "results"
defaultMountReadOnly = false
)
// GenPluginDefConfig are the input options for running
type GenPluginDefConfig struct {
def manifest.Manifest
// env holds the values that will be parsed into the manifest env vars.
env EnvVars
// driver holds the values that will be parsed into the plugin driver.
// Allows validation during flag parsing by having a custom type.
driver pluginDriver
// If set, the default pod spec used by Sonobuoy will be included in the output
showDefaultPodSpec bool
}
// NewCmdGenPluginDef ...
func NewCmdGenPluginDef() *cobra.Command {
genPluginOpts := GenPluginDefConfig{
def: defaultManifest(),
env: map[string]string{},
driver: defaultPluginDriver,
}
cmd := &cobra.Command{
Use: "plugin",
Short: "Generates the manifest Sonobuoy uses to define a plugin",
Run: genPluginDefWrapper(&genPluginOpts),
Args: cobra.NoArgs,
}
genPluginSet := pflag.NewFlagSet("generate plugin", pflag.ExitOnError)
genPluginSet.StringVarP(
&genPluginOpts.def.SonobuoyConfig.PluginName, "name", "n", "",
"Plugin name",
)
genPluginSet.VarP(
&genPluginOpts.driver, "type", "t",
"Plugin Driver (job or daemonset)",
)
genPluginSet.StringVarP(
&genPluginOpts.def.SonobuoyConfig.ResultFormat, "format", "f", results.ResultFormatRaw,
"Result format (junit or raw)",
)
genPluginSet.StringVarP(
&genPluginOpts.def.Spec.Image, "image", "i", "",
"Plugin image",
)
genPluginSet.StringArrayVarP(
&genPluginOpts.def.Spec.Command, "cmd", "c", []string{"./run.sh"},
`Command to run when starting the plugin's container. Can be set multiple times (e.g. --cmd /bin/sh -c "-c")`,
)
genPluginSet.VarP(
&genPluginOpts.env, "env", "e",
"Env var values to set on the plugin (e.g. --env FOO=bar)",
)
genPluginSet.StringArrayVarP(
&genPluginOpts.def.Spec.Args, "arg", "a", []string{},
"Arg values to set on the plugin. Can be set multiple times (e.g. --arg 'arg 1' --arg arg2)",
)
AddShowDefaultPodSpecFlag(&genPluginOpts.showDefaultPodSpec, genPluginSet)
cmd.Flags().AddFlagSet(genPluginSet)
cmd.MarkFlagRequired("name")
cmd.MarkFlagRequired("image")
return cmd
}
// defaultManifest returns the basic manifest the user's options will be placed
// on top of.
func defaultManifest() manifest.Manifest {
m := manifest.Manifest{}
m.Spec.Name = defaultPluginName
m.SonobuoyConfig.Driver = defaultPluginDriver
m.Spec.VolumeMounts = []v1.VolumeMount{
{
MountPath: plugin.ResultsDir,
Name: defaultMountName,
ReadOnly: defaultMountReadOnly,
},
}
return m
}
// genPluginDefWrapper returns a closure around a given *GenPluginDefConfig that
// will adhere to the method signature needed by cobra. It prints the result of
// the action or logs an error and exits with non-zero status.
func genPluginDefWrapper(cfg *GenPluginDefConfig) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
s, err := genPluginDef(cfg)
if err != nil {
errlog.LogError(err)
os.Exit(1)
}
fmt.Println(string(s))
}
}
// genPluginDef returns the YAML for the plugin which Sonobuoy would expect as
// a configMap in order to run/gen a typical run.
func genPluginDef(cfg *GenPluginDefConfig) ([]byte, error) {
// Copy the validated value to the actual field.
cfg.def.SonobuoyConfig.Driver = cfg.driver.String()
// Add env vars to the container spec.
cfg.def.Spec.Env = []v1.EnvVar{}
for k, v := range cfg.env.Map() {
cfg.def.Spec.Env = append(cfg.def.Spec.Env, v1.EnvVar{
Name: k,
Value: v,
})
}
if cfg.showDefaultPodSpec {
cfg.def.PodSpec = &manifest.PodSpec{
PodSpec: driver.DefaultPodSpec(cfg.def.SonobuoyConfig.Driver),
}
}
yaml, err := kuberuntime.Encode(manifest.Encoder, &cfg.def)
return yaml, errors.Wrap(err, "serializing as YAML")
}