Skip to content

Commit ded8dc7

Browse files
authored
refactor!: remove bundle manifest (#485)
* refactor: remove bundle manifest Signed-off-by: Nikita Pivkin <[email protected]> * test: update trivy matrix Signed-off-by: Nikita Pivkin <[email protected]> --------- Signed-off-by: Nikita Pivkin <[email protected]>
1 parent 598b89b commit ded8dc7

File tree

4 files changed

+10
-81
lines changed

4 files changed

+10
-81
lines changed

cmd/bundle/main.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,15 @@ func main() {
1414
output := flag.String("out", "bundle.tar.gz", "Output archive file path")
1515
flag.Parse()
1616

17-
githubRef := os.Getenv("GITHUB_REF")
18-
if githubRef == "" {
19-
log.Println("GITHUB_REF environment variable is not provided")
20-
}
21-
2217
log.Printf("Building bundle from root %q to %q", *root, *output)
23-
if err := buildBundle(*root, *output, githubRef); err != nil {
18+
if err := buildBundle(*root, *output); err != nil {
2419
log.Fatalf("Error: %v", err)
2520
}
2621
log.Printf("Bundle %q successfully created", *output)
2722
}
2823

29-
func buildBundle(root, outFile, githubRef string) error {
30-
b := bundler.New(".", os.DirFS(root), bundler.WithGithubRef(githubRef))
24+
func buildBundle(root, outFile string) error {
25+
b := bundler.New(".", os.DirFS(root))
3126

3227
f, err := os.Create(outFile)
3328
if err != nil {

integration/check_examples_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
"github.com/aquasecurity/trivy-checks/pkg/rego/metadata"
3333
)
3434

35-
var trivyVersions = []string{"0.57.1", "0.58.1", "latest", "canary"}
35+
var trivyVersions = []string{"0.61.0", "latest", "canary"}
3636

3737
func TestScanCheckExamples(t *testing.T) {
3838
ctx := context.Background()

internal/bundler/bundler.go

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package bundler
22

33
import (
44
"archive/tar"
5-
"bytes"
65
"compress/gzip"
76
"fmt"
87
"io"
@@ -29,13 +28,11 @@ type Bundler struct {
2928
root string
3029
fsys fs.FS
3130
prefix string // Prefix path inside the archive, e.g. "bundle"
32-
githubRef string // GitHub ref string, e.g. "refs/tags/v1.2.3"
3331
filters []FileFilter
3432
plainTransforms []PlainTransform
3533

36-
jobs []copyJob
37-
files map[string]string // map of source file paths to their relative destination paths in the bundle
38-
manifest []byte // prepared manifest content with placeholders replaced
34+
jobs []copyJob
35+
files map[string]string // map of source file paths to their relative destination paths in the bundle
3936
}
4037

4138
type Option func(*Bundler)
@@ -55,14 +52,6 @@ func WithPlainTransforms(transforms ...PlainTransform) Option {
5552
}
5653
}
5754

58-
// WithGithubRef sets the GitHub ref string to be used for manifest substitution.
59-
// The ref should be in the format "refs/tags/v1.2.3".
60-
func WithGithubRef(ref string) Option {
61-
return func(b *Bundler) {
62-
b.githubRef = ref
63-
}
64-
}
65-
6655
// New creates a new Bundler instance rooted at at the given directory and using the provided fs.FS.
6756
func New(root string, fsys fs.FS, opts ...Option) *Bundler {
6857
b := &Bundler{
@@ -113,15 +102,13 @@ func defaultFilters() []FileFilter {
113102
}
114103
}
115104

116-
// Build prepares the list of files to archive, processes the manifest, and writes
105+
// Build prepares the list of files to archive and writes
117106
// the resulting archive as a compressed tar.gz to the provided io.Writer.
118107
func (b *Bundler) Build(w io.Writer) error {
119108
if err := b.prepareFiles(); err != nil {
120109
return err
121110
}
122-
if err := b.prepareManifest(); err != nil {
123-
return err
124-
}
111+
125112
return b.archive(w)
126113
}
127114

@@ -170,44 +157,14 @@ func (b *Bundler) prepareFiles() error {
170157
return nil
171158
}
172159

173-
// prepareManifest reads the manifest template file, replaces the placeholder
174-
// "[GITHUB_SHA]" with the GitHub release version extracted from b.githubRef
175-
func (b *Bundler) prepareManifest() error {
176-
const (
177-
placeholder = "[GITHUB_SHA]"
178-
prefix = "refs/tags/v"
179-
)
180-
181-
data, err := fs.ReadFile(b.fsys, filepath.Join(b.root, "checks", ".manifest"))
182-
if err != nil {
183-
return fmt.Errorf("read .manifest: %w", err)
184-
}
185-
186-
if b.githubRef != "" {
187-
releaseVersion, ok := strings.CutPrefix(b.githubRef, prefix)
188-
if !ok {
189-
log.Printf("GitHub ref %q does not start with %q — using unchanged value", b.githubRef, prefix)
190-
}
191-
log.Printf("Using GitHub ref %q -> release version %q", b.githubRef, releaseVersion)
192-
data = bytes.ReplaceAll(data, []byte(placeholder), []byte(releaseVersion))
193-
}
194-
195-
b.manifest = data
196-
return nil
197-
}
198-
199-
// archive writes all prepared files and the manifest into a compressed tar.gz archive
160+
// archive writes all prepared files into a compressed tar.gz archive
200161
func (b *Bundler) archive(w io.Writer) error {
201162
gw := gzip.NewWriter(w)
202163
defer gw.Close()
203164

204165
tw := tar.NewWriter(gw)
205166
defer tw.Close()
206167

207-
if err := b.writeManifest(tw); err != nil {
208-
return fmt.Errorf("write manifest: %w", err)
209-
}
210-
211168
var added int
212169
for src, dst := range b.files {
213170
err := b.addFileToTar(tw, src, b.prefix+dst)
@@ -221,21 +178,6 @@ func (b *Bundler) archive(w io.Writer) error {
221178
return nil
222179
}
223180

224-
func (b *Bundler) writeManifest(tw *tar.Writer) error {
225-
hdr := &tar.Header{
226-
Name: b.prefix + ".manifest",
227-
Mode: 0o644,
228-
Size: int64(len(b.manifest)),
229-
}
230-
if err := tw.WriteHeader(hdr); err != nil {
231-
return fmt.Errorf("write manifest header: %w", err)
232-
}
233-
if _, err := tw.Write(b.manifest); err != nil {
234-
return fmt.Errorf("write manifest content: %w", err)
235-
}
236-
return nil
237-
}
238-
239181
func (b *Bundler) addFileToTar(tw *tar.Writer, src, dst string) error {
240182
fi, err := fs.Stat(b.fsys, src)
241183
if err != nil {

internal/bundler/bundler_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
func TestBundlerBuild(t *testing.T) {
1717
fsys := fstest.MapFS{
18-
"checks/.manifest": &fstest.MapFile{Data: []byte("version: [GITHUB_SHA]")},
1918
"checks/kubernetes/policy.rego": &fstest.MapFile{Data: []byte("package kubernetes")},
2019
"checks/kubernetes/policy.yaml": &fstest.MapFile{Data: []byte("should be filtered out")},
2120
"checks/kubernetes/policy_test.rego": &fstest.MapFile{Data: []byte("should be filtered out")},
@@ -32,7 +31,7 @@ func TestBundlerBuild(t *testing.T) {
3231
"pkg/compliance/README.md": &fstest.MapFile{Data: []byte("should be filtered out")},
3332
}
3433

35-
b := bundler.New(".", fsys, bundler.WithGithubRef("refs/tags/v1.2.3"))
34+
b := bundler.New(".", fsys)
3635

3736
var buf bytes.Buffer
3837
require.NoError(t, b.Build(&buf))
@@ -52,12 +51,6 @@ func TestBundlerBuild(t *testing.T) {
5251
}
5352
require.NoError(t, err)
5453
foundFiles[hdr.Name] = true
55-
56-
if hdr.Name == ".manifest" {
57-
data, err := io.ReadAll(tr)
58-
require.NoError(t, err)
59-
assert.Contains(t, string(data), "version: 1.2.3")
60-
}
6154
}
6255

6356
t.Log(foundFiles)
@@ -73,7 +66,6 @@ func TestBundlerBuild(t *testing.T) {
7366
"commands/config/test.yaml",
7467
"commands/kubernetes/test.yaml",
7568
"specs/compliance/test.yaml",
76-
".manifest",
7769
}
7870

7971
assert.Len(t, foundFiles, len(expectedFiles))

0 commit comments

Comments
 (0)