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

zarf package mirror command introduction #1913

Merged
merged 20 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (

ZarfDeployStage = "Deploy"
ZarfCreateStage = "Create"
ZarfUnloadStage = "Unload"
Noxsios marked this conversation as resolved.
Show resolved Hide resolved
)

// Zarf Global Configuration Variables.
Expand Down
94 changes: 94 additions & 0 deletions src/pkg/packager/unload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package packager contains functions for interacting with, managing and deploying Zarf packages.
package packager

import (
"fmt"
"strings"

"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
)

// Unload pulls resources from a package (images, git repositories, etc) and pushes them to remotes in the air gap without deploying them
func (p *Packager) Unload(packageName string) (err error) {
spinner := message.NewProgressSpinner("Unloading zarf package %s", packageName)
Racer159 marked this conversation as resolved.
Show resolved Hide resolved
defer spinner.Stop()

if utils.IsOCIURL(p.cfg.DeployOpts.PackagePath) {
err := p.SetOCIRemote(p.cfg.DeployOpts.PackagePath)
if err != nil {
return err
}
}

if err := p.loadZarfPkg(); err != nil {
return fmt.Errorf("unable to load the Zarf Package: %w", err)
}

if err := p.validatePackageSignature(p.cfg.DeployOpts.PublicKeyPath); err != nil {
return err
}

// Confirm the overall package unload
if !p.confirmAction(config.ZarfUnloadStage, p.cfg.SBOMViewFiles) {
return fmt.Errorf("unload cancelled")
}

state := types.ZarfState{
RegistryInfo: p.cfg.InitOpts.RegistryInfo,
GitServer: p.cfg.InitOpts.GitServer,
ArtifactServer: p.cfg.InitOpts.ArtifactServer,
}
p.cfg.State = state

// Filter out components that are not compatible with this system if we have loaded from a tarball
p.filterComponents(true)
requestedComponentNames := getRequestedComponentList(p.cfg.DeployOpts.Components)

for _, component := range p.cfg.Pkg.Components {
if len(requestedComponentNames) == 0 || helpers.SliceContains(requestedComponentNames, component.Name) {
if err := p.unloadComponent(component, false); err != nil {
return err
}
}
}

return nil
}

// unloadComponent unloads a Zarf Component.
func (p *Packager) unloadComponent(component types.ZarfComponent, noImgChecksum bool) error {
message.Debugf("packager.deployComponent(%#v, %#v", p.tmp, component)

// Toggles for general deploy operations
componentPath, err := p.createOrGetComponentPaths(component)
if err != nil {
return fmt.Errorf("unable to create the component paths: %w", err)
}

// All components now require a name
message.HeaderInfof("📦 %s COMPONENT", strings.ToUpper(component.Name))

hasImages := len(component.Images) > 0
hasRepos := len(component.Repos) > 0

if hasImages {
if err := p.pushImagesToRegistry(component.Images, noImgChecksum); err != nil {
return fmt.Errorf("unable to push images to the registry: %w", err)
}
}

if hasRepos {
if err = p.pushReposToRepository(componentPath.Repos, component.Repos); err != nil {
return fmt.Errorf("unable to push the repos to the repository: %w", err)
}
}

return nil
}