-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: package generation ALPHA (#2269)
## Description Introduce Zarf package generation from a source ## Related Issue Fixes #821 ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Other (security config, docs update, etc) ## Checklist before merging - [x] Test, docs, adr added or updated as needed - [x] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow) followed --------- Signed-off-by: razzle <[email protected]> Co-authored-by: corang <[email protected]> Co-authored-by: razzle <[email protected]>
- Loading branch information
1 parent
cc7b6fc
commit 9608731
Showing
9 changed files
with
259 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# zarf dev generate | ||
<!-- Auto-generated by hack/gen-cli-docs.sh --> | ||
|
||
[alpha] Creates a zarf.yaml automatically from a given remote (git) Helm chart | ||
|
||
``` | ||
zarf dev generate NAME [flags] | ||
``` | ||
|
||
## Examples | ||
|
||
``` | ||
zarf dev generate podinfo --url https://github.com/stefanprodan/podinfo.git --version 6.4.0 --gitPath charts/podinfo | ||
``` | ||
|
||
## Options | ||
|
||
``` | ||
--gitPath string Relative path to the chart in the git repository | ||
-h, --help help for generate | ||
--kube-version string Override the default helm template KubeVersion when performing a package chart template | ||
--output-directory string Output directory for the generated zarf.yaml | ||
--url string URL to the source git repository | ||
--version string The Version of the chart to use | ||
``` | ||
|
||
## Options inherited from parent commands | ||
|
||
``` | ||
-a, --architecture string Architecture for OCI images and Zarf packages | ||
--insecure Allow access to insecure registries and disable other recommended security enforcements such as package checksum and signature validation. This flag should only be used if you have a specific reason and accept the reduced security posture. | ||
-l, --log-level string Log level when running Zarf. Valid options are: warn, info, debug, trace (default "info") | ||
--no-color Disable colors in output | ||
--no-log-file Disable log file creation | ||
--no-progress Disable fancy UI progress bars, spinners, logos, etc | ||
--tmpdir string Specify the temporary directory to use for intermediate files | ||
--zarf-cache string Specify the location of the Zarf cache directory (default "~/.zarf-cache") | ||
``` | ||
|
||
## SEE ALSO | ||
|
||
* [zarf dev](zarf_dev.md) - Commands useful for developing packages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// 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" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/defenseunicorns/zarf/src/config" | ||
"github.com/defenseunicorns/zarf/src/internal/packager/validate" | ||
"github.com/defenseunicorns/zarf/src/pkg/layout" | ||
"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" | ||
goyaml "github.com/goccy/go-yaml" | ||
) | ||
|
||
// Generate generates a Zarf package definition. | ||
func (p *Packager) Generate() (err error) { | ||
generatedZarfYAMLPath := filepath.Join(p.cfg.GenerateOpts.Output, layout.ZarfYAML) | ||
spinner := message.NewProgressSpinner("Generating package for %q at %s", p.cfg.GenerateOpts.Name, generatedZarfYAMLPath) | ||
|
||
if !utils.InvalidPath(generatedZarfYAMLPath) { | ||
prefixed := filepath.Join(p.cfg.GenerateOpts.Output, fmt.Sprintf("%s-%s", p.cfg.GenerateOpts.Name, layout.ZarfYAML)) | ||
|
||
message.Warnf("%s already exists, writing to %s", generatedZarfYAMLPath, prefixed) | ||
|
||
generatedZarfYAMLPath = prefixed | ||
|
||
if !utils.InvalidPath(generatedZarfYAMLPath) { | ||
return fmt.Errorf("unable to generate package, %s already exists", generatedZarfYAMLPath) | ||
} | ||
} | ||
|
||
generatedComponent := types.ZarfComponent{ | ||
Name: p.cfg.GenerateOpts.Name, | ||
Required: true, | ||
Charts: []types.ZarfChart{ | ||
{ | ||
Name: p.cfg.GenerateOpts.Name, | ||
Version: p.cfg.GenerateOpts.Version, | ||
Namespace: p.cfg.GenerateOpts.Name, | ||
URL: p.cfg.GenerateOpts.URL, | ||
GitPath: p.cfg.GenerateOpts.GitPath, | ||
}, | ||
}, | ||
} | ||
|
||
p.cfg.Pkg = types.ZarfPackage{ | ||
Kind: types.ZarfPackageConfig, | ||
Metadata: types.ZarfMetadata{ | ||
Name: p.cfg.GenerateOpts.Name, | ||
Version: p.cfg.GenerateOpts.Version, | ||
Description: "auto-generated using `zarf dev generate`", | ||
}, | ||
Components: []types.ZarfComponent{ | ||
generatedComponent, | ||
}, | ||
} | ||
p.arch = config.GetArch() | ||
|
||
images, err := p.findImages() | ||
if err != nil { | ||
// purposefully not returning error here, as we can still generate the package without images | ||
message.Warnf("Unable to find images: %s", err.Error()) | ||
} | ||
|
||
for i := range p.cfg.Pkg.Components { | ||
name := p.cfg.Pkg.Components[i].Name | ||
p.cfg.Pkg.Components[i].Images = images[name] | ||
} | ||
|
||
if err := validate.Run(p.cfg.Pkg); err != nil { | ||
return err | ||
} | ||
|
||
if err := utils.CreateDirectory(p.cfg.GenerateOpts.Output, helpers.ReadExecuteAllWriteUser); err != nil { | ||
return err | ||
} | ||
|
||
b, err := goyaml.MarshalWithOptions(p.cfg.Pkg, goyaml.IndentSequence(true), goyaml.UseSingleQuote(false)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
schemaComment := fmt.Sprintf("# yaml-language-server: $schema=https://raw.githubusercontent.com/%s/%s/zarf.schema.json", config.GithubProject, config.CLIVersion) | ||
content := schemaComment + "\n" + string(b) | ||
|
||
// lets space things out a bit | ||
content = strings.Replace(content, "kind:\n", "\nkind:\n", 1) | ||
content = strings.Replace(content, "metadata:\n", "\nmetadata:\n", 1) | ||
content = strings.Replace(content, "components:\n", "\ncomponents:\n", 1) | ||
|
||
spinner.Successf("Generated package for %q at %s", p.cfg.GenerateOpts.Name, generatedZarfYAMLPath) | ||
|
||
return os.WriteFile(generatedZarfYAMLPath, []byte(content), helpers.ReadAllWriteUser) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
// Package test provides e2e tests for Zarf. | ||
package test | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/defenseunicorns/zarf/src/pkg/layout" | ||
"github.com/defenseunicorns/zarf/src/pkg/utils" | ||
"github.com/defenseunicorns/zarf/src/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestZarfDevGenerate(t *testing.T) { | ||
t.Log("E2E: Zarf Dev Generate") | ||
|
||
t.Run("Test generate podinfo", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
|
||
url := "https://github.com/stefanprodan/podinfo.git" | ||
version := "6.4.0" | ||
gitPath := "charts/podinfo" | ||
|
||
stdOut, stdErr, err := e2e.Zarf("dev", "generate", "podinfo", "--url", url, "--version", version, "--gitPath", gitPath, "--output-directory", tmpDir) | ||
require.NoError(t, err, stdOut, stdErr) | ||
|
||
zarfPackage := types.ZarfPackage{} | ||
packageLocation := filepath.Join(tmpDir, layout.ZarfYAML) | ||
err = utils.ReadYaml(packageLocation, &zarfPackage) | ||
require.NoError(t, err) | ||
require.Equal(t, zarfPackage.Components[0].Charts[0].URL, url) | ||
require.Equal(t, zarfPackage.Components[0].Charts[0].Version, version) | ||
require.Equal(t, zarfPackage.Components[0].Charts[0].GitPath, gitPath) | ||
require.NotEmpty(t, zarfPackage.Components[0].Images) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters