refact ep

mine
derailed 2019-06-20 20:02:54 -06:00
parent 2c2de7b269
commit 71a0ebc3bf
3 changed files with 49 additions and 32 deletions

View File

@ -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, ","))
}
}
}

View File

@ -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 {

View File

@ -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,
}
}