From 71a0ebc3bf606553a2becc12b0ef91d17f145cb4 Mon Sep 17 00:00:00 2001 From: derailed Date: Thu, 20 Jun 2019 20:02:54 -0600 Subject: [PATCH] refact ep --- internal/resource/ep.go | 47 ++++++++++++++++++-------------- internal/resource/log_options.go | 22 ++++++++++----- internal/views/logs.go | 12 ++++---- 3 files changed, 49 insertions(+), 32 deletions(-) diff --git a/internal/resource/ep.go b/internal/resource/ep.go index fdb6607f..f4da04ec 100644 --- a/internal/resource/ep.go +++ b/internal/resource/ep.go @@ -94,28 +94,35 @@ func (r *Endpoints) Fields(ns string) Row { func (r *Endpoints) toEPs(ss []v1.EndpointSubset) string { aa := make([]string, 0, len(ss)) - max := 3 for _, s := range ss { - pp := make([]string, 0, len(s.Ports)) - for _, p := range s.Ports { - pp = append(pp, strconv.Itoa(int(p.Port))) - } - - for _, a := range s.Addresses { - if len(a.IP) == 0 { - continue - } - if len(pp) == 0 { - aa = append(aa, a.IP) - continue - } - add := a.IP + ":" + strings.Join(pp, ",") - if len(pp) > max { - add = a.IP + ":" + strings.Join(pp[:max], ",") + "..." - } - aa = append(aa, add) - } + pp := make([]string, len(s.Ports)) + portsToStrs(s.Ports, pp) + proccessIPs(aa, pp, s.Addresses) } return strings.Join(aa, ",") } + +func portsToStrs(pp []v1.EndpointPort, ss []string) { + for i := 0; i < len(pp); i++ { + ss[i] = strconv.Itoa(int(pp[i].Port)) + } +} + +func proccessIPs(aa []string, pp []string, addrs []v1.EndpointAddress) { + const maxIPs = 3 + for _, a := range addrs { + if len(a.IP) == 0 { + continue + } + if len(pp) == 0 { + aa = append(aa, a.IP) + continue + } + if len(pp) > maxIPs { + aa = append(aa, a.IP+":"+strings.Join(pp[:maxIPs], ",")+"...") + } else { + aa = append(aa, a.IP+":"+strings.Join(pp, ",")) + } + } +} diff --git a/internal/resource/log_options.go b/internal/resource/log_options.go index 19143142..556683ff 100644 --- a/internal/resource/log_options.go +++ b/internal/resource/log_options.go @@ -6,13 +6,21 @@ import ( "github.com/derailed/k9s/internal/color" ) -// LogOptions represent logger options. -type LogOptions struct { - Namespace, Name, Container string - Lines int64 - Previous bool - Color color.Paint -} +type ( + // Fqn uniquely describes a container + Fqn struct { + Namespace, Name, Container string + } + + // LogOptions represent logger options. + LogOptions struct { + Fqn + + Lines int64 + Previous bool + Color color.Paint + } +) // HasContainer checks if a container is present. func (o LogOptions) HasContainer() bool { diff --git a/internal/views/logs.go b/internal/views/logs.go index f04ecbc4..e8118edc 100644 --- a/internal/views/logs.go +++ b/internal/views/logs.go @@ -124,11 +124,13 @@ func (v *logsView) doLoad(path, co string, prevLogs bool) error { func (v *logsView) logOpts(path, co string, prevLogs bool) resource.LogOptions { ns, po := namespaced(path) return resource.LogOptions{ - Namespace: ns, - Name: po, - Container: co, - Lines: int64(v.app.config.K9s.LogRequestSize), - Previous: prevLogs, + Fqn: resource.Fqn{ + Namespace: ns, + Name: po, + Container: co, + }, + Lines: int64(v.app.config.K9s.LogRequestSize), + Previous: prevLogs, } }