Skip to content

Commit

Permalink
feat:Rollout get command Add canary strategy progress..
Browse files Browse the repository at this point in the history
Only canary Strastegy has it.

Effect display:

Strategy: Canary
Step: 6/6
Steps: ... [2]setWeight:25 -> [3]pause:30s -> [4]setWeight:50 ...
SetWeight: 100
ActualWeight: 100

Strategy: Canary
Step: 6/6
Steps: [1]setWeight:25 -> [2]pause:30s -> [3]setWeight:50 ...
SetWeight: 100
ActualWeight: 100

Strategy: Canary
Step: 1/2
Steps: [1]setWeight:50 -> [2]pause:∞
SetWeight: 50
ActualWeight: 50

Signed-off-by: zhaozhiqiang <[email protected]>
  • Loading branch information
zhaozhiqiang committed Feb 11, 2021
1 parent 1beffd4 commit 8c78561
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
64 changes: 64 additions & 0 deletions pkg/kubectl-argo-rollouts/cmd/get/get_rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (o *GetOptions) PrintRollout(roInfo *info.RolloutInfo) {
fmt.Fprintf(o.Out, tableFormat, "Strategy:", roInfo.Strategy)
if roInfo.Strategy == "Canary" {
fmt.Fprintf(o.Out, tableFormat, " Step:", roInfo.Step)
o.PrintCanarySteps(roInfo)
fmt.Fprintf(o.Out, tableFormat, " SetWeight:", roInfo.SetWeight)
fmt.Fprintf(o.Out, tableFormat, " ActualWeight:", roInfo.ActualWeight)
}
Expand Down Expand Up @@ -235,3 +236,66 @@ func (o *GetOptions) PrintJob(w io.Writer, jobInfo info.JobInfo, prefix string,
name := o.colorizeStatus(jobInfo.Name, jobInfo.Status)
fmt.Fprintf(w, "%s%s %s\t%s\t%s %s\t%s\t%v\n", prefix, IconJob, name, "Job", o.colorize(jobInfo.Icon), jobInfo.Status, jobInfo.Age(), "")
}

func (o *GetOptions) setCanarySteps(roInfo *info.RolloutInfo) ([]string, int) {
newSteps := make([]string, 0)
currentIndex := 0
for k, step := range roInfo.Steps {
stepIndex := fmt.Sprintf("[%v]", k+1)
if strings.Contains(step, "[*]") {
tempStep := strings.Replace(step, "[*]", "", -1)
newSteps = append(newSteps, fmt.Sprintf("%s%s", stepIndex, o.colorizeStatus(tempStep, info.InfoTagStable)))
currentIndex = k
} else {
newSteps = append(newSteps, fmt.Sprintf("%s%s", stepIndex, step))
}
}
return newSteps, currentIndex
}

const (
stepsMsg = " Steps:"
maxPrintStep = 3
)

func (o *GetOptions) PrintCanarySteps(roInfo *info.RolloutInfo) {
newSteps, currentIndex := o.setCanarySteps(roInfo)
maxSteps := len(newSteps) - 1
if maxSteps <= maxPrintStep {
fmt.Fprintf(o.Out, tableFormat, stepsMsg, strings.Join(newSteps, " -> "))
return
}

centre := currentIndex
if currentIndex == 0 {
if maxSteps > maxPrintStep {
fmt.Fprintf(o.Out, tableFormat, stepsMsg, strings.Join(newSteps[0:3], " -> ")+" ...")
} else {
fmt.Fprintf(o.Out, tableFormat, stepsMsg, strings.Join(newSteps[0:maxSteps+1], " -> "))
}
return
}
if currentIndex >= maxSteps {
fmt.Fprintf(o.Out, tableFormat, stepsMsg, "... "+strings.Join(newSteps[maxSteps-2:maxSteps+1], " -> "))
return
}

printSteps := make([]string, 0)
if centre+1 <= maxSteps {
if currentIndex >= 1 {
printSteps = append(printSteps, "... ")
printSteps = append(printSteps, newSteps[centre-1:centre+2]...)
printSteps = append(printSteps, " ...")
fmt.Fprintf(o.Out, tableFormat, stepsMsg, strings.Join(printSteps, " -> "))
} else {
printSteps = append(printSteps, newSteps[0:centre+3]...)
printSteps = append(printSteps, " ...")
fmt.Fprintf(o.Out, tableFormat, stepsMsg, strings.Join(printSteps, " -> "))
}
return
}
if centre+1 >= maxSteps {
fmt.Fprintf(o.Out, tableFormat, stepsMsg, "... "+strings.Join(newSteps[centre-2:maxSteps+1], " -> "))
return
}
}
5 changes: 5 additions & 0 deletions pkg/kubectl-argo-rollouts/cmd/get/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ Status: ✖ Degraded
Message: ProgressDeadlineExceeded: ReplicaSet "canary-demo-65fb5ffc84" has timed out progressing.
Strategy: Canary
Step: 0/8
Steps: [1]setWeight:20 -> [2]pause:∞ -> [3]setWeight:40 ...
SetWeight: 20
ActualWeight: 0
Images: argoproj/rollouts-demo:does-not-exist (canary)
Expand Down Expand Up @@ -310,6 +311,7 @@ Status: ✖ Degraded
Message: ProgressDeadlineExceeded: ReplicaSet "rollout-experiment-analysis-6f646bf7b7" has timed out progressing.
Strategy: Canary
Step: 1/2
Steps: [1]setWeight:25
SetWeight: 25
ActualWeight: 25
Images: argoproj/rollouts-demo:blue (stable)
Expand Down Expand Up @@ -390,6 +392,7 @@ Status: ◌ Progressing
Message: more replicas need to be updated
Strategy: Canary
Step: 0/1
Steps:
SetWeight: 0
ActualWeight: 0
Images: argoproj/rollouts-demo:blue (Σ:canary-preview)
Expand Down Expand Up @@ -439,6 +442,7 @@ Status: ✖ Degraded
Message: InvalidSpec: The Rollout "rollout-invalid" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"doesnt-match"}: `+"`selector`"+` does not match template `+"`labels`"+`
Strategy: Canary
Step:
Steps:
SetWeight: 100
ActualWeight: 100
Replicas:
Expand Down Expand Up @@ -472,6 +476,7 @@ Status: ✖ Degraded
Message: RolloutAborted: metric "web" assessed Failed due to failed (1) > failureLimit (0)
Strategy: Canary
Step: 0/2
Steps: [1]setWeight:10 -> [2]pause:∞
SetWeight: 0
ActualWeight: 0
Images: argoproj/rollouts-demo:blue (stable)
Expand Down
1 change: 1 addition & 0 deletions pkg/kubectl-argo-rollouts/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
IconBad = "✖"
IconPaused = "॥"
IconNeutral = "•"
IconAlways = "∞"
)

const (
Expand Down
35 changes: 35 additions & 0 deletions pkg/kubectl-argo-rollouts/info/rollout_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type RolloutInfo struct {
Icon string
Strategy string
Step string
Steps []string
SetWeight string
ActualWeight string

Expand Down Expand Up @@ -80,6 +81,8 @@ func NewRolloutInfo(
roInfo.ActualWeight = roInfo.SetWeight
}
}

roInfo.Steps = RolloutStepsDisplay(ro)
} else if ro.Spec.Strategy.BlueGreen != nil {
roInfo.Strategy = "BlueGreen"
}
Expand Down Expand Up @@ -326,3 +329,35 @@ func (r *RolloutInfo) AnalysisRunsByRevision(rev int) []AnalysisRunInfo {
}
return runs
}

func addSteps(stepsIndex int, index *int32, stepsData *[]string, steps v1alpha1.CanaryStep, ro *v1alpha1.Rollout) {
currentIcon := ""
if int32(stepsIndex)+1 == *index && *index <= int32(len(ro.Spec.Strategy.Canary.Steps)) {
currentIcon = "[*]"
}
if steps.SetWeight != nil {
*stepsData = append(*stepsData, fmt.Sprintf("%vsetWeight:%v", currentIcon, *steps.SetWeight))
}
if steps.Pause != nil {
pause := fmt.Sprintf("%vpause:%s", currentIcon, IconAlways)
if steps.Pause.Duration != nil {
pause = fmt.Sprintf("%vpause:%v", currentIcon, steps.Pause.Duration)
}
*stepsData = append(*stepsData, pause)
}

}

func RolloutStepsDisplay(ro *v1alpha1.Rollout) []string {
stepsData := make([]string, 0)
_, index := replicasetutil.GetCurrentCanaryStep(ro)
if index == nil {
return stepsData
}
for k, steps := range ro.Spec.Strategy.Canary.Steps {
if steps.SetWeight != nil || steps.Pause != nil {
addSteps(k, index, &stepsData, steps, ro)
}
}
return stepsData
}

0 comments on commit 8c78561

Please sign in to comment.