-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from replicatedhq/analyze-spec
Analyze spec
- Loading branch information
Showing
12 changed files
with
242 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/replicatedhq/troubleshoot/pkg/logger" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
var ( | ||
KubernetesConfigFlags *genericclioptions.ConfigFlags | ||
) | ||
|
||
func RootCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "analyze [url]", | ||
Args: cobra.MinimumNArgs(1), | ||
Short: "Analyze a support bundle", | ||
Long: `Run a series of analyzers on a support bundle archive`, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
viper.BindPFlags(cmd.Flags()) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
v := viper.GetViper() | ||
|
||
logger.SetQuiet(v.GetBool("quiet")) | ||
|
||
return runAnalyzers(v, args[0]) | ||
}, | ||
} | ||
|
||
cobra.OnInitialize(initConfig) | ||
|
||
cmd.Flags().String("analyzers", "", "filename or url of the analyzers to use") | ||
|
||
viper.BindPFlags(cmd.Flags()) | ||
|
||
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) | ||
|
||
KubernetesConfigFlags = genericclioptions.NewConfigFlags(false) | ||
KubernetesConfigFlags.AddFlags(cmd.Flags()) | ||
|
||
return cmd | ||
} | ||
|
||
func InitAndExecute() { | ||
if err := RootCmd().Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func initConfig() { | ||
viper.SetEnvPrefix("TROUBLESHOOT") | ||
viper.AutomaticEnv() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func runAnalyzers(v *viper.Viper, bundlePath string) error { | ||
specPath := v.GetString("analyzers") | ||
|
||
specContent := "" | ||
if !isURL(specPath) { | ||
if _, err := os.Stat(specPath); os.IsNotExist(err) { | ||
return fmt.Errorf("%s was not found", specPath) | ||
} | ||
|
||
b, err := ioutil.ReadFile(specPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
specContent = string(b) | ||
} else { | ||
req, err := http.NewRequest("GET", specPath, nil) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("User-Agent", "Replicated_Analyzer/v1beta1") | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
specContent = string(body) | ||
} | ||
|
||
analyzeResults, err := analyzer.DownloadAndAnalyze(specContent, bundlePath) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to download and analyze bundle") | ||
} | ||
|
||
for _, analyzeResult := range analyzeResults { | ||
if analyzeResult.IsPass { | ||
fmt.Printf("Pass: %s\n %s\n", analyzeResult.Title, analyzeResult.Message) | ||
} else if analyzeResult.IsWarn { | ||
fmt.Printf("Warn: %s\n %s\n", analyzeResult.Title, analyzeResult.Message) | ||
} else if analyzeResult.IsFail { | ||
fmt.Printf("Fail: %s\n %s\n", analyzeResult.Title, analyzeResult.Message) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func isURL(str string) bool { | ||
parsed, err := url.ParseRequestURI(str) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return parsed.Scheme != "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/replicatedhq/troubleshoot/cmd/analyze/cli" | ||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | ||
) | ||
|
||
func main() { | ||
cli.InitAndExecute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
apiVersion: troubleshoot.replicated.com/v1beta1 | ||
kind: Analyzer | ||
metadata: | ||
name: defaultAnalyzers | ||
spec: | ||
analyzers: | ||
- clusterVersion: | ||
outcomes: | ||
- fail: | ||
when: "< 1.13.0" | ||
message: The application requires at Kubernetes 1.13.0 or later, and recommends 1.15.0. | ||
uri: https://www.kubernetes.io | ||
- warn: | ||
when: "< 1.15.0" | ||
message: Your cluster meets the minimum version of Kubernetes, but we recommend you update to 1.15.0 or later. | ||
uri: https://kubernetes.io | ||
- pass: | ||
when: ">= 1.15.0" | ||
message: Your cluster meets the recommended and required versions of Kubernetes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.