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: add ability to uds create to local output path #547

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion src/pkg/bundle/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/defenseunicorns/uds-cli/src/config"
"github.com/defenseunicorns/uds-cli/src/pkg/bundler/fetcher"
"github.com/defenseunicorns/uds-cli/src/pkg/utils"
"github.com/defenseunicorns/uds-cli/src/types"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/message"
Expand Down Expand Up @@ -154,7 +155,7 @@ func (b *Bundle) ValidateBundleResources(bundle *types.UDSBundle, spinner *messa
}
} else {
// atm we don't support outputting a bundle with local pkgs outputting to OCI
if b.cfg.CreateOpts.Output != "" {
if utils.IsRegistryURL(b.cfg.CreateOpts.Output) {
return fmt.Errorf("detected local Zarf package: %s, outputting to an OCI registry is not supported when using local Zarf packages", pkg.Name)
}
var fullPkgName string
Expand Down
11 changes: 6 additions & 5 deletions src/pkg/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package bundler

import (
"github.com/defenseunicorns/uds-cli/src/pkg/utils"
"github.com/defenseunicorns/uds-cli/src/types"
)

Expand Down Expand Up @@ -41,15 +42,15 @@ func NewBundler(opts *Options) *Bundler {

// Create creates a bundle
func (b *Bundler) Create() error {
if b.output == "" {
localBundle := NewLocalBundle(&LocalBundleOpts{Bundle: b.bundle, TmpDstDir: b.tmpDstDir, SourceDir: b.sourceDir})
err := localBundle.create(nil)
if utils.IsRegistryURL(b.output) {
remoteBundle := NewRemoteBundle(&RemoteBundleOpts{Bundle: b.bundle, Output: b.output})
err := remoteBundle.create(nil)
if err != nil {
return err
}
} else {
remoteBundle := NewRemoteBundle(&RemoteBundleOpts{Bundle: b.bundle, Output: b.output})
err := remoteBundle.create(nil)
localBundle := NewLocalBundle(&LocalBundleOpts{Bundle: b.bundle, TmpDstDir: b.tmpDstDir, SourceDir: b.sourceDir, OutputDir: b.output})
err := localBundle.create(nil)
if err != nil {
return err
}
Expand Down
17 changes: 14 additions & 3 deletions src/pkg/bundler/localbundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/defenseunicorns/uds-cli/src/types"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/oci"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/pkg/zoci"
goyaml "github.com/goccy/go-yaml"
"github.com/mholt/archiver/v4"
Expand All @@ -32,13 +33,15 @@ type LocalBundleOpts struct {
Bundle *types.UDSBundle
TmpDstDir string
SourceDir string
OutputDir string
}

// LocalBundle enables create ops with local bundles
type LocalBundle struct {
bundle *types.UDSBundle
tmpDstDir string
sourceDir string
outputDir string
}

// NewLocalBundle creates a new local bundle
Expand All @@ -47,6 +50,7 @@ func NewLocalBundle(opts *LocalBundleOpts) *LocalBundle {
bundle: opts.Bundle,
tmpDstDir: opts.TmpDstDir,
sourceDir: opts.SourceDir,
outputDir: opts.OutputDir,
}
}

Expand Down Expand Up @@ -159,8 +163,11 @@ func (lo *LocalBundle) create(signature []byte) error {
return err
}

if lo.outputDir == "" {
lo.outputDir = lo.sourceDir
}
// tarball the bundle
err = writeTarball(bundle, artifactPathMap, lo.sourceDir)
err = writeTarball(bundle, artifactPathMap, lo.outputDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -207,14 +214,18 @@ func pushManifestConfig(store *ocistore.Store, metadata types.UDSMetadata, build
}

// writeTarball builds and writes a bundle tarball to disk based on a file map
func writeTarball(bundle *types.UDSBundle, artifactPathMap types.PathMap, sourceDir string) error {
func writeTarball(bundle *types.UDSBundle, artifactPathMap types.PathMap, outputDir string) error {
format := archiver.CompressedArchive{
Compression: archiver.Zstd{},
Archival: archiver.Tar{},
}
filename := fmt.Sprintf("%s%s-%s-%s.tar.zst", config.BundlePrefix, bundle.Metadata.Name, bundle.Metadata.Architecture, bundle.Metadata.Version)

dst := filepath.Join(sourceDir, filename)
if !helpers.IsDir(outputDir) {
os.MkdirAll(outputDir, 0755)
}

dst := filepath.Join(outputDir, filename)

_ = os.RemoveAll(dst)

Expand Down
46 changes: 46 additions & 0 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/defenseunicorns/uds-cli/src/config"
Expand Down Expand Up @@ -138,3 +139,48 @@
func IsRemotePkg(pkg types.Package) bool {
return pkg.Repository != ""
}

func hasScheme(s string) bool {
return strings.Contains(s, "://")
}

func hasDomain(s string) bool {
return strings.Contains(s, ".") && len(s) > 1
TristanHoladay marked this conversation as resolved.
Show resolved Hide resolved
}

func hasPort(s string) bool {
// look for colon and port (e.g localhost:31999)
colonIndex := strings.Index(s, ":")
firstSlashIndex := strings.Index(s, "/")
endIndex := firstSlashIndex
if firstSlashIndex == -1 {
endIndex = len(s) - 1
}
if colonIndex != -1 {
port := s[colonIndex+1 : endIndex]

// port valid number ?
_, err := strconv.Atoi(port)
if err == nil {
return true
}
}
return false
}

// Checks if string is an oci url

Check warning on line 171 in src/pkg/utils/utils.go

View workflow job for this annotation

GitHub Actions / validate

comment on exported function IsRegistryURL should be of the form "IsRegistryURL ..."
TristanHoladay marked this conversation as resolved.
Show resolved Hide resolved
func IsRegistryURL(s string) bool {
if hasScheme(s) {
return true
}

if hasDomain(s) {
return true
}

if hasPort(s) {
return true
}

return false
}
16 changes: 16 additions & 0 deletions src/test/e2e/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,22 @@ func TestBundleWithYmlFile(t *testing.T) {
remove(t, bundlePath)
}

func TestLocalBundleWithOutput(t *testing.T) {
path := "src/test/packages/nginx"
args := strings.Split(fmt.Sprintf("zarf package create %s -o %s --confirm", path, path), " ")
_, _, err := e2e.UDS(args...)
require.NoError(t, err)

bundleDir := "src/test/bundles/09-uds-bundle-yml"
destDir := "src/test/test/"
bundlePath := filepath.Join(destDir, fmt.Sprintf("uds-bundle-yml-example-%s-0.0.1.tar.zst", e2e.Arch))
createLocalWithOuputFlag(t, bundleDir, destDir, e2e.Arch)

cmd := strings.Split(fmt.Sprintf("inspect %s", bundlePath), " ")
_, _, err = e2e.UDS(cmd...)
require.NoError(t, err)
}

func TestLocalBundleWithNoSBOM(t *testing.T) {
path := "src/test/packages/nginx"
args := strings.Split(fmt.Sprintf("zarf package create %s -o %s --skip-sbom --confirm", path, path), " ")
Expand Down
6 changes: 6 additions & 0 deletions src/test/e2e/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func createLocalError(bundlePath string, arch string) (stderr string) {
return stderr
}

func createLocalWithOuputFlag(t *testing.T, bundlePath string, destPath string, arch string) {
cmd := strings.Split(fmt.Sprintf("create %s -o %s --insecure --confirm -a %s", bundlePath, destPath, arch), " ")
_, _, err := e2e.UDS(cmd...)
require.NoError(t, err)
}

func createRemoteInsecure(t *testing.T, bundlePath, registry, arch string) {
cmd := strings.Split(fmt.Sprintf("create %s -o %s --confirm --insecure -a %s", bundlePath, registry, arch), " ")
_, _, err := e2e.UDS(cmd...)
Expand Down
Loading