2016-08-18 11:52:38 -04:00
/ *
2018-08-24 15:03:55 -04:00
Copyright The Helm Authors .
2016-08-18 11:52:38 -04:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2025-02-24 10:11:54 -05:00
package cmd
2016-08-18 11:52:38 -04:00
import (
"fmt"
"io"
2019-07-31 14:28:28 -04:00
"strconv"
2019-05-09 22:53:52 -04:00
"time"
2016-08-18 11:52:38 -04:00
"github.com/spf13/cobra"
2024-12-26 16:33:51 -05:00
"helm.sh/helm/v4/pkg/action"
2025-02-24 10:11:54 -05:00
"helm.sh/helm/v4/pkg/cmd/require"
2016-08-18 11:52:38 -04:00
)
const rollbackDesc = `
2016-12-19 15:00:09 -05:00
This command rolls back a release to a previous revision .
The first argument of the rollback command is the name of a release , and the
2023-08-28 10:17:08 -04:00
second is a revision ( version ) number . If this argument is omitted or set to
0 , it will roll back to the previous release .
2019-07-31 14:28:28 -04:00
To see revision numbers , run ' helm history RELEASE ' .
2016-08-18 11:52:38 -04:00
`
2019-02-08 19:02:57 -05:00
func newRollbackCmd ( cfg * action . Configuration , out io . Writer ) * cobra . Command {
client := action . NewRollback ( cfg )
2016-08-18 11:52:38 -04:00
cmd := & cobra . Command {
2019-07-31 14:28:28 -04:00
Use : "rollback <RELEASE> [REVISION]" ,
2018-04-14 16:31:31 -04:00
Short : "roll back a release to a previous revision" ,
Long : rollbackDesc ,
2019-07-31 14:28:28 -04:00
Args : require . MinimumNArgs ( 1 ) ,
2024-03-11 17:13:34 -04:00
ValidArgsFunction : func ( _ * cobra . Command , args [ ] string , toComplete string ) ( [ ] string , cobra . ShellCompDirective ) {
2020-06-28 19:53:29 -04:00
if len ( args ) == 0 {
2021-03-13 16:28:37 -05:00
return compListReleases ( toComplete , args , cfg )
2020-04-11 14:06:56 -04:00
}
2020-06-28 19:53:29 -04:00
if len ( args ) == 1 {
return compListRevisions ( toComplete , cfg , args [ 0 ] )
}
2024-08-17 10:00:23 -04:00
return noMoreArgsComp ( )
2020-04-11 14:06:56 -04:00
} ,
2025-05-04 15:41:40 -04:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2019-07-31 14:28:28 -04:00
if len ( args ) > 1 {
ver , err := strconv . Atoi ( args [ 1 ] )
if err != nil {
return fmt . Errorf ( "could not convert revision to a number: %v" , err )
}
client . Version = ver
}
2025-05-04 15:41:40 -04:00
dryRunStrategy , err := cmdGetDryRunFlagStrategy ( cmd , false )
if err != nil {
return err
}
client . DryRunStrategy = dryRunStrategy
2019-08-19 13:22:27 -04:00
if err := client . Run ( args [ 0 ] ) ; err != nil {
2019-02-08 19:02:57 -05:00
return err
2016-10-17 10:26:46 -04:00
}
2019-02-08 19:02:57 -05:00
fmt . Fprintf ( out , "Rollback was a success! Happy Helming!\n" )
return nil
2016-08-18 11:52:38 -04:00
} ,
}
2019-03-13 11:58:35 -04:00
f := cmd . Flags ( )
2025-06-15 14:31:48 -04:00
f . BoolVar ( & client . ForceReplace , "force-replace" , false , "force resource updates by replacement" )
f . BoolVar ( & client . ForceReplace , "force" , false , "deprecated" )
f . MarkDeprecated ( "force" , "use --force-replace instead" )
2025-04-28 00:15:46 -04:00
f . BoolVar ( & client . ForceConflicts , "force-conflicts" , false , "if set server-side apply will force changes against conflicts" )
f . StringVar ( & client . ServerSideApply , "server-side" , "auto" , "must be \"true\", \"false\" or \"auto\". Object updates run in the server instead of the client (\"auto\" defaults the value from the previous chart release's method)" )
2019-03-13 11:58:35 -04:00
f . BoolVar ( & client . DisableHooks , "no-hooks" , false , "prevent hooks from running during rollback" )
2019-05-09 22:53:52 -04:00
f . DurationVar ( & client . Timeout , "timeout" , 300 * time . Second , "time to wait for any individual Kubernetes operation (like Jobs for hooks)" )
2020-11-03 06:48:29 -05:00
f . BoolVar ( & client . WaitForJobs , "wait-for-jobs" , false , "if set and --wait enabled, will wait until all Jobs have been completed before marking the release as successful. It will wait for as long as --timeout" )
2019-09-23 18:13:02 -04:00
f . BoolVar ( & client . CleanupOnFail , "cleanup-on-fail" , false , "allow deletion of new resources created in this rollback when rollback fails" )
2020-07-09 05:40:36 -04:00
f . IntVar ( & client . MaxHistory , "history-max" , settings . MaxHistory , "limit the maximum number of revisions saved per release. Use 0 for no limit" )
2025-05-04 15:41:40 -04:00
addDryRunFlag ( cmd )
2025-03-25 09:55:39 -04:00
AddWaitFlag ( cmd , & client . WaitStrategy )
2025-04-28 00:15:46 -04:00
cmd . MarkFlagsMutuallyExclusive ( "force-replace" , "force-conflicts" )
cmd . MarkFlagsMutuallyExclusive ( "force" , "force-conflicts" )
2016-10-14 23:05:04 -04:00
2016-08-18 11:52:38 -04:00
return cmd
}