1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-25 08:19:40 +02:00

feat(helm): rollback helm chart [r8s-287] (#660)

This commit is contained in:
Ali 2025-04-23 08:58:34 +12:00 committed by GitHub
parent 61d6ac035d
commit c91c8a6467
13 changed files with 701 additions and 32 deletions

View file

@ -0,0 +1,19 @@
package options
import "time"
// RollbackOptions defines options for rollback.
type RollbackOptions struct {
// Required
Name string
Namespace string
KubernetesClusterAccess *KubernetesClusterAccess
// Optional with defaults
Version int // Target revision to rollback to (0 means previous revision)
Timeout time.Duration // Default: 5 minutes
Wait bool // Default: false
WaitForJobs bool // Default: false
Recreate bool // Default: false - whether to recreate pods
Force bool // Default: false - whether to force recreation
}

111
pkg/libhelm/sdk/rollback.go Normal file
View file

@ -0,0 +1,111 @@
package sdk
import (
"time"
"github.com/pkg/errors"
"github.com/portainer/portainer/pkg/libhelm/options"
"github.com/portainer/portainer/pkg/libhelm/release"
"github.com/rs/zerolog/log"
"helm.sh/helm/v3/pkg/action"
)
// Rollback would implement the HelmPackageManager interface by using the Helm SDK to rollback a release to a previous revision.
func (hspm *HelmSDKPackageManager) Rollback(rollbackOpts options.RollbackOptions) (*release.Release, error) {
log.Debug().
Str("context", "HelmClient").
Str("name", rollbackOpts.Name).
Str("namespace", rollbackOpts.Namespace).
Int("revision", rollbackOpts.Version).
Bool("wait", rollbackOpts.Wait).
Msg("Rolling back Helm release")
if rollbackOpts.Name == "" {
log.Error().
Str("context", "HelmClient").
Msg("Name is required for helm release rollback")
return nil, errors.New("name is required for helm release rollback")
}
// Initialize action configuration with kubernetes config
actionConfig := new(action.Configuration)
err := hspm.initActionConfig(actionConfig, rollbackOpts.Namespace, rollbackOpts.KubernetesClusterAccess)
if err != nil {
return nil, errors.Wrap(err, "failed to initialize helm configuration for helm release rollback")
}
rollbackClient := initRollbackClient(actionConfig, rollbackOpts)
// Run the rollback
err = rollbackClient.Run(rollbackOpts.Name)
if err != nil {
log.Error().
Str("context", "HelmClient").
Str("name", rollbackOpts.Name).
Str("namespace", rollbackOpts.Namespace).
Int("revision", rollbackOpts.Version).
Err(err).
Msg("Failed to rollback helm release")
return nil, errors.Wrap(err, "helm was not able to rollback the release")
}
// Get the release info after rollback
statusClient := action.NewStatus(actionConfig)
rel, err := statusClient.Run(rollbackOpts.Name)
if err != nil {
log.Error().
Str("context", "HelmClient").
Str("name", rollbackOpts.Name).
Str("namespace", rollbackOpts.Namespace).
Int("revision", rollbackOpts.Version).
Err(err).
Msg("Failed to get status after rollback")
return nil, errors.Wrap(err, "failed to get status after rollback")
}
return &release.Release{
Name: rel.Name,
Namespace: rel.Namespace,
Version: rel.Version,
Info: &release.Info{
Status: release.Status(rel.Info.Status),
Notes: rel.Info.Notes,
Description: rel.Info.Description,
},
Manifest: rel.Manifest,
Chart: release.Chart{
Metadata: &release.Metadata{
Name: rel.Chart.Metadata.Name,
Version: rel.Chart.Metadata.Version,
AppVersion: rel.Chart.Metadata.AppVersion,
},
},
Labels: rel.Labels,
}, nil
}
// initRollbackClient initializes the rollback client with the given options
// and returns the rollback client.
func initRollbackClient(actionConfig *action.Configuration, rollbackOpts options.RollbackOptions) *action.Rollback {
rollbackClient := action.NewRollback(actionConfig)
// Set version to rollback to (if specified)
if rollbackOpts.Version > 0 {
rollbackClient.Version = rollbackOpts.Version
}
rollbackClient.Wait = rollbackOpts.Wait
rollbackClient.WaitForJobs = rollbackOpts.WaitForJobs
rollbackClient.CleanupOnFail = true // Sane default to clean up on failure
rollbackClient.Recreate = rollbackOpts.Recreate
rollbackClient.Force = rollbackOpts.Force
// Set default values if not specified
if rollbackOpts.Timeout == 0 {
rollbackClient.Timeout = 5 * time.Minute // Sane default of 5 minutes
} else {
rollbackClient.Timeout = rollbackOpts.Timeout
}
return rollbackClient
}

View file

@ -0,0 +1,123 @@
package sdk
import (
"testing"
"github.com/portainer/portainer/pkg/libhelm/options"
"github.com/portainer/portainer/pkg/libhelm/test"
"github.com/stretchr/testify/assert"
)
func TestRollback(t *testing.T) {
test.EnsureIntegrationTest(t)
is := assert.New(t)
// Create a new SDK package manager
hspm := NewHelmSDKPackageManager()
t.Run("should return error when name is not provided", func(t *testing.T) {
rollbackOpts := options.RollbackOptions{
Namespace: "default",
}
_, err := hspm.Rollback(rollbackOpts)
is.Error(err, "should return an error when name is not provided")
is.Equal("name is required for helm release rollback", err.Error(), "should return correct error message")
})
t.Run("should return error when release doesn't exist", func(t *testing.T) {
rollbackOpts := options.RollbackOptions{
Name: "non-existent-release",
Namespace: "default",
}
_, err := hspm.Rollback(rollbackOpts)
is.Error(err, "should return an error when release doesn't exist")
})
t.Run("should successfully rollback to previous revision", func(t *testing.T) {
// First install a release
installOpts := options.InstallOptions{
Name: "hello-world",
Chart: "hello-world",
Namespace: "default",
Repo: "https://helm.github.io/examples",
}
// Ensure the release doesn't exist before test
hspm.Uninstall(options.UninstallOptions{
Name: installOpts.Name,
})
// Install first version
release, err := hspm.Upgrade(installOpts)
is.NoError(err, "should successfully install release")
is.Equal(1, release.Version, "first version should be 1")
// Upgrade to second version
_, err = hspm.Upgrade(installOpts)
is.NoError(err, "should successfully upgrade release")
// Rollback to first version
rollbackOpts := options.RollbackOptions{
Name: installOpts.Name,
Namespace: "default",
Version: 0, // Previous revision
}
rolledBackRelease, err := hspm.Rollback(rollbackOpts)
defer hspm.Uninstall(options.UninstallOptions{
Name: installOpts.Name,
})
is.NoError(err, "should successfully rollback release")
is.NotNil(rolledBackRelease, "should return non-nil release")
is.Equal(3, rolledBackRelease.Version, "version should be incremented to 3")
})
t.Run("should successfully rollback to specific revision", func(t *testing.T) {
// First install a release
installOpts := options.InstallOptions{
Name: "hello-world",
Chart: "hello-world",
Namespace: "default",
Repo: "https://helm.github.io/examples",
}
// Ensure the release doesn't exist before test
hspm.Uninstall(options.UninstallOptions{
Name: installOpts.Name,
})
// Install first version
release, err := hspm.Upgrade(installOpts)
is.NoError(err, "should successfully install release")
is.Equal(1, release.Version, "first version should be 1")
// Upgrade to second version
_, err = hspm.Upgrade(installOpts)
is.NoError(err, "should successfully upgrade release")
// Upgrade to third version
_, err = hspm.Upgrade(installOpts)
is.NoError(err, "should successfully upgrade release again")
// Rollback to first version
rollbackOpts := options.RollbackOptions{
Name: installOpts.Name,
Namespace: "default",
Version: 1, // Specific revision
}
rolledBackRelease, err := hspm.Rollback(rollbackOpts)
defer hspm.Uninstall(options.UninstallOptions{
Name: installOpts.Name,
})
is.NoError(err, "should successfully rollback to specific revision")
is.NotNil(rolledBackRelease, "should return non-nil release")
is.Equal(4, rolledBackRelease.Version, "version should be incremented to 4")
})
}

View file

@ -19,7 +19,6 @@ var tests = []testCase{
{"ingress helm repo", "https://kubernetes.github.io/ingress-nginx", false},
{"portainer helm repo", "https://portainer.github.io/k8s/", false},
{"elastic helm repo with trailing slash", "https://helm.elastic.co/", false},
{"lensesio helm repo without trailing slash", "https://lensesio.github.io/kafka-helm-charts", false},
}
func Test_SearchRepo(t *testing.T) {

View file

@ -79,6 +79,11 @@ func (hpm *helmMockPackageManager) Upgrade(upgradeOpts options.InstallOptions) (
return hpm.Install(upgradeOpts)
}
// Rollback a helm chart (not thread safe)
func (hpm *helmMockPackageManager) Rollback(rollbackOpts options.RollbackOptions) (*release.Release, error) {
return hpm.Rollback(rollbackOpts)
}
// Show values/readme/chart etc
func (hpm *helmMockPackageManager) Show(showOpts options.ShowOptions) ([]byte, error) {
switch showOpts.OutputFormat {

View file

@ -16,6 +16,7 @@ type HelmPackageManager interface {
Uninstall(uninstallOpts options.UninstallOptions) error
Get(getOpts options.GetOptions) (*release.Release, error)
GetHistory(historyOpts options.HistoryOptions) ([]*release.Release, error)
Rollback(rollbackOpts options.RollbackOptions) (*release.Release, error)
}
type Repository interface {