From e41ac6ee22918fc6d39d94d51f69b75078eab187 Mon Sep 17 00:00:00 2001 From: John Schnake Date: Fri, 13 Dec 2019 22:40:35 -0600 Subject: [PATCH] Adds commands to print default plugin definitions We have the `sonobuoy gen` command which prints a lot of different types of resources and `sonobuoy gen plugin` which prints custom plugin definitions. Sometimes it would be helpful to be able to print the default e2e or sytemdlogs plugin definitions so that they can be tweaked. Added two commands `sonobuoy gen plugins e2e` and `sonobuoy gen plugins systemd-logs` which does this. Minor tweak to code organization to improve code reuse. Fixes #991 Signed-off-by: John Schnake --- cmd/sonobuoy/app/gen.go | 3 ++ cmd/sonobuoy/app/gen_plugin_def.go | 58 +++++++++++++++++++++++++++- cmd/sonobuoy/app/root.go | 6 ++- pkg/client/gen.go | 20 ++++------ pkg/plugin/manifest/helper/helper.go | 37 ++++++++++++++++++ 5 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 pkg/plugin/manifest/helper/helper.go diff --git a/cmd/sonobuoy/app/gen.go b/cmd/sonobuoy/app/gen.go index e56c9251d..683ba036c 100644 --- a/cmd/sonobuoy/app/gen.go +++ b/cmd/sonobuoy/app/gen.go @@ -67,7 +67,10 @@ type genFlags struct { genflags *pflag.FlagSet } +// TODO(jschnake): Avoid using these globals if possible. var genflags genFlags +var genSystemdLogsflags genFlags +var genE2Eflags genFlags func GenFlagSet(cfg *genFlags, rbac RBACMode) *pflag.FlagSet { genset := pflag.NewFlagSet("generate", pflag.ExitOnError) diff --git a/cmd/sonobuoy/app/gen_plugin_def.go b/cmd/sonobuoy/app/gen_plugin_def.go index 2de3a5e1a..037f48c92 100644 --- a/cmd/sonobuoy/app/gen_plugin_def.go +++ b/cmd/sonobuoy/app/gen_plugin_def.go @@ -20,15 +20,17 @@ import ( "fmt" "os" + "github.com/vmware-tanzu/sonobuoy/pkg/client" "github.com/vmware-tanzu/sonobuoy/pkg/client/results" "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/manifest" + manifesthelper "github.com/vmware-tanzu/sonobuoy/pkg/plugin/manifest/helper" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" - v1 "k8s.io/api/core/v1" kuberuntime "k8s.io/apimachinery/pkg/runtime" ) @@ -169,3 +171,57 @@ func genPluginDef(cfg *GenPluginDefConfig) ([]byte, error) { yaml, err := kuberuntime.Encode(manifest.Encoder, &cfg.def) return yaml, errors.Wrap(err, "serializing as YAML") } + +func NewCmdGenE2E() *cobra.Command { + var cmd = &cobra.Command{ + Use: "e2e", + Short: "Generates the e2e plugin definition based on the given options", + RunE: genE2EManifest, + Args: cobra.NoArgs, + } + cmd.Flags().AddFlagSet(GenFlagSet(&genE2Eflags, EnabledRBACMode)) + return cmd +} + +func genE2EManifest(cmd *cobra.Command, args []string) error { + cfg, err := genflags.Config() + if err != nil { + return err + } + + m := client.E2EManifest(cfg) + yaml, err := manifesthelper.ToYAML(m, cfg.ShowDefaultPodSpec) + if err != nil { + return err + } + + fmt.Println(string(yaml)) + return nil +} + +func NewCmdGenSystemdLogs() *cobra.Command { + var cmd = &cobra.Command{ + Use: "systemd-logs", + Short: "Generates the systemd-logs plugin definition based on the given options", + RunE: genSystemdLogsManifest, + Args: cobra.NoArgs, + } + cmd.Flags().AddFlagSet(GenFlagSet(&genSystemdLogsflags, EnabledRBACMode)) + return cmd +} + +func genSystemdLogsManifest(cmd *cobra.Command, args []string) error { + cfg, err := genflags.Config() + if err != nil { + return err + } + + m := client.SystemdLogsManifest(cfg) + yaml, err := manifesthelper.ToYAML(m, cfg.ShowDefaultPodSpec) + if err != nil { + return err + } + + fmt.Println(string(yaml)) + return nil +} diff --git a/cmd/sonobuoy/app/root.go b/cmd/sonobuoy/app/root.go index 131974f0d..a8390934c 100644 --- a/cmd/sonobuoy/app/root.go +++ b/cmd/sonobuoy/app/root.go @@ -40,9 +40,13 @@ func NewSonobuoyCommand() *cobra.Command { cmds.AddCommand(NewCmdE2E()) gen := NewCmdGen() - gen.AddCommand(NewCmdGenPluginDef()) + genPlugin := NewCmdGenPluginDef() + genPlugin.AddCommand(NewCmdGenE2E()) + genPlugin.AddCommand(NewCmdGenSystemdLogs()) + gen.AddCommand(genPlugin) gen.AddCommand(NewCmdGenConfig()) gen.AddCommand(NewCmdGenImageRepoConfig()) + cmds.AddCommand(gen) cmds.AddCommand(NewCmdLogs()) diff --git a/pkg/client/gen.go b/pkg/client/gen.go index 1fed6acea..d97331f31 100644 --- a/pkg/client/gen.go +++ b/pkg/client/gen.go @@ -30,12 +30,11 @@ import ( "github.com/vmware-tanzu/sonobuoy/pkg/config" "github.com/vmware-tanzu/sonobuoy/pkg/plugin" - "github.com/vmware-tanzu/sonobuoy/pkg/plugin/driver" "github.com/vmware-tanzu/sonobuoy/pkg/plugin/manifest" + manifesthelper "github.com/vmware-tanzu/sonobuoy/pkg/plugin/manifest/helper" "github.com/vmware-tanzu/sonobuoy/pkg/templates" corev1 "k8s.io/api/core/v1" - kuberuntime "k8s.io/apimachinery/pkg/runtime" ) const ( @@ -116,9 +115,9 @@ func (*SonobuoyClient) GenerateManifest(cfg *GenConfig) ([]byte, error) { for _, v := range cfg.DynamicPlugins { switch v { case e2ePluginName: - plugins = append(plugins, e2eManifest(cfg)) + plugins = append(plugins, E2EManifest(cfg)) case systemdLogsName: - plugins = append(plugins, systemdLogsManifest(cfg)) + plugins = append(plugins, SystemdLogsManifest(cfg)) } } plugins = append(plugins, cfg.StaticPlugins...) @@ -164,14 +163,9 @@ func (*SonobuoyClient) GenerateManifest(cfg *GenConfig) ([]byte, error) { pluginYAML := []string{} for _, v := range plugins { - if cfg.ShowDefaultPodSpec && v.PodSpec == nil { - v.PodSpec = &manifest.PodSpec{ - PodSpec: driver.DefaultPodSpec(v.SonobuoyConfig.Driver), - } - } - yaml, err := kuberuntime.Encode(manifest.Encoder, v) + yaml, err := manifesthelper.ToYAML(v, cfg.ShowDefaultPodSpec) if err != nil { - return nil, errors.Wrapf(err, "serializing plugin %v as YAML", v.SonobuoyConfig.PluginName) + return nil, err } pluginYAML = append(pluginYAML, strings.TrimSpace(string(yaml))) } @@ -249,7 +243,7 @@ func mergeEnv(e1, e2 []corev1.EnvVar, removeKeys map[string]struct{}) []corev1.E return returnEnv } -func systemdLogsManifest(cfg *GenConfig) *manifest.Manifest { +func SystemdLogsManifest(cfg *GenConfig) *manifest.Manifest { trueVal := true return &manifest.Manifest{ SonobuoyConfig: manifest.SonobuoyConfig{ @@ -291,7 +285,7 @@ func systemdLogsManifest(cfg *GenConfig) *manifest.Manifest { } } -func e2eManifest(cfg *GenConfig) *manifest.Manifest { +func E2EManifest(cfg *GenConfig) *manifest.Manifest { if cfg.Config == nil { cfg.Config = config.New() } diff --git a/pkg/plugin/manifest/helper/helper.go b/pkg/plugin/manifest/helper/helper.go new file mode 100644 index 000000000..3e9c0110a --- /dev/null +++ b/pkg/plugin/manifest/helper/helper.go @@ -0,0 +1,37 @@ +/* +Copyright 2019 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 manifest + +import ( + "github.com/vmware-tanzu/sonobuoy/pkg/plugin/driver" + "github.com/vmware-tanzu/sonobuoy/pkg/plugin/manifest" + + "github.com/pkg/errors" + kuberuntime "k8s.io/apimachinery/pkg/runtime" +) + +// ToYAML will serialize the manifest and add the default podspec (based on the appropriate drive) +// if not already set in the manifest. +func ToYAML(m *manifest.Manifest, showDefaultPodSpec bool) ([]byte, error) { + if showDefaultPodSpec && m.PodSpec == nil { + m.PodSpec = &manifest.PodSpec{ + PodSpec: driver.DefaultPodSpec(m.SonobuoyConfig.Driver), + } + } + yaml, err := kuberuntime.Encode(manifest.Encoder, m) + return yaml, errors.Wrapf(err, "serializing plugin %v as YAML", m.SonobuoyConfig.PluginName) +}