Skip to content

Commit

Permalink
Allow specifying output file name. (#148)
Browse files Browse the repository at this point in the history
* Allow specifying output file name for Eck-diagnostics output file.

Signed-off-by: Michael Montgomery <[email protected]>
  • Loading branch information
naemono authored Feb 15, 2023
1 parent 557d349 commit 9e85883
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
55 changes: 51 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"time"

"github.com/elastic/eck-diagnostics/internal"
internal_filters "github.com/elastic/eck-diagnostics/internal/filters"
"github.com/spf13/cobra"
)

const (
operatorNamespaces = "operator-namespaces"
resourcesNamespaces = "resources-namespaces"
)

var (
filters []string
diagParams = internal.Params{}
Expand All @@ -25,7 +31,7 @@ func main() {
Use: "eck-diagnostics",
Short: "ECK support diagnostics tool",
Long: "Dump ECK and Kubernetes data for support and troubleshooting purposes.",
PreRunE: parseFilters,
PreRunE: preRunOperations,
Version: internal.Version(),
RunE: func(cmd *cobra.Command, args []string) error {
return internal.Run(diagParams)
Expand All @@ -34,16 +40,17 @@ func main() {
cmd.Flags().StringVar(&diagParams.DiagnosticImage, "diagnostic-image", internal.DiagnosticImage, "Diagnostic image to be used for stack diagnostics, see run-stack-diagnostics")
cmd.Flags().BoolVar(&diagParams.RunStackDiagnostics, "run-stack-diagnostics", true, "Run diagnostics on deployed Elasticsearch clusters and Kibana instances, requires deploying diagnostic Pods into the cluster")
cmd.Flags().BoolVar(&diagParams.RunAgentDiagnostics, "run-agent-diagnostics", false, "Run diagnostics on deployed Elastic Agents. Warning: credentials will not be redacted and appear as plain text in the archive")
cmd.Flags().StringSliceVarP(&diagParams.OperatorNamespaces, "operator-namespaces", "o", []string{"elastic-system"}, "Comma-separated list of namespace(s) in which operator(s) are running")
cmd.Flags().StringSliceVarP(&diagParams.ResourcesNamespaces, "resources-namespaces", "r", nil, "Comma-separated list of namespace(s) in which resources are managed")
cmd.Flags().StringSliceVarP(&diagParams.OperatorNamespaces, operatorNamespaces, "o", []string{"elastic-system"}, "Comma-separated list of namespace(s) in which operator(s) are running")
cmd.Flags().StringSliceVarP(&diagParams.ResourcesNamespaces, resourcesNamespaces, "r", nil, "Comma-separated list of namespace(s) in which resources are managed")
cmd.Flags().StringSliceVarP(&filters, "filters", "f", nil, fmt.Sprintf(`Comma-separated list of filters in format "type=name". ex: elasticsearch=my-cluster (Supported types %v)`, internal_filters.ValidTypes))
cmd.Flags().StringVar(&diagParams.ECKVersion, "eck-version", "", "ECK version in use, will try to autodetect if not specified")
cmd.Flags().StringVar(&diagParams.OutputDir, "output-directory", "", "Path where to output diagnostic results")
cmd.Flags().StringVarP(&diagParams.OutputName, "output-name", "n", fmt.Sprintf("eck-diagnostics-%s.zip", time.Now().Format("2006-01-02T15-04-05")), "Name of the output diagnostics file")
cmd.Flags().StringVar(&diagParams.Kubeconfig, "kubeconfig", "", "optional path to kube config, defaults to $HOME/.kube/config")
cmd.Flags().BoolVar(&diagParams.Verbose, "verbose", false, "Verbose mode")
cmd.Flags().DurationVar(&diagParams.StackDiagnosticsTimeout, "stack-diagnostics-timeout", 5*time.Minute, "Maximum time to wait for Elaticsearch and Kibana diagnostics to complete")

if err := cmd.MarkFlagRequired("resources-namespaces"); err != nil {
if err := cmd.MarkFlagRequired(resourcesNamespaces); err != nil {
exitWithError(err)
}

Expand All @@ -53,6 +60,46 @@ func main() {
}
}

func preRunOperations(cmd *cobra.Command, args []string) error {
if err := validation(cmd, args); err != nil {
return err
}
return parseFilters(cmd, args)
}

func validation(_ *cobra.Command, _ []string) error {
if diagParams.OutputName == "" {
return fmt.Errorf("output-name cannot be empty")
}
if filepath.Ext(diagParams.OutputName) != ".zip" {
return fmt.Errorf("output-name extension must end in '.zip'")
}

type validations struct {
namespaces []string
name string
}
for _, v := range []validations{
{
namespaces: diagParams.OperatorNamespaces,
name: operatorNamespaces,
}, {
namespaces: diagParams.ResourcesNamespaces,
name: resourcesNamespaces,
},
} {
if len(v.namespaces) == 0 {
return fmt.Errorf("%s is a required parameter", v.name)
}
for _, ns := range v.namespaces {
if ns == "" {
return fmt.Errorf("%s cannot be an empty string", v.name)
}
}
}
return nil
}

func parseFilters(_ *cobra.Command, _ []string) error {
filters, err := internal_filters.New(filters)
if err != nil {
Expand Down
13 changes: 2 additions & 11 deletions internal/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package internal
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/signal"
Expand Down Expand Up @@ -35,6 +34,7 @@ type Params struct {
OperatorNamespaces []string
ResourcesNamespaces []string
OutputDir string
OutputName string
RunStackDiagnostics bool
RunAgentDiagnostics bool
Verbose bool
Expand Down Expand Up @@ -77,7 +77,7 @@ func Run(params Params) error {
return err
}

zipFileName := diagnosticFilename(params.OutputDir)
zipFileName := filepath.Join(params.OutputDir, params.OutputName)
zipFile, err := archive.NewZipFile(zipFileName, about().Version, logger)
if err != nil {
return err
Expand Down Expand Up @@ -266,12 +266,3 @@ func getResources(f func(string, string, filters.Filters, io.Writer) error, ns s
}
return m
}

// diagnosticFilename calculates a file name to be used for the diagnostic archive based on the current time.
func diagnosticFilename(dir string) string {
file := fmt.Sprintf("eck-diagnostic-%s.zip", time.Now().Format("2006-01-02T15-04-05"))
if dir != "" {
file = filepath.Join(dir, file)
}
return file
}
1 change: 0 additions & 1 deletion internal/filters/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ func TestNew(t *testing.T) {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() = diff: %s", cmp.Diff(got, tt.want))
}

})
}
}
Expand Down

0 comments on commit 9e85883

Please sign in to comment.