mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-08-08 11:59:17 +00:00
fixed version check, removed duplicate kube config, fix flags warning, fixed log of invalid config (#227)
This commit is contained in:
parent
2ae0a2400d
commit
bf27e94003
@ -2,28 +2,28 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/creasty/defaults"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/up9inc/mizu/cli/config"
|
"github.com/up9inc/mizu/cli/config"
|
||||||
|
"github.com/up9inc/mizu/cli/config/configStructs"
|
||||||
"github.com/up9inc/mizu/cli/logger"
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
"github.com/up9inc/mizu/cli/telemetry"
|
"github.com/up9inc/mizu/cli/telemetry"
|
||||||
"github.com/up9inc/mizu/cli/uiUtils"
|
"github.com/up9inc/mizu/cli/uiUtils"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
var regenerateFile bool
|
|
||||||
|
|
||||||
var configCmd = &cobra.Command{
|
var configCmd = &cobra.Command{
|
||||||
Use: "config",
|
Use: "config",
|
||||||
Short: "Generate config with default values",
|
Short: "Generate config with default values",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
go telemetry.ReportRun("config", config.Config)
|
go telemetry.ReportRun("config", config.Config.Config)
|
||||||
|
|
||||||
template, err := config.GetConfigWithDefaults()
|
template, err := config.GetConfigWithDefaults()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log.Errorf("Failed generating config with defaults %v", err)
|
logger.Log.Errorf("Failed generating config with defaults %v", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if regenerateFile {
|
if config.Config.Config.Regenerate {
|
||||||
data := []byte(template)
|
data := []byte(template)
|
||||||
if err := ioutil.WriteFile(config.GetConfigFilePath(), data, 0644); err != nil {
|
if err := ioutil.WriteFile(config.GetConfigFilePath(), data, 0644); err != nil {
|
||||||
logger.Log.Errorf("Failed writing config %v", err)
|
logger.Log.Errorf("Failed writing config %v", err)
|
||||||
@ -40,5 +40,9 @@ var configCmd = &cobra.Command{
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(configCmd)
|
rootCmd.AddCommand(configCmd)
|
||||||
configCmd.Flags().BoolVarP(®enerateFile, "regenerate", "r", false, fmt.Sprintf("Regenerate the config file with default values %s", config.GetConfigFilePath()))
|
|
||||||
|
defaultConfigConfig := configStructs.ConfigConfig{}
|
||||||
|
defaults.Set(&defaultConfigConfig)
|
||||||
|
|
||||||
|
configCmd.Flags().BoolP(configStructs.RegenerateConfigName, "r", defaultConfigConfig.Regenerate, fmt.Sprintf("Regenerate the config file with default values %s", config.GetConfigFilePath()))
|
||||||
}
|
}
|
||||||
|
@ -2,42 +2,37 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/creasty/defaults"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/up9inc/mizu/cli/config"
|
"github.com/up9inc/mizu/cli/config"
|
||||||
|
"github.com/up9inc/mizu/cli/config/configStructs"
|
||||||
|
"github.com/up9inc/mizu/cli/errormessage"
|
||||||
"github.com/up9inc/mizu/cli/kubernetes"
|
"github.com/up9inc/mizu/cli/kubernetes"
|
||||||
"github.com/up9inc/mizu/cli/logger"
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
"github.com/up9inc/mizu/cli/mizu/fsUtils"
|
"github.com/up9inc/mizu/cli/mizu/fsUtils"
|
||||||
"github.com/up9inc/mizu/cli/telemetry"
|
"github.com/up9inc/mizu/cli/telemetry"
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var filePath string
|
|
||||||
|
|
||||||
var logsCmd = &cobra.Command{
|
var logsCmd = &cobra.Command{
|
||||||
Use: "logs",
|
Use: "logs",
|
||||||
Short: "Create a zip file with logs for Github issue or troubleshoot",
|
Short: "Create a zip file with logs for Github issue or troubleshoot",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
go telemetry.ReportRun("logs", config.Config)
|
go telemetry.ReportRun("logs", config.Config.Logs)
|
||||||
|
|
||||||
kubernetesProvider, err := kubernetes.NewProvider(config.Config.View.KubeConfigPath)
|
kubernetesProvider, err := kubernetes.NewProvider(config.Config.KubeConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ctx, _ := context.WithCancel(context.Background())
|
ctx, _ := context.WithCancel(context.Background())
|
||||||
|
|
||||||
if filePath == "" {
|
if validationErr := config.Config.Logs.Validate(); validationErr != nil {
|
||||||
pwd, err := os.Getwd()
|
return errormessage.FormatError(validationErr)
|
||||||
if err != nil {
|
|
||||||
logger.Log.Errorf("Failed to get PWD, %v (try using `mizu logs -f <full path dest zip file>)`", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
filePath = path.Join(pwd, "mizu_logs.zip")
|
|
||||||
}
|
}
|
||||||
logger.Log.Debugf("Using file path %s", filePath)
|
|
||||||
|
|
||||||
if err := fsUtils.DumpLogs(kubernetesProvider, ctx, filePath); err != nil {
|
logger.Log.Debugf("Using file path %s", config.Config.Logs.FilePath())
|
||||||
logger.Log.Errorf("Failed dump logs %v", err)
|
|
||||||
|
if dumpLogsErr := fsUtils.DumpLogs(kubernetesProvider, ctx, config.Config.Logs.FilePath()); dumpLogsErr != nil {
|
||||||
|
logger.Log.Errorf("Failed dump logs %v", dumpLogsErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -46,5 +41,9 @@ var logsCmd = &cobra.Command{
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(logsCmd)
|
rootCmd.AddCommand(logsCmd)
|
||||||
logsCmd.Flags().StringVarP(&filePath, "file", "f", "", "Path for zip file (default current <pwd>\\mizu_logs.zip)")
|
|
||||||
|
defaultLogsConfig := configStructs.LogsConfig{}
|
||||||
|
defaults.Set(&defaultLogsConfig)
|
||||||
|
|
||||||
|
logsCmd.Flags().StringP(configStructs.FileLogsName, "f", defaultLogsConfig.FileStr, "Path for zip file (default current <pwd>\\mizu_logs.zip)")
|
||||||
}
|
}
|
||||||
|
@ -265,7 +265,7 @@ func cleanUpMizuResources(kubernetesProvider *kubernetes.Provider) {
|
|||||||
|
|
||||||
if config.Config.DumpLogs {
|
if config.Config.DumpLogs {
|
||||||
mizuDir := mizu.GetMizuFolderPath()
|
mizuDir := mizu.GetMizuFolderPath()
|
||||||
filePath = path.Join(mizuDir, fmt.Sprintf("mizu_logs_%s.zip", time.Now().Format("2006_01_02__15_04_05")))
|
filePath := path.Join(mizuDir, fmt.Sprintf("mizu_logs_%s.zip", time.Now().Format("2006_01_02__15_04_05")))
|
||||||
if err := fsUtils.DumpLogs(kubernetesProvider, removalCtx, filePath); err != nil {
|
if err := fsUtils.DumpLogs(kubernetesProvider, removalCtx, filePath); err != nil {
|
||||||
logger.Log.Errorf("Failed dump logs %v", err)
|
logger.Log.Errorf("Failed dump logs %v", err)
|
||||||
}
|
}
|
||||||
|
@ -25,5 +25,4 @@ func init() {
|
|||||||
defaults.Set(&defaultViewConfig)
|
defaults.Set(&defaultViewConfig)
|
||||||
|
|
||||||
viewCmd.Flags().Uint16P(configStructs.GuiPortViewName, "p", defaultViewConfig.GuiPort, "Provide a custom port for the web interface webserver")
|
viewCmd.Flags().Uint16P(configStructs.GuiPortViewName, "p", defaultViewConfig.GuiPort, "Provide a custom port for the web interface webserver")
|
||||||
viewCmd.Flags().StringP(configStructs.KubeConfigPathViewName, "k", defaultViewConfig.KubeConfigPath, "Path to kube-config file")
|
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func runMizuView() {
|
func runMizuView() {
|
||||||
kubernetesProvider, err := kubernetes.NewProvider(config.Config.View.KubeConfigPath)
|
kubernetesProvider, err := kubernetes.NewProvider(config.Config.KubeConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log.Error(err)
|
logger.Log.Error(err)
|
||||||
return
|
return
|
||||||
|
@ -51,8 +51,8 @@ func InitConfig(cmd *cobra.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := mergeConfigFile(); err != nil {
|
if err := mergeConfigFile(); err != nil {
|
||||||
return fmt.Errorf("invalid config %w\n"+
|
return fmt.Errorf("invalid config, %w\n" +
|
||||||
"you can regenerate the file using `mizu config -r` or just remove it %v", err, GetConfigFilePath())
|
"you can regenerate the file by removing it (%v) and using `mizu config -r`", err, GetConfigFilePath())
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().Visit(initFlag)
|
cmd.Flags().Visit(initFlag)
|
||||||
|
@ -16,12 +16,14 @@ type ConfigStruct struct {
|
|||||||
Fetch configStructs.FetchConfig `yaml:"fetch"`
|
Fetch configStructs.FetchConfig `yaml:"fetch"`
|
||||||
Version configStructs.VersionConfig `yaml:"version"`
|
Version configStructs.VersionConfig `yaml:"version"`
|
||||||
View configStructs.ViewConfig `yaml:"view"`
|
View configStructs.ViewConfig `yaml:"view"`
|
||||||
|
Logs configStructs.LogsConfig `yaml:"logs"`
|
||||||
|
Config configStructs.ConfigConfig `yaml:"config,omitempty"`
|
||||||
AgentImage string `yaml:"agent-image,omitempty" readonly:""`
|
AgentImage string `yaml:"agent-image,omitempty" readonly:""`
|
||||||
ImagePullPolicyStr string `yaml:"image-pull-policy" default:"Always"`
|
ImagePullPolicyStr string `yaml:"image-pull-policy" default:"Always"`
|
||||||
MizuResourcesNamespace string `yaml:"mizu-resources-namespace" default:"mizu"`
|
MizuResourcesNamespace string `yaml:"mizu-resources-namespace" default:"mizu"`
|
||||||
Telemetry bool `yaml:"telemetry" default:"true"`
|
Telemetry bool `yaml:"telemetry" default:"true"`
|
||||||
DumpLogs bool `yaml:"dump-logs" default:"false"`
|
DumpLogs bool `yaml:"dump-logs" default:"false"`
|
||||||
KubeConfigPath string `yaml:"kube-config-path" default:""`
|
KubeConfigPath string `yaml:"kube-config-path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *ConfigStruct) SetDefaults() {
|
func (config *ConfigStruct) SetDefaults() {
|
||||||
|
9
cli/config/configStructs/configConfig.go
Normal file
9
cli/config/configStructs/configConfig.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package configStructs
|
||||||
|
|
||||||
|
const (
|
||||||
|
RegenerateConfigName = "regenerate"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ConfigConfig struct {
|
||||||
|
Regenerate bool `yaml:"regenerate,omitempty" default:"false" readonly:""`
|
||||||
|
}
|
35
cli/config/configStructs/logsConfig.go
Normal file
35
cli/config/configStructs/logsConfig.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package configStructs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
FileLogsName = "file"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LogsConfig struct {
|
||||||
|
FileStr string `yaml:"file"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *LogsConfig) Validate() error {
|
||||||
|
if config.FileStr == "" {
|
||||||
|
_, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get PWD, %v (try using `mizu logs -f <full path dest zip file>)`", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *LogsConfig) FilePath() string {
|
||||||
|
if config.FileStr == "" {
|
||||||
|
pwd, _ := os.Getwd()
|
||||||
|
return path.Join(pwd, "mizu_logs.zip")
|
||||||
|
}
|
||||||
|
|
||||||
|
return config.FileStr
|
||||||
|
}
|
@ -1,11 +1,9 @@
|
|||||||
package configStructs
|
package configStructs
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GuiPortViewName = "gui-port"
|
GuiPortViewName = "gui-port"
|
||||||
KubeConfigPathViewName = "kube-config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ViewConfig struct {
|
type ViewConfig struct {
|
||||||
GuiPort uint16 `yaml:"gui-port" default:"8899"`
|
GuiPort uint16 `yaml:"gui-port" default:"8899"`
|
||||||
KubeConfigPath string `yaml:"kube-config"`
|
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,10 @@ func CheckNewerVersion() {
|
|||||||
gitHubVersion := string(data)
|
gitHubVersion := string(data)
|
||||||
gitHubVersion = gitHubVersion[:len(gitHubVersion)-1]
|
gitHubVersion = gitHubVersion[:len(gitHubVersion)-1]
|
||||||
logger.Log.Debugf("Finished version validation, took %v", time.Since(start))
|
logger.Log.Debugf("Finished version validation, took %v", time.Since(start))
|
||||||
if mizu.SemVer < gitHubVersion {
|
|
||||||
|
gitHubVersionSemVer := semver.SemVersion(gitHubVersion)
|
||||||
|
currentSemVer := semver.SemVersion(mizu.SemVer)
|
||||||
|
if gitHubVersionSemVer.GreaterThan(currentSemVer) {
|
||||||
logger.Log.Infof(uiUtils.Yellow, fmt.Sprintf("Update available! %v -> %v (%v)", mizu.SemVer, gitHubVersion, *latestRelease.HTMLURL))
|
logger.Log.Infof(uiUtils.Yellow, fmt.Sprintf("Update available! %v -> %v (%v)", mizu.SemVer, gitHubVersion, *latestRelease.HTMLURL))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,3 +26,23 @@ func (v SemVersion) Patch() string {
|
|||||||
_, _, patch := v.Breakdown()
|
_, _, patch := v.Breakdown()
|
||||||
return patch
|
return patch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v SemVersion) GreaterThan(v2 SemVersion) bool {
|
||||||
|
if v.Major() > v2.Major() {
|
||||||
|
return true
|
||||||
|
} else if v.Major() < v2.Major() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.Minor() > v2.Minor() {
|
||||||
|
return true
|
||||||
|
} else if v.Minor() < v2.Minor() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.Patch() > v2.Patch() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user