-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: AiRanthem <[email protected]>
- Loading branch information
Showing
6 changed files
with
455 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
/* | ||
Copyright 2025 The Kruise Authors. | ||
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. | ||
*/ | ||
|
||
package rollout | ||
|
||
import ( | ||
"fmt" | ||
|
||
internalapi "github.com/openkruise/kruise-tools/pkg/api" | ||
"github.com/openkruise/kruise-tools/pkg/cmd/util" | ||
internalpolymorphichelpers "github.com/openkruise/kruise-tools/pkg/internal/polymorphichelpers" | ||
"github.com/spf13/cobra" | ||
"k8s.io/apimachinery/pkg/types" | ||
utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"k8s.io/cli-runtime/pkg/printers" | ||
"k8s.io/cli-runtime/pkg/resource" | ||
"k8s.io/kubectl/pkg/cmd/set" | ||
cmdutil "k8s.io/kubectl/pkg/cmd/util" | ||
"k8s.io/kubectl/pkg/scheme" | ||
"k8s.io/kubectl/pkg/util/i18n" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
) | ||
|
||
// RollbackOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of | ||
// referencing the cmd.Flags() | ||
type RollbackOptions struct { | ||
PrintFlags *genericclioptions.PrintFlags | ||
ToPrinter func(string) (printers.ResourcePrinter, error) | ||
|
||
Resources []string | ||
|
||
Builder func() *resource.Builder | ||
RollbackFunc internalpolymorphichelpers.RolloutRollbackFuncGetter | ||
Namespace string | ||
EnforceNamespace bool | ||
|
||
resource.FilenameOptions | ||
genericclioptions.IOStreams | ||
} | ||
|
||
var ( | ||
rollbackLong = templates.LongDesc(` | ||
Rollback a rollout to a previous step during release progress. | ||
**CAUTION** This command only works on rollouts version >= 0.6.0 | ||
You can rollback to a specific previous step using the --step flag. | ||
If not specified, kubectl-kruise will look for a previous step that has no traffic and the most replicas to perform the rollback.`) | ||
|
||
rollbackExample = templates.Examples(` | ||
# Rollback a kruise rollout named "rollout-demo" in "ns-demo" namespace | ||
kubectl-kruise rollout rollback rollout/rollout-demo -n ns-demo | ||
# Rollback a kruise rollout resource to a specific step | ||
kubectl-kruise rollout rollback rollout/rollout-demo --step 1`) | ||
|
||
targetStep int32 | ||
) | ||
|
||
// NewRollbackOptions returns an initialized RollbackOptions instance | ||
func NewRollbackOptions(streams genericclioptions.IOStreams) *RollbackOptions { | ||
return &RollbackOptions{ | ||
PrintFlags: genericclioptions.NewPrintFlags("rolled back").WithTypeSetter(internalapi.GetScheme()), | ||
IOStreams: streams, | ||
} | ||
} | ||
|
||
// NewCmdRolloutRollback returns a Command instance for 'rollout rollback' sub command | ||
func NewCmdRolloutRollback(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { | ||
o := NewRollbackOptions(streams) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "rollback RESOURCE", | ||
DisableFlagsInUseLine: true, | ||
Short: i18n.T("Rollback a resource"), | ||
Long: rollbackLong, | ||
Example: rollbackExample, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmdutil.CheckErr(o.Complete(f, cmd, args)) | ||
cmdutil.CheckErr(o.Validate()) | ||
cmdutil.CheckErr(o.RunRollback()) | ||
}, | ||
} | ||
|
||
usage := "identifying the resource to get from a server." | ||
cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage) | ||
o.PrintFlags.AddFlags(cmd) | ||
cmd.Flags().Int32Var(&targetStep, "step", -1, "Rollback to a specific previous step") | ||
return cmd | ||
} | ||
|
||
// Complete completes all the required options | ||
func (o *RollbackOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error { | ||
o.Resources = args | ||
|
||
o.RollbackFunc = internalpolymorphichelpers.RolloutRollbackGetter | ||
|
||
var err error | ||
o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
o.ToPrinter = func(operation string) (printers.ResourcePrinter, error) { | ||
o.PrintFlags.NamePrintFlags.Operation = operation | ||
return o.PrintFlags.ToPrinter() | ||
} | ||
|
||
o.Builder = f.NewBuilder | ||
|
||
return nil | ||
} | ||
|
||
func (o *RollbackOptions) Validate() error { | ||
if len(o.Resources) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) { | ||
return fmt.Errorf("required resource not specified") | ||
} | ||
return nil | ||
} | ||
|
||
// RunRollback performs the execution of 'rollout rollback' sub command | ||
func (o *RollbackOptions) RunRollback() error { | ||
r := o.Builder(). | ||
WithScheme(internalapi.GetScheme(), scheme.Scheme.PrioritizedVersionsAllGroups()...). | ||
NamespaceParam(o.Namespace).DefaultNamespace(). | ||
FilenameParam(o.EnforceNamespace, &o.FilenameOptions). | ||
ResourceTypeOrNameArgs(true, o.Resources...). | ||
ContinueOnError(). | ||
Latest(). | ||
Flatten(). | ||
Do() | ||
if err := r.Err(); err != nil { | ||
return err | ||
} | ||
|
||
allErrs := []error{} | ||
infos, err := r.Infos() | ||
if err != nil { | ||
// restore previous command behavior where | ||
// an error caused by retrieving infos due to | ||
// at least a single broken object did not result | ||
// in an immediate return, but rather an overall | ||
// aggregation of errors. | ||
allErrs = append(allErrs, err) | ||
} | ||
|
||
for _, patch := range set.CalculatePatches(infos, scheme.DefaultJSONEncoder(), o.RollbackFunc(targetStep)) { | ||
info := patch.Info | ||
|
||
if patch.Err != nil { | ||
resourceString := info.Mapping.Resource.Resource | ||
if len(info.Mapping.Resource.Group) > 0 { | ||
resourceString = resourceString + "." + info.Mapping.Resource.Group | ||
} | ||
allErrs = append(allErrs, fmt.Errorf("error: %s %q %v", resourceString, info.Name, patch.Err)) | ||
continue | ||
} | ||
|
||
if string(patch.Patch) == "{}" || len(patch.Patch) == 0 { | ||
printer, err := o.ToPrinter("already rolled back") | ||
if err != nil { | ||
allErrs = append(allErrs, err) | ||
continue | ||
} | ||
if err = printer.PrintObj(info.Object, o.Out); err != nil { | ||
allErrs = append(allErrs, err) | ||
} | ||
continue | ||
} | ||
|
||
obj, err := util.PatchSubResource(info.Client, info.Mapping.Resource.Resource, "status", info.Namespace, info.Name, info.Namespaced(), types.MergePatchType, patch.Patch, nil) | ||
if err != nil { | ||
allErrs = append(allErrs, fmt.Errorf("failed to patch: %v", err)) | ||
continue | ||
} | ||
|
||
info.Refresh(obj, true) | ||
printer, err := o.ToPrinter("rolled back" + | ||
"") | ||
if err != nil { | ||
allErrs = append(allErrs, err) | ||
continue | ||
} | ||
if err = printer.PrintObj(info.Object, o.Out); err != nil { | ||
allErrs = append(allErrs, err) | ||
} | ||
} | ||
|
||
return utilerrors.NewAggregate(allErrs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package polymorphichelpers | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/openkruise/kruise-rollout-api/rollouts/v1beta1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
"k8s.io/klog/v2" | ||
"k8s.io/kubectl/pkg/scheme" | ||
) | ||
|
||
func rolloutRollbackGetter(targetStep int32) func(runtime.Object) ([]byte, error) { | ||
return func(obj runtime.Object) ([]byte, error) { | ||
switch rollout := obj.(type) { | ||
case *v1beta1.Rollout: | ||
steps := rollout.Spec.Strategy.GetSteps() | ||
curStep := rollout.Status.CurrentStepIndex | ||
if len(steps) < int(curStep) { | ||
return nil, fmt.Errorf("has %d steps, but current step is too large %d", len(steps), curStep) | ||
} | ||
if curStep <= 1 { | ||
return nil, fmt.Errorf("already at the first step, use kubectl-kruise rollout undo to cancel the release") | ||
} | ||
if targetStep == -1 { | ||
s, err := findPreviousStepWithNoTrafficAndMostReplicas(steps, rollout.Status.CurrentStepIndex) | ||
if err != nil { | ||
return nil, err | ||
} | ||
targetStep = s | ||
} | ||
if targetStep >= rollout.Status.CurrentStepIndex { | ||
return nil, fmt.Errorf("specified step %d is not a previous step (current step is %d)", targetStep, rollout.Status.CurrentStepIndex) | ||
} | ||
style := rollout.Spec.Strategy.GetRollingStyle() | ||
switch style { | ||
case v1beta1.BlueGreenRollingStyle: | ||
rollout.Status.BlueGreenStatus.NextStepIndex = targetStep | ||
default: | ||
// canary and partition | ||
rollout.Status.CanaryStatus.NextStepIndex = targetStep | ||
} | ||
return runtime.Encode(scheme.Codecs.LegacyCodec(v1beta1.GroupVersion), rollout) | ||
default: | ||
return nil, fmt.Errorf("rollback is not supported on given object") | ||
} | ||
} | ||
} | ||
|
||
func findPreviousStepWithNoTrafficAndMostReplicas(steps []v1beta1.CanaryStep, curStep int32) (int32, error) { | ||
maxReplicas := 0 | ||
var targetStep int32 = -1 | ||
for i := curStep - 2; i >= 0; i-- { | ||
step := steps[i] | ||
if hasTraffic(step) { | ||
klog.V(5).InfoS("has traffic", "step", i+1) | ||
continue | ||
} | ||
replicas, _ := intstr.GetScaledValueFromIntOrPercent(step.Replicas, 100, true) | ||
klog.V(5).InfoS("replicas percent", "percent", replicas, "step", i+1, "obj", step) | ||
if replicas > maxReplicas { | ||
maxReplicas = replicas | ||
targetStep = i + 1 | ||
} | ||
} | ||
if targetStep == -1 { | ||
return 0, fmt.Errorf("no previous step with no traffic found") | ||
} | ||
return targetStep, nil | ||
} | ||
|
||
func hasTraffic(step v1beta1.CanaryStep) bool { | ||
if step.Traffic == nil { | ||
return false | ||
} | ||
is := intstr.FromString(*step.Traffic) | ||
trafficPercent, _ := intstr.GetScaledValueFromIntOrPercent(&is, 100, true) | ||
return trafficPercent != 0 | ||
} |
Oops, something went wrong.