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

Adding crane ls command #1835

Merged
merged 10 commits into from
Jun 26, 2023
42 changes: 42 additions & 0 deletions docs/2-the-zarf-cli/100-cli-commands/zarf_tools_registry_ls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# zarf tools registry ls
<!-- Auto-generated by docs/gen-cli-docs.sh -->

List the tags in a repo

```
zarf tools registry ls REPO [flags]
```

## Examples

```

# list the tags for a repo internal to Zarf
$ zarf tools registry ls 127.0.0.1:31999/stefanprodan/podinfo

# list the tags for a repo hosted at reg.example.com
$ zarf tools registry ls reg.example.com/stefanprodan/podinfo

```

## Options

```
--full-ref (Optional) if true, print the full image reference
-h, --help help for ls
--omit-digest-tags (Optional), if true, omit digest tags (e.g., ':sha256-...')
```

## Options inherited from parent commands

```
--allow-nondistributable-artifacts Allow pushing non-distributable (foreign) layers
--insecure Allow image references to be fetched without TLS
--platform string Specifies the platform in the form os/arch[/variant][:osversion] (e.g. linux/amd64). (default "all")
-v, --verbose Enable debug logs
```

## SEE ALSO

* [zarf tools registry](zarf_tools_registry.md) - Tools for working with container registries using go-containertools

63 changes: 61 additions & 2 deletions src/cmd/tools/crane.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
package tools

import (
"os"

"fmt"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/internal/cluster"
Expand All @@ -16,6 +15,8 @@ import (
"github.com/google/go-containerregistry/pkg/logs"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/spf13/cobra"
"os"
"strings"
)

func init() {
Expand Down Expand Up @@ -69,6 +70,7 @@ func init() {

registryCmd.AddCommand(craneCopy)
registryCmd.AddCommand(zarfCraneCatalog(&craneOptions))
registryCmd.AddCommand(zarfCraneList(&craneOptions))

registryCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, lang.CmdToolsRegistryFlagVerbose)
registryCmd.PersistentFlags().BoolVar(&insecure, "insecure", false, lang.CmdToolsRegistryFlagInsecure)
Expand Down Expand Up @@ -118,3 +120,60 @@ func zarfCraneCatalog(cranePlatformOptions *[]crane.Option) *cobra.Command {

return craneCatalog
}

// Wrap the original crane list with a zarf specific version
func zarfCraneList(cranePlatformOptions *[]crane.Option) *cobra.Command {
craneList := craneCmd.NewCmdList(cranePlatformOptions)

craneList.Example = lang.CmdToolsRegistryListExample
craneList.Args = nil

originalListFn := craneList.RunE

craneList.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
message.Fatal(nil, lang.CmdToolsCraneListNoRepoSpecified)
}

// Try to connect to a Zarf initialized cluster otherwise then pass it down to crane.
zarfCluster, err := cluster.NewCluster()
if err != nil {
return originalListFn(cmd, args)
}

// Load the state
zarfState, err := zarfCluster.LoadZarfState()
if err != nil {
return err
}

// Check to see if it matches the existing internal address.
if !strings.HasPrefix(args[0], zarfState.RegistryInfo.Address) {
return originalListFn(cmd, args)
}

if zarfState.RegistryInfo.InternalRegistry {
// Open a tunnel to the Zarf registry
tunnelReg, err := cluster.NewZarfTunnel()
if err != nil {
return err
}
err = tunnelReg.Connect(cluster.ZarfRegistry, false)
if err != nil {
return err
}

givenAddress := fmt.Sprintf("%s/", zarfState.RegistryInfo.Address)
tunnelAddress := fmt.Sprintf("%s/", tunnelReg.Endpoint())
args[0] = strings.Replace(args[0], givenAddress, tunnelAddress, 1)
}

// Add the correct authentication to the crane command options
authOption := config.GetCraneAuthOption(zarfState.RegistryInfo.PullUsername, zarfState.RegistryInfo.PullPassword)
*cranePlatformOptions = append(*cranePlatformOptions, authOption)

return originalListFn(cmd, args)
}

return craneList
}
12 changes: 11 additions & 1 deletion src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,18 @@ const (
CmdToolsRegistryCatalogExample = `
# list the repos internal to Zarf
$ zarf tools registry catalog

# list the repos for reg.example.com
$ zarf tools registry catalog reg.example.com
`
CmdToolsRegistryListExample = `
# list the tags for a repo internal to Zarf
$ zarf tools registry ls 127.0.0.1:31999/stefanprodan/podinfo

# list the tags for a repo hosted at reg.example.com
$ zarf tools registry ls reg.example.com/stefanprodan/podinfo
`

CmdToolsRegistryInvalidPlatformErr = "Invalid platform '%s': %s"
CmdToolsRegistryFlagVerbose = "Enable debug logs"
CmdToolsRegistryFlagInsecure = "Allow image references to be fetched without TLS"
Expand All @@ -351,6 +359,8 @@ const (
CmdToolsClearCacheSuccess = "Successfully cleared the cache from %s"
CmdToolsClearCacheFlagCachePath = "Specify the location of the Zarf artifact cache (images and git repositories)"

CmdToolsCraneListNoRepoSpecified = "You must specify a repository name to return a list of tags for."

CmdToolsDownloadInitShort = "Downloads the init package for the current Zarf version into the specified directory"
CmdToolsDownloadInitFlagOutputDirectory = "Specify a directory to place the init package in."
CmdToolsDownloadInitErr = "Unable to download the init package: %s"
Expand Down