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

chore: small refactors and docs updates #671

Merged
merged 2 commits into from
Jun 7, 2024
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ Inspect the `uds-bundle.yaml` of a bundle
1. From your local filesystem: `uds inspect uds-bundle-<name>.tar.zst`

#### Viewing Images in a Bundle
It is possible derive images from a `uds-bundle.yaml`. This can be useful for situations where you need to know what images will be bundled before you actually create the bundle. This is accomplished with the `--list-images`. For example:
It is possible to derive images from a `uds-bundle.yaml`. This can be useful for situations where you need to know what images will be bundled before you actually create the bundle. This is accomplished with the `--list-images` flag. For example:

`uds inspect ./uds-bundle.yaml --list-images`

This command will return a list of images derived from the bundle's packages and taking into account optional and required package components.
This command will return a list of images derived from the bundle's packages, taking into account optional and required package components.

#### Viewing SBOMs
There are 2 additional flags for the `uds inspect` command you can use to extract and view SBOMs:
Expand Down Expand Up @@ -163,7 +163,7 @@ When deploying a local bundle, the bundle's architecture will be used for compar
> [!NOTE]
> It is possible to override the bundle architecture used at validation time by using the `--architecture` / `-a` flag.

If, for example, you have a multi-arch remote bundle that you want to deploy from an arm64 machine to an amd64 cluster, the validation with fail because the system arch does not match the cluster arch. However, you can pull the correct bundle version by specificying the arch with the command line architecture flag.
If, for example, you have a multi-arch remote bundle that you want to deploy from an arm64 machine to an amd64 cluster, the validation with fail because the system arch does not match the cluster arch. However, you can pull the correct bundle version by specifying the arch with the command line architecture flag.

e.g.
`uds deploy -a amd64 <remote-multi-arch-bundle.tar.zst> --confirm`
Expand Down
110 changes: 63 additions & 47 deletions src/pkg/bundle/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/defenseunicorns/pkg/oci"
"github.com/defenseunicorns/uds-cli/src/config"
"github.com/defenseunicorns/uds-cli/src/pkg/utils"
"github.com/defenseunicorns/uds-cli/src/types"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/packager/filters"
zarfSources "github.com/defenseunicorns/zarf/src/pkg/packager/sources"
Expand All @@ -28,23 +29,10 @@ import (
func (b *Bundle) Inspect() error {
// handle --list-images flag
if b.cfg.InspectOpts.ListImages {
err := utils.CheckYAMLSourcePath(b.cfg.InspectOpts.Source)
err := b.listImages()
if err != nil {
return err
}

if err := utils.ReadYAMLStrict(b.cfg.InspectOpts.Source, &b.bundle); err != nil {
return err
}

// find images in the packages taking into account optional components
imgs, err := b.getPackageImages()
if err != nil {
return err
}

formattedImgs := pterm.Color(color.FgHiMagenta).Sprintf(strings.Join(imgs, "\n"))
pterm.Printfln("\n%s\n", formattedImgs)
return nil
}

Expand Down Expand Up @@ -90,45 +78,35 @@ func (b *Bundle) Inspect() error {
return nil
}

func (b *Bundle) listImages() error {
if err := utils.CheckYAMLSourcePath(b.cfg.InspectOpts.Source); err != nil {
return err
}

if err := utils.ReadYAMLStrict(b.cfg.InspectOpts.Source, &b.bundle); err != nil {
return err
}

// find images in the packages taking into account optional components
imgs, err := b.getPackageImages()
if err != nil {
return err
}

formattedImgs := pterm.Color(color.FgHiMagenta).Sprintf(strings.Join(imgs, "\n"))
pterm.Printfln("\n%s\n", formattedImgs)
return nil
}

func (b *Bundle) getPackageImages() ([]string, error) {
// use a map to track the images for easy de-duping
imgMap := make(map[string]string)

for _, pkg := range b.bundle.Packages {
// get package source
var source zarfSources.PackageSource
if pkg.Repository != "" {
// handle remote packages
url := fmt.Sprintf("oci://%s:%s", pkg.Repository, pkg.Ref)
platform := ocispec.Platform{
Architecture: config.GetArch(),
OS: oci.MultiOS,
}
remote, err := zoci.NewRemote(url, platform)
if err != nil {
return nil, err
}

source = &zarfSources.OCISource{
ZarfPackageOptions: &zarfTypes.ZarfPackageOptions{},
Remote: remote,
}
} else if pkg.Path != "" {
// handle local packages
err := os.Chdir(filepath.Dir(b.cfg.InspectOpts.Source)) // change to the bundle's directory
if err != nil {
return nil, err
}

bundleArch := config.GetArch(b.bundle.Metadata.Architecture)
tarballName := fmt.Sprintf("zarf-package-%s-%s-%s.tar.zst", pkg.Name, bundleArch, pkg.Ref)
source = &zarfSources.TarballSource{
ZarfPackageOptions: &zarfTypes.ZarfPackageOptions{
PackageSource: filepath.Join(pkg.Path, tarballName),
},
}
} else {
return nil, fmt.Errorf("package %s is missing a repository or path", pkg.Name)
source, err := b.getSource(pkg)
if err != nil {
return nil, err
}

tmpDir, err := zarfUtils.MakeTempDir(config.CommonOptions.TempDirectory)
Expand Down Expand Up @@ -168,3 +146,41 @@ func (b *Bundle) getPackageImages() ([]string, error) {

return images, nil
}

func (b *Bundle) getSource(pkg types.Package) (zarfSources.PackageSource, error) {
var source zarfSources.PackageSource
if pkg.Repository != "" {
// handle remote packages
url := fmt.Sprintf("oci://%s:%s", pkg.Repository, pkg.Ref)
platform := ocispec.Platform{
Architecture: config.GetArch(),
OS: oci.MultiOS,
}
remote, err := zoci.NewRemote(url, platform)
if err != nil {
return nil, err
}

source = &zarfSources.OCISource{
ZarfPackageOptions: &zarfTypes.ZarfPackageOptions{},
Remote: remote,
}
} else if pkg.Path != "" {
// handle local packages
err := os.Chdir(filepath.Dir(b.cfg.InspectOpts.Source)) // change to the bundle's directory
if err != nil {
return nil, err
}

bundleArch := config.GetArch(b.bundle.Metadata.Architecture)
tarballName := fmt.Sprintf("zarf-package-%s-%s-%s.tar.zst", pkg.Name, bundleArch, pkg.Ref)
source = &zarfSources.TarballSource{
ZarfPackageOptions: &zarfTypes.ZarfPackageOptions{
PackageSource: filepath.Join(pkg.Path, tarballName),
},
}
} else {
return nil, fmt.Errorf("package %s is missing a repository or path", pkg.Name)
}
return source, nil
}
4 changes: 2 additions & 2 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ func CheckYAMLSourcePath(source string) error {
return fmt.Errorf("source must have .yaml or yml file extension")
}
// Check if the file exists
if _, err := os.Stat(source); os.IsNotExist(err) {
return fmt.Errorf("file %s does not exist", source)
if isInvalid := helpers.InvalidPath(source); isInvalid {
return fmt.Errorf("file %s does not exist or has incorrect permissions", source)
}

return nil
Expand Down
1 change: 0 additions & 1 deletion src/types/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type UDSBundle struct {
type Package struct {
Name string `json:"name" jsonschema:"name=Name of the Zarf package"`
Description string `json:"description,omitempty" jsonschema:"description=Description of the Zarf package"`
Images []string `json:"images,omitempty" jsonschema:"description=List of images included in the Zarf package"`
Repository string `json:"repository,omitempty" jsonschema:"description=The repository to import the package from"`
Path string `json:"path,omitempty" jsonschema:"description=The local path to import the package from"`
Ref string `json:"ref" jsonschema:"description=Ref (tag) of the Zarf package"`
Expand Down
7 changes: 0 additions & 7 deletions uds.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,6 @@
"type": "string",
"description": "Description of the Zarf package"
},
"images": {
"items": {
"type": "string"
},
"type": "array",
"description": "List of images included in the Zarf package"
},
"repository": {
"type": "string",
"description": "The repository to import the package from"
Expand Down
Loading