Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support globs for image and helm bundles #183

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cmd/mindthegap/importcmd/imagebundle/image_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func NewCommand(out output.Output) *cobra.Command {
cleaner.AddCleanupFn(func() { _ = os.RemoveAll(tempDir) })
out.EndOperation(true)

imageBundleFiles, err = utils.FilesWithGlobs(imageBundleFiles)
if err != nil {
return err
}
cfg, _, err := utils.ExtractBundles(tempDir, out, imageBundleFiles...)
if err != nil {
return err
Expand Down Expand Up @@ -119,8 +123,8 @@ func NewCommand(out output.Output) *cobra.Command {
},
}

cmd.Flags().
StringSliceVar(&imageBundleFiles, "image-bundle", nil, "Tarball containing list of images to import")
cmd.Flags().StringSliceVar(&imageBundleFiles, "image-bundle", nil,
"Tarball containing list of images to import. Can also be a glob pattern.")
_ = cmd.MarkFlagRequired("image-bundle")
cmd.Flags().StringVar(&containerdNamespace, "containerd-namespace", "k8s.io",
"Containerd namespace to import images into")
Expand Down
8 changes: 6 additions & 2 deletions cmd/mindthegap/push/helmbundle/helm_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func NewCommand(out output.Output) *cobra.Command {
cleaner.AddCleanupFn(func() { _ = os.RemoveAll(tempDir) })
out.EndOperation(true)

helmBundleFiles, err = utils.FilesWithGlobs(helmBundleFiles)
if err != nil {
return err
}
_, cfg, err := utils.ExtractBundles(tempDir, out, helmBundleFiles...)
if err != nil {
return err
Expand Down Expand Up @@ -116,8 +120,8 @@ func NewCommand(out output.Output) *cobra.Command {
},
}

cmd.Flags().
StringSliceVar(&helmBundleFiles, "helm-bundle", nil, "Tarball containing list of Helm charts to push")
cmd.Flags().StringSliceVar(&helmBundleFiles, "helm-bundle", nil,
"Tarball containing list of Helm charts to push. Can also be a glob pattern.")
_ = cmd.MarkFlagRequired("helm-bundle")
cmd.Flags().StringVar(&destRepository, "to-repository", "", "Repository to push images to")
_ = cmd.MarkFlagRequired("to-repository")
Expand Down
8 changes: 6 additions & 2 deletions cmd/mindthegap/push/imagebundle/image_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func NewCommand(out output.Output) *cobra.Command {
cleaner.AddCleanupFn(func() { _ = os.RemoveAll(tempDir) })
out.EndOperation(true)

imageBundleFiles, err = utils.FilesWithGlobs(imageBundleFiles)
if err != nil {
return err
}
cfg, _, err := utils.ExtractBundles(tempDir, out, imageBundleFiles...)
if err != nil {
return err
Expand Down Expand Up @@ -117,8 +121,8 @@ func NewCommand(out output.Output) *cobra.Command {
},
}

cmd.Flags().
StringSliceVar(&imageBundleFiles, "image-bundle", nil, "Tarball containing list of images to push")
cmd.Flags().StringSliceVar(&imageBundleFiles, "image-bundle", nil,
"Tarball containing list of images to push. Can also be a glob pattern.")
_ = cmd.MarkFlagRequired("image-bundle")
cmd.Flags().StringVar(&destRegistry, "to-registry", "", "Registry to push images to")
_ = cmd.MarkFlagRequired("to-registry")
Expand Down
8 changes: 6 additions & 2 deletions cmd/mindthegap/serve/helmbundle/helm_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func NewCommand(out output.Output) *cobra.Command {

out.EndOperation(true)

helmBundleFiles, err = utils.FilesWithGlobs(helmBundleFiles)
if err != nil {
return err
}
_, cfg, err := utils.ExtractBundles(tempDir, out, helmBundleFiles...)
if err != nil {
return err
Expand Down Expand Up @@ -79,8 +83,8 @@ func NewCommand(out output.Output) *cobra.Command {
},
}

cmd.Flags().
StringSliceVar(&helmBundleFiles, "helm-bundle", nil, "Tarball of Helm charts to serve")
cmd.Flags().StringSliceVar(&helmBundleFiles, "helm-bundle", nil,
"Tarball of Helm charts to serve. Can also be a glob pattern.")
_ = cmd.MarkFlagRequired("helm-bundle")
cmd.Flags().StringVar(&listenAddress, "listen-address", "localhost", "Address to listen on")
cmd.Flags().
Expand Down
8 changes: 6 additions & 2 deletions cmd/mindthegap/serve/imagebundle/image_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func NewCommand(out output.Output) *cobra.Command {

out.EndOperation(true)

imageBundleFiles, err = utils.FilesWithGlobs(imageBundleFiles)
if err != nil {
return err
}
cfg, _, err := utils.ExtractBundles(tempDir, out, imageBundleFiles...)
if err != nil {
return err
Expand Down Expand Up @@ -79,8 +83,8 @@ func NewCommand(out output.Output) *cobra.Command {
},
}

cmd.Flags().
StringSliceVar(&imageBundleFiles, "images-bundle", nil, "Tarball of images to serve")
cmd.Flags().StringSliceVar(&imageBundleFiles, "images-bundle", nil,
"Tarball of images to serve. Can also be a glob pattern.")
_ = cmd.MarkFlagRequired("images-bundle")
cmd.Flags().StringVar(&listenAddress, "listen-address", "localhost", "Address to listen on")
cmd.Flags().
Expand Down
27 changes: 27 additions & 0 deletions cmd/mindthegap/utils/glob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2021 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package utils

import (
"fmt"
"path/filepath"
)

// FilesWithGlobs expects a list of files and/or globs, and returns a new list of files.
// Returns an error if in does not match any files on the disk.
func FilesWithGlobs(in []string) ([]string, error) {
var out []string
for _, file := range in {
matches, err := filepath.Glob(file)
if err != nil {
return nil, fmt.Errorf("error finding matching files for %q: %w", file, err)
}
if len(matches) == 0 {
return nil, fmt.Errorf("did find any matching files for %q", file)
}
out = append(out, matches...)
}

return out, nil
}
104 changes: 104 additions & 0 deletions cmd/mindthegap/utils/glob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2021 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package utils

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestFilesWithGlobs(t *testing.T) {
t.Parallel()

// filepath.Glob expects the files to exist
testFiles, createdFiles := tempFiles(t)
testGlobs, createdGlobFiles := tempGlobs(t)

//nolint:gocritic // want to a append to a new slice, its fine in tests
combinedTestFiles := append(testFiles, testGlobs...)
//nolint:gocritic // want to a append to a new slice, its fine in tests
combinedCreatedFiles := append(createdFiles, createdGlobFiles...)

tests := []struct {
name string
in []string
expectedOutput []string
wantErr error
}{
{
name: "error: file does not exist",
in: []string{"doesnotexist.tar"},
wantErr: fmt.Errorf("did find any matching files for \"doesnotexist.tar\""),
},
{
name: "all files",
in: testFiles,
expectedOutput: createdFiles,
},
{
name: "all globs",
in: testGlobs,
expectedOutput: createdGlobFiles,
},
{
name: "files and globs",
in: combinedTestFiles,
expectedOutput: combinedCreatedFiles,
},
}
for ti := range tests {
tt := tests[ti]
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
out, err := FilesWithGlobs(tt.in)
require.Equal(t, tt.wantErr, err)
require.Equal(t, tt.expectedOutput, out)
})
}
}

//nolint:gocritic // no need for named parameters
func tempGlobs(t *testing.T) ([]string, []string) {
t.Helper()
tempDir1 := t.TempDir()
tempDir2 := t.TempDir()
globs := []string{
fmt.Sprintf("%s/*.tar", tempDir1),
fmt.Sprintf("%s/*.tar", tempDir2),
}

filesToCreate := []string{
filepath.Join(tempDir1, "images1.tar"),
filepath.Join(tempDir1, "images2.tar"),
filepath.Join(tempDir2, "images1.tar"),
}
for _, createFile := range filesToCreate {
_, err := os.Create(createFile)
require.NoError(t, err)
}

return globs, filesToCreate
}

//nolint:gocritic // no need for named parameters
func tempFiles(t *testing.T) ([]string, []string) {
t.Helper()
tempDir1 := t.TempDir()
tempDir2 := t.TempDir()
filesToCreate := []string{
filepath.Join(tempDir1, "images1.tar"),
filepath.Join(tempDir1, "images2.tar"),
filepath.Join(tempDir2, "images1.tar"),
}
for _, createFile := range filesToCreate {
_, err := os.Create(createFile)
require.NoError(t, err)
}

return filesToCreate, filesToCreate
}