forked from vmware-tanzu/sonobuoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
110 lines (91 loc) · 2.64 KB
/
run.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
/*
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 app
import (
"fmt"
"os"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/heptio/sonobuoy/pkg/client"
"github.com/heptio/sonobuoy/pkg/errlog"
)
type runFlags struct {
genFlags
skipPreflight bool
}
var runflags runFlags
func RunFlagSet(cfg *runFlags) *pflag.FlagSet {
runset := pflag.NewFlagSet("run", pflag.ExitOnError)
// Default to detect since we need kubeconfig regardless
runset.AddFlagSet(GenFlagSet(&cfg.genFlags, DetectRBACMode, ConformanceImageVersionAuto))
AddSkipPreflightFlag(&cfg.skipPreflight, runset)
return runset
}
func (r *runFlags) Config() (*client.RunConfig, error) {
gencfg, err := r.genFlags.Config()
if err != nil {
return nil, err
}
return &client.RunConfig{
GenConfig: *gencfg,
}, nil
}
func init() {
cmd := &cobra.Command{
Use: "run",
Short: "Submits a sonobuoy run",
Run: submitSonobuoyRun,
Args: cobra.ExactArgs(0),
}
cmd.Flags().AddFlagSet(RunFlagSet(&runflags))
RootCmd.AddCommand(cmd)
}
func submitSonobuoyRun(cmd *cobra.Command, args []string) {
cfg, err := runflags.kubecfg.Get()
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't get REST client"))
os.Exit(1)
}
runCfg, err := runflags.Config()
if err != nil {
errlog.LogError(errors.Wrap(err, "could not retrieve E2E config"))
os.Exit(1)
}
sbc, err := getSonobuoyClient(cfg)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
}
plugins := make([]string, len(runCfg.Config.PluginSelections))
for i, plugin := range runCfg.Config.PluginSelections {
plugins[i] = plugin.Name
}
if len(plugins) > 0 {
fmt.Printf("Running plugins: %v\n", strings.Join(plugins, ", "))
}
if !runflags.skipPreflight {
if errs := sbc.PreflightChecks(&client.PreflightConfig{Namespace: runflags.namespace}); len(errs) > 0 {
errlog.LogError(errors.New("Preflight checks failed"))
for _, err := range errs {
errlog.LogError(err)
}
os.Exit(1)
}
}
if err := sbc.Run(runCfg); err != nil {
errlog.LogError(errors.Wrap(err, "error attempting to run sonobuoy"))
os.Exit(1)
}
}