Skip to content

Commit

Permalink
Adds commands to print default plugin definitions
Browse files Browse the repository at this point in the history
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 <jschnake@vmware.com>
  • Loading branch information
johnSchnake committed Dec 17, 2019
1 parent be8e183 commit e41ac6e
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 15 deletions.
3 changes: 3 additions & 0 deletions cmd/sonobuoy/app/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
58 changes: 57 additions & 1 deletion cmd/sonobuoy/app/gen_plugin_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion cmd/sonobuoy/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
20 changes: 7 additions & 13 deletions pkg/client/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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)))
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()
}
Expand Down
37 changes: 37 additions & 0 deletions pkg/plugin/manifest/helper/helper.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit e41ac6e

Please sign in to comment.