k9s/internal/resource/context.go

81 lines
1.7 KiB
Go

package resource
import (
"github.com/derailed/k9s/internal/k8s"
"github.com/rs/zerolog/log"
)
// SwitchableRes represents a resource that can be switched.
type SwitchableRes interface {
k8s.Connection
k8s.ContextRes
}
// Context tracks a kubernetes resource.
type Context struct {
*Base
instance *k8s.NamedContext
}
// NewContextList returns a new resource list.
func NewContextList(c k8s.Connection, ns string) List {
return newList(NotNamespaced, "ctx", NewContext(c), SwitchAccess)
}
// NewContext instantiates a new Context.
func NewContext(c k8s.Connection) *Context {
ctx := &Context{&Base{connection: c, resource: k8s.NewContext(c)}, nil}
ctx.Factory = ctx
return ctx
}
// New builds a new Context instance from a k8s resource.
func (r *Context) New(i interface{}) Columnar {
c := NewContext(r.connection)
switch instance := i.(type) {
case *k8s.NamedContext:
c.instance = instance
case k8s.NamedContext:
c.instance = &instance
default:
log.Fatal().Msgf("unknown context type %#v", i)
}
c.path = c.instance.Name
return c
}
// Switch out current context.
func (r *Context) Switch(c string) error {
return r.resource.(k8s.ContextRes).Switch(c)
}
// Marshal the resource to yaml.
func (r *Context) Marshal(path string) (string, error) {
return "", nil
}
// Header return resource header.
func (*Context) Header(string) Row {
return append(Row{}, "NAME", "CLUSTER", "AUTHINFO", "NAMESPACE")
}
// Fields retrieves displayable fields.
func (r *Context) Fields(ns string) Row {
ff := make(Row, 0, len(r.Header(ns)))
i := r.instance
name := i.Name
if i.MustCurrentContextName() == name {
name += "*"
}
return append(ff,
name,
i.Context.Cluster,
i.Context.AuthInfo,
i.Context.Namespace,
)
}