-
Notifications
You must be signed in to change notification settings - Fork 236
feat: let zarf packages define requirements #4606
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
Open
Shasties
wants to merge
6
commits into
zarf-dev:main
Choose a base branch
from
Shasties:Shasties/issue4605
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a31fa38
feat: Let zarf packages define requirements
Shasties c18fb20
examples are expected to be zarf package create-able
Shasties 716e8a3
turn REQUIREMENTS into requirements
Shasties 70fd387
Fix package for unit test
Shasties e6044d9
follow convention for skipRequirementsCheck
Shasties 2b32787
import apiextensions directly now
Shasties File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,4 @@ | ||
| apiVersion: v1 | ||
| kind: Namespace | ||
| metadata: | ||
| name: requirements-test |
This file contains hidden or 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,49 @@ | ||
| # YAML file that specifies prereqs for package to have a clean deploy | ||
| # This is a very minimal & functional example | ||
| # agent - specify utils & vars that must exist on the agent doing the zarf package deploy | ||
| agent: | ||
| tools: | ||
| - name: kubectl | ||
| version: ">=1.20.0" | ||
| reason: Required to apply manifests | ||
| # cluster - specify crds, k8s resources, or other zarf packages that must exist on the cluster being deployed into | ||
| cluster: | ||
| resources: | ||
| - apiVersion: v1 | ||
| kind: Namespace | ||
| name: kube-system | ||
|
|
||
| # While this is more elaborate | ||
| #agent: | ||
| # tools: | ||
| # - name: yq | ||
| # version: ">= 4.40.5" | ||
| # - name: custom_binary | ||
| # version: ">= 3.14.0" | ||
| # versionCommand: /path/to/custom_binary --flag-for-version | ||
| # reason: "Used in our create onBefore scripts for X" | ||
| # | ||
| # env: | ||
| # - name: HTTPS_PROXY | ||
| # required: false | ||
| # | ||
| # Things that must be present on the cluster being deployed into | ||
| # Can specify crds, specific resources, or other zarf packages | ||
| #cluster: | ||
| # crds: | ||
| # - name: gateways.gateway.networking.k8s.io | ||
| # version: ">= 1.0.0" | ||
| # - name: certificates.cert-manager.io | ||
| # resources: | ||
| # - apiVersion: v1 | ||
| # kind: Namespace | ||
| # name: cert-manager | ||
| # - apiVersion: apps/v1 | ||
| # kind: Deployment | ||
| # namespace: zarf | ||
| # name: zarf-injector | ||
| # packages: | ||
| # - name: iffy-package | ||
| # version: ">=1.4.0, !=1.6.2, !=1.6.3" | ||
| # reason: "our package is incompatible with iffy-package 1.6.2–1.6.3" | ||
| # | ||
This file contains hidden or 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,16 @@ | ||
| kind: ZarfPackageConfig | ||
| metadata: | ||
| name: requirements-test | ||
| description: Simple package to test requirements handling | ||
| version: 0.0.1 | ||
| architecture: amd64 | ||
|
|
||
| components: | ||
| - name: create-namespace | ||
| description: Creates a test namespace | ||
| required: true | ||
| manifests: | ||
| - name: namespace | ||
| namespace: "" | ||
| files: | ||
| - manifests/namespace.yaml |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,201 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
|
||
| package requirements | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/Masterminds/semver/v3" | ||
| ) | ||
|
|
||
| type requirementsValidationError struct { | ||
| Failures []string | ||
| } | ||
|
|
||
| func (e *requirementsValidationError) Error() string { | ||
| return "REQUIREMENTS validation failed:\n - " + strings.Join(e.Failures, "\n - ") | ||
| } | ||
|
|
||
| func validateAgentRequirements(ctx context.Context, req agentRequirements) error { | ||
| var failures []string | ||
|
|
||
| // env checks | ||
| for _, e := range req.Env { | ||
| if !e.Required { | ||
| continue | ||
| } | ||
| if _, ok := os.LookupEnv(e.Name); !ok { | ||
| msg := fmt.Sprintf("agent env var %q is required but not set", e.Name) | ||
| if e.Reason != "" { | ||
| msg += fmt.Sprintf(" (reason: %s)", e.Reason) | ||
| } | ||
| failures = append(failures, msg) | ||
| } | ||
| } | ||
|
|
||
| // tool checks | ||
| for _, t := range req.Tools { | ||
| if err := validateTool(ctx, t); err != nil { | ||
| if t.Optional { | ||
| continue | ||
| } | ||
| failures = append(failures, err.Error()) | ||
| } | ||
| } | ||
|
|
||
| if len(failures) > 0 { | ||
| return &requirementsValidationError{Failures: failures} | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func validateTool(ctx context.Context, t toolRequirement) error { | ||
| if strings.TrimSpace(t.Name) == "" { | ||
| return fmt.Errorf("agent tool requirement has empty name") | ||
| } | ||
|
|
||
| path, err := exec.LookPath(t.Name) | ||
| if err != nil { | ||
| msg := fmt.Sprintf("agent tool %q is missing from PATH", t.Name) | ||
| if t.Reason != "" { | ||
| msg += fmt.Sprintf(" (reason: %s)", t.Reason) | ||
| } | ||
| return fmt.Errorf("%s", msg) | ||
| } | ||
|
|
||
| // If no version constraint provided, presence is enough. | ||
| if strings.TrimSpace(t.Version) == "" { | ||
| return nil | ||
| } | ||
|
|
||
| constraint, err := semver.NewConstraint(t.Version) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid semver constraint for tool %q: %q: %w", t.Name, t.Version, err) | ||
| } | ||
|
|
||
| cmdline := t.VersionCommand | ||
| if strings.TrimSpace(cmdline) == "" { | ||
| // common defaults | ||
| cmdline = t.Name + " --version" | ||
| } | ||
|
|
||
| out, err := runShellish(ctx, cmdline) | ||
| if err != nil { | ||
| return fmt.Errorf("failed running version check for tool %q (%s): %w", t.Name, cmdline, err) | ||
| } | ||
|
|
||
| ver, err := extractSemver(out, t.VersionRegex) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to parse version for tool %q from output %q: %w", t.Name, strings.TrimSpace(out), err) | ||
| } | ||
|
|
||
| if !constraint.Check(ver) { | ||
| msg := fmt.Sprintf("agent tool %q at %q does not satisfy constraint %q (resolved binary: %s)", | ||
| t.Name, ver.Original(), t.Version, path) | ||
| if t.Reason != "" { | ||
| msg += fmt.Sprintf(" (reason: %s)", t.Reason) | ||
| } | ||
| return fmt.Errorf("%s", msg) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // runShellish runs a command string in a conservative way: split on spaces unless quoted. | ||
| // Keeps it dependency-free (no bash required). | ||
| func runShellish(ctx context.Context, command string) (string, error) { | ||
| parts := splitArgs(command) | ||
| if len(parts) == 0 { | ||
| return "", fmt.Errorf("empty command") | ||
| } | ||
| cmd := exec.CommandContext(ctx, parts[0], parts[1:]...) | ||
| var stdout, stderr bytes.Buffer | ||
| cmd.Stdout = &stdout | ||
| cmd.Stderr = &stderr | ||
| err := cmd.Run() | ||
| out := stdout.String() | ||
| if err != nil { | ||
| // include stderr for debugging | ||
| if s := strings.TrimSpace(stderr.String()); s != "" { | ||
| return out, fmt.Errorf("%w: %s", err, s) | ||
| } | ||
| return out, err | ||
| } | ||
| return out, nil | ||
| } | ||
|
|
||
| // extractSemver pulls the first semver-like token from output (supports leading "v"). | ||
| // If regex is provided, it must contain either a named group "ver" or group 1. | ||
| func extractSemver(output string, versionRegex string) (*semver.Version, error) { | ||
| s := strings.TrimSpace(output) | ||
| if s == "" { | ||
| return nil, fmt.Errorf("empty output") | ||
| } | ||
|
|
||
| if versionRegex != "" { | ||
| re, err := regexp.Compile(versionRegex) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid versionRegex: %w", err) | ||
| } | ||
| m := re.FindStringSubmatch(s) | ||
| if len(m) == 0 { | ||
| return nil, fmt.Errorf("regex did not match") | ||
| } | ||
| // Named group? | ||
| if idx := re.SubexpIndex("ver"); idx > 0 && idx < len(m) { | ||
| return semver.NewVersion(strings.TrimPrefix(m[idx], "v")) | ||
| } | ||
| if len(m) >= 2 { | ||
| return semver.NewVersion(strings.TrimPrefix(m[1], "v")) | ||
| } | ||
| return nil, fmt.Errorf("regex matched but no capture group found") | ||
| } | ||
|
|
||
| // Default: find first semver token | ||
| // e.g. "yq (https://...) version v4.40.5" | ||
| re := regexp.MustCompile(`v?(\d+\.\d+\.\d+)([-+][0-9A-Za-z\.\-]+)?`) | ||
| m := re.FindString(s) | ||
| if m == "" { | ||
| return nil, fmt.Errorf("no semver token found") | ||
| } | ||
| return semver.NewVersion(strings.TrimPrefix(m, "v")) | ||
| } | ||
|
|
||
| // splitArgs is a tiny quoted-arg splitter (handles "..." and '...'). | ||
| func splitArgs(in string) []string { | ||
| var out []string | ||
| var cur strings.Builder | ||
| var quote rune | ||
| flush := func() { | ||
| if cur.Len() > 0 { | ||
| out = append(out, cur.String()) | ||
| cur.Reset() | ||
| } | ||
| } | ||
|
|
||
| for _, r := range strings.TrimSpace(in) { | ||
| switch { | ||
| case quote != 0: | ||
| if r == quote { | ||
| quote = 0 | ||
| } else { | ||
| cur.WriteRune(r) | ||
| } | ||
| case r == '"' || r == '\'': | ||
| quote = r | ||
| case r == ' ' || r == '\t' || r == '\n': | ||
| flush() | ||
| default: | ||
| cur.WriteRune(r) | ||
| } | ||
| } | ||
| flush() | ||
| return out | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.