Skip to content

Commit

Permalink
Get rid of Linux specific scripts as much as possible (#1072)
Browse files Browse the repository at this point in the history
If we are to support Windows nodes, we want to avoid using bash scripts
so that we don't have to duplicate the logic and translate it into a
powershell script.

Changes include:
 - remove the run_master.sh script and instead rely on the Dockerfile
to have the proper aggregator invocation. This way, regardless of the
underlying OS, the image knows the right command to use.
 - remove the script for running the worker in single-node mode. This
only served to run Sonobuoy and then sleep for some amount of time to
avoid restarting the container. Instead, a flag was added to the
single-node command and golang handles the sleep functionality now. By
default it sleeps 0 seconds, consistent with the existing logic.
 - Slight modifications to command structure so that the subcommands
can use the cobra RunE method and logging can be done at the top level
only. This helps avoid using `os.Exit` which hinders testability.

xref #732

Signed-off-by: John Schnake <[email protected]>
  • Loading branch information
johnSchnake authored Jan 21, 2020
1 parent c927a9f commit a49641f
Show file tree
Hide file tree
Showing 27 changed files with 79 additions and 177 deletions.
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
# limitations under the License.

FROM BASEIMAGE
MAINTAINER Timothy St. Clair "tstclair@heptio.com"
MAINTAINER John Schnake "jschnake@vmware.com"

CMD1

ADD BINARY /sonobuoy
ADD scripts/run_master.sh /run_master.sh
ADD scripts/run_single_node_worker.sh /run_single_node_worker.sh
WORKDIR /
CMD ["/bin/sh", "-c", "/run_master.sh"]
CMD /sonobuoy aggregator --no-exit -v 3 --logtostderr
75 changes: 48 additions & 27 deletions cmd/sonobuoy/app/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ import (
"syscall"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
"github.com/vmware-tanzu/sonobuoy/pkg/plugin"
"github.com/vmware-tanzu/sonobuoy/pkg/plugin/aggregation"
"github.com/vmware-tanzu/sonobuoy/pkg/worker"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// NewCmdWorker is the cobra command that acts as the entrypoint for Sonobuoy when running
Expand All @@ -50,24 +50,34 @@ func NewCmdWorker() *cobra.Command {
Args: cobra.ExactArgs(0),
}

workerCmd.AddCommand(singleNodeCmd)
workerCmd.AddCommand(globalCmd)
workerCmd.AddCommand(newSingleNodeCmd())
workerCmd.AddCommand(newGlobalCmd())

return workerCmd
}

var globalCmd = &cobra.Command{
Use: "global",
Short: "Submit results scoped to the whole cluster",
Run: runGatherGlobal,
Args: cobra.ExactArgs(0),
func newGlobalCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "global",
Short: "Submit results scoped to the whole cluster",
RunE: runGatherGlobal,
Args: cobra.ExactArgs(0),
}

return cmd
}

var singleNodeCmd = &cobra.Command{
Use: "single-node",
Short: "Submit results scoped to a single node",
Run: runGatherSingleNode,
Args: cobra.ExactArgs(0),
func newSingleNodeCmd() *cobra.Command {
var sleep int64
cmd := &cobra.Command{
Use: "single-node",
Short: "Submit results scoped to a single node",
RunE: runGatherSingleNode(&sleep),
Args: cobra.ExactArgs(0),
}

cmd.Flags().Int64Var(&sleep, "sleep", 0, "After sending results, keeps the process alive for N seconds to avoid restarting the container. If N<0, Sonobuoy sleeps forever.")
return cmd
}

// sigHandler returns a channel that will receive a message after the timeout
Expand Down Expand Up @@ -115,19 +125,30 @@ func loadAndValidateConfig() (*plugin.WorkerConfig, error) {
return cfg, nil
}

func runGatherSingleNode(cmd *cobra.Command, args []string) {
err := runGather(false)
if err != nil {
errlog.LogError(err)
os.Exit(1)
}
func runGatherGlobal(cmd *cobra.Command, args []string) error {
return runGather(true)
}

func runGatherGlobal(cmd *cobra.Command, args []string) {
err := runGather(true)
if err != nil {
errlog.LogError(err)
os.Exit(1)
// runGatherSingleNode returns a closure which will run the data gathering and then sleep
// for the specified amount of seconds.
func runGatherSingleNode(sleep *int64) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
err := runGather(false)

switch {
case sleep == nil || *sleep == 0:
// No sleep.
case *sleep < 0:
// Sleep forever.
logrus.Infof("Results transmitted to aggregator. Sleeping forever.")
for {
time.Sleep(60 * time.Minute)
}
case *sleep > 0:
logrus.Infof("Results transmitted to aggregator. Sleeping for %v seconds", *sleep)
time.Sleep(time.Duration(*sleep) * time.Second)
}
return err
}
}

Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
"os"

"github.com/vmware-tanzu/sonobuoy/cmd/sonobuoy/app"
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
)

func main() {
err := app.NewSonobuoyCommand().Execute()
if err != nil {
// Execute takes care of printing the error
errlog.LogError(err)
os.Exit(1)
}
}
6 changes: 1 addition & 5 deletions pkg/client/testdata/custom-systemd-logs-image.golden
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/default-plugins-via-nil-selection.golden
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ metadata:
namespace: sonobuoy
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/default-pod-spec.golden
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/default.golden
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ metadata:
namespace: sonobuoy
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/e2e-default.golden
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ metadata:
namespace: sonobuoy
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/e2e-progress-custom-port.golden
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/e2e-progress-vs-user-defined.golden
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/e2e-progress.golden
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/envoverrides.golden
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/goRunnerRemoved.golden
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/imagePullSecrets.golden
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/manual-custom-plugin-plus-e2e.golden
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/manual-custom-plugin-plus-systemd.golden
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/manual-custom-plugin.golden
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/manual-e2e.golden
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/no-plugins-via-selection.golden
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ metadata:
namespace: sonobuoy
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/plugins-and-pluginSelection.golden
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ metadata:
namespace: sonobuoy
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/ssh.golden
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/systemd-logs-default.golden
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ metadata:
namespace: sonobuoy
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/testdata/use-existing-pod-spec.golden
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ metadata:
namespace:
spec:
containers:
- command:
- /bin/bash
- -c
- /sonobuoy master --no-exit=true -v 3 --logtostderr
env:
- env:
- name: SONOBUOY_ADVERTISE_IP
valueFrom:
fieldRef:
Expand Down
Loading

0 comments on commit a49641f

Please sign in to comment.