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: Update upgrade-matrix.yaml in post-upgrade based on env variable #2654

Merged
merged 2 commits into from
Sep 26, 2024
Merged
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
9 changes: 9 additions & 0 deletions hack/release/cmd/postrelease/postrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/mesosphere/kommander-applications/hack/release/pkg/appversion"
"github.com/mesosphere/kommander-applications/hack/release/pkg/chartversion"
"github.com/mesosphere/kommander-applications/hack/release/pkg/upgradematrix"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -54,6 +55,14 @@ func init() { //nolint:gochecknoinits // Initializing cobra application.
}

fmt.Fprintf(cmd.OutOrStdout(), "Updated Kommander chart version to %s", chartVersion)

if err := upgradematrix.UpdateUpgradeMatrix(
cmd.Context(),
kommanderApplicationsRepo,
); err != nil {
return err
}

return nil
},
}
Expand Down
44 changes: 44 additions & 0 deletions hack/release/pkg/upgradematrix/upgradematrix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package upgradematrix

import (
"context"
"errors"
"log"
"os"
"path/filepath"
"strings"
)

const (
upgradeMatrixEnv = "NKP_RELEASE_AUTOMATION_UPGRADE_MATRIX"
upgradeMatrixFile = "upgrade-matrix.yaml"
)

var ErrIncorrectFormat = errors.New("upgrade matrix does not appear to be in the correct format, unable to update upgrade-matrix.yaml")

// UpdateUpgradeMatrix updates the upgrade-matrix.yaml file in the kommander-applications repository with the contents \
// of the upgradeMatrixEnv environment variable.
func UpdateUpgradeMatrix(ctx context.Context, kommanderApplicationsRepo string) error {
// Get the upgrade matrix environment variable
upgradeMatrix := os.Getenv(upgradeMatrixEnv)
if upgradeMatrix == "" {
log.Printf("upgrade matrix environment variable %s is empty, unable to update upgrade-matrix.yaml", upgradeMatrixEnv)
return nil
}

// Check that upgradeMatrix appears to be correct
if !strings.HasPrefix(upgradeMatrix, "upgrades:") {
return ErrIncorrectFormat
}

// Write the upgrade matrix to the file
err := os.WriteFile(filepath.Join(kommanderApplicationsRepo, upgradeMatrixFile), []byte(upgradeMatrix), 0644)
if err != nil {
log.Print("cannot write upgrade-matrix.yaml")
return err
}

log.Print("Updated upgrade matrix")

return nil
}
83 changes: 83 additions & 0 deletions hack/release/pkg/upgradematrix/upgradematrix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package upgradematrix

import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

const testFile = `upgrades:
- from: v1.2.3
to: v4.5.6
k8s_version: "1.34"
`

const expectedContent = `upgrades:
- from: v2.8.0
to: v2.8.2-dev
k8s_version: "1.28"
- from: v2.8.1
to: v2.8.2-dev
k8s_version: "1.28"
- from: v2.8.2-dev
to: v2.12.0-dev
k8s_version: "1.28"
`

func TestUpdateUpgradeMatrix(t *testing.T) {
dir := fetchRepo(t)

// Replace the existing file with known test data
err := os.WriteFile(filepath.Join(dir, "upgrade-matrix.yaml"), []byte(testFile), 0644)
require.NoError(t, err)

// Expected update value
err = os.Setenv(upgradeMatrixEnv, expectedContent)
require.NoError(t, err)

err = UpdateUpgradeMatrix(context.Background(), dir)
require.NoError(t, err)

// Check that the file has been regenerated
fileContent, err := os.ReadFile(filepath.Join(dir, "upgrade-matrix.yaml"))
require.NoError(t, err)

require.Equal(t, string(fileContent), expectedContent)
require.NotContains(t, string(fileContent), testFile)
}

func TestUpdateUpgradeMatrixNoEnv(t *testing.T) {
dir := fetchRepo(t)

// Replace the existing file with known test data
err := os.WriteFile(filepath.Join(dir, "upgrade-matrix.yaml"), []byte(testFile), 0644)
require.NoError(t, err)

// Ensure that the environment variable is empty
err = os.Setenv(upgradeMatrixEnv, "")
require.NoError(t, err)

err = UpdateUpgradeMatrix(context.Background(), dir)
require.NoError(t, err)

// Check that the file has *not* been regenerated
fileContent, err := os.ReadFile(filepath.Join(dir, "upgrade-matrix.yaml"))
require.NoError(t, err)

require.Equal(t, string(fileContent), testFile)
}

func fetchRepo(t *testing.T) string {
t.Helper()

dir := t.TempDir()
cmd := exec.Command("git", "clone", strings.Repeat("../", 4), dir)
require.NoError(t, cmd.Run())

return dir
}
Loading