Skip to content

Commit

Permalink
feat(must gather): better handling of auth errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Aug 22, 2022
1 parent 59a73a5 commit 81e9300
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 141 deletions.
2 changes: 2 additions & 0 deletions rhoc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
Expand Down
3 changes: 3 additions & 0 deletions rhoc/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,14 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
Expand Down
95 changes: 23 additions & 72 deletions rhoc/pkg/cmd/must_gather/must_gather.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
package must_gather

import (
"bufio"
"errors"
"fmt"
"io"
"os"

"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/cmdutil"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/kubernetes/pods"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/collections"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/kubernetes"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/kubernetes/resources"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
)

Expand All @@ -30,6 +23,7 @@ type options struct {
file string
resources []string
gvrs []schema.GroupVersionResource
o *cmdutil.OutputWriter
f *factory.Factory
}

Expand All @@ -51,22 +45,13 @@ func NewMustGatherCommand(f *factory.Factory) *cobra.Command {
if err != nil {
return err
}
client, _ := kubernetes.NewForConfig(config)
if err != nil {
return err
}
dynamicClient, err := dynamic.NewForConfig(config)
client, err := kubernetes.NewClient(f.Context, config)
if err != nil {
return err
}

if opts.resources == nil {
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return err
}

opts.gvrs, err = resources.Discover(discoveryClient)
opts.gvrs, err = client.ServerResources()
if err != nil {
return err
}
Expand All @@ -77,43 +62,34 @@ func NewMustGatherCommand(f *factory.Factory) *cobra.Command {
}
}

for i := range opts.gvrs {
// exclude secrets
if opts.gvrs[i].Group == "" && opts.gvrs[i].Version == "v1" && opts.gvrs[i].Resource == "secrets" {
opts.gvrs[i].Group = ""
opts.gvrs[i].Version = ""
opts.gvrs[i].Resource = ""
opts.gvrs = collections.Filter(opts.gvrs, func(gvr schema.GroupVersionResource) bool {
if gvr.Group == "" && gvr.Version == "v1" && gvr.Resource == "secrets" {
return false
}
}

var o io.Writer
var mustClose bool
return true
})

if opts.file == "" {
o = f.IOStreams.Out
mustClose = false
opts.o, err = cmdutil.NewOutputWriter(f.IOStreams.Out)
if err != nil {
return err
}
} else {
f, err := os.Create(opts.file)
opts.o, err = cmdutil.NewOutputFileWriter(opts.file)
if err != nil {
return err
}

o = f
mustClose = true
}

out := bufio.NewWriter(o)
defer func() {
_ = out.Flush()

if mustClose {
if c, ok := o.(io.Closer); ok {
_ = c.Close()
}
}
_ = opts.o.Close()
}()

items, err := resources.List(f.Context, dynamicClient, opts.gvrs, opts.id)
items, err := client.List(opts.gvrs, metav1.ListOptions{
LabelSelector: "cos.bf2.org/connector.id=" + opts.id,
})

if err != nil {
return err
}
Expand All @@ -124,7 +100,7 @@ func NewMustGatherCommand(f *factory.Factory) *cobra.Command {
return err
}

_, err = out.Write(raw)
_, err = opts.o.Write(raw)
if err != nil {
return err
}
Expand All @@ -133,32 +109,7 @@ func NewMustGatherCommand(f *factory.Factory) *cobra.Command {
if opts.logs {
for _, item := range items {
if item.GetAPIVersion() == "v1" && item.GetKind() == "Pod" {
containers, err := pods.ListContainers(f.Context, client, item.GetNamespace(), item.GetName())
if err != nil {
return err
}

for _, container := range containers {
_, err = fmt.Fprintf(
out,
"%s/%s:%s:%s@%s\n",
item.GetAPIVersion(),
item.GetKind(),
item.GetNamespace(),
item.GetName(),
container)

if err != nil {
return err
}

err := pods.Logs(f.Context, client, item.GetNamespace(), item.GetName(), container, out)
if err != nil && !errors.Is(err, io.EOF) {
return err
}
}

_, err = out.Write([]byte{'\n'})
err = client.Logs(item.GetNamespace(), item.GetName(), opts.o)
if err != nil {
return err
}
Expand Down
61 changes: 61 additions & 0 deletions rhoc/pkg/util/cmdutil/cmdutil_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmdutil

import (
"bufio"
"github.com/hashicorp/go-multierror"
"io"
"os"
)

type OutputWriter struct {
delegate io.Writer
writer *bufio.Writer
mustClose bool
}

func NewOutputWriter(delegate io.Writer) (*OutputWriter, error) {
answer := OutputWriter{
delegate: delegate,
writer: bufio.NewWriter(delegate),
mustClose: false,
}

return &answer, nil
}

func NewOutputFileWriter(path string) (*OutputWriter, error) {
delegate, err := os.Create(path)
if err != nil {
return nil, err
}

answer := OutputWriter{
delegate: delegate,
writer: bufio.NewWriter(delegate),
mustClose: true,
}

return &answer, nil
}

func (in *OutputWriter) Write(p []byte) (n int, err error) {
return in.writer.Write(p)
}

func (in *OutputWriter) Close() error {
var err error

if ferr := in.writer.Flush(); err != nil {
err = multierror.Append(err, ferr)
}

if in.mustClose {
if c, ok := in.delegate.(io.Closer); ok {
if cerr := c.Close(); cerr != nil {
err = multierror.Append(err, cerr)
}
}
}

return err
}
12 changes: 12 additions & 0 deletions rhoc/pkg/util/collections/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ func Contains[T comparable](elems []T, v T) bool {
}
return false
}

func Filter[T comparable](elems []T, predicate func(T) bool) []T {
answer := make([]T, len(elems))

for i := range elems {
if predicate(elems[i]) {
answer = append(answer, elems[i])
}
}

return answer
}
Loading

0 comments on commit 81e9300

Please sign in to comment.