Skip to content

Commit a49641f

Browse files
authored
Get rid of Linux specific scripts as much as possible (#1072)
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]>
1 parent c927a9f commit a49641f

27 files changed

+79
-177
lines changed

Dockerfile

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
# limitations under the License.
1414

1515
FROM BASEIMAGE
16-
MAINTAINER Timothy St. Clair "tstclair@heptio.com"
16+
MAINTAINER John Schnake "jschnake@vmware.com"
1717

1818
CMD1
1919

2020
ADD BINARY /sonobuoy
21-
ADD scripts/run_master.sh /run_master.sh
22-
ADD scripts/run_single_node_worker.sh /run_single_node_worker.sh
2321
WORKDIR /
24-
CMD ["/bin/sh", "-c", "/run_master.sh"]
22+
CMD /sonobuoy aggregator --no-exit -v 3 --logtostderr

cmd/sonobuoy/app/worker.go

+48-27
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ import (
3030
"syscall"
3131
"time"
3232

33-
"github.com/pkg/errors"
34-
"github.com/sirupsen/logrus"
35-
"github.com/spf13/cobra"
36-
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
3733
"github.com/vmware-tanzu/sonobuoy/pkg/plugin"
3834
"github.com/vmware-tanzu/sonobuoy/pkg/plugin/aggregation"
3935
"github.com/vmware-tanzu/sonobuoy/pkg/worker"
36+
37+
"github.com/pkg/errors"
38+
"github.com/sirupsen/logrus"
39+
"github.com/spf13/cobra"
4040
)
4141

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

53-
workerCmd.AddCommand(singleNodeCmd)
54-
workerCmd.AddCommand(globalCmd)
53+
workerCmd.AddCommand(newSingleNodeCmd())
54+
workerCmd.AddCommand(newGlobalCmd())
5555

5656
return workerCmd
5757
}
5858

59-
var globalCmd = &cobra.Command{
60-
Use: "global",
61-
Short: "Submit results scoped to the whole cluster",
62-
Run: runGatherGlobal,
63-
Args: cobra.ExactArgs(0),
59+
func newGlobalCmd() *cobra.Command {
60+
cmd := &cobra.Command{
61+
Use: "global",
62+
Short: "Submit results scoped to the whole cluster",
63+
RunE: runGatherGlobal,
64+
Args: cobra.ExactArgs(0),
65+
}
66+
67+
return cmd
6468
}
6569

66-
var singleNodeCmd = &cobra.Command{
67-
Use: "single-node",
68-
Short: "Submit results scoped to a single node",
69-
Run: runGatherSingleNode,
70-
Args: cobra.ExactArgs(0),
70+
func newSingleNodeCmd() *cobra.Command {
71+
var sleep int64
72+
cmd := &cobra.Command{
73+
Use: "single-node",
74+
Short: "Submit results scoped to a single node",
75+
RunE: runGatherSingleNode(&sleep),
76+
Args: cobra.ExactArgs(0),
77+
}
78+
79+
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.")
80+
return cmd
7181
}
7282

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

118-
func runGatherSingleNode(cmd *cobra.Command, args []string) {
119-
err := runGather(false)
120-
if err != nil {
121-
errlog.LogError(err)
122-
os.Exit(1)
123-
}
128+
func runGatherGlobal(cmd *cobra.Command, args []string) error {
129+
return runGather(true)
124130
}
125131

126-
func runGatherGlobal(cmd *cobra.Command, args []string) {
127-
err := runGather(true)
128-
if err != nil {
129-
errlog.LogError(err)
130-
os.Exit(1)
132+
// runGatherSingleNode returns a closure which will run the data gathering and then sleep
133+
// for the specified amount of seconds.
134+
func runGatherSingleNode(sleep *int64) func(cmd *cobra.Command, args []string) error {
135+
return func(cmd *cobra.Command, args []string) error {
136+
err := runGather(false)
137+
138+
switch {
139+
case sleep == nil || *sleep == 0:
140+
// No sleep.
141+
case *sleep < 0:
142+
// Sleep forever.
143+
logrus.Infof("Results transmitted to aggregator. Sleeping forever.")
144+
for {
145+
time.Sleep(60 * time.Minute)
146+
}
147+
case *sleep > 0:
148+
logrus.Infof("Results transmitted to aggregator. Sleeping for %v seconds", *sleep)
149+
time.Sleep(time.Duration(*sleep) * time.Second)
150+
}
151+
return err
131152
}
132153
}
133154

main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ import (
2020
"os"
2121

2222
"github.com/vmware-tanzu/sonobuoy/cmd/sonobuoy/app"
23+
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
2324
)
2425

2526
func main() {
2627
err := app.NewSonobuoyCommand().Execute()
2728
if err != nil {
28-
// Execute takes care of printing the error
29+
errlog.LogError(err)
2930
os.Exit(1)
3031
}
3132
}

pkg/client/testdata/custom-systemd-logs-image.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ metadata:
7474
namespace:
7575
spec:
7676
containers:
77-
- command:
78-
- /bin/bash
79-
- -c
80-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
81-
env:
77+
- env:
8278
- name: SONOBUOY_ADVERTISE_IP
8379
valueFrom:
8480
fieldRef:

pkg/client/testdata/default-plugins-via-nil-selection.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ metadata:
9292
namespace: sonobuoy
9393
spec:
9494
containers:
95-
- command:
96-
- /bin/bash
97-
- -c
98-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
99-
env:
95+
- env:
10096
- name: SONOBUOY_ADVERTISE_IP
10197
valueFrom:
10298
fieldRef:

pkg/client/testdata/default-pod-spec.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,7 @@ metadata:
117117
namespace:
118118
spec:
119119
containers:
120-
- command:
121-
- /bin/bash
122-
- -c
123-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
124-
env:
120+
- env:
125121
- name: SONOBUOY_ADVERTISE_IP
126122
valueFrom:
127123
fieldRef:

pkg/client/testdata/default.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ metadata:
9292
namespace: sonobuoy
9393
spec:
9494
containers:
95-
- command:
96-
- /bin/bash
97-
- -c
98-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
99-
env:
95+
- env:
10096
- name: SONOBUOY_ADVERTISE_IP
10197
valueFrom:
10298
fieldRef:

pkg/client/testdata/e2e-default.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ metadata:
6363
namespace: sonobuoy
6464
spec:
6565
containers:
66-
- command:
67-
- /bin/bash
68-
- -c
69-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
70-
env:
66+
- env:
7167
- name: SONOBUOY_ADVERTISE_IP
7268
valueFrom:
7369
fieldRef:

pkg/client/testdata/e2e-progress-custom-port.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ metadata:
6666
namespace:
6767
spec:
6868
containers:
69-
- command:
70-
- /bin/bash
71-
- -c
72-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
73-
env:
69+
- env:
7470
- name: SONOBUOY_ADVERTISE_IP
7571
valueFrom:
7672
fieldRef:

pkg/client/testdata/e2e-progress-vs-user-defined.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ metadata:
6666
namespace:
6767
spec:
6868
containers:
69-
- command:
70-
- /bin/bash
71-
- -c
72-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
73-
env:
69+
- env:
7470
- name: SONOBUOY_ADVERTISE_IP
7571
valueFrom:
7672
fieldRef:

pkg/client/testdata/e2e-progress.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ metadata:
6666
namespace:
6767
spec:
6868
containers:
69-
- command:
70-
- /bin/bash
71-
- -c
72-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
73-
env:
69+
- env:
7470
- name: SONOBUOY_ADVERTISE_IP
7571
valueFrom:
7672
fieldRef:

pkg/client/testdata/envoverrides.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ metadata:
6666
namespace:
6767
spec:
6868
containers:
69-
- command:
70-
- /bin/bash
71-
- -c
72-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
73-
env:
69+
- env:
7470
- name: SONOBUOY_ADVERTISE_IP
7571
valueFrom:
7672
fieldRef:

pkg/client/testdata/goRunnerRemoved.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ metadata:
6161
namespace:
6262
spec:
6363
containers:
64-
- command:
65-
- /bin/bash
66-
- -c
67-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
68-
env:
64+
- env:
6965
- name: SONOBUOY_ADVERTISE_IP
7066
valueFrom:
7167
fieldRef:

pkg/client/testdata/imagePullSecrets.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ metadata:
6363
namespace:
6464
spec:
6565
containers:
66-
- command:
67-
- /bin/bash
68-
- -c
69-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
70-
env:
66+
- env:
7167
- name: SONOBUOY_ADVERTISE_IP
7268
valueFrom:
7369
fieldRef:

pkg/client/testdata/manual-custom-plugin-plus-e2e.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,7 @@ metadata:
7070
namespace:
7171
spec:
7272
containers:
73-
- command:
74-
- /bin/bash
75-
- -c
76-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
77-
env:
73+
- env:
7874
- name: SONOBUOY_ADVERTISE_IP
7975
valueFrom:
8076
fieldRef:

pkg/client/testdata/manual-custom-plugin-plus-systemd.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ metadata:
8080
namespace:
8181
spec:
8282
containers:
83-
- command:
84-
- /bin/bash
85-
- -c
86-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
87-
env:
83+
- env:
8884
- name: SONOBUOY_ADVERTISE_IP
8985
valueFrom:
9086
fieldRef:

pkg/client/testdata/manual-custom-plugin.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ metadata:
5151
namespace:
5252
spec:
5353
containers:
54-
- command:
55-
- /bin/bash
56-
- -c
57-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
58-
env:
54+
- env:
5955
- name: SONOBUOY_ADVERTISE_IP
6056
valueFrom:
6157
fieldRef:

pkg/client/testdata/manual-e2e.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ metadata:
6363
namespace:
6464
spec:
6565
containers:
66-
- command:
67-
- /bin/bash
68-
- -c
69-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
70-
env:
66+
- env:
7167
- name: SONOBUOY_ADVERTISE_IP
7268
valueFrom:
7369
fieldRef:

pkg/client/testdata/no-plugins-via-selection.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ metadata:
4343
namespace: sonobuoy
4444
spec:
4545
containers:
46-
- command:
47-
- /bin/bash
48-
- -c
49-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
50-
env:
46+
- env:
5147
- name: SONOBUOY_ADVERTISE_IP
5248
valueFrom:
5349
fieldRef:

pkg/client/testdata/plugins-and-pluginSelection.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ metadata:
5858
namespace: sonobuoy
5959
spec:
6060
containers:
61-
- command:
62-
- /bin/bash
63-
- -c
64-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
65-
env:
61+
- env:
6662
- name: SONOBUOY_ADVERTISE_IP
6763
valueFrom:
6864
fieldRef:

pkg/client/testdata/ssh.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ metadata:
8787
namespace:
8888
spec:
8989
containers:
90-
- command:
91-
- /bin/bash
92-
- -c
93-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
94-
env:
90+
- env:
9591
- name: SONOBUOY_ADVERTISE_IP
9692
valueFrom:
9793
fieldRef:

pkg/client/testdata/systemd-logs-default.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,7 @@ metadata:
7373
namespace: sonobuoy
7474
spec:
7575
containers:
76-
- command:
77-
- /bin/bash
78-
- -c
79-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
80-
env:
76+
- env:
8177
- name: SONOBUOY_ADVERTISE_IP
8278
valueFrom:
8379
fieldRef:

pkg/client/testdata/use-existing-pod-spec.golden

+1-5
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ metadata:
5353
namespace:
5454
spec:
5555
containers:
56-
- command:
57-
- /bin/bash
58-
- -c
59-
- /sonobuoy master --no-exit=true -v 3 --logtostderr
60-
env:
56+
- env:
6157
- name: SONOBUOY_ADVERTISE_IP
6258
valueFrom:
6359
fieldRef:

0 commit comments

Comments
 (0)