Skip to content

Commit

Permalink
worked on logs + ansi support + bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed committed Mar 10, 2019
1 parent 0b0a723 commit f0a0c8f
Show file tree
Hide file tree
Showing 56 changed files with 296 additions and 229 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ K9s uses aliases to navigate most K8s resources.

## Known Issues

This initial drop is brittle. K9s will most likely blow up if...
This initial drop is brittle. K9s will most likely blow up...

1. You're running older versions of Kubernetes. K9s works best Kubernetes 1.10+
1. You don't have enough RBAC fu to manage your cluster
2. Your cluster does not run a metric server.
1. Your cluster does not run a metric server.

---

Expand Down
26 changes: 26 additions & 0 deletions change_logs/release_0.2.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Release v0.2.2

## Notes

Thank you to all that contributed with flushing out issues with K9s! I'll try
to mark some of these issues as fixed. But if you don't mind grab the latest
rev and see if we're happier with some of the fixes!

If you've filed an issue please help me verify and close.

Thank you so much for your support!!

---

## Change Logs

+ [Feature #98](https://github.com/derailed/k9s/issues/98) Pod view with node name.
+ [Feature #29](https://github.com/derailed/k9s/issues/29) Support ANSI colors in logs.
+ [Feature #105](https://github.com/derailed/k9s/issues/29) [Experimental] Add support for manual refresh.

---

## Resolved Bugs

+ [Issue #102](https://github.com/derailed/k9s/issues/102)
+ [Issue #104](https://github.com/derailed/k9s/issues/104)
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ require (
github.com/onsi/gomega v1.4.3 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/petergtz/pegomock v0.0.0-20181206220228-b113d17a7e81
github.com/sirupsen/logrus v1.3.0
github.com/rs/zerolog v1.12.0
github.com/sirupsen/logrus v1.3.0 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
github.com/stretchr/testify v1.2.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ github.com/prometheus/common v0.0.0-20181218105931-67670fe90761/go.mod h1:daVV7q
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/rivo/tview v0.0.0-20190213202703-b373355e9db4/go.mod h1:J4W+hErFfITUbyFAEXizpmkuxX7ZN56dopxHB4XQhMw=
github.com/rs/zerolog v1.12.0 h1:aqZ1XRadoS8IBknR5IDFvGzbHly1X9ApIqOroooQF/c=
github.com/rs/zerolog v1.12.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
Expand Down
36 changes: 24 additions & 12 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/k8s"
"github.com/derailed/k9s/internal/views"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
Expand Down Expand Up @@ -55,7 +56,7 @@ func init() {
}

func initK9s() {
log.Info("🐶 K9s starting up...")
log.Info().Msg("🐶 K9s starting up...")

// Load K9s config file...
cfg := k8s.NewConfig(k8sFlags)
Expand All @@ -64,14 +65,14 @@ func initK9s() {

// Init K8s connection...
k8s.InitConnectionOrDie(cfg)
log.Info("✅ Kubernetes connectivity")
log.Info().Msg("✅ Kubernetes connectivity")

config.Root.Save()
}

func initK9sConfig() {
if err := config.Root.Load(config.K9sConfigFile); err != nil {
log.Warnf("Unable to locate K9s config. Generating new configuration...")
log.Warn().Msg("Unable to locate K9s config. Generating new configuration...")
}
config.Root.K9s.RefreshRate = refreshRate

Expand All @@ -93,7 +94,7 @@ func initK9sConfig() {
config.Root.SetActiveNamespace(cfg.Contexts[ctx].Namespace)
}
}
log.Debugf("Active Context `%v`", ctx)
log.Debug().Msgf("Active Context `%v`", ctx)

if isSet(k8sFlags.Namespace) {
config.Root.SetActiveNamespace(*k8sFlags.Namespace)
Expand All @@ -120,16 +121,27 @@ func isSet(s *string) bool {
// Execute root command
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Panic(err)
log.Panic().Err(err)
}
}

func run(cmd *cobra.Command, args []string) {
level, err := log.ParseLevel(logLevel)
if err != nil {
level = log.DebugLevel
func parseLevel(level string) zerolog.Level {
switch level {
case "debug":
return zerolog.DebugLevel
case "warn":
return zerolog.WarnLevel
case "error":
return zerolog.ErrorLevel
case "fatal":
return zerolog.FatalLevel
default:
return zerolog.InfoLevel
}
log.SetLevel(level)
}

func run(cmd *cobra.Command, args []string) {
zerolog.SetGlobalLevel(parseLevel(logLevel))

initK9s()

Expand All @@ -140,7 +152,7 @@ func run(cmd *cobra.Command, args []string) {
clearScreen()
if err := recover(); err != nil {
app.Stop()
fmt.Println(err)
log.Error().Msgf("Boom! %#v", err)
debug.PrintStack()
}
}()
Expand Down
16 changes: 8 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"path/filepath"

"github.com/derailed/k9s/internal/resource"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -76,11 +76,11 @@ func (c *Config) FavNamespaces() []string {

// SetActiveNamespace set the active namespace in the current cluster.
func (c *Config) SetActiveNamespace(ns string) error {
log.Debugf("Setting active namespace `%s", ns)
log.Debug().Msgf("Setting active namespace `%s", ns)
if c.K9s.ActiveCluster() != nil {
return c.K9s.ActiveCluster().Namespace.SetActive(ns, c.settings)
}
log.Error("Doh! no active cluster. unable to set active namespace")
log.Error().Msg("Doh! no active cluster. unable to set active namespace")
return fmt.Errorf("no active cluster. unable to set active namespace")
}

Expand Down Expand Up @@ -117,7 +117,7 @@ func (c *Config) Load(path string) error {

// Save configuration to disk.
func (c *Config) Save() error {
log.Debugf("[Config] Saving configuration...")
log.Debug().Msg("[Config] Saving configuration...")
c.Validate()
return c.SaveFile(K9sConfigFile)
}
Expand All @@ -127,7 +127,7 @@ func (c *Config) SaveFile(path string) error {
EnsurePath(path, DefaultDirMod)
cfg, err := yaml.Marshal(c)
if err != nil {
log.Errorf("[Config] Unable to save K9s config file: %v", err)
log.Error().Msgf("[Config] Unable to save K9s config file: %v", err)
return err
}
return ioutil.WriteFile(path, cfg, 0644)
Expand All @@ -140,9 +140,9 @@ func (c *Config) Validate() {

// Dump debug...
func (c *Config) Dump(msg string) {
log.Debug(msg)
log.Debugf("Current Context: %s\n", c.K9s.CurrentCluster)
log.Debug().Msg(msg)
log.Debug().Msgf("Current Context: %s\n", c.K9s.CurrentCluster)
for k, cl := range c.K9s.Clusters {
log.Debugf("K9s cluster: %s -- %s\n", k, cl.Namespace)
log.Debug().Msgf("K9s cluster: %s -- %s\n", k, cl.Namespace)
}
}
5 changes: 5 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import (

"github.com/derailed/k9s/internal/config"
m "github.com/petergtz/pegomock"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)

func init() {
zerolog.SetGlobalLevel(zerolog.FatalLevel)
}

func TestConfigValidate(t *testing.T) {
setup(t)

Expand Down
4 changes: 2 additions & 2 deletions internal/config/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os/user"
"path/filepath"

log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -56,7 +56,7 @@ func EnsurePath(path string, mod os.FileMode) {
dir := filepath.Dir(path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err = os.Mkdir(dir, mod); err != nil {
log.Errorf("Unable to create K9s home config dir: %v", err)
log.Error().Msgf("Unable to create K9s home config dir: %v", err)
panic(err)
}
}
Expand Down
10 changes: 5 additions & 5 deletions internal/config/ns.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
)

const (
Expand All @@ -27,28 +27,28 @@ func NewNamespace() *Namespace {

// Validate a namespace is setup correctly
func (n *Namespace) Validate(ks KubeSettings) {
log.Debug("Validating favorites...", n.Active)
log.Debug().Msgf("Validating favorites... %s", n.Active)
nn, err := ks.NamespaceNames()
if err != nil {
return
}

if !n.isAllNamespace() && !InList(nn, n.Active) {
log.Debugf("[Config] Validation error active namespace resetting to `default")
log.Debug().Msg("[Config] Validation error active namespace resetting to `default")
n.Active = defaultNS
}

for _, ns := range n.Favorites {
if ns != allNS && !InList(nn, ns) {
log.Debugf("[Config] Invalid favorite found '%s' - %t", ns, n.isAllNamespace())
log.Debug().Msgf("[Config] Invalid favorite found '%s' - %t", ns, n.isAllNamespace())
n.rmFavNS(ns)
}
}
}

// SetActive set the active namespace.
func (n *Namespace) SetActive(ns string, ks KubeSettings) error {
log.Debug("Setting active ns ", ns)
log.Debug().Msgf("Setting active ns %s", ns)
n.Active = ns
n.addFavNS(ns)
return nil
Expand Down
8 changes: 4 additions & 4 deletions internal/k8s/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"

log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
restclient "k8s.io/client-go/rest"
Expand Down Expand Up @@ -239,7 +239,7 @@ func (c *Config) ConfigAccess() (clientcmd.ConfigAccess, error) {
// RawConfig fetch the current kubeconfig with no overrides.
func (c *Config) RawConfig() (clientcmdapi.Config, error) {
if c.rawConfig != nil && c.rawConfig.CurrentContext != c.currentContext {
log.Debugf("Context swith detected...")
log.Debug().Msg("Context swith detected...")
c.currentContext = c.rawConfig.CurrentContext
c.reset()
}
Expand All @@ -248,7 +248,7 @@ func (c *Config) RawConfig() (clientcmdapi.Config, error) {
if err := c.configFromFlags(); err != nil {
return clientcmdapi.Config{}, err
}
log.Debugf("Reloading RawConfig...")
log.Debug().Msg("Reloading RawConfig...")
cfg, err := c.clientConfig.RawConfig()
if err != nil {
return cfg, err
Expand All @@ -269,7 +269,7 @@ func (c *Config) RESTConfig() (*restclient.Config, error) {
if err != nil {
return c.restConfig, err
}
log.Debugf("Connecting to API Server %s", c.restConfig.Host)
log.Debug().Msgf("Connecting to API Server %s", c.restConfig.Host)
}
return c.restConfig, nil
}
Expand Down
6 changes: 5 additions & 1 deletion internal/k8s/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import (
"testing"

"github.com/derailed/k9s/internal/k8s"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func init() {
zerolog.SetGlobalLevel(zerolog.FatalLevel)
}

func TestConfigCurrentContext(t *testing.T) {
name, kubeConfig := "blee", "./assets/config"
uu := []struct {
Expand Down Expand Up @@ -78,7 +83,6 @@ func TestConfigCurrentNamespace(t *testing.T) {
for _, u := range uu {
cfg := k8s.NewConfig(u.flags)
ns, err := cfg.CurrentNamespaceName()
fmt.Println("CRap", ns, err)
assert.Equal(t, u.err, err)
assert.Equal(t, u.namespace, ns)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/k8s/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ func (*HPA) List(ns string) (Collection, error) {
// Delete a service
func (*HPA) Delete(ns, n string) error {
opts := metav1.DeleteOptions{}
return conn.dialOrDie().Autoscaling().HorizontalPodAutoscalers(ns).Delete(n, &opts)
return conn.dialOrDie().AutoscalingV2beta2().HorizontalPodAutoscalers(ns).Delete(n, &opts)
}
5 changes: 2 additions & 3 deletions internal/k8s/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
restclient "k8s.io/client-go/rest"
Expand Down Expand Up @@ -53,7 +53,7 @@ func (j *Job) Containers(ns, n string, includeInit bool) ([]string, error) {
if err != nil {
return nil, err
}
log.Debug("Containers found assoc pod", pod)
log.Debug().Msgf("Containers found assoc pod %v", pod)
return NewPod().(Loggable).Containers(ns, pod, includeInit)
}

Expand All @@ -63,7 +63,6 @@ func (j *Job) Logs(ns, n, co string, lines int64, prev bool) *restclient.Request
if err != nil {
return nil
}
log.Println("Logs found assoc pod", pod)
return NewPod().(Loggable).Logs(ns, pod, co, lines, prev)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/k8s/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package k8s
import (
"fmt"

log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -73,7 +73,7 @@ func (r *Resource) getClient() *rest.RESTClient {
crConfig.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: codecs}
crRestClient, err := rest.RESTClientFor(&crConfig)
if err != nil {
log.Fatal(err)
log.Fatal().Err(err)
}
return crRestClient
}
Expand Down
Loading

0 comments on commit f0a0c8f

Please sign in to comment.