refactor reconcile

mine
derailed 2019-06-20 10:08:15 -06:00
parent fb765d1089
commit ddc1f74a41
2 changed files with 28 additions and 20 deletions

View File

@ -298,12 +298,13 @@ func (l *list) Reconcile(informer *wa.Informer, path *string) error {
ns = *path ns = *path
} }
var items Columnars var (
if rr, err := l.fetchFromStore(informer, ns); err == nil { items Columnars
items = rr err error
} else { )
items, err = l.resource.List(l.namespace) items, err = l.fetchFromStore(informer, ns)
if err != nil { if err != nil {
if items, err = l.resource.List(l.namespace); err != nil {
return err return err
} }
} }
@ -323,20 +324,30 @@ func (l *list) Reconcile(informer *wa.Informer, path *string) error {
dd := make(Row, len(ff)) dd := make(Row, len(ff))
kk = append(kk, i.Name()) kk = append(kk, i.Name())
if evt, ok := l.cache[i.Name()]; ok { if evt, ok := l.cache[i.Name()]; ok {
f1, f2 := evt.Fields[:len(evt.Fields)-1], ff[:len(ff)-1] a = computeDeltas(evt, ff[:len(ff)-1], dd)
a = Unchanged
if !reflect.DeepEqual(f1, f2) {
for i, f := range f1 {
if f != f2[i] {
dd[i] = f
}
}
a = watch.Modified
}
} }
l.cache[i.Name()] = newRowEvent(a, ff, dd) l.cache[i.Name()] = newRowEvent(a, ff, dd)
} }
l.ensureDeletes(kk)
return nil
}
func computeDeltas(evt *RowEvent, newRow, deltas Row) watch.EventType {
oldRow := evt.Fields[:len(evt.Fields)-1]
a := Unchanged
if !reflect.DeepEqual(oldRow, newRow) {
for i, field := range oldRow {
if field != newRow[i] {
deltas[i] = field
}
}
a = watch.Modified
}
return a
}
func (l *list) ensureDeletes(kk []string) {
// Check for deletions! // Check for deletions!
for k := range l.cache { for k := range l.cache {
var found bool var found bool
@ -350,6 +361,4 @@ func (l *list) Reconcile(informer *wa.Informer, path *string) error {
delete(l.cache, k) delete(l.cache, k)
} }
} }
return nil
} }

View File

@ -8,7 +8,6 @@ import (
"github.com/derailed/k9s/internal/config" "github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/resource" "github.com/derailed/k9s/internal/resource"
"github.com/rs/zerolog/log"
) )
var labelCmd = regexp.MustCompile(`\A\-l`) var labelCmd = regexp.MustCompile(`\A\-l`)
@ -30,13 +29,13 @@ func trimLabelSelector(s string) string {
} }
func skinTitle(fmat string, style config.Frame) string { func skinTitle(fmat string, style config.Frame) string {
log.Debug().Msgf("BG color %#v", style.Title.BgColor)
fmat = strings.Replace(fmat, "[fg:bg", "["+style.Title.FgColor+":"+style.Title.BgColor, -1) fmat = strings.Replace(fmat, "[fg:bg", "["+style.Title.FgColor+":"+style.Title.BgColor, -1)
fmat = strings.Replace(fmat, "[hilite", "["+style.Title.HighlightColor, 1) fmat = strings.Replace(fmat, "[hilite", "["+style.Title.HighlightColor, 1)
fmat = strings.Replace(fmat, "[key", "["+style.Menu.NumKeyColor, 1) fmat = strings.Replace(fmat, "[key", "["+style.Menu.NumKeyColor, 1)
fmat = strings.Replace(fmat, "[filter", "["+style.Title.FilterColor, 1) fmat = strings.Replace(fmat, "[filter", "["+style.Title.FilterColor, 1)
fmat = strings.Replace(fmat, "[count", "["+style.Title.CounterColor, 1) fmat = strings.Replace(fmat, "[count", "["+style.Title.CounterColor, 1)
fmat = strings.Replace(fmat, ":bg:", ":"+style.Title.BgColor+":", -1) fmat = strings.Replace(fmat, ":bg:", ":"+style.Title.BgColor+":", -1)
return fmat return fmat
} }