cleaning up

mine
derailed 2020-10-28 19:10:29 -06:00
parent a3f11bab2e
commit 4045cb56a4
9 changed files with 43 additions and 94 deletions

View File

@ -1,15 +1,16 @@
package dao
import (
"github.com/stretchr/testify/require"
"testing"
"github.com/stretchr/testify/require"
)
func TestGetTemplateJsonPatch(t *testing.T) {
type args struct {
imageSpecs ImageSpecs
}
tests := map[string]struct {
uu := map[string]struct {
args args
want string
wantErr bool
@ -35,14 +36,15 @@ func TestGetTemplateJsonPatch(t *testing.T) {
wantErr: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := GetTemplateJsonPatch(tt.args.imageSpecs)
if (err != nil) != tt.wantErr {
t.Errorf("GetTemplateJsonPatch() error = %v, wantErr %v", err, tt.wantErr)
for k := range uu {
u := uu[k]
t.Run(k, func(t *testing.T) {
got, err := GetTemplateJsonPatch(u.args.imageSpecs)
if (err != nil) != u.wantErr {
t.Errorf("GetTemplateJsonPatch() error = %v, wantErr %v", err, u.wantErr)
return
}
require.JSONEq(t, tt.want, string(got), "Json strings should be equal")
require.JSONEq(t, u.want, string(got), "Json strings should be equal")
})
}
}
@ -51,7 +53,7 @@ func TestGetJsonPatch(t *testing.T) {
type args struct {
imageSpecs ImageSpecs
}
tests := map[string]struct {
uu := map[string]struct {
args args
want string
wantErr bool
@ -77,14 +79,15 @@ func TestGetJsonPatch(t *testing.T) {
wantErr: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := GetJsonPatch(tt.args.imageSpecs)
if (err != nil) != tt.wantErr {
t.Errorf("GetTemplateJsonPatch() error = %v, wantErr %v", err, tt.wantErr)
for k := range uu {
u := uu[k]
t.Run(k, func(t *testing.T) {
got, err := GetJsonPatch(u.args.imageSpecs)
if (err != nil) != u.wantErr {
t.Errorf("GetTemplateJsonPatch() error = %v, wantErr %v", err, u.wantErr)
return
}
require.JSONEq(t, tt.want, string(got), "Json strings should be equal")
require.JSONEq(t, u.want, string(got), "Json strings should be equal")
})
}
}

View File

@ -471,10 +471,11 @@ func (p *Pod) SetImages(ctx context.Context, path string, imageSpecs ImageSpecs)
if !auth {
return fmt.Errorf("user is not authorized to patch a deployment")
}
if manager, isManaged, err := p.isControlled(path); isManaged {
if err != nil {
return err
}
manager, isManaged, err := p.isControlled(path)
if err != nil {
return err
}
if isManaged {
return fmt.Errorf("Unable to set image. This pod is managed by %s. Please set the image on the controller", manager)
}
jsonPatch, err := GetJsonPatch(imageSpecs)
@ -501,7 +502,7 @@ func (p *Pod) isControlled(path string) (string, bool, error) {
return "", false, err
}
references := pod.GetObjectMeta().GetOwnerReferences()
if len(references) != 0 && *references[0].Controller == true {
if len(references) > 0 {
return fmt.Sprintf("%s/%s", references[0].Kind, references[0].Name), true, nil
}
return "", false, nil

View File

@ -350,7 +350,7 @@ func (l *Log) applyFilter(q string) ([][]byte, error) {
}
func (l *Log) fireLogBuffChanged(lines dao.LogItems) {
ll := make([][]byte, len(l.lines))
ll := make([][]byte, len(lines))
if l.filter == "" {
l.lines.Render(l.logOptions.ShowTimestamp, ll)
} else {

View File

@ -13,7 +13,6 @@ import (
"github.com/rs/zerolog/log"
"golang.org/x/text/language"
"golang.org/x/text/message"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/duration"
@ -151,10 +150,6 @@ func AsPerc(p string) string {
return "(" + p + ")"
}
func printValAndPerc(v string, p int) string {
return strconv.Itoa(p) + "% (" + v + ")"
}
// PrintPerc prints a number as percentage.
func PrintPerc(p int) string {
return strconv.Itoa(p) + "%"
@ -262,25 +257,6 @@ func mapToIfc(m interface{}) (s string) {
return
}
func toCPUPerc(cpu, acpu *resource.Quantity) string {
c, ac := cpu.MilliValue(), acpu.MilliValue()
return toMc(c) + " " + AsPerc(strconv.Itoa(client.ToPercentage(c, ac)))
}
func toMEMPerc(mem, amem *resource.Quantity) string {
m, am := mem.MilliValue(), amem.MilliValue()
return toMi(m) + " " + AsPerc(strconv.Itoa(client.ToPercentage(m, am)))
}
func toResourcesMcPerc(res resources, a v1.ResourceList) string {
cpu := a.Cpu().MilliValue()
rcpu, lcpu := toResourcesMc(res)
return rcpu +
AsPerc(strconv.Itoa(client.ToPercentage(res[requestCPU].MilliValue(), cpu))) + ":" +
lcpu +
AsPerc(strconv.Itoa(client.ToPercentage(res[limitCPU].MilliValue(), cpu)))
}
func toMcPerc(v1, v2 *resource.Quantity) string {
m := v1.MilliValue()
return toMc(m) + " (" +
@ -309,40 +285,6 @@ func toMi(v int64) string {
return p.Sprintf("%d", client.ToMB(v))
}
func toResourcesMc(res resources) (string, string) {
var v1, v2 int64
if v, ok := res[requestCPU]; ok && v != nil {
v1 = v.MilliValue()
}
if v, ok := res[limitCPU]; ok && v != nil {
v2 = v.MilliValue()
}
if v1 == 0 && v2 == 0 {
return NAValue, NAValue
}
return toMc(v1), toMc(v2)
}
func asMcStr(q *resource.Quantity) string {
if q == nil {
return ZeroValue
}
return toMc(q.MilliValue())
}
func asMiStr(q *resource.Quantity) string {
if q == nil {
return ZeroValue
}
return toMi(q.MilliValue())
}
// // ToMi returns the megabytes unit.
// func ToMi(v int64) string {
// p := message.NewPrinter(language.English)
// return p.Sprintf("%dMi", bytesToMb(v))
// }
func boolPtrToStr(b *bool) string {
if b == nil {
return "false"

View File

@ -18,9 +18,9 @@ import (
const (
requestCPU qualifiedResource = "rcpu"
requestMEM = "rmem"
limitCPU = "lcpu"
limitMEM = "lmem"
requestMEM qualifiedResource = "rmem"
limitCPU qualifiedResource = "lcpu"
limitMEM qualifiedResource = "lmem"
)
type (
@ -274,8 +274,8 @@ func newResourceList(cpu, mem *resource.Quantity) v1.ResourceList {
func podRequests(spec v1.PodSpec) v1.ResourceList {
cpu, mem := new(resource.Quantity), new(resource.Quantity)
for _, co := range spec.Containers {
rl := containerRequests(&co)
for i := range spec.Containers {
rl := containerRequests(&spec.Containers[i])
if rl.Cpu() != nil {
cpu.Add(*rl.Cpu())
}

View File

@ -127,7 +127,9 @@ func (b *Browser) Start() {
b.GetModel().AddListener(b)
b.Table.Start()
b.CmdBuff().AddListener(b)
b.GetModel().Watch(b.prepareContext())
if err := b.GetModel().Watch(b.prepareContext()); err != nil {
log.Error().Err(err).Msgf("Watcher failed for %s", b.GVR())
}
}
// Stop terminates browser updates.
@ -158,7 +160,9 @@ func (b *Browser) BufferActive(state bool, k model.BufferKind) {
if state {
return
}
b.GetModel().Refresh(b.prepareContext())
if err := b.GetModel().Refresh(b.prepareContext()); err != nil {
log.Error().Err(err).Msgf("Refresh failed for %s", b.GVR())
}
b.app.QueueUpdateDraw(func() {
b.Update(b.GetModel().Peek())
if b.GetRowCount() > 1 {

View File

@ -61,9 +61,6 @@ func defaultEnv(c *client.Config, path string, header render.Header, row render.
}
func describeResource(app *App, m ui.Tabular, gvr, path string) {
ctx := context.Background()
ctx = context.WithValue(ctx, internal.KeyFactory, app.factory)
v := NewLiveView(app, "Describe", model.NewDescribe(client.NewGVR(gvr), path))
if err := app.inject(v); err != nil {
app.Flash().Err(err)

View File

@ -13,6 +13,7 @@ import (
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tview"
"github.com/gdamore/tcell"
"github.com/rs/zerolog/log"
"github.com/sahilm/fuzzy"
)
@ -170,7 +171,9 @@ func (v *LiveView) Start() {
var ctx context.Context
ctx, v.cancel = context.WithCancel(v.defaultCtx())
v.model.Watch(ctx)
if err := v.model.Watch(ctx); err != nil {
log.Error().Err(err).Msgf("LiveView watcher failed")
}
}
func (v *LiveView) defaultCtx() context.Context {
@ -316,7 +319,7 @@ func (v *LiveView) updateTitle() {
if v.title == "" {
return
}
fmat := fmt.Sprintf(detailsTitleFmt, v.title, v.model.GetPath())
fmat := fmt.Sprintf(liveViewTitleFmt, v.title, v.model.GetPath())
buff := v.cmdBuff.GetText()
if buff == "" {

View File

@ -2,6 +2,7 @@ package view
import (
"context"
"github.com/atotto/clipboard"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/model"
@ -10,8 +11,6 @@ import (
"github.com/gdamore/tcell"
)
const loggerTitleFmt = "[fg:bg:b] %s([hilite:bg:b]%s[fg:bg:-])[fg:bg:-] "
// Logger represents a generic log viewer.
type Logger struct {
*tview.TextView
@ -170,4 +169,4 @@ func (l *Logger) cpCmd(evt *tcell.EventKey) *tcell.EventKey {
}
return nil
}
}