Skip to content

Commit

Permalink
Merge pull request #631 from homeport/add/node-details
Browse files Browse the repository at this point in the history
Add node details in node-exec error
  • Loading branch information
HeavyWombat committed May 29, 2024
2 parents 2b99731 + 4ccf484 commit b302778
Show file tree
Hide file tree
Showing 24 changed files with 2,790 additions and 9 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/gonvenience/wrap v1.2.0
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mattn/go-isatty v0.0.20
github.com/olekukonko/tablewriter v0.0.5
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/spf13/cobra v1.8.0
Expand Down Expand Up @@ -59,6 +60,7 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/spdystream v0.2.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 h1:BXxTozrOU8zg
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3/go.mod h1:x1uk6vxTiVuNt6S5R2UYgdhpj3oKojXvOXauHZ7dEnI=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand All @@ -134,6 +136,8 @@ github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo/v2 v2.19.0 h1:9Cnnf7UHo57Hy3k6/m5k3dRfGTMXGvxhHFvkDTCTpvA=
github.com/onsi/ginkgo/v2 v2.19.0/go.mod h1:rlwLi9PilAFJ8jCg9UE1QP6VBpd6/xj3SRC0d6TU0To=
Expand Down
30 changes: 26 additions & 4 deletions internal/cmd/nexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package cmd

import (
"bytes"
"fmt"
"io"
"os"
Expand All @@ -35,6 +36,7 @@ import (

"github.com/gonvenience/bunt"
"github.com/homeport/havener/pkg/havener"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -300,13 +302,33 @@ func availableNodesError(h havener.Havener, title string, fArgs ...interface{})
return fmt.Errorf("failed to find any node in cluster")
}

names := make([]string, len(nodes))
for i, node := range nodes {
names[i] = node.Name
var buf bytes.Buffer
t := tablewriter.NewWriter(&buf)
t.SetBorder(false)
t.SetColumnSeparator("")
// t.SetAutoWrapText(false)
t.SetAlignment(tablewriter.ALIGN_LEFT)

for _, node := range nodes {
var tmp []string
for _, condition := range node.Status.Conditions {
if condition.Status == corev1.ConditionTrue {
tmp = append(tmp, string(condition.Type))
}
}

t.Append([]string{
node.Name,
strings.Join(tmp, ", "),
fmt.Sprintf("%s/%s", node.Status.NodeInfo.OperatingSystem, node.Status.NodeInfo.Architecture),
node.Status.NodeInfo.KubeletVersion,
})
}

t.Render()

return fmt.Errorf("%s: %w",
fmt.Sprintf(title, fArgs...),
bunt.Errorf("*list of available nodes:*\n%s\n\nor, use _all_ to target all nodes", strings.Join(names, "\n")),
bunt.Errorf("List of available nodes:\n%s\nAlternatively, use _all_ to target all nodes.", buf.String()),
)
}
26 changes: 21 additions & 5 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ import (
"github.com/gonvenience/wrap"
)

type wrappedError interface {
Error() string
Unwrap() error
}

func title(w wrappedError) string {
var parts = strings.SplitN(w.Error(), ":", 2)
return parts[0]
}

func cause(w wrappedError) string {
return w.Unwrap().Error()
}

var kubeConfig string

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -72,25 +86,27 @@ func Execute() {
}()

if err := rootCmd.Execute(); err != nil {
var (
headline string
content string
)
var headline, content string

switch err := err.(type) {
case wrap.ContextError:
headline = bunt.Sprintf("*Error:* _%s_", err.Context())
content = err.Cause().Error()

case wrappedError:
headline = title(err)
content = cause(err)

default:
headline = "Error occurred"
content = fmt.Sprint(err)
content = err.Error()
}

neat.Box(os.Stderr,
headline, strings.NewReader(content),
neat.HeadlineColor(bunt.Coral),
neat.ContentColor(bunt.DimGray),
neat.NoLineWrap(),
)

os.Exit(1)
Expand Down
16 changes: 16 additions & 0 deletions vendor/github.com/mattn/go-runewidth/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/mattn/go-runewidth/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/mattn/go-runewidth/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/mattn/go-runewidth/go.test.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b302778

Please sign in to comment.