Skip to content

Commit

Permalink
feat: command to print troubleshoot specs to stdout
Browse files Browse the repository at this point in the history
* Remove duplicate version.go files
  • Loading branch information
banjoh committed Aug 25, 2023
1 parent 7e9ff94 commit 8bc624a
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 59 deletions.
5 changes: 4 additions & 1 deletion cmd/analyze/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"strings"

"github.com/replicatedhq/troubleshoot/cmd/util"
"github.com/replicatedhq/troubleshoot/cmd/internal/util"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/replicatedhq/troubleshoot/pkg/logger"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -43,6 +43,9 @@ func RootCmd() *cobra.Command {

cobra.OnInitialize(initConfig)

cmd.AddCommand(util.DumpSpecCmd())
cmd.AddCommand(util.VersionCmd())

cmd.Flags().String("analyzers", "", "filename or url of the analyzers to use")
cmd.Flags().Bool("debug", false, "enable debug logging")

Expand Down
5 changes: 3 additions & 2 deletions cmd/collect/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"strings"

"github.com/replicatedhq/troubleshoot/cmd/util"
"github.com/replicatedhq/troubleshoot/cmd/internal/util"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/replicatedhq/troubleshoot/pkg/logger"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -43,7 +43,8 @@ func RootCmd() *cobra.Command {

cobra.OnInitialize(initConfig)

cmd.AddCommand(VersionCmd())
cmd.AddCommand(util.DumpSpecCmd())
cmd.AddCommand(util.VersionCmd())

cmd.Flags().StringSlice("redactors", []string{}, "names of the additional redactors to use")
cmd.Flags().Bool("redact", true, "enable/disable default redactions")
Expand Down
22 changes: 0 additions & 22 deletions cmd/collect/cli/version.go

This file was deleted.

62 changes: 62 additions & 0 deletions cmd/internal/util/dump_spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package util

import (
"context"
"fmt"

"github.com/pkg/errors"
"github.com/replicatedhq/troubleshoot/internal/specs"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/replicatedhq/troubleshoot/pkg/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/client-go/kubernetes"
)

func DumpSpecCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "dump [URI]",
Args: cobra.MinimumNArgs(0),
Short: "Print loaded specs to stdout",
RunE: func(cmd *cobra.Command, args []string) error {
return printSpecs(args)
},
}

cmd.Flags().StringSliceP("selector", "l", []string{"troubleshoot.sh/kind=support-bundle"}, "selector to filter on for loading additional support bundle specs found in secrets within the cluster")
cmd.Flags().Bool("load-cluster-specs", false, "enable/disable loading additional troubleshoot specs found within the cluster. required when no specs are provided on the command line")

// Initialize klog flags
logger.InitKlogFlags(cmd)

k8sutil.AddFlags(cmd.Flags())

return cmd
}

func printSpecs(args []string) error {
config, err := k8sutil.GetRESTConfig()
if err != nil {
return errors.Wrap(err, "failed to convert kube flags to rest config")
}

client, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Wrap(err, "failed to convert create k8s client")
}

ctx := context.Background()
kinds, err := specs.LoadFromCLIArgs(ctx, client, args, viper.GetViper())
if err != nil {
return err
}

asYaml, err := kinds.ToYaml()
if err != nil {
return err
}

fmt.Println(asYaml)

return nil
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli
package util

import (
"fmt"
Expand Down
5 changes: 3 additions & 2 deletions cmd/preflight/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"strings"

"github.com/replicatedhq/troubleshoot/cmd/util"
"github.com/replicatedhq/troubleshoot/cmd/internal/util"
"github.com/replicatedhq/troubleshoot/internal/traces"
"github.com/replicatedhq/troubleshoot/pkg/constants"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
Expand Down Expand Up @@ -64,8 +64,9 @@ that a cluster meets the requirements to run an application.`,

cobra.OnInitialize(initConfig)

cmd.AddCommand(VersionCmd())
cmd.AddCommand(util.VersionCmd())
cmd.AddCommand(OciFetchCmd())
cmd.AddCommand(util.DumpSpecCmd())
preflight.AddFlags(cmd.PersistentFlags())

k8sutil.AddFlags(cmd.Flags())
Expand Down
23 changes: 0 additions & 23 deletions cmd/preflight/cli/version.go

This file was deleted.

5 changes: 3 additions & 2 deletions cmd/troubleshoot/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"strings"

"github.com/replicatedhq/troubleshoot/cmd/util"
"github.com/replicatedhq/troubleshoot/cmd/internal/util"
"github.com/replicatedhq/troubleshoot/internal/traces"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/replicatedhq/troubleshoot/pkg/logger"
Expand Down Expand Up @@ -62,7 +62,8 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust

cmd.AddCommand(Analyze())
cmd.AddCommand(Redact())
cmd.AddCommand(VersionCmd())
cmd.AddCommand(util.DumpSpecCmd())
cmd.AddCommand(util.VersionCmd())

cmd.Flags().StringSlice("redactors", []string{}, "names of the additional redactors to use")
cmd.Flags().Bool("redact", true, "enable/disable default redactions")
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/tj/go-spin v1.1.0
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/sdk v1.16.0
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
golang.org/x/sync v0.3.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.27.4
Expand All @@ -48,6 +49,7 @@ require (
k8s.io/klog/v2 v2.100.1
oras.land/oras-go v1.2.3
sigs.k8s.io/controller-runtime v0.15.1
sigs.k8s.io/e2e-framework v0.3.0
)

require (
Expand Down Expand Up @@ -77,10 +79,8 @@ require (
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/tools v0.9.1 // indirect
sigs.k8s.io/e2e-framework v0.2.0 // indirect
)

require (
Expand Down Expand Up @@ -220,7 +220,7 @@ require (
sigs.k8s.io/kustomize/api v0.13.2 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.1 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/yaml v1.3.0
)

replace (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1492,8 +1492,8 @@ rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/controller-runtime v0.15.1 h1:9UvgKD4ZJGcj24vefUFgZFP3xej/3igL9BsOUTb/+4c=
sigs.k8s.io/controller-runtime v0.15.1/go.mod h1:7ngYvp1MLT+9GeZ+6lH3LOlcHkp/+tzA/fmHa4iq9kk=
sigs.k8s.io/e2e-framework v0.2.0 h1:gD6AWWAHFcHibI69E9TgkNFhh0mVwWtRCHy2RU057jQ=
sigs.k8s.io/e2e-framework v0.2.0/go.mod h1:E6JXj/V4PIlb95jsn2WrNKG+Shb45xaaI7C0+BH4PL8=
sigs.k8s.io/e2e-framework v0.3.0 h1:eqQALBtPCth8+ulTs6lcPK7ytV5rZSSHJzQHZph4O7U=
sigs.k8s.io/e2e-framework v0.3.0/go.mod h1:C+ef37/D90Dc7Xq1jQnNbJYscrUGpxrWog9bx2KIa+c=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/kustomize/api v0.13.2 h1:kejWfLeJhUsTGioDoFNJET5LQe/ajzXhJGYoU+pJsiA=
Expand Down
28 changes: 27 additions & 1 deletion pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package loader

import (
"context"
"reflect"
"strings"

"github.com/pkg/errors"
"github.com/replicatedhq/troubleshoot/internal/util"
Expand All @@ -10,10 +12,10 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/constants"
"github.com/replicatedhq/troubleshoot/pkg/docrewrite"
"github.com/replicatedhq/troubleshoot/pkg/types"
"gopkg.in/yaml.v2"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
"sigs.k8s.io/yaml"
)

var decoder runtime.Decoder
Expand Down Expand Up @@ -97,6 +99,30 @@ func (kinds *TroubleshootKinds) Add(other *TroubleshootKinds) {
kinds.SupportBundlesV1Beta2 = append(kinds.SupportBundlesV1Beta2, other.SupportBundlesV1Beta2...)
}

func (kinds *TroubleshootKinds) ToYaml() (string, error) {
rawList := []string{}
obj := reflect.ValueOf(*kinds)

for i := 0; i < obj.NumField(); i++ {
field := obj.Field(i)
if field.Kind() != reflect.Slice {
continue
}

// skip empty slices to avoid empty yaml documents
for count := 0; count < field.Len(); count++ {
val := field.Index(count)
yamlOut, err := yaml.Marshal(val.Interface())
if err != nil {
return "", err
}
rawList = append(rawList, string(yamlOut))
}
}

return strings.Join(rawList, "---\n"), nil
}

func NewTroubleshootKinds() *TroubleshootKinds {
return &TroubleshootKinds{}
}
Expand Down
57 changes: 57 additions & 0 deletions pkg/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,60 @@ func TestAddingKinds(t *testing.T) {
}
assert.Equal(t, k2, k1)
}

func TestToYaml(t *testing.T) {
k := &TroubleshootKinds{
AnalyzersV1Beta2: []troubleshootv1beta2.Analyzer{
{
TypeMeta: metav1.TypeMeta{
Kind: "Analyzer",
APIVersion: "troubleshoot.sh/v1beta2",
},
Spec: troubleshootv1beta2.AnalyzerSpec{
Analyzers: []*troubleshootv1beta2.Analyze{
{
ClusterVersion: &troubleshootv1beta2.ClusterVersion{
Outcomes: []*troubleshootv1beta2.Outcome{
{
Pass: &troubleshootv1beta2.SingleOutcome{
Message: "Cluster is up to date",
},
},
},
},
},
},
},
},
},
SupportBundlesV1Beta2: []troubleshootv1beta2.SupportBundle{
{
TypeMeta: metav1.TypeMeta{
Kind: "SupportBundle",
APIVersion: "troubleshoot.sh/v1beta2",
},
Spec: troubleshootv1beta2.SupportBundleSpec{
Collectors: []*troubleshootv1beta2.Collect{
{
ClusterResources: &troubleshootv1beta2.ClusterResources{
IgnoreRBAC: true,
},
},
},
},
},
},
}

y, err := k.ToYaml()
require.NoError(t, err)
assert.Contains(t, string(y), `apiVersion: troubleshoot.sh/v1beta2
kind: SupportBundle
metadata:
creationTimestamp: null
spec:
collectors:
- clusterResources:
ignoreRBAC: true`)
assert.Contains(t, string(y), "message: Cluster is up to date")
}

0 comments on commit 8bc624a

Please sign in to comment.