Skip to content

Commit 4daa503

Browse files
author
Vladimir Vivien
authored
Merge pull request #1205 from wilsonehusin/distroless
Use distroless images
2 parents cccc0b0 + a5c8ea2 commit 4daa503

13 files changed

+139
-23
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ CMD1
1818

1919
ADD BINARY /sonobuoy
2020
WORKDIR /
21-
CMD /sonobuoy aggregator --no-exit -v 3 --logtostderr
21+
CMD ["/sonobuoy", "aggregator", "--no-exit", "-v", "3", "--logtostderr"]

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ VERBOSE_FLAG = -v
4848
endif
4949
BUILDMNT = /go/src/$(GOTARGET)
5050
BUILD_IMAGE ?= golang:1.15-buster
51-
AMD_IMAGE ?= debian:buster-slim
51+
AMD_IMAGE ?= gcr.io/distroless/static-debian10:latest
5252
ARM_IMAGE ?= arm64v8/ubuntu:16.04
5353
WIN_IMAGE ?= mcr.microsoft.com/windows/servercore:1809
5454

5555
TESTARGS ?= $(VERBOSE_FLAG) -timeout 60s
5656
COVERARGS ?= -coverprofile=coverage.txt -covermode=atomic
5757
TEST_PKGS ?= $(GOTARGET)/cmd/... $(GOTARGET)/pkg/...
58-
TEST_CMD = go test $(TESTARGS)
59-
TEST = GODEBUG=x509ignoreCN=0 $(TEST_CMD) $(COVERARGS) $(TEST_PKGS)
58+
TEST_CMD = GODEBUG=x509ignoreCN=0 go test $(TESTARGS)
59+
TEST = $(TEST_CMD) $(COVERARGS) $(TEST_PKGS)
6060

6161
INT_TEST_PKGS ?= $(GOTARGET)/test/integration/...
6262
INT_TEST= $(TEST_CMD) $(INT_TEST_PKGS) -tags=integration

cmd/sonobuoy/app/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func NewSonobuoyCommand() *cobra.Command {
5757
cmds.AddCommand(NewCmdRun())
5858
cmds.AddCommand(NewCmdImages())
5959
cmds.AddCommand(NewCmdResults())
60+
cmds.AddCommand(NewCmdSplat())
6061

6162
klog.InitFlags(nil)
6263
cmds.PersistentFlags().AddGoFlagSet(flag.CommandLine)

cmd/sonobuoy/app/splat.go

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright 2021 VMware Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package app
18+
19+
import (
20+
"archive/tar"
21+
"compress/gzip"
22+
"io/ioutil"
23+
"os"
24+
"path/filepath"
25+
26+
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
27+
28+
"github.com/spf13/cobra"
29+
)
30+
31+
func NewCmdSplat() *cobra.Command {
32+
cmd := &cobra.Command{
33+
Use: "splat AGGREGATOR_RESULTS_PATH",
34+
Short: "Reads all tarballs in the specified directory and prints the content to STDOUT (for internal use)",
35+
Run: func(cmd *cobra.Command, args []string) {
36+
if err := runSplat(args[0]); err != nil {
37+
errlog.LogError(err)
38+
os.Exit(1)
39+
}
40+
},
41+
Hidden: true,
42+
Args: cobra.ExactArgs(1),
43+
}
44+
return cmd
45+
}
46+
47+
func runSplat(dirPath string) error {
48+
sonobuoyResults, err := filepath.Glob(filepath.Join(dirPath, "*.tar.gz"))
49+
if err != nil {
50+
return err
51+
}
52+
53+
if err = loadResults(os.Stdout, sonobuoyResults); err != nil {
54+
return err
55+
}
56+
57+
return nil
58+
}
59+
60+
func loadResults(w *os.File, filenames []string) error {
61+
gzipWriter := gzip.NewWriter(w)
62+
defer gzipWriter.Close()
63+
64+
tarWriter := tar.NewWriter(gzipWriter)
65+
defer tarWriter.Close()
66+
67+
for _, filename := range filenames {
68+
if err := addFileToTarball(tarWriter, filename); err != nil {
69+
return err
70+
}
71+
}
72+
73+
return nil
74+
}
75+
76+
func addFileToTarball(tarWriter *tar.Writer, filename string) error {
77+
file, err := os.Open(filename)
78+
if err != nil {
79+
return err
80+
}
81+
defer file.Close()
82+
83+
infoHeader, err := file.Stat()
84+
if err != nil {
85+
return err
86+
}
87+
88+
// archive path of content as basename instead of full path
89+
// so un-archiving will result in the working directory, not AGGREGATOR_RESULTS_PATH
90+
header, err := tar.FileInfoHeader(infoHeader, infoHeader.Name())
91+
if err != nil {
92+
return err
93+
}
94+
95+
if err = tarWriter.WriteHeader(header); err != nil {
96+
return err
97+
}
98+
99+
fileContent, err := ioutil.ReadAll(file)
100+
if err != nil {
101+
return err
102+
}
103+
104+
if _, err := tarWriter.Write(fileContent); err != nil {
105+
return err
106+
}
107+
108+
return nil
109+
}

pkg/client/retrieve.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package client
1818

1919
import (
2020
"archive/tar"
21+
"compress/gzip"
2122
"context"
2223
"fmt"
2324
"io"
@@ -44,10 +45,9 @@ const (
4445

4546
var (
4647
linuxTarCommand = []string{
47-
"/usr/bin/env",
48-
"bash",
49-
"-c",
50-
fmt.Sprintf("tar cf - %s/*.tar.gz", config.AggregatorResultsPath),
48+
"/sonobuoy",
49+
"splat",
50+
config.AggregatorResultsPath,
5151
}
5252

5353
winTarCommand = []string{
@@ -158,8 +158,14 @@ func isPodRunningOnWindowsNode(client kubernetes.Interface, ns, podName string)
158158
func UntarAll(reader io.Reader, destFile, prefix string) (filenames []string, returnErr error) {
159159
entrySeq := -1
160160
filenames = []string{}
161-
// TODO: use compression here?
162-
tarReader := tar.NewReader(reader)
161+
// Adding compression per `splat` subcommand implementation
162+
gzReader, err := gzip.NewReader(reader)
163+
if err != nil {
164+
returnErr = err
165+
return
166+
}
167+
defer gzReader.Close()
168+
tarReader := tar.NewReader(gzReader)
163169

164170
// Ensure the reader gets drained. Tar on some platforms doesn't
165171
// seem to consume the end-of-archive marker (long string of 0s).

test/integration/testImage/Dockerfile

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
FROM golang:1.14-stretch AS base
15+
FROM golang:1.15-buster AS base
1616
WORKDIR /src
1717

1818
# Handle the go modules first to take advantage of Docker cache.
@@ -22,10 +22,10 @@ RUN go mod download
2222

2323
# Get the rest of the files and build.
2424
COPY src .
25-
RUN go build -o /go/bin/testImage ./...
25+
RUN CGO_ENABLED=0 go build -o /go/bin/testImage ./...
2626

27-
FROM debian:stretch-slim
28-
MAINTAINER John Schnake "[email protected]"
29-
COPY --from=base /go/bin/testImage /bin/testImage
27+
FROM gcr.io/distroless/static-debian10:latest
28+
WORKDIR /
29+
COPY --from=base /go/bin/testImage /testImage
3030
COPY resources /resources
31-
CMD [ "testImage" ]
31+
CMD [ "/testImage" ]

test/integration/testImage/yaml/ds-junit-passing-tar.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ spec:
88
- /resources/hello-world.txt
99
- /resources/junit-multi-suite-single-failure.xml
1010
command:
11-
- testImage
11+
- /testImage
1212
image: sonobuoy/testimage:v0.1
1313
name: plugin
1414
resources: {}

test/integration/testImage/yaml/ds-manual.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ spec:
1111
- /resources/manual-results-1.yaml
1212
- /resources/manual-results-2.yaml
1313
command:
14-
- testImage
14+
- /testImage
1515
image: sonobuoy/testimage:v0.1
1616
name: plugin
1717
resources: {}

test/integration/testImage/yaml/ds-raw-passing-tar.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ spec:
88
- /resources/hello-world.txt
99
- /resources/junit-multi-suite-single-failure.xml
1010
command:
11-
- testImage
11+
- /testImage
1212
image: sonobuoy/testimage:v0.1
1313
name: plugin
1414
resources: {}

test/integration/testImage/yaml/job-junit-passing-singlefile.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ spec:
77
- single-file
88
- /resources/junit-passing-tests.xml
99
command:
10-
- testImage
10+
- /testImage
1111
image: sonobuoy/testimage:v0.1
1212
name: plugin
1313
resources: {}

test/integration/testImage/yaml/job-manual.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ spec:
1111
- /resources/manual-results-1.yaml
1212
- /resources/manual-results-2.yaml
1313
command:
14-
- testImage
14+
- /testImage
1515
image: sonobuoy/testimage:v0.1
1616
name: plugin
1717
resources: {}

test/integration/testImage/yaml/job-raw-singlefile.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ spec:
77
- single-file
88
- /resources/hello-world.txt
99
command:
10-
- testImage
10+
- /testImage
1111
image: sonobuoy/testimage:v0.1
1212
name: plugin
1313
resources: {}

test/integration/testImage/yaml/manual-with-arbitrary-details.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ spec:
99
- single-file
1010
- /resources/manual-results-with-arbitrary-details.yaml
1111
command:
12-
- testImage
12+
- /testImage
1313
image: sonobuoy/testimage:v0.1
1414
name: plugin
1515
resources: {}

0 commit comments

Comments
 (0)