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

feat: translate deprecated fields to annotations #2925

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions src/api/v1beta1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ type GitRepoSource struct {
URL string `json:"url"`
// The sub directory to the chart within a git repo.
Path string `json:"path,omitempty"`
// The Tag of the repo where the helm chart is stored.
Tag string `json:"tag,omitempty"`
}

// LocalRepoSource represents a Helm chart stored locally.
Expand Down
55 changes: 55 additions & 0 deletions src/api/v1beta1/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package v1beta1
import (
"fmt"
"regexp"
"strings"

"github.com/zarf-dev/zarf/src/api/v1alpha1/extensions"
)

// VariableType represents a type of a Zarf package variable
Expand Down Expand Up @@ -59,6 +62,58 @@ func (pkg ZarfPackage) IsInitConfig() bool {
return pkg.Kind == ZarfInitConfig
}

func (pkg ZarfPackage) IsAirGap() bool {
if pkg.Metadata.Airgap == nil {
return true
}
return *pkg.Metadata.Airgap

}

// GetDeprecatedGroup gets the group of the component, if it is set. This should only be set
// by annotations for backwards compatibility with v1alpha1 packages.
func (pkg ZarfPackage) GetDeprecatedGroup(compIdx int) string {
if pkg.Metadata.Annotations == nil {
return ""
}

return pkg.Metadata.Annotations[fmt.Sprintf("group-%d", compIdx)]
}

// GetDeprecatedGroup gets the group of the component, if it is set. This should only be set
// by annotations for backwards compatibility with v1alpha1 packages.
func (pkg ZarfPackage) GetDeprecatedCosignKey(compIdx int) string {
if pkg.Metadata.Annotations == nil {
return ""
}

return pkg.Metadata.Annotations[fmt.Sprintf("cosign-key-path-%d", compIdx)]
}

// GetDeprecatedExtension gets the extensions of the component, if it is set. This should only be set
// by annotations for backwards compatibility with v1alpha1 packages.
func (pkg ZarfPackage) GetDeprecatedExtensions(compIdx int) extensions.ZarfComponentExtensions {
if pkg.Metadata.Annotations == nil {
return extensions.ZarfComponentExtensions{}
}

if pkg.Metadata.Annotations[fmt.Sprintf("big-bang-%d-version", compIdx)] == "" {
return extensions.ZarfComponentExtensions{}
}

extensions := extensions.ZarfComponentExtensions{
BigBang: &extensions.BigBang{
Version: pkg.Metadata.Annotations[fmt.Sprintf("big-bang-%d-version", compIdx)],
ValuesFiles: strings.Split(pkg.Metadata.Annotations[fmt.Sprintf("big-bang-%d-values-files", compIdx)], ","),
SkipFlux: pkg.Metadata.Annotations[fmt.Sprintf("big-bang-%d-skip-flux", compIdx)] == "true",
Repo: pkg.Metadata.Annotations[fmt.Sprintf("big-bang-%d-repo", compIdx)],
FluxPatchFiles: strings.Split(pkg.Metadata.Annotations[fmt.Sprintf("big-bang-%d-values-files", compIdx)], ","),
},
}

return extensions
}

// HasImages returns true if one of the components contains an image.
func (pkg ZarfPackage) HasImages() bool {
for _, component := range pkg.Components {
Expand Down
27 changes: 27 additions & 0 deletions src/api/v1beta1/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package v1beta1
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -63,7 +64,33 @@ func TranslateAlphaPackage(alphaPkg v1alpha1.ZarfPackage) (ZarfPackage, error) {

for i := range betaPkg.Components {
betaPkg.Components[i].Optional = helpers.BoolPtr(!alphaPkg.Components[i].IsRequired())
if alphaPkg.Components[i].DeprecatedGroup != "" {
betaPkg.Metadata.Annotations[fmt.Sprintf("group-%d", i)] = alphaPkg.Components[i].DeprecatedGroup
}

for j := range betaPkg.Components[i].Charts {
if alphaPkg.Components[i].Extensions.BigBang != nil {
if alphaPkg.Components[i].Extensions.BigBang.Version != "" {
betaPkg.Metadata.Annotations[fmt.Sprintf("big-bang-%d-version", i)] = alphaPkg.Components[i].Extensions.BigBang.Version
}
if alphaPkg.Components[i].Extensions.BigBang.Repo != "" {
betaPkg.Metadata.Annotations[fmt.Sprintf("big-bang-%d-repo", i)] = alphaPkg.Components[i].Extensions.BigBang.Repo
}

if alphaPkg.Components[i].Extensions.BigBang.FluxPatchFiles != nil {
betaPkg.Metadata.Annotations[fmt.Sprintf("big-bang-%d-flux-patch-files", i)] = strings.Join(alphaPkg.Components[i].Extensions.BigBang.FluxPatchFiles, ",")
}

if alphaPkg.Components[i].Extensions.BigBang.ValuesFiles != nil {
betaPkg.Metadata.Annotations[fmt.Sprintf("big-bang-%d-values-files", i)] = strings.Join(alphaPkg.Components[i].Extensions.BigBang.ValuesFiles, ",")
}
betaPkg.Metadata.Annotations[fmt.Sprintf("big-bang-%d-skip-flux", i)] = strconv.FormatBool(alphaPkg.Components[i].Extensions.BigBang.SkipFlux)
}

if alphaPkg.Components[i].DeprecatedCosignKeyPath != "" {
betaPkg.Metadata.Annotations[fmt.Sprintf("cosign-key-path-%d", i)] = alphaPkg.Components[i].DeprecatedCosignKeyPath
}

oldURL := alphaPkg.Components[i].Charts[j].URL
if helpers.IsOCIURL(oldURL) {
betaPkg.Components[i].Charts[j].OCI.URL = oldURL
Expand Down
72 changes: 40 additions & 32 deletions src/extensions/bigbang/bigbang.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
fluxSrcCtrl "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/api/v1alpha1/extensions"
"github.com/zarf-dev/zarf/src/api/v1beta1"
"github.com/zarf-dev/zarf/src/internal/packager/helm"
"github.com/zarf-dev/zarf/src/pkg/layout"
"github.com/zarf-dev/zarf/src/pkg/message"
Expand All @@ -43,19 +44,20 @@ var tenMins = metav1.Duration{

// Run mutates a component that should deploy Big Bang to a set of manifests
// that contain the flux deployment of Big Bang
func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c v1alpha1.ZarfComponent) (v1alpha1.ZarfComponent, error) {
cfg := c.Extensions.BigBang
manifests := []v1alpha1.ZarfManifest{}
func Run(ctx context.Context, tmpPaths *layout.ComponentPaths, pkg v1beta1.ZarfPackage, compIdx int) (v1beta1.ZarfComponent, error) {
manifests := []v1beta1.ZarfManifest{}
cfg := pkg.GetDeprecatedExtensions(compIdx).BigBang
c := pkg.Components[compIdx]

validVersionResponse, err := isValidVersion(cfg.Version)

if err != nil {
return c, fmt.Errorf("could not parse the Big Bang version %s: %w", cfg.Version, err)
return v1beta1.ZarfComponent{}, fmt.Errorf("could not parse the Big Bang version %s: %w", cfg.Version, err)
}

// Make sure the version is valid.
if !validVersionResponse {
return c, fmt.Errorf("Big Bang version %s must be at least %s", cfg.Version, bbMinRequiredVersion)
return v1beta1.ZarfComponent{}, fmt.Errorf("Big Bang version %s must be at least %s", cfg.Version, bbMinRequiredVersion)
}

// Print the banner for Big Bang.
Expand All @@ -68,15 +70,19 @@ func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c v1al

// By default, we want to deploy flux.
if !cfg.SkipFlux {
if cfg.Repo == "" {
cfg.Repo = bbRepo
}

fluxManifest, images, err := getFlux(tmpPaths.Temp, cfg)
if err != nil {
return c, err
return v1beta1.ZarfComponent{}, err
}

// Add the flux manifests to the list of manifests to be pulled down by Zarf.
manifests = append(manifests, fluxManifest)

if !YOLO {
if pkg.IsAirGap() {
// Add the images to the list of images to be pulled down by Zarf.
c.Images = append(c.Images, images...)
}
Expand All @@ -86,21 +92,23 @@ func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c v1al

// Configure helm to pull down the Big Bang chart.
helmCfg := helm.New(
v1alpha1.ZarfChart{
Name: bb,
Namespace: bb,
URL: bbRepo,
Version: cfg.Version,
v1beta1.ZarfChart{
Name: bb,
Namespace: bb,
Git: v1beta1.GitRepoSource{
URL: bbRepo,
Tag: cfg.Version,
Path: "./chart",
},
ValuesFiles: cfg.ValuesFiles,
GitPath: "./chart",
},
path.Join(tmpPaths.Temp, bb),
path.Join(tmpPaths.Temp, bb, "values"),
helm.WithVariableConfig(&variables.VariableConfig{}),
)

// Download the chart from Git and save it to a temporary directory.
err = helmCfg.PackageChartFromGit(ctx, c.DeprecatedCosignKeyPath)
err = helmCfg.PackageChartFromGit(ctx, pkg.GetDeprecatedCosignKey(compIdx))
if err != nil {
return c, fmt.Errorf("unable to download Big Bang Chart: %w", err)
}
Expand All @@ -113,7 +121,7 @@ func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c v1al
}

// Add the Big Bang repo to the list of repos to be pulled down by Zarf.
if !YOLO {
if pkg.IsAirGap() {
bbRepo := fmt.Sprintf("%s@%s", cfg.Repo, cfg.Version)
c.Repos = append(c.Repos, bbRepo)
}
Expand All @@ -122,7 +130,7 @@ func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c v1al
if err != nil {
return c, fmt.Errorf("unable to find Big Bang resources: %w", err)
}
if !YOLO {
if pkg.IsAirGap() {
for _, gitRepo := range gitRepos {
c.Repos = append(c.Repos, gitRepo)
}
Expand All @@ -139,21 +147,21 @@ func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c v1al
}

// ten minutes in seconds
maxTotalSeconds := 10 * 60
maxTotalSeconds := time.Duration(10 * 60 * time.Second)

defaultMaxTotalSeconds := c.Actions.OnDeploy.Defaults.MaxTotalSeconds
defaultMaxTotalSeconds := c.Actions.OnDeploy.Defaults.Timeout.Duration
if defaultMaxTotalSeconds > maxTotalSeconds {
maxTotalSeconds = defaultMaxTotalSeconds
}

// Add wait actions for each of the helm releases in generally the order they should be deployed.
for _, hrNamespacedName := range namespacedHelmReleaseNames {
hr := hrDependencies[hrNamespacedName]
action := v1alpha1.ZarfComponentAction{
Description: fmt.Sprintf("Big Bang Helm Release `%s` to be ready", hrNamespacedName),
MaxTotalSeconds: &maxTotalSeconds,
Wait: &v1alpha1.ZarfComponentActionWait{
Cluster: &v1alpha1.ZarfComponentActionWaitCluster{
action := v1beta1.ZarfComponentAction{
Description: fmt.Sprintf("Big Bang Helm Release `%s` to be ready", hrNamespacedName),
Timeout: &metav1.Duration{Duration: maxTotalSeconds},
Wait: &v1beta1.ZarfComponentActionWait{
Cluster: &v1beta1.ZarfComponentActionWaitCluster{
Kind: "HelmRelease",
Name: hr.Metadata.Name,
Namespace: hr.Metadata.Namespace,
Expand All @@ -168,7 +176,7 @@ func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c v1al
// https://repo1.dso.mil/big-bang/bigbang/-/blob/1.54.0/chart/templates/metrics-server/helmrelease.yaml
if hr.Metadata.Name == "metrics-server" {
action.Description = "K8s metric server to exist or be deployed by Big Bang"
action.Wait.Cluster = &v1alpha1.ZarfComponentActionWaitCluster{
action.Wait.Cluster = &v1beta1.ZarfComponentActionWaitCluster{
Kind: "APIService",
// https://github.com/kubernetes-sigs/metrics-server#compatibility-matrix
Name: "v1beta1.metrics.k8s.io",
Expand All @@ -195,27 +203,27 @@ func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c v1al

// Add onFailure actions with additional troubleshooting information.
for _, cmd := range failureGeneral {
c.Actions.OnDeploy.OnFailure = append(c.Actions.OnDeploy.OnFailure, v1alpha1.ZarfComponentAction{
c.Actions.OnDeploy.OnFailure = append(c.Actions.OnDeploy.OnFailure, v1beta1.ZarfComponentAction{
Cmd: fmt.Sprintf("./zarf tools kubectl %s", cmd),
})
}

for _, cmd := range failureDebug {
c.Actions.OnDeploy.OnFailure = append(c.Actions.OnDeploy.OnFailure, v1alpha1.ZarfComponentAction{
c.Actions.OnDeploy.OnFailure = append(c.Actions.OnDeploy.OnFailure, v1beta1.ZarfComponentAction{
Mute: &t,
Description: "Storing debug information to the log for troubleshooting.",
Cmd: fmt.Sprintf("./zarf tools kubectl %s", cmd),
})
}

// Add a pre-remove action to suspend the Big Bang HelmReleases to prevent reconciliation during removal.
c.Actions.OnRemove.Before = append(c.Actions.OnRemove.Before, v1alpha1.ZarfComponentAction{
c.Actions.OnRemove.Before = append(c.Actions.OnRemove.Before, v1beta1.ZarfComponentAction{
Description: "Suspend Big Bang HelmReleases to prevent reconciliation during removal.",
Cmd: `./zarf tools kubectl patch helmrelease -n bigbang bigbang --type=merge -p '{"spec":{"suspend":true}}'`,
})

// Select the images needed to support the repos for this configuration of Big Bang.
if !YOLO {
if pkg.IsAirGap() {
for _, hr := range hrDependencies {
namespacedName := getNamespacedNameFromMeta(hr.Metadata)
gitRepo := gitRepos[hr.NamespacedSource]
Expand All @@ -234,7 +242,7 @@ func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c v1al
}

// Create the flux wrapper around Big Bang for deployment.
manifest, err := addBigBangManifests(YOLO, tmpPaths.Temp, cfg)
manifest, err := addBigBangManifests(pkg.IsAirGap(), tmpPaths.Temp, cfg)
if err != nil {
return c, err
}
Expand Down Expand Up @@ -453,9 +461,9 @@ func findBBResources(t string) (gitRepos map[string]string, helmReleaseDeps map[
}

// addBigBangManifests creates the manifests component for deploying Big Bang.
func addBigBangManifests(YOLO bool, manifestDir string, cfg *extensions.BigBang) (v1alpha1.ZarfManifest, error) {
func addBigBangManifests(airgap bool, manifestDir string, cfg *extensions.BigBang) (v1beta1.ZarfManifest, error) {
// Create a manifest component that we add to the zarf package for bigbang.
manifest := v1alpha1.ZarfManifest{
manifest := v1beta1.ZarfManifest{
Name: bb,
Namespace: bb,
}
Expand Down Expand Up @@ -484,7 +492,7 @@ func addBigBangManifests(YOLO bool, manifestDir string, cfg *extensions.BigBang)
var hrValues []fluxHelmCtrl.ValuesReference

// If YOLO mode is enabled, do not include the zarf-credentials secret
if !YOLO {
if airgap {
// Create the zarf-credentials secret manifest.
if err := addManifest("bb-ext-zarf-credentials.yaml", manifestZarfCredentials(cfg.Version)); err != nil {
return manifest, err
Expand Down
10 changes: 3 additions & 7 deletions src/extensions/bigbang/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

"github.com/defenseunicorns/pkg/helpers/v2"
fluxHelmCtrl "github.com/fluxcd/helm-controller/api/v2beta1"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/api/v1alpha1/extensions"
"github.com/zarf-dev/zarf/src/api/v1beta1"
"github.com/zarf-dev/zarf/src/internal/packager/kustomize"
"github.com/zarf-dev/zarf/src/pkg/utils"
"helm.sh/helm/v3/pkg/chartutil"
Expand Down Expand Up @@ -43,14 +43,10 @@ func (h HelmReleaseDependency) Dependencies() []string {
}

// getFlux Creates a component to deploy Flux.
func getFlux(baseDir string, cfg *extensions.BigBang) (manifest v1alpha1.ZarfManifest, images []string, err error) {
func getFlux(baseDir string, cfg extensions.BigBang) (manifest v1beta1.ZarfManifest, images []string, err error) {
localPath := path.Join(baseDir, "bb-ext-flux.yaml")
kustomizePath := path.Join(baseDir, "kustomization.yaml")

if cfg.Repo == "" {
cfg.Repo = bbRepo
}

remotePath := fmt.Sprintf("%s//base/flux?ref=%s", cfg.Repo, cfg.Version)

fluxKustomization := krustytypes.Kustomization{
Expand All @@ -72,7 +68,7 @@ func getFlux(baseDir string, cfg *extensions.BigBang) (manifest v1alpha1.ZarfMan
}

// Add the flux.yaml file to the component manifests.
manifest = v1alpha1.ZarfManifest{
manifest = v1beta1.ZarfManifest{
Name: "flux-system",
Namespace: "flux-system",
Files: []string{localPath},
Expand Down
3 changes: 2 additions & 1 deletion src/internal/packager/helm/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"time"

"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/api/v1beta1"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/pkg/cluster"
"github.com/zarf-dev/zarf/src/pkg/message"
Expand Down Expand Up @@ -51,9 +52,9 @@
type Modifier func(*Helm)

// New returns a new Helm config struct.
func New(chart v1alpha1.ZarfChart, chartPath string, valuesPath string, mods ...Modifier) *Helm {
func New(chart v1beta1.ZarfChart, chartPath string, valuesPath string, mods ...Modifier) *Helm {
h := &Helm{
chart: chart,

Check failure on line 57 in src/internal/packager/helm/common.go

View workflow job for this annotation

GitHub Actions / validate-docs-and-schema

cannot use chart (variable of type "github.com/zarf-dev/zarf/src/api/v1beta1".ZarfChart) as "github.com/zarf-dev/zarf/src/api/v1alpha1".ZarfChart value in struct literal

Check failure on line 57 in src/internal/packager/helm/common.go

View workflow job for this annotation

GitHub Actions / validate-unit

cannot use chart (variable of type "github.com/zarf-dev/zarf/src/api/v1beta1".ZarfChart) as "github.com/zarf-dev/zarf/src/api/v1alpha1".ZarfChart value in struct literal

Check failure on line 57 in src/internal/packager/helm/common.go

View workflow job for this annotation

GitHub Actions / build-upgrade

cannot use chart (variable of type "github.com/zarf-dev/zarf/src/api/v1beta1".ZarfChart) as "github.com/zarf-dev/zarf/src/api/v1alpha1".ZarfChart value in struct literal

Check failure on line 57 in src/internal/packager/helm/common.go

View workflow job for this annotation

GitHub Actions / build-e2e

cannot use chart (variable of type "github.com/zarf-dev/zarf/src/api/v1beta1".ZarfChart) as "github.com/zarf-dev/zarf/src/api/v1alpha1".ZarfChart value in struct literal

Check failure on line 57 in src/internal/packager/helm/common.go

View workflow job for this annotation

GitHub Actions / validate-external

cannot use chart (variable of type "github.com/zarf-dev/zarf/src/api/v1beta1".ZarfChart) as "github.com/zarf-dev/zarf/src/api/v1alpha1".ZarfChart value in struct literal

Check failure on line 57 in src/internal/packager/helm/common.go

View workflow job for this annotation

GitHub Actions / build-bigbang

cannot use chart (variable of type "github.com/zarf-dev/zarf/src/api/v1beta1".ZarfChart) as "github.com/zarf-dev/zarf/src/api/v1alpha1".ZarfChart value in struct literal

Check failure on line 57 in src/internal/packager/helm/common.go

View workflow job for this annotation

GitHub Actions / codeql-scan (go)

cannot use chart (variable of type "github.com/zarf-dev/zarf/src/api/v1beta1".ZarfChart) as "github.com/zarf-dev/zarf/src/api/v1alpha1".ZarfChart value in struct literal
chartPath: chartPath,
valuesPath: valuesPath,
timeout: config.ZarfDefaultTimeout,
Expand Down
Loading
Loading