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: Pause/Resume Deployments #2954

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions internal/dao/dp.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ func (d *Deployment) Restart(ctx context.Context, path string) error {
return err
}

func (d *Deployment) TogglePause(ctx context.Context, path string) error {
ns, n := client.Namespaced(path)
auth, err := d.Client().CanI(ns, d.GVR(), n, []string{client.GetVerb, client.UpdateVerb})
if err != nil {
return err
}
if !auth {
return fmt.Errorf("user is not authorized to pause/resume deployments")
}

dial, err := d.Client().Dial()
if err != nil {
return err
}
dp, err := dial.AppsV1().Deployments(ns).Get(ctx, n, metav1.GetOptions{})
if err != nil {
return err
}
dp.Spec.Paused = !dp.Spec.Paused
_, err = dial.AppsV1().Deployments(ns).Update(ctx, dp, metav1.UpdateOptions{})

return err

}

// TailLogs tail logs for all pods represented by this Deployment.
func (d *Deployment) TailLogs(ctx context.Context, opts *LogOptions) ([]LogChan, error) {
dp, err := d.GetInstance(opts.Path)
Expand Down
5 changes: 5 additions & 0 deletions internal/dao/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ type ReplicasGetter interface {
Replicas(ctx context.Context, path string) (int32, error)
}

// Pausable represents resources that can be paused/resumed
type Pausable interface {
TogglePause(ctx context.Context, path string) error
}

// Controller represents a pod controller.
type Controller interface {
// Pod returns a pod instance matching the selector.
Expand Down
2 changes: 2 additions & 0 deletions internal/render/dp.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (Deployment) defaultHeader() model1.Header {
model1.HeaderColumn{Name: "READY", Attrs: model1.Attrs{Align: tview.AlignRight}},
model1.HeaderColumn{Name: "UP-TO-DATE", Attrs: model1.Attrs{Align: tview.AlignRight}},
model1.HeaderColumn{Name: "AVAILABLE", Attrs: model1.Attrs{Align: tview.AlignRight}},
model1.HeaderColumn{Name: "PAUSED", Attrs: model1.Attrs{Align: tview.AlignRight}},
model1.HeaderColumn{Name: "LABELS", Attrs: model1.Attrs{Wide: true}},
model1.HeaderColumn{Name: "VALID", Attrs: model1.Attrs{Wide: true}},
model1.HeaderColumn{Name: "AGE", Attrs: model1.Attrs{Time: true}},
Expand Down Expand Up @@ -99,6 +100,7 @@ func (d Deployment) defaultRow(raw *unstructured.Unstructured, r *model1.Row) er
strconv.Itoa(int(dp.Status.AvailableReplicas)) + "/" + strconv.Itoa(int(dp.Status.Replicas)),
strconv.Itoa(int(dp.Status.UpdatedReplicas)),
strconv.Itoa(int(dp.Status.AvailableReplicas)),
strconv.FormatBool(dp.Spec.Paused),
mapToStr(dp.Labels),
AsStatus(d.diagnose(dp.Status.Replicas, dp.Status.AvailableReplicas)),
ToAge(dp.GetCreationTimestamp()),
Expand Down
8 changes: 5 additions & 3 deletions internal/view/dp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ func NewDeploy(gvr client.GVR) ResourceViewer {
NewVulnerabilityExtender(
NewRestartExtender(
NewScaleExtender(
NewImageExtender(
NewOwnerExtender(
NewLogsExtender(NewBrowser(gvr), d.logOptions),
NewPauseExtender(
NewImageExtender(
NewOwnerExtender(
NewLogsExtender(NewBrowser(gvr), d.logOptions),
),
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion internal/view/dp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ func TestDeploy(t *testing.T) {

assert.Nil(t, v.Init(makeCtx()))
assert.Equal(t, "Deployments", v.Name())
assert.Equal(t, 16, len(v.Hints()))
assert.Equal(t, 17, len(v.Hints()))
}
153 changes: 153 additions & 0 deletions internal/view/pause_extender.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s

package view

import (
"context"
"fmt"

"github.com/derailed/k9s/internal/config"

"github.com/derailed/k9s/internal/dao"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tcell/v2"
"github.com/derailed/tview"
"github.com/rs/zerolog/log"
)

// PauseExtender adds pausing extensions.
type PauseExtender struct {
ResourceViewer
}

// NewPauseExtender returns a new extender.
func NewPauseExtender(r ResourceViewer) ResourceViewer {
p := PauseExtender{ResourceViewer: r}
p.AddBindKeysFn(p.bindKeys)

return &p
}

const (
PAUSE = "Pause"
RESUME = "Resume"
PAUSE_RESUME = "Pause/Resume"
)

func (p *PauseExtender) bindKeys(aa *ui.KeyActions) {
if p.App().Config.K9s.IsReadOnly() {
return
}

aa.Add(ui.KeyZ, ui.NewKeyActionWithOpts(PAUSE_RESUME, p.togglePauseCmd,
ui.ActionOpts{
Visible: true,
Dangerous: true,
},
))
}

func (p *PauseExtender) togglePauseCmd(evt *tcell.EventKey) *tcell.EventKey {
path := p.GetTable().GetSelectedItem()

p.Stop()
defer p.Start()

styles := p.App().Styles.Dialog()
form := p.makeStyledForm(styles)

action := PAUSE
isPaused, err := p.valueOf("PAUSED")
if err != nil {
log.Error().Err(err).Msg("Reading 'PAUSED' state failed")
p.App().Flash().Err(err)
return nil
}

if isPaused == "true" {
action = RESUME
}

form.AddButton("OK", func() {
defer p.dismissDialog()

ctx, cancel := context.WithTimeout(context.Background(), p.App().Conn().Config().CallTimeout())
defer cancel()

if err := p.togglePause(ctx, path, action); err != nil {
log.Error().Err(err).Msgf("DP %s pausing failed", path)
p.App().Flash().Err(err)
return
}

p.App().Flash().Infof("%s paused successfully", singularize(p.GVR().R()))
})

form.AddButton("Cancel", func() {
p.dismissDialog()
})
for i := 0; i < 2; i++ {
if b := form.GetButton(i); b != nil {
b.SetBackgroundColorActivated(styles.ButtonFocusBgColor.Color())
b.SetLabelColorActivated(styles.ButtonFocusFgColor.Color())
}
}

confirm := tview.NewModalForm("Pause/Resume", form)
msg := fmt.Sprintf("%s %s %s?", action, singularize(p.GVR().R()), path)

confirm.SetText(msg)
confirm.SetDoneFunc(func(int, string) {
p.dismissDialog()
})
p.App().Content.AddPage(pauseDialogKey, confirm, false, false)
p.App().Content.ShowPage(pauseDialogKey)

return nil
}

func (p *PauseExtender) togglePause(ctx context.Context, path string, action string) error {
res, err := dao.AccessorFor(p.App().factory, p.GVR())
if err != nil {
p.App().Flash().Err(err)
return nil
}
pauser, ok := res.(dao.Pausable)
if !ok {
p.App().Flash().Err(fmt.Errorf("expecting a pausable resource for %q", p.GVR()))
return nil
}

if err := pauser.TogglePause(ctx, path); err != nil {
p.App().Flash().Err(fmt.Errorf("failed to %s: %q", action, err))
}

return nil
}

func (p *PauseExtender) valueOf(col string) (string, error) {
colIdx, ok := p.GetTable().HeaderIndex(col)
if !ok {
return "", fmt.Errorf("no column index for %s", col)
}
return p.GetTable().GetSelectedCell(colIdx), nil
}

const pauseDialogKey = "pause"

func (p *PauseExtender) dismissDialog() {
p.App().Content.RemovePage(pauseDialogKey)
}

func (p *PauseExtender) makeStyledForm(styles config.Dialog) *tview.Form {
f := tview.NewForm()
f.SetItemPadding(0)
f.SetButtonsAlign(tview.AlignCenter).
SetButtonBackgroundColor(styles.ButtonBgColor.Color()).
SetButtonTextColor(styles.ButtonBgColor.Color()).
SetLabelColor(styles.LabelFgColor.Color()).
SetFieldTextColor(styles.FieldFgColor.Color())

return f
}
1 change: 1 addition & 0 deletions internal/view/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
uptodateCol = "UP-TO-DATE"
readyCol = "READY"
availCol = "AVAILABLE"
pausedCol = "PAUSED"
)

type (
Expand Down
Loading