Skip to content

Commit

Permalink
Merge branch 'main' into delete-agent-e2e-label-test
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinAbro321 authored May 31, 2024
2 parents 344cca5 + 69a8507 commit 0e8692f
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 106 deletions.
8 changes: 7 additions & 1 deletion src/pkg/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package message
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"runtime/debug"
Expand Down Expand Up @@ -59,6 +60,11 @@ func (d *DebugWriter) Write(raw []byte) (int, error) {
}

func init() {
InitializePTerm(os.Stderr)
}

// InitializePTerm sets the default styles and output for pterm.
func InitializePTerm(w io.Writer) {
pterm.ThemeDefault.SuccessMessageStyle = *pterm.NewStyle(pterm.FgLightGreen)
// Customize default error.
pterm.Success.Prefix = pterm.Prefix{
Expand All @@ -73,7 +79,7 @@ func init() {
Text: " •",
}

pterm.SetDefaultOutput(os.Stderr)
pterm.SetDefaultOutput(w)
}

// UseLogFile wraps a given file in a PausableWriter
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (p *Packager) attemptClusterChecks(ctx context.Context) (err error) {
// Check for any breaking changes between the initialized Zarf version and this CLI
if existingInitPackage, _ := p.cluster.GetDeployedPackage(ctx, "init"); existingInitPackage != nil {
// Use the build version instead of the metadata since this will support older Zarf versions
deprecated.PrintBreakingChanges(existingInitPackage.Data.Build.Version)
deprecated.PrintBreakingChanges(os.Stderr, existingInitPackage.Data.Build.Version, config.CLIVersion)
}

spinner.Success()
Expand Down
77 changes: 40 additions & 37 deletions src/pkg/packager/deprecated/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,33 @@ package deprecated

import (
"fmt"
"io"
"strings"

"slices"

"github.com/Masterminds/semver/v3"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/types"
"github.com/pterm/pterm"
)

// BreakingChange represents a breaking change that happened on a specified Zarf version
// BreakingChange represents a breaking change that happened on a specified Zarf version.
type BreakingChange struct {
version *semver.Version
title string
mitigation string
}

// String returns the string representation of the BreakingChange.
func (bc BreakingChange) String() string {
return fmt.Sprintf("%s\n\n - %s\n %s\n",
pterm.Bold.Sprintf(bc.title),
pterm.Bold.Sprint("Mitigation:"),
strings.ReplaceAll(message.Paragraphn(96, "%s", pterm.FgLightCyan.Sprint(bc.mitigation)), "\n", "\n "),
)
}

// List of migrations tracked in the zarf.yaml build data.
const (
// This should be updated when a breaking change is introduced to the Zarf package structure. See: https://github.com/defenseunicorns/zarf/releases/tag/v0.27.0
Expand All @@ -32,15 +41,6 @@ const (
PluralizeSetVariable = "pluralize-set-variable"
)

// List of breaking changes to warn the user of.
var breakingChanges = []BreakingChange{
{
version: semver.New(0, 26, 0, "", ""),
title: "Zarf container images are now mutated based on tag instead of repository name.",
mitigation: "Reinitialize the cluster using v0.26.0 or later and redeploy existing packages to update the image references (you can view existing packages with 'zarf package list' and view cluster images with 'zarf tools registry catalog').",
},
}

// MigrateComponent runs all migrations on a component.
// Build should be empty on package create, but include just in case someone copied a zarf.yaml from a zarf package.
func MigrateComponent(build types.ZarfBuildData, component types.ZarfComponent) (migratedComponent types.ZarfComponent, warnings []string) {
Expand Down Expand Up @@ -77,47 +77,50 @@ func MigrateComponent(build types.ZarfBuildData, component types.ZarfComponent)
return migratedComponent, warnings
}

// PrintBreakingChanges prints the breaking changes between the provided version and the current CLIVersion
func PrintBreakingChanges(deployedZarfVersion string) {
// PrintBreakingChanges prints the breaking changes between the provided version and the current CLIVersion.
func PrintBreakingChanges(w io.Writer, deployedZarfVersion, cliVersion string) {
deployedSemver, err := semver.NewVersion(deployedZarfVersion)
if err != nil {
message.Debugf("Unable to check for breaking changes between Zarf versions")
return
}

applicableBreakingChanges := []BreakingChange{}
// List of breaking changes to warn the user of.
var breakingChanges = []BreakingChange{
{
version: semver.MustParse("0.26.0"),
title: "Zarf container images are now mutated based on tag instead of repository name.",
mitigation: "Reinitialize the cluster using v0.26.0 or later and redeploy existing packages to update the image references (you can view existing packages with 'zarf package list' and view cluster images with 'zarf tools registry catalog').",
},
}

// Calculate the applicable breaking changes
// Calculate the applicable breaking changes.
var applicableBreakingChanges []BreakingChange
for _, breakingChange := range breakingChanges {
if deployedSemver.LessThan(breakingChange.version) {
applicableBreakingChanges = append(applicableBreakingChanges, breakingChange)
}
}

if len(applicableBreakingChanges) > 0 {
// Print header information
message.HorizontalRule()
message.Title("Potential Breaking Changes", "breaking changes that may cause issues with this package")

// Print information about the versions
format := pterm.FgYellow.Sprint("CLI version ") + "%s" + pterm.FgYellow.Sprint(" is being used to deploy to a cluster that was initialized with ") +
"%s" + pterm.FgYellow.Sprint(". Between these versions there are the following breaking changes to consider:")
cliVersion := pterm.Bold.Sprintf(config.CLIVersion)
deployedVersion := pterm.Bold.Sprintf(deployedZarfVersion)
message.Warnf(format, cliVersion, deployedVersion)

// Print each applicable breaking change
for idx, applicableBreakingChange := range applicableBreakingChanges {
titleFormat := pterm.Bold.Sprintf("\n %d. ", idx+1) + "%s"

pterm.Printfln(titleFormat, applicableBreakingChange.title)
if len(applicableBreakingChanges) == 0 {
return
}

mitigationText := message.Paragraphn(96, "%s", pterm.FgLightCyan.Sprint(applicableBreakingChange.mitigation))
// Print header information
message.HorizontalRule()
message.Title("Potential Breaking Changes", "breaking changes that may cause issues with this package")

pterm.Printfln("\n - %s", pterm.Bold.Sprint("Mitigation:"))
pterm.Printfln(" %s", strings.ReplaceAll(mitigationText, "\n", "\n "))
}
// Print information about the versions
format := pterm.FgYellow.Sprint("CLI version ") + "%s" + pterm.FgYellow.Sprint(" is being used to deploy to a cluster that was initialized with ") +
"%s" + pterm.FgYellow.Sprint(". Between these versions there are the following breaking changes to consider:")
cliVersion = pterm.Bold.Sprintf(cliVersion)
deployedZarfVersion = pterm.Bold.Sprintf(deployedZarfVersion)
message.Warnf(format, cliVersion, deployedZarfVersion)

message.HorizontalRule()
// Print each applicable breaking change
for i, applicableBreakingChange := range applicableBreakingChanges {
fmt.Fprintf(w, "\n %d. %s", i+1, applicableBreakingChange.String())
}

message.HorizontalRule()
}
58 changes: 58 additions & 0 deletions src/pkg/packager/deprecated/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package deprecated handles package deprecations and migrations
package deprecated

import (
"bytes"
"testing"

"github.com/Masterminds/semver/v3"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/stretchr/testify/require"
)

func TestPrintBreakingChanges(t *testing.T) {
t.Parallel()

tests := []struct {
name string
deployedVersion string
cliVersion string
breakingChanges []BreakingChange
}{
{
name: "No breaking changes",
deployedVersion: "0.26.0",
cliVersion: "0.26.0",
breakingChanges: []BreakingChange{},
},
{
name: "agent breaking change",
deployedVersion: "0.25.0",
cliVersion: "0.26.0",
breakingChanges: []BreakingChange{
{
version: semver.MustParse("0.26.0"),
title: "Zarf container images are now mutated based on tag instead of repository name.",
mitigation: "Reinitialize the cluster using v0.26.0 or later and redeploy existing packages to update the image references (you can view existing packages with 'zarf package list' and view cluster images with 'zarf tools registry catalog').",
},
},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var output bytes.Buffer
message.InitializePTerm(&output)
PrintBreakingChanges(&output, tt.deployedVersion, tt.cliVersion)
for _, bc := range tt.breakingChanges {
require.Contains(t, output.String(), bc.String())
}
t.Log(output.String())
})
}
}
File renamed without changes.
67 changes: 0 additions & 67 deletions src/test/e2e/29_mismatched_checks_test.go

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
35 changes: 35 additions & 0 deletions src/test/e2e/36_pod_without_labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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/stretchr/testify/require"
)

func TestPodWithoutLabels(t *testing.T) {
t.Log("E2E: Pod Without Labels")
e2e.SetupWithCluster(t)

// Path to pod manifest containing 0 lavbels
buildPath := filepath.Join("src", "test", "packages", "37-pod-without-labels", "pod.yaml")

// Create the testing namespace
_, _, err := e2e.Kubectl("create", "ns", "pod-label")
require.NoError(t, err)

// Create the pod without labels
// This is not an image zarf will have in the registry - but the agent was failing to admit on an internal server error before completing admission
_, _, err = e2e.Kubectl("create", "-f", buildPath, "-n", "pod-label")
require.NoError(t, err)

// Cleanup
_, _, err = e2e.Kubectl("delete", "-f", buildPath, "-n", "pod-label")
require.NoError(t, err)
_, _, err = e2e.Kubectl("delete", "ns", "pod-label")
require.NoError(t, err)
}

0 comments on commit 0e8692f

Please sign in to comment.