mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-08-12 13:53:30 +00:00
Cli pkg refactor (2) (#200)
This commit is contained in:
parent
56dc6843e0
commit
7b73004e85
@ -3,7 +3,8 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/config"
|
||||||
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
"github.com/up9inc/mizu/cli/uiUtils"
|
"github.com/up9inc/mizu/cli/uiUtils"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
@ -14,20 +15,20 @@ 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 {
|
||||||
template, err := mizu.GetConfigWithDefaults()
|
template, err := config.GetConfigWithDefaults()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mizu.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 regenerateFile {
|
||||||
data := []byte(template)
|
data := []byte(template)
|
||||||
if err := ioutil.WriteFile(mizu.GetConfigFilePath(), data, 0644); err != nil {
|
if err := ioutil.WriteFile(config.GetConfigFilePath(), data, 0644); err != nil {
|
||||||
mizu.Log.Errorf("Failed writing config %v", err)
|
logger.Log.Errorf("Failed writing config %v", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
mizu.Log.Infof(fmt.Sprintf("Template File written to %s", fmt.Sprintf(uiUtils.Purple, mizu.GetConfigFilePath())))
|
logger.Log.Infof(fmt.Sprintf("Template File written to %s", fmt.Sprintf(uiUtils.Purple, config.GetConfigFilePath())))
|
||||||
} else {
|
} else {
|
||||||
mizu.Log.Debugf("Writing template config.\n%v", template)
|
logger.Log.Debugf("Writing template config.\n%v", template)
|
||||||
fmt.Printf("%v", template)
|
fmt.Printf("%v", template)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -36,5 +37,5 @@ 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", mizu.GetConfigFilePath()))
|
configCmd.Flags().BoolVarP(®enerateFile, "regenerate", "r", false, fmt.Sprintf("Regenerate the config file with default values %s", config.GetConfigFilePath()))
|
||||||
}
|
}
|
||||||
|
@ -3,16 +3,18 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"github.com/creasty/defaults"
|
"github.com/creasty/defaults"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/config"
|
||||||
"github.com/up9inc/mizu/cli/mizu/configStructs"
|
"github.com/up9inc/mizu/cli/config/configStructs"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu/version"
|
||||||
|
"github.com/up9inc/mizu/cli/telemetry"
|
||||||
)
|
)
|
||||||
|
|
||||||
var fetchCmd = &cobra.Command{
|
var fetchCmd = &cobra.Command{
|
||||||
Use: "fetch",
|
Use: "fetch",
|
||||||
Short: "Download recorded traffic to files",
|
Short: "Download recorded traffic to files",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
go mizu.ReportRun("fetch", mizu.Config.Fetch)
|
go telemetry.ReportRun("fetch", config.Config.Fetch)
|
||||||
if isCompatible, err := mizu.CheckVersionCompatibility(mizu.Config.Fetch.GuiPort); err != nil {
|
if isCompatible, err := version.CheckVersionCompatibility(config.Config.Fetch.GuiPort); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if !isCompatible {
|
} else if !isCompatible {
|
||||||
return nil
|
return nil
|
||||||
|
@ -4,8 +4,9 @@ import (
|
|||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/up9inc/mizu/cli/config"
|
||||||
"github.com/up9inc/mizu/cli/kubernetes"
|
"github.com/up9inc/mizu/cli/kubernetes"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
@ -16,8 +17,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func RunMizuFetch() {
|
func RunMizuFetch() {
|
||||||
mizuProxiedUrl := kubernetes.GetMizuApiServerProxiedHostAndPath(mizu.Config.Fetch.GuiPort)
|
mizuProxiedUrl := kubernetes.GetMizuApiServerProxiedHostAndPath(config.Config.Fetch.GuiPort)
|
||||||
resp, err := http.Get(fmt.Sprintf("http://%s/api/har?from=%v&to=%v", mizuProxiedUrl, mizu.Config.Fetch.FromTimestamp, mizu.Config.Fetch.ToTimestamp))
|
resp, err := http.Get(fmt.Sprintf("http://%s/api/har?from=%v&to=%v", mizuProxiedUrl, config.Config.Fetch.FromTimestamp, config.Config.Fetch.ToTimestamp))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -34,7 +35,7 @@ func RunMizuFetch() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = Unzip(zipReader, mizu.Config.Fetch.Directory)
|
_ = Unzip(zipReader, config.Config.Fetch.Directory)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Unzip(reader *zip.Reader, dest string) error {
|
func Unzip(reader *zip.Reader, dest string) error {
|
||||||
@ -64,7 +65,7 @@ func Unzip(reader *zip.Reader, dest string) error {
|
|||||||
_ = os.MkdirAll(path, f.Mode())
|
_ = os.MkdirAll(path, f.Mode())
|
||||||
} else {
|
} else {
|
||||||
_ = os.MkdirAll(filepath.Dir(path), f.Mode())
|
_ = os.MkdirAll(filepath.Dir(path), f.Mode())
|
||||||
mizu.Log.Infof("writing HAR file [ %v ]", path)
|
logger.Log.Infof("writing HAR file [ %v ]", path)
|
||||||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -73,7 +74,7 @@ func Unzip(reader *zip.Reader, dest string) error {
|
|||||||
if err := f.Close(); err != nil {
|
if err := f.Close(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
mizu.Log.Info(" done")
|
logger.Log.Info(" done")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
_, err = io.Copy(f, rc)
|
_, err = io.Copy(f, rc)
|
||||||
|
@ -3,9 +3,10 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/up9inc/mizu/cli/fsUtils"
|
"github.com/up9inc/mizu/cli/config"
|
||||||
"github.com/up9inc/mizu/cli/kubernetes"
|
"github.com/up9inc/mizu/cli/kubernetes"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu/fsUtils"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
@ -16,7 +17,7 @@ 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 {
|
||||||
kubernetesProvider, err := kubernetes.NewProvider(mizu.Config.View.KubeConfigPath)
|
kubernetesProvider, err := kubernetes.NewProvider(config.Config.View.KubeConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -25,15 +26,15 @@ var logsCmd = &cobra.Command{
|
|||||||
if filePath == "" {
|
if filePath == "" {
|
||||||
pwd, err := os.Getwd()
|
pwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mizu.Log.Errorf("Failed to get PWD, %v (try using `mizu logs -f <full path dest zip file>)`", err)
|
logger.Log.Errorf("Failed to get PWD, %v (try using `mizu logs -f <full path dest zip file>)`", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
filePath = path.Join(pwd, "mizu_logs.zip")
|
filePath = path.Join(pwd, "mizu_logs.zip")
|
||||||
}
|
}
|
||||||
mizu.Log.Debugf("Using file path %s", filePath)
|
logger.Log.Debugf("Using file path %s", filePath)
|
||||||
|
|
||||||
if err := fsUtils.DumpLogs(kubernetesProvider, ctx, filePath); err != nil {
|
if err := fsUtils.DumpLogs(kubernetesProvider, ctx, filePath); err != nil {
|
||||||
mizu.Log.Errorf("Failed dump logs %v", err)
|
logger.Log.Errorf("Failed dump logs %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -3,8 +3,10 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/up9inc/mizu/cli/fsUtils"
|
"github.com/up9inc/mizu/cli/config"
|
||||||
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/mizu"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu/fsUtils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
@ -14,11 +16,11 @@ var rootCmd = &cobra.Command{
|
|||||||
Further info is available at https://github.com/up9inc/mizu`,
|
Further info is available at https://github.com/up9inc/mizu`,
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if err := fsUtils.EnsureDir(mizu.GetMizuFolderPath()); err != nil {
|
if err := fsUtils.EnsureDir(mizu.GetMizuFolderPath()); err != nil {
|
||||||
mizu.Log.Errorf("Failed to use mizu folder, %v", err)
|
logger.Log.Errorf("Failed to use mizu folder, %v", err)
|
||||||
}
|
}
|
||||||
mizu.InitLogger()
|
logger.InitLogger()
|
||||||
if err := mizu.InitConfig(cmd); err != nil {
|
if err := config.InitConfig(cmd); err != nil {
|
||||||
mizu.Log.Fatal(err)
|
logger.Log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -26,7 +28,7 @@ Further info is available at https://github.com/up9inc/mizu`,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.PersistentFlags().StringSlice(mizu.SetCommandName, []string{}, fmt.Sprintf("Override values using --%s", mizu.SetCommandName))
|
rootCmd.PersistentFlags().StringSlice(config.SetCommandName, []string{}, fmt.Sprintf("Override values using --%s", config.SetCommandName))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
|
@ -2,13 +2,15 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/up9inc/mizu/cli/config"
|
||||||
|
"github.com/up9inc/mizu/cli/config/configStructs"
|
||||||
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
|
"github.com/up9inc/mizu/cli/telemetry"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/creasty/defaults"
|
"github.com/creasty/defaults"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/up9inc/mizu/cli/errormessage"
|
"github.com/up9inc/mizu/cli/errormessage"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
|
||||||
"github.com/up9inc/mizu/cli/mizu/configStructs"
|
|
||||||
"github.com/up9inc/mizu/cli/uiUtils"
|
"github.com/up9inc/mizu/cli/uiUtils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,31 +22,31 @@ var tapCmd = &cobra.Command{
|
|||||||
Long: `Record the ingoing traffic of a kubernetes pod.
|
Long: `Record the ingoing traffic of a kubernetes pod.
|
||||||
Supported protocols are HTTP and gRPC.`,
|
Supported protocols are HTTP and gRPC.`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
go mizu.ReportRun("tap", mizu.Config.Tap)
|
go telemetry.ReportRun("tap", config.Config.Tap)
|
||||||
RunMizuTap()
|
RunMizuTap()
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
mizu.Config.Tap.PodRegexStr = args[0]
|
config.Config.Tap.PodRegexStr = args[0]
|
||||||
} else if len(args) > 1 {
|
} else if len(args) > 1 {
|
||||||
return errors.New("unexpected number of arguments")
|
return errors.New("unexpected number of arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := mizu.Config.Validate(); err != nil {
|
if err := config.Config.Validate(); err != nil {
|
||||||
return errormessage.FormatError(err)
|
return errormessage.FormatError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := mizu.Config.Tap.Validate(); err != nil {
|
if err := config.Config.Tap.Validate(); err != nil {
|
||||||
return errormessage.FormatError(err)
|
return errormessage.FormatError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mizu.Log.Infof("Mizu will store up to %s of traffic, old traffic will be cleared once the limit is reached.", mizu.Config.Tap.HumanMaxEntriesDBSize)
|
logger.Log.Infof("Mizu will store up to %s of traffic, old traffic will be cleared once the limit is reached.", config.Config.Tap.HumanMaxEntriesDBSize)
|
||||||
|
|
||||||
if mizu.Config.Tap.Analysis {
|
if config.Config.Tap.Analysis {
|
||||||
mizu.Log.Infof(analysisMessageToConfirm)
|
logger.Log.Infof(analysisMessageToConfirm)
|
||||||
if !uiUtils.AskForConfirmation("Would you like to proceed [Y/n]: ") {
|
if !uiUtils.AskForConfirmation("Would you like to proceed [Y/n]: ") {
|
||||||
mizu.Log.Infof("You can always run mizu without analysis, aborting")
|
logger.Log.Infof("You can always run mizu without analysis, aborting")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/up9inc/mizu/cli/fsUtils"
|
"github.com/up9inc/mizu/cli/config"
|
||||||
"github.com/up9inc/mizu/cli/goUtils"
|
"github.com/up9inc/mizu/cli/config/configStructs"
|
||||||
"github.com/up9inc/mizu/cli/mizu/configStructs"
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu/fsUtils"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu/goUtils"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu/version"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@ -46,21 +49,21 @@ var state tapState
|
|||||||
func RunMizuTap() {
|
func RunMizuTap() {
|
||||||
mizuApiFilteringOptions, err := getMizuApiFilteringOptions()
|
mizuApiFilteringOptions, err := getMizuApiFilteringOptions()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error parsing regex-masking: %v", errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error parsing regex-masking: %v", errormessage.FormatError(err)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var mizuValidationRules string
|
var mizuValidationRules string
|
||||||
if mizu.Config.Tap.EnforcePolicyFile != "" {
|
if config.Config.Tap.EnforcePolicyFile != "" {
|
||||||
mizuValidationRules, err = readValidationRules(mizu.Config.Tap.EnforcePolicyFile)
|
mizuValidationRules, err = readValidationRules(config.Config.Tap.EnforcePolicyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error reading policy file: %v", errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error reading policy file: %v", errormessage.FormatError(err)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
kubernetesProvider, err := kubernetes.NewProvider(mizu.Config.KubeConfigPath)
|
kubernetesProvider, err := kubernetes.NewProvider(config.Config.KubeConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mizu.Log.Error(err)
|
logger.Log.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,11 +78,11 @@ func RunMizuTap() {
|
|||||||
} else {
|
} else {
|
||||||
namespacesStr = "all namespaces"
|
namespacesStr = "all namespaces"
|
||||||
}
|
}
|
||||||
mizu.CheckNewerVersion()
|
version.CheckNewerVersion()
|
||||||
mizu.Log.Infof("Tapping pods in %s", namespacesStr)
|
logger.Log.Infof("Tapping pods in %s", namespacesStr)
|
||||||
|
|
||||||
if err, _ := updateCurrentlyTappedPods(kubernetesProvider, ctx, targetNamespaces); err != nil {
|
if err, _ := updateCurrentlyTappedPods(kubernetesProvider, ctx, targetNamespaces); err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error getting pods by regex: %v", errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error getting pods by regex: %v", errormessage.FormatError(err)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,10 +91,10 @@ func RunMizuTap() {
|
|||||||
if !mizu.Contains(targetNamespaces, mizu.K8sAllNamespaces) {
|
if !mizu.Contains(targetNamespaces, mizu.K8sAllNamespaces) {
|
||||||
suggestionStr = ". Select a different namespace with -n or tap all namespaces with -A"
|
suggestionStr = ". Select a different namespace with -n or tap all namespaces with -A"
|
||||||
}
|
}
|
||||||
mizu.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Did not find any pods matching the regex argument%s", suggestionStr))
|
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Did not find any pods matching the regex argument%s", suggestionStr))
|
||||||
}
|
}
|
||||||
|
|
||||||
if mizu.Config.Tap.DryRun {
|
if config.Config.Tap.DryRun {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +102,7 @@ func RunMizuTap() {
|
|||||||
|
|
||||||
defer cleanUpMizuResources(kubernetesProvider)
|
defer cleanUpMizuResources(kubernetesProvider)
|
||||||
if err := createMizuResources(ctx, kubernetesProvider, nodeToTappedPodIPMap, mizuApiFilteringOptions, mizuValidationRules); err != nil {
|
if err := createMizuResources(ctx, kubernetesProvider, nodeToTappedPodIPMap, mizuApiFilteringOptions, mizuValidationRules); err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error creating resources: %v", errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error creating resources: %v", errormessage.FormatError(err)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +123,7 @@ func readValidationRules(file string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createMizuResources(ctx context.Context, kubernetesProvider *kubernetes.Provider, nodeToTappedPodIPMap map[string][]string, mizuApiFilteringOptions *shared.TrafficFilteringOptions, mizuValidationRules string) error {
|
func createMizuResources(ctx context.Context, kubernetesProvider *kubernetes.Provider, nodeToTappedPodIPMap map[string][]string, mizuApiFilteringOptions *shared.TrafficFilteringOptions, mizuValidationRules string) error {
|
||||||
if !mizu.Config.IsNsRestrictedMode() {
|
if !config.Config.IsNsRestrictedMode() {
|
||||||
if err := createMizuNamespace(ctx, kubernetesProvider); err != nil {
|
if err := createMizuNamespace(ctx, kubernetesProvider); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -135,7 +138,7 @@ func createMizuResources(ctx context.Context, kubernetesProvider *kubernetes.Pro
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := createMizuConfigmap(ctx, kubernetesProvider, mizuValidationRules); err != nil {
|
if err := createMizuConfigmap(ctx, kubernetesProvider, mizuValidationRules); err != nil {
|
||||||
mizu.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Failed to create resources required for policy validation. Mizu will not validate policy rules. error: %v\n", errormessage.FormatError(err)))
|
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Failed to create resources required for policy validation. Mizu will not validate policy rules. error: %v\n", errormessage.FormatError(err)))
|
||||||
state.doNotRemoveConfigMap = true
|
state.doNotRemoveConfigMap = true
|
||||||
} else if mizuValidationRules == "" {
|
} else if mizuValidationRules == "" {
|
||||||
state.doNotRemoveConfigMap = true
|
state.doNotRemoveConfigMap = true
|
||||||
@ -145,12 +148,12 @@ func createMizuResources(ctx context.Context, kubernetesProvider *kubernetes.Pro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createMizuConfigmap(ctx context.Context, kubernetesProvider *kubernetes.Provider, data string) error {
|
func createMizuConfigmap(ctx context.Context, kubernetesProvider *kubernetes.Provider, data string) error {
|
||||||
err := kubernetesProvider.CreateConfigMap(ctx, mizu.Config.MizuResourcesNamespace, mizu.ConfigMapName, data)
|
err := kubernetesProvider.CreateConfigMap(ctx, config.Config.MizuResourcesNamespace, mizu.ConfigMapName, data)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func createMizuNamespace(ctx context.Context, kubernetesProvider *kubernetes.Provider) error {
|
func createMizuNamespace(ctx context.Context, kubernetesProvider *kubernetes.Provider) error {
|
||||||
_, err := kubernetesProvider.CreateNamespace(ctx, mizu.Config.MizuResourcesNamespace)
|
_, err := kubernetesProvider.CreateNamespace(ctx, config.Config.MizuResourcesNamespace)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,7 +162,7 @@ func createMizuApiServer(ctx context.Context, kubernetesProvider *kubernetes.Pro
|
|||||||
|
|
||||||
state.mizuServiceAccountExists, err = createRBACIfNecessary(ctx, kubernetesProvider)
|
state.mizuServiceAccountExists, err = createRBACIfNecessary(ctx, kubernetesProvider)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mizu.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Failed to ensure the resources required for IP resolving. Mizu will not resolve target IPs to names. error: %v", errormessage.FormatError(err)))
|
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Failed to ensure the resources required for IP resolving. Mizu will not resolve target IPs to names. error: %v", errormessage.FormatError(err)))
|
||||||
}
|
}
|
||||||
|
|
||||||
var serviceAccountName string
|
var serviceAccountName string
|
||||||
@ -170,25 +173,25 @@ func createMizuApiServer(ctx context.Context, kubernetesProvider *kubernetes.Pro
|
|||||||
}
|
}
|
||||||
|
|
||||||
opts := &kubernetes.ApiServerOptions{
|
opts := &kubernetes.ApiServerOptions{
|
||||||
Namespace: mizu.Config.MizuResourcesNamespace,
|
Namespace: config.Config.MizuResourcesNamespace,
|
||||||
PodName: mizu.ApiServerPodName,
|
PodName: mizu.ApiServerPodName,
|
||||||
PodImage: mizu.Config.AgentImage,
|
PodImage: config.Config.AgentImage,
|
||||||
ServiceAccountName: serviceAccountName,
|
ServiceAccountName: serviceAccountName,
|
||||||
IsNamespaceRestricted: mizu.Config.IsNsRestrictedMode(),
|
IsNamespaceRestricted: config.Config.IsNsRestrictedMode(),
|
||||||
MizuApiFilteringOptions: mizuApiFilteringOptions,
|
MizuApiFilteringOptions: mizuApiFilteringOptions,
|
||||||
MaxEntriesDBSizeBytes: mizu.Config.Tap.MaxEntriesDBSizeBytes(),
|
MaxEntriesDBSizeBytes: config.Config.Tap.MaxEntriesDBSizeBytes(),
|
||||||
}
|
}
|
||||||
_, err = kubernetesProvider.CreateMizuApiServerPod(ctx, opts)
|
_, err = kubernetesProvider.CreateMizuApiServerPod(ctx, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mizu.Log.Debugf("Successfully created API server pod: %s", mizu.ApiServerPodName)
|
logger.Log.Debugf("Successfully created API server pod: %s", mizu.ApiServerPodName)
|
||||||
|
|
||||||
state.apiServerService, err = kubernetesProvider.CreateService(ctx, mizu.Config.MizuResourcesNamespace, mizu.ApiServerPodName, mizu.ApiServerPodName)
|
state.apiServerService, err = kubernetesProvider.CreateService(ctx, config.Config.MizuResourcesNamespace, mizu.ApiServerPodName, mizu.ApiServerPodName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mizu.Log.Debugf("Successfully created service: %s", mizu.ApiServerPodName)
|
logger.Log.Debugf("Successfully created service: %s", mizu.ApiServerPodName)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -196,9 +199,9 @@ func createMizuApiServer(ctx context.Context, kubernetesProvider *kubernetes.Pro
|
|||||||
func getMizuApiFilteringOptions() (*shared.TrafficFilteringOptions, error) {
|
func getMizuApiFilteringOptions() (*shared.TrafficFilteringOptions, error) {
|
||||||
var compiledRegexSlice []*shared.SerializableRegexp
|
var compiledRegexSlice []*shared.SerializableRegexp
|
||||||
|
|
||||||
if mizu.Config.Tap.PlainTextFilterRegexes != nil && len(mizu.Config.Tap.PlainTextFilterRegexes) > 0 {
|
if config.Config.Tap.PlainTextFilterRegexes != nil && len(config.Config.Tap.PlainTextFilterRegexes) > 0 {
|
||||||
compiledRegexSlice = make([]*shared.SerializableRegexp, 0)
|
compiledRegexSlice = make([]*shared.SerializableRegexp, 0)
|
||||||
for _, regexStr := range mizu.Config.Tap.PlainTextFilterRegexes {
|
for _, regexStr := range config.Config.Tap.PlainTextFilterRegexes {
|
||||||
compiledRegex, err := shared.CompileRegexToSerializableRegexp(regexStr)
|
compiledRegex, err := shared.CompileRegexToSerializableRegexp(regexStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -209,8 +212,8 @@ func getMizuApiFilteringOptions() (*shared.TrafficFilteringOptions, error) {
|
|||||||
|
|
||||||
return &shared.TrafficFilteringOptions{
|
return &shared.TrafficFilteringOptions{
|
||||||
PlainTextMaskingRegexes: compiledRegexSlice,
|
PlainTextMaskingRegexes: compiledRegexSlice,
|
||||||
HealthChecksUserAgentHeaders: mizu.Config.Tap.HealthChecksUserAgentHeaders,
|
HealthChecksUserAgentHeaders: config.Config.Tap.HealthChecksUserAgentHeaders,
|
||||||
DisableRedaction: mizu.Config.Tap.DisableRedaction,
|
DisableRedaction: config.Config.Tap.DisableRedaction,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,20 +228,20 @@ func updateMizuTappers(ctx context.Context, kubernetesProvider *kubernetes.Provi
|
|||||||
|
|
||||||
if err := kubernetesProvider.ApplyMizuTapperDaemonSet(
|
if err := kubernetesProvider.ApplyMizuTapperDaemonSet(
|
||||||
ctx,
|
ctx,
|
||||||
mizu.Config.MizuResourcesNamespace,
|
config.Config.MizuResourcesNamespace,
|
||||||
mizu.TapperDaemonSetName,
|
mizu.TapperDaemonSetName,
|
||||||
mizu.Config.AgentImage,
|
config.Config.AgentImage,
|
||||||
mizu.TapperPodName,
|
mizu.TapperPodName,
|
||||||
fmt.Sprintf("%s.%s.svc.cluster.local", state.apiServerService.Name, state.apiServerService.Namespace),
|
fmt.Sprintf("%s.%s.svc.cluster.local", state.apiServerService.Name, state.apiServerService.Namespace),
|
||||||
nodeToTappedPodIPMap,
|
nodeToTappedPodIPMap,
|
||||||
serviceAccountName,
|
serviceAccountName,
|
||||||
mizu.Config.Tap.TapOutgoing(),
|
config.Config.Tap.TapOutgoing(),
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mizu.Log.Debugf("Successfully created %v tappers", len(nodeToTappedPodIPMap))
|
logger.Log.Debugf("Successfully created %v tappers", len(nodeToTappedPodIPMap))
|
||||||
} else {
|
} else {
|
||||||
if err := kubernetesProvider.RemoveDaemonSet(ctx, mizu.Config.MizuResourcesNamespace, mizu.TapperDaemonSetName); err != nil {
|
if err := kubernetesProvider.RemoveDaemonSet(ctx, config.Config.MizuResourcesNamespace, mizu.TapperDaemonSetName); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -251,65 +254,65 @@ func cleanUpMizuResources(kubernetesProvider *kubernetes.Provider) {
|
|||||||
removalCtx, cancel := context.WithTimeout(context.Background(), cleanupTimeout)
|
removalCtx, cancel := context.WithTimeout(context.Background(), cleanupTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if mizu.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 {
|
||||||
mizu.Log.Errorf("Failed dump logs %v", err)
|
logger.Log.Errorf("Failed dump logs %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mizu.Log.Infof("\nRemoving mizu resources\n")
|
logger.Log.Infof("\nRemoving mizu resources\n")
|
||||||
|
|
||||||
if !mizu.Config.IsNsRestrictedMode() {
|
if !config.Config.IsNsRestrictedMode() {
|
||||||
if err := kubernetesProvider.RemoveNamespace(removalCtx, mizu.Config.MizuResourcesNamespace); err != nil {
|
if err := kubernetesProvider.RemoveNamespace(removalCtx, config.Config.MizuResourcesNamespace); err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing Namespace %s: %v", mizu.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing Namespace %s: %v", config.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := kubernetesProvider.RemovePod(removalCtx, mizu.Config.MizuResourcesNamespace, mizu.ApiServerPodName); err != nil {
|
if err := kubernetesProvider.RemovePod(removalCtx, config.Config.MizuResourcesNamespace, mizu.ApiServerPodName); err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing Pod %s in namespace %s: %v", mizu.ApiServerPodName, mizu.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing Pod %s in namespace %s: %v", mizu.ApiServerPodName, config.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := kubernetesProvider.RemoveService(removalCtx, mizu.Config.MizuResourcesNamespace, mizu.ApiServerPodName); err != nil {
|
if err := kubernetesProvider.RemoveService(removalCtx, config.Config.MizuResourcesNamespace, mizu.ApiServerPodName); err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing Service %s in namespace %s: %v", mizu.ApiServerPodName, mizu.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing Service %s in namespace %s: %v", mizu.ApiServerPodName, config.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := kubernetesProvider.RemoveDaemonSet(removalCtx, mizu.Config.MizuResourcesNamespace, mizu.TapperDaemonSetName); err != nil {
|
if err := kubernetesProvider.RemoveDaemonSet(removalCtx, config.Config.MizuResourcesNamespace, mizu.TapperDaemonSetName); err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing DaemonSet %s in namespace %s: %v", mizu.TapperDaemonSetName, mizu.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing DaemonSet %s in namespace %s: %v", mizu.TapperDaemonSetName, config.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
||||||
}
|
}
|
||||||
|
|
||||||
if !state.doNotRemoveConfigMap {
|
if !state.doNotRemoveConfigMap {
|
||||||
if err := kubernetesProvider.RemoveConfigMap(removalCtx, mizu.Config.MizuResourcesNamespace, mizu.ConfigMapName); err != nil {
|
if err := kubernetesProvider.RemoveConfigMap(removalCtx, config.Config.MizuResourcesNamespace, mizu.ConfigMapName); err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing ConfigMap %s in namespace %s: %v", mizu.ConfigMapName, mizu.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing ConfigMap %s in namespace %s: %v", mizu.ConfigMapName, config.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if state.mizuServiceAccountExists {
|
if state.mizuServiceAccountExists {
|
||||||
if !mizu.Config.IsNsRestrictedMode() {
|
if !config.Config.IsNsRestrictedMode() {
|
||||||
if err := kubernetesProvider.RemoveNonNamespacedResources(removalCtx, mizu.ClusterRoleName, mizu.ClusterRoleBindingName); err != nil {
|
if err := kubernetesProvider.RemoveNonNamespacedResources(removalCtx, mizu.ClusterRoleName, mizu.ClusterRoleBindingName); err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing non-namespaced resources: %v", errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing non-namespaced resources: %v", errormessage.FormatError(err)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := kubernetesProvider.RemoveServicAccount(removalCtx, mizu.Config.MizuResourcesNamespace, mizu.ServiceAccountName); err != nil {
|
if err := kubernetesProvider.RemoveServicAccount(removalCtx, config.Config.MizuResourcesNamespace, mizu.ServiceAccountName); err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing Service Account %s in namespace %s: %v", mizu.ServiceAccountName, mizu.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing Service Account %s in namespace %s: %v", mizu.ServiceAccountName, config.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := kubernetesProvider.RemoveRole(removalCtx, mizu.Config.MizuResourcesNamespace, mizu.RoleName); err != nil {
|
if err := kubernetesProvider.RemoveRole(removalCtx, config.Config.MizuResourcesNamespace, mizu.RoleName); err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing Role %s in namespace %s: %v", mizu.RoleName, mizu.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing Role %s in namespace %s: %v", mizu.RoleName, config.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := kubernetesProvider.RemoveRoleBinding(removalCtx, mizu.Config.MizuResourcesNamespace, mizu.RoleBindingName); err != nil {
|
if err := kubernetesProvider.RemoveRoleBinding(removalCtx, config.Config.MizuResourcesNamespace, mizu.RoleBindingName); err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing RoleBinding %s in namespace %s: %v", mizu.RoleBindingName, mizu.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error removing RoleBinding %s in namespace %s: %v", mizu.RoleBindingName, config.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !mizu.Config.IsNsRestrictedMode() {
|
if !config.Config.IsNsRestrictedMode() {
|
||||||
waitUntilNamespaceDeleted(removalCtx, cancel, kubernetesProvider)
|
waitUntilNamespaceDeleted(removalCtx, cancel, kubernetesProvider)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -320,20 +323,20 @@ func waitUntilNamespaceDeleted(ctx context.Context, cancel context.CancelFunc, k
|
|||||||
waitForFinish(ctx, cancel)
|
waitForFinish(ctx, cancel)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := kubernetesProvider.WaitUtilNamespaceDeleted(ctx, mizu.Config.MizuResourcesNamespace); err != nil {
|
if err := kubernetesProvider.WaitUtilNamespaceDeleted(ctx, config.Config.MizuResourcesNamespace); err != nil {
|
||||||
switch {
|
switch {
|
||||||
case ctx.Err() == context.Canceled:
|
case ctx.Err() == context.Canceled:
|
||||||
// Do nothing. User interrupted the wait.
|
// Do nothing. User interrupted the wait.
|
||||||
case err == wait.ErrWaitTimeout:
|
case err == wait.ErrWaitTimeout:
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Timeout while removing Namespace %s", mizu.Config.MizuResourcesNamespace))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Timeout while removing Namespace %s", config.Config.MizuResourcesNamespace))
|
||||||
default:
|
default:
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error while waiting for Namespace %s to be deleted: %v", mizu.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error while waiting for Namespace %s to be deleted: %v", config.Config.MizuResourcesNamespace, errormessage.FormatError(err)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func reportTappedPods() {
|
func reportTappedPods() {
|
||||||
mizuProxiedUrl := kubernetes.GetMizuApiServerProxiedHostAndPath(mizu.Config.Tap.GuiPort)
|
mizuProxiedUrl := kubernetes.GetMizuApiServerProxiedHostAndPath(config.Config.Tap.GuiPort)
|
||||||
tappedPodsUrl := fmt.Sprintf("http://%s/status/tappedPods", mizuProxiedUrl)
|
tappedPodsUrl := fmt.Sprintf("http://%s/status/tappedPods", mizuProxiedUrl)
|
||||||
|
|
||||||
podInfos := make([]shared.PodInfo, 0)
|
podInfos := make([]shared.PodInfo, 0)
|
||||||
@ -343,30 +346,30 @@ func reportTappedPods() {
|
|||||||
tapStatus := shared.TapStatus{Pods: podInfos}
|
tapStatus := shared.TapStatus{Pods: podInfos}
|
||||||
|
|
||||||
if jsonValue, err := json.Marshal(tapStatus); err != nil {
|
if jsonValue, err := json.Marshal(tapStatus); err != nil {
|
||||||
mizu.Log.Debugf("[ERROR] failed Marshal the tapped pods %v", err)
|
logger.Log.Debugf("[ERROR] failed Marshal the tapped pods %v", err)
|
||||||
} else {
|
} else {
|
||||||
if response, err := http.Post(tappedPodsUrl, "application/json", bytes.NewBuffer(jsonValue)); err != nil {
|
if response, err := http.Post(tappedPodsUrl, "application/json", bytes.NewBuffer(jsonValue)); err != nil {
|
||||||
mizu.Log.Debugf("[ERROR] failed sending to API server the tapped pods %v", err)
|
logger.Log.Debugf("[ERROR] failed sending to API server the tapped pods %v", err)
|
||||||
} else if response.StatusCode != 200 {
|
} else if response.StatusCode != 200 {
|
||||||
mizu.Log.Debugf("[ERROR] failed sending to API server the tapped pods, response status code %v", response.StatusCode)
|
logger.Log.Debugf("[ERROR] failed sending to API server the tapped pods, response status code %v", response.StatusCode)
|
||||||
} else {
|
} else {
|
||||||
mizu.Log.Debugf("Reported to server API about %d taped pods successfully", len(podInfos))
|
logger.Log.Debugf("Reported to server API about %d taped pods successfully", len(podInfos))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func watchPodsForTapping(ctx context.Context, kubernetesProvider *kubernetes.Provider, targetNamespaces []string, cancel context.CancelFunc) {
|
func watchPodsForTapping(ctx context.Context, kubernetesProvider *kubernetes.Provider, targetNamespaces []string, cancel context.CancelFunc) {
|
||||||
added, modified, removed, errorChan := kubernetes.FilteredWatch(ctx, kubernetesProvider, targetNamespaces, mizu.Config.Tap.PodRegex())
|
added, modified, removed, errorChan := kubernetes.FilteredWatch(ctx, kubernetesProvider, targetNamespaces, config.Config.Tap.PodRegex())
|
||||||
|
|
||||||
restartTappers := func() {
|
restartTappers := func() {
|
||||||
err, changeFound := updateCurrentlyTappedPods(kubernetesProvider, ctx, targetNamespaces)
|
err, changeFound := updateCurrentlyTappedPods(kubernetesProvider, ctx, targetNamespaces)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Failed to update currently tapped pods: %v", err))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Failed to update currently tapped pods: %v", err))
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !changeFound {
|
if !changeFound {
|
||||||
mizu.Log.Debugf("Nothing changed update tappers not needed")
|
logger.Log.Debugf("Nothing changed update tappers not needed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -374,11 +377,11 @@ func watchPodsForTapping(ctx context.Context, kubernetesProvider *kubernetes.Pro
|
|||||||
|
|
||||||
nodeToTappedPodIPMap := getNodeHostToTappedPodIpsMap(state.currentlyTappedPods)
|
nodeToTappedPodIPMap := getNodeHostToTappedPodIpsMap(state.currentlyTappedPods)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error building node to ips map: %v", errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error building node to ips map: %v", errormessage.FormatError(err)))
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
if err := updateMizuTappers(ctx, kubernetesProvider, nodeToTappedPodIPMap); err != nil {
|
if err := updateMizuTappers(ctx, kubernetesProvider, nodeToTappedPodIPMap); err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error updating daemonset: %v", errormessage.FormatError(err)))
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error updating daemonset: %v", errormessage.FormatError(err)))
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -387,13 +390,13 @@ func watchPodsForTapping(ctx context.Context, kubernetesProvider *kubernetes.Pro
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case pod := <-added:
|
case pod := <-added:
|
||||||
mizu.Log.Debugf("Added matching pod %s, ns: %s", pod.Name, pod.Namespace)
|
logger.Log.Debugf("Added matching pod %s, ns: %s", pod.Name, pod.Namespace)
|
||||||
restartTappersDebouncer.SetOn()
|
restartTappersDebouncer.SetOn()
|
||||||
case pod := <-removed:
|
case pod := <-removed:
|
||||||
mizu.Log.Debugf("Removed matching pod %s, ns: %s", pod.Name, pod.Namespace)
|
logger.Log.Debugf("Removed matching pod %s, ns: %s", pod.Name, pod.Namespace)
|
||||||
restartTappersDebouncer.SetOn()
|
restartTappersDebouncer.SetOn()
|
||||||
case pod := <-modified:
|
case pod := <-modified:
|
||||||
mizu.Log.Debugf("Modified matching pod %s, ns: %s, phase: %s, ip: %s", pod.Name, pod.Namespace, pod.Status.Phase, pod.Status.PodIP)
|
logger.Log.Debugf("Modified matching pod %s, ns: %s, phase: %s, ip: %s", pod.Name, pod.Namespace, pod.Status.Phase, pod.Status.PodIP)
|
||||||
// Act only if the modified pod has already obtained an IP address.
|
// Act only if the modified pod has already obtained an IP address.
|
||||||
// After filtering for IPs, on a normal pod restart this includes the following events:
|
// After filtering for IPs, on a normal pod restart this includes the following events:
|
||||||
// - Pod deletion
|
// - Pod deletion
|
||||||
@ -405,13 +408,13 @@ func watchPodsForTapping(ctx context.Context, kubernetesProvider *kubernetes.Pro
|
|||||||
}
|
}
|
||||||
|
|
||||||
case err := <-errorChan:
|
case err := <-errorChan:
|
||||||
mizu.Log.Debugf("Watching pods loop, got error %v, stopping `restart tappers debouncer`", err)
|
logger.Log.Debugf("Watching pods loop, got error %v, stopping `restart tappers debouncer`", err)
|
||||||
restartTappersDebouncer.Cancel()
|
restartTappersDebouncer.Cancel()
|
||||||
// TODO: Does this also perform cleanup?
|
// TODO: Does this also perform cleanup?
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
mizu.Log.Debugf("Watching pods loop, context done, stopping `restart tappers debouncer`")
|
logger.Log.Debugf("Watching pods loop, context done, stopping `restart tappers debouncer`")
|
||||||
restartTappersDebouncer.Cancel()
|
restartTappersDebouncer.Cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -420,18 +423,18 @@ func watchPodsForTapping(ctx context.Context, kubernetesProvider *kubernetes.Pro
|
|||||||
|
|
||||||
func updateCurrentlyTappedPods(kubernetesProvider *kubernetes.Provider, ctx context.Context, targetNamespaces []string) (error, bool) {
|
func updateCurrentlyTappedPods(kubernetesProvider *kubernetes.Provider, ctx context.Context, targetNamespaces []string) (error, bool) {
|
||||||
changeFound := false
|
changeFound := false
|
||||||
if matchingPods, err := kubernetesProvider.ListAllRunningPodsMatchingRegex(ctx, mizu.Config.Tap.PodRegex(), targetNamespaces); err != nil {
|
if matchingPods, err := kubernetesProvider.ListAllRunningPodsMatchingRegex(ctx, config.Config.Tap.PodRegex(), targetNamespaces); err != nil {
|
||||||
return err, false
|
return err, false
|
||||||
} else {
|
} else {
|
||||||
podsToTap := excludeMizuPods(matchingPods)
|
podsToTap := excludeMizuPods(matchingPods)
|
||||||
addedPods, removedPods := getPodArrayDiff(state.currentlyTappedPods, podsToTap)
|
addedPods, removedPods := getPodArrayDiff(state.currentlyTappedPods, podsToTap)
|
||||||
for _, addedPod := range addedPods {
|
for _, addedPod := range addedPods {
|
||||||
changeFound = true
|
changeFound = true
|
||||||
mizu.Log.Infof(uiUtils.Green, fmt.Sprintf("+%s", addedPod.Name))
|
logger.Log.Infof(uiUtils.Green, fmt.Sprintf("+%s", addedPod.Name))
|
||||||
}
|
}
|
||||||
for _, removedPod := range removedPods {
|
for _, removedPod := range removedPods {
|
||||||
changeFound = true
|
changeFound = true
|
||||||
mizu.Log.Infof(uiUtils.Red, fmt.Sprintf("-%s", removedPod.Name))
|
logger.Log.Infof(uiUtils.Red, fmt.Sprintf("-%s", removedPod.Name))
|
||||||
}
|
}
|
||||||
state.currentlyTappedPods = podsToTap
|
state.currentlyTappedPods = podsToTap
|
||||||
}
|
}
|
||||||
@ -479,86 +482,86 @@ func getMissingPods(pods1 []core.Pod, pods2 []core.Pod) []core.Pod {
|
|||||||
|
|
||||||
func createProxyToApiServerPod(ctx context.Context, kubernetesProvider *kubernetes.Provider, cancel context.CancelFunc) {
|
func createProxyToApiServerPod(ctx context.Context, kubernetesProvider *kubernetes.Provider, cancel context.CancelFunc) {
|
||||||
podExactRegex := regexp.MustCompile(fmt.Sprintf("^%s$", mizu.ApiServerPodName))
|
podExactRegex := regexp.MustCompile(fmt.Sprintf("^%s$", mizu.ApiServerPodName))
|
||||||
added, modified, removed, errorChan := kubernetes.FilteredWatch(ctx, kubernetesProvider, []string{mizu.Config.MizuResourcesNamespace}, podExactRegex)
|
added, modified, removed, errorChan := kubernetes.FilteredWatch(ctx, kubernetesProvider, []string{config.Config.MizuResourcesNamespace}, podExactRegex)
|
||||||
isPodReady := false
|
isPodReady := false
|
||||||
timeAfter := time.After(25 * time.Second)
|
timeAfter := time.After(25 * time.Second)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
mizu.Log.Debugf("Watching API Server pod loop, ctx done")
|
logger.Log.Debugf("Watching API Server pod loop, ctx done")
|
||||||
return
|
return
|
||||||
case <-added:
|
case <-added:
|
||||||
mizu.Log.Debugf("Watching API Server pod loop, added")
|
logger.Log.Debugf("Watching API Server pod loop, added")
|
||||||
continue
|
continue
|
||||||
case <-removed:
|
case <-removed:
|
||||||
mizu.Log.Infof("%s removed", mizu.ApiServerPodName)
|
logger.Log.Infof("%s removed", mizu.ApiServerPodName)
|
||||||
cancel()
|
cancel()
|
||||||
return
|
return
|
||||||
case modifiedPod := <-modified:
|
case modifiedPod := <-modified:
|
||||||
if modifiedPod == nil {
|
if modifiedPod == nil {
|
||||||
mizu.Log.Debugf("Watching API Server pod loop, modifiedPod with nil")
|
logger.Log.Debugf("Watching API Server pod loop, modifiedPod with nil")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
mizu.Log.Debugf("Watching API Server pod loop, modified: %v", modifiedPod.Status.Phase)
|
logger.Log.Debugf("Watching API Server pod loop, modified: %v", modifiedPod.Status.Phase)
|
||||||
if modifiedPod.Status.Phase == core.PodRunning && !isPodReady {
|
if modifiedPod.Status.Phase == core.PodRunning && !isPodReady {
|
||||||
isPodReady = true
|
isPodReady = true
|
||||||
go startProxyReportErrorIfAny(kubernetesProvider, cancel)
|
go startProxyReportErrorIfAny(kubernetesProvider, cancel)
|
||||||
mizu.Log.Infof("Mizu is available at http://%s\n", kubernetes.GetMizuApiServerProxiedHostAndPath(mizu.Config.Tap.GuiPort))
|
logger.Log.Infof("Mizu is available at http://%s\n", kubernetes.GetMizuApiServerProxiedHostAndPath(config.Config.Tap.GuiPort))
|
||||||
time.Sleep(time.Second * 5) // Waiting to be sure the proxy is ready
|
time.Sleep(time.Second * 5) // Waiting to be sure the proxy is ready
|
||||||
requestForAnalysis()
|
requestForAnalysis()
|
||||||
reportTappedPods()
|
reportTappedPods()
|
||||||
}
|
}
|
||||||
case <-timeAfter:
|
case <-timeAfter:
|
||||||
if !isPodReady {
|
if !isPodReady {
|
||||||
mizu.Log.Errorf(uiUtils.Error, "Mizu API server was not ready in time")
|
logger.Log.Errorf(uiUtils.Error, "Mizu API server was not ready in time")
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
case <-errorChan:
|
case <-errorChan:
|
||||||
mizu.Log.Debugf("[ERROR] Agent creation, watching %v namespace", mizu.Config.MizuResourcesNamespace)
|
logger.Log.Debugf("[ERROR] Agent creation, watching %v namespace", config.Config.MizuResourcesNamespace)
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func startProxyReportErrorIfAny(kubernetesProvider *kubernetes.Provider, cancel context.CancelFunc) {
|
func startProxyReportErrorIfAny(kubernetesProvider *kubernetes.Provider, cancel context.CancelFunc) {
|
||||||
err := kubernetes.StartProxy(kubernetesProvider, mizu.Config.Tap.GuiPort, mizu.Config.MizuResourcesNamespace, mizu.ApiServerPodName)
|
err := kubernetes.StartProxy(kubernetesProvider, config.Config.Tap.GuiPort, config.Config.MizuResourcesNamespace, mizu.ApiServerPodName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mizu.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error occured while running k8s proxy %v\n"+
|
logger.Log.Errorf(uiUtils.Error, fmt.Sprintf("Error occured while running k8s proxy %v\n"+
|
||||||
"Try setting different port by using --%s", errormessage.FormatError(err), configStructs.GuiPortTapName))
|
"Try setting different port by using --%s", errormessage.FormatError(err), configStructs.GuiPortTapName))
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func requestForAnalysis() {
|
func requestForAnalysis() {
|
||||||
if !mizu.Config.Tap.Analysis {
|
if !config.Config.Tap.Analysis {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mizuProxiedUrl := kubernetes.GetMizuApiServerProxiedHostAndPath(mizu.Config.Tap.GuiPort)
|
mizuProxiedUrl := kubernetes.GetMizuApiServerProxiedHostAndPath(config.Config.Tap.GuiPort)
|
||||||
urlPath := fmt.Sprintf("http://%s/api/uploadEntries?dest=%s&interval=%v", mizuProxiedUrl, url.QueryEscape(mizu.Config.Tap.AnalysisDestination), mizu.Config.Tap.SleepIntervalSec)
|
urlPath := fmt.Sprintf("http://%s/api/uploadEntries?dest=%s&interval=%v", mizuProxiedUrl, url.QueryEscape(config.Config.Tap.AnalysisDestination), config.Config.Tap.SleepIntervalSec)
|
||||||
u, parseErr := url.ParseRequestURI(urlPath)
|
u, parseErr := url.ParseRequestURI(urlPath)
|
||||||
if parseErr != nil {
|
if parseErr != nil {
|
||||||
mizu.Log.Fatal("Failed parsing the URL (consider changing the analysis dest URL), err: %v", parseErr)
|
logger.Log.Fatal("Failed parsing the URL (consider changing the analysis dest URL), err: %v", parseErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
mizu.Log.Debugf("Sending get request to %v", u.String())
|
logger.Log.Debugf("Sending get request to %v", u.String())
|
||||||
if response, requestErr := http.Get(u.String()); requestErr != nil {
|
if response, requestErr := http.Get(u.String()); requestErr != nil {
|
||||||
mizu.Log.Errorf("Failed to notify agent for analysis, err: %v", requestErr)
|
logger.Log.Errorf("Failed to notify agent for analysis, err: %v", requestErr)
|
||||||
} else if response.StatusCode != 200 {
|
} else if response.StatusCode != 200 {
|
||||||
mizu.Log.Errorf("Failed to notify agent for analysis, status code: %v", response.StatusCode)
|
logger.Log.Errorf("Failed to notify agent for analysis, status code: %v", response.StatusCode)
|
||||||
} else {
|
} else {
|
||||||
mizu.Log.Infof(uiUtils.Purple, "Traffic is uploading to UP9 for further analysis")
|
logger.Log.Infof(uiUtils.Purple, "Traffic is uploading to UP9 for further analysis")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRBACIfNecessary(ctx context.Context, kubernetesProvider *kubernetes.Provider) (bool, error) {
|
func createRBACIfNecessary(ctx context.Context, kubernetesProvider *kubernetes.Provider) (bool, error) {
|
||||||
if !mizu.Config.IsNsRestrictedMode() {
|
if !config.Config.IsNsRestrictedMode() {
|
||||||
err := kubernetesProvider.CreateMizuRBAC(ctx, mizu.Config.MizuResourcesNamespace, mizu.ServiceAccountName, mizu.ClusterRoleName, mizu.ClusterRoleBindingName, mizu.RBACVersion)
|
err := kubernetesProvider.CreateMizuRBAC(ctx, config.Config.MizuResourcesNamespace, mizu.ServiceAccountName, mizu.ClusterRoleName, mizu.ClusterRoleBindingName, mizu.RBACVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err := kubernetesProvider.CreateMizuRBACNamespaceRestricted(ctx, mizu.Config.MizuResourcesNamespace, mizu.ServiceAccountName, mizu.RoleName, mizu.RoleBindingName, mizu.RBACVersion)
|
err := kubernetesProvider.CreateMizuRBACNamespaceRestricted(ctx, config.Config.MizuResourcesNamespace, mizu.ServiceAccountName, mizu.RoleName, mizu.RoleBindingName, mizu.RBACVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@ -593,10 +596,10 @@ func waitForFinish(ctx context.Context, cancel context.CancelFunc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getNamespaces(kubernetesProvider *kubernetes.Provider) []string {
|
func getNamespaces(kubernetesProvider *kubernetes.Provider) []string {
|
||||||
if mizu.Config.Tap.AllNamespaces {
|
if config.Config.Tap.AllNamespaces {
|
||||||
return []string{mizu.K8sAllNamespaces}
|
return []string{mizu.K8sAllNamespaces}
|
||||||
} else if len(mizu.Config.Tap.Namespaces) > 0 {
|
} else if len(config.Config.Tap.Namespaces) > 0 {
|
||||||
return mizu.Config.Tap.Namespaces
|
return config.Config.Tap.Namespaces
|
||||||
} else {
|
} else {
|
||||||
return []string{kubernetesProvider.CurrentNamespace()}
|
return []string{kubernetesProvider.CurrentNamespace()}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,30 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/up9inc/mizu/cli/config"
|
||||||
|
"github.com/up9inc/mizu/cli/config/configStructs"
|
||||||
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
|
"github.com/up9inc/mizu/cli/telemetry"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/creasty/defaults"
|
"github.com/creasty/defaults"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/mizu"
|
||||||
"github.com/up9inc/mizu/cli/mizu/configStructs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var versionCmd = &cobra.Command{
|
var versionCmd = &cobra.Command{
|
||||||
Use: "version",
|
Use: "version",
|
||||||
Short: "Print version info",
|
Short: "Print version info",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
go mizu.ReportRun("version", mizu.Config.Version)
|
go telemetry.ReportRun("version", config.Config.Version)
|
||||||
if mizu.Config.Version.DebugInfo {
|
if config.Config.Version.DebugInfo {
|
||||||
timeStampInt, _ := strconv.ParseInt(mizu.BuildTimestamp, 10, 0)
|
timeStampInt, _ := strconv.ParseInt(mizu.BuildTimestamp, 10, 0)
|
||||||
mizu.Log.Infof("Version: %s \nBranch: %s (%s)", mizu.SemVer, mizu.Branch, mizu.GitCommitHash)
|
logger.Log.Infof("Version: %s \nBranch: %s (%s)", mizu.SemVer, mizu.Branch, mizu.GitCommitHash)
|
||||||
mizu.Log.Infof("Build Time: %s (%s)", mizu.BuildTimestamp, time.Unix(timeStampInt, 0))
|
logger.Log.Infof("Build Time: %s (%s)", mizu.BuildTimestamp, time.Unix(timeStampInt, 0))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
mizu.Log.Infof("Version: %s (%s)", mizu.SemVer, mizu.Branch)
|
logger.Log.Infof("Version: %s (%s)", mizu.SemVer, mizu.Branch)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
@ -3,15 +3,16 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"github.com/creasty/defaults"
|
"github.com/creasty/defaults"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/config"
|
||||||
"github.com/up9inc/mizu/cli/mizu/configStructs"
|
"github.com/up9inc/mizu/cli/config/configStructs"
|
||||||
|
"github.com/up9inc/mizu/cli/telemetry"
|
||||||
)
|
)
|
||||||
|
|
||||||
var viewCmd = &cobra.Command{
|
var viewCmd = &cobra.Command{
|
||||||
Use: "view",
|
Use: "view",
|
||||||
Short: "Open GUI in browser",
|
Short: "Open GUI in browser",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
go mizu.ReportRun("view", mizu.Config.View)
|
go telemetry.ReportRun("view", config.Config.View)
|
||||||
runMizuView()
|
runMizuView()
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
@ -3,46 +3,49 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/up9inc/mizu/cli/config"
|
||||||
"github.com/up9inc/mizu/cli/kubernetes"
|
"github.com/up9inc/mizu/cli/kubernetes"
|
||||||
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/mizu"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu/version"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runMizuView() {
|
func runMizuView() {
|
||||||
kubernetesProvider, err := kubernetes.NewProvider(mizu.Config.View.KubeConfigPath)
|
kubernetesProvider, err := kubernetes.NewProvider(config.Config.View.KubeConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mizu.Log.Error(err)
|
logger.Log.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
exists, err := kubernetesProvider.DoesServicesExist(ctx, mizu.Config.MizuResourcesNamespace, mizu.ApiServerPodName)
|
exists, err := kubernetesProvider.DoesServicesExist(ctx, config.Config.MizuResourcesNamespace, mizu.ApiServerPodName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mizu.Log.Errorf("Failed to found mizu service %v", err)
|
logger.Log.Errorf("Failed to found mizu service %v", err)
|
||||||
cancel()
|
cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
mizu.Log.Infof("%s service not found, you should run `mizu tap` command first", mizu.ApiServerPodName)
|
logger.Log.Infof("%s service not found, you should run `mizu tap` command first", mizu.ApiServerPodName)
|
||||||
cancel()
|
cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mizuProxiedUrl := kubernetes.GetMizuApiServerProxiedHostAndPath(mizu.Config.View.GuiPort)
|
mizuProxiedUrl := kubernetes.GetMizuApiServerProxiedHostAndPath(config.Config.View.GuiPort)
|
||||||
_, err = http.Get(fmt.Sprintf("http://%s/", mizuProxiedUrl))
|
_, err = http.Get(fmt.Sprintf("http://%s/", mizuProxiedUrl))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
mizu.Log.Infof("Found a running service %s and open port %d", mizu.ApiServerPodName, mizu.Config.View.GuiPort)
|
logger.Log.Infof("Found a running service %s and open port %d", mizu.ApiServerPodName, config.Config.View.GuiPort)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mizu.Log.Debugf("Found service %s, creating k8s proxy", mizu.ApiServerPodName)
|
logger.Log.Debugf("Found service %s, creating k8s proxy", mizu.ApiServerPodName)
|
||||||
|
|
||||||
go startProxyReportErrorIfAny(kubernetesProvider, cancel)
|
go startProxyReportErrorIfAny(kubernetesProvider, cancel)
|
||||||
|
|
||||||
mizu.Log.Infof("Mizu is available at http://%s\n", kubernetes.GetMizuApiServerProxiedHostAndPath(mizu.Config.View.GuiPort))
|
logger.Log.Infof("Mizu is available at http://%s\n", kubernetes.GetMizuApiServerProxiedHostAndPath(config.Config.View.GuiPort))
|
||||||
if isCompatible, err := mizu.CheckVersionCompatibility(mizu.Config.View.GuiPort); err != nil {
|
if isCompatible, err := version.CheckVersionCompatibility(config.Config.View.GuiPort); err != nil {
|
||||||
mizu.Log.Errorf("Failed to check versions compatibility %v", err)
|
logger.Log.Errorf("Failed to check versions compatibility %v", err)
|
||||||
cancel()
|
cancel()
|
||||||
return
|
return
|
||||||
} else if !isCompatible {
|
} else if !isCompatible {
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package mizu
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/up9inc/mizu/cli/config/configStructs"
|
||||||
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -13,7 +16,6 @@ import (
|
|||||||
"github.com/creasty/defaults"
|
"github.com/creasty/defaults"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"github.com/up9inc/mizu/cli/mizu/configStructs"
|
|
||||||
"github.com/up9inc/mizu/cli/uiUtils"
|
"github.com/up9inc/mizu/cli/uiUtils"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
@ -62,7 +64,7 @@ func InitConfig(cmd *cobra.Command) error {
|
|||||||
cmd.Flags().Visit(initFlag)
|
cmd.Flags().Visit(initFlag)
|
||||||
|
|
||||||
finalConfigPrettified, _ := uiUtils.PrettyJson(Config)
|
finalConfigPrettified, _ := uiUtils.PrettyJson(Config)
|
||||||
Log.Debugf("Init config finished\n Final config: %v", finalConfigPrettified)
|
logger.Log.Debugf("Init config finished\n Final config: %v", finalConfigPrettified)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -80,7 +82,7 @@ func GetConfigWithDefaults() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetConfigFilePath() string {
|
func GetConfigFilePath() string {
|
||||||
return path.Join(GetMizuFolderPath(), "config.yaml")
|
return path.Join(mizu.GetMizuFolderPath(), "config.yaml")
|
||||||
}
|
}
|
||||||
|
|
||||||
func mergeConfigFile() error {
|
func mergeConfigFile() error {
|
||||||
@ -97,7 +99,7 @@ func mergeConfigFile() error {
|
|||||||
if err := yaml.Unmarshal(buf, &Config); err != nil {
|
if err := yaml.Unmarshal(buf, &Config); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
Log.Debugf("Found config file, merged to default options")
|
logger.Log.Debugf("Found config file, merged to default options")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -124,13 +126,13 @@ func mergeSetFlag(configElem reflect.Value, setValues []string) {
|
|||||||
|
|
||||||
for _, setValue := range setValues {
|
for _, setValue := range setValues {
|
||||||
if !strings.Contains(setValue, Separator) {
|
if !strings.Contains(setValue, Separator) {
|
||||||
Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument %s (set argument format: <flag name>=<flag value>)", setValue))
|
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument %s (set argument format: <flag name>=<flag value>)", setValue))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
split := strings.SplitN(setValue, Separator, 2)
|
split := strings.SplitN(setValue, Separator, 2)
|
||||||
if len(split) != 2 {
|
if len(split) != 2 {
|
||||||
Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument %s (set argument format: <flag name>=<flag value>)", setValue))
|
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument %s (set argument format: <flag name>=<flag value>)", setValue))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,8 +142,8 @@ func mergeSetFlag(configElem reflect.Value, setValues []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for argumentKey, argumentValues := range setMap {
|
for argumentKey, argumentValues := range setMap {
|
||||||
if !Contains(allowedSetFlags, argumentKey) {
|
if !mizu.Contains(allowedSetFlags, argumentKey) {
|
||||||
Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument name \"%s\", flag name must be one of the following: \"%s\"", argumentKey, strings.Join(allowedSetFlags, "\", \"")))
|
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument name \"%s\", flag name must be one of the following: \"%s\"", argumentKey, strings.Join(allowedSetFlags, "\", \"")))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,7 +177,7 @@ func mergeFlagValue(currentElem reflect.Value, flagKey string, flagValue string)
|
|||||||
|
|
||||||
parsedValue, err := getParsedValue(currentFieldKind, flagValue)
|
parsedValue, err := getParsedValue(currentFieldKind, flagValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Warningf(uiUtils.Warning, fmt.Sprintf("Invalid value %s for flag name %s, expected %s", flagValue, flagKey, currentFieldKind))
|
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Invalid value %s for flag name %s, expected %s", flagValue, flagKey, currentFieldKind))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,7 +201,7 @@ func mergeFlagValues(currentElem reflect.Value, flagKey string, flagValues []str
|
|||||||
}
|
}
|
||||||
|
|
||||||
if currentFieldKind != reflect.Slice {
|
if currentFieldKind != reflect.Slice {
|
||||||
Log.Warningf(uiUtils.Warning, fmt.Sprintf("Invalid values %s for flag name %s, expected %s", strings.Join(flagValues, ","), flagKey, currentFieldKind))
|
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Invalid values %s for flag name %s, expected %s", strings.Join(flagValues, ","), flagKey, currentFieldKind))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,7 +211,7 @@ func mergeFlagValues(currentElem reflect.Value, flagKey string, flagValues []str
|
|||||||
for _, flagValue := range flagValues {
|
for _, flagValue := range flagValues {
|
||||||
parsedValue, err := getParsedValue(flagValueKind, flagValue)
|
parsedValue, err := getParsedValue(flagValueKind, flagValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Warningf(uiUtils.Warning, fmt.Sprintf("Invalid value %s for flag name %s, expected %s", flagValue, flagKey, flagValueKind))
|
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Invalid value %s for flag name %s, expected %s", flagValue, flagKey, flagValueKind))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -1,9 +1,9 @@
|
|||||||
package mizu
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/up9inc/mizu/cli/config/configStructs"
|
||||||
"github.com/up9inc/mizu/cli/mizu/configStructs"
|
"github.com/up9inc/mizu/cli/mizu"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -27,7 +27,7 @@ type ConfigStruct struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (config *ConfigStruct) SetDefaults() {
|
func (config *ConfigStruct) SetDefaults() {
|
||||||
config.AgentImage = fmt.Sprintf("gcr.io/up9-docker-hub/mizu/%s:%s", Branch, SemVer)
|
config.AgentImage = fmt.Sprintf("gcr.io/up9-docker-hub/mizu/%s:%s", mizu.Branch, mizu.SemVer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *ConfigStruct) IsNsRestrictedMode() bool {
|
func (config *ConfigStruct) IsNsRestrictedMode() bool {
|
@ -1,7 +1,7 @@
|
|||||||
package mizu_test
|
package config_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/config"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -10,10 +10,10 @@ import (
|
|||||||
func TestConfigWriteIgnoresReadonlyFields(t *testing.T) {
|
func TestConfigWriteIgnoresReadonlyFields(t *testing.T) {
|
||||||
var readonlyFields []string
|
var readonlyFields []string
|
||||||
|
|
||||||
configElem := reflect.ValueOf(&mizu.ConfigStruct{}).Elem()
|
configElem := reflect.ValueOf(&config.ConfigStruct{}).Elem()
|
||||||
getFieldsWithReadonlyTag(configElem, &readonlyFields)
|
getFieldsWithReadonlyTag(configElem, &readonlyFields)
|
||||||
|
|
||||||
config, _ := mizu.GetConfigWithDefaults()
|
config, _ := config.GetConfigWithDefaults()
|
||||||
for _, readonlyField := range readonlyFields {
|
for _, readonlyField := range readonlyFields {
|
||||||
if strings.Contains(config, readonlyField) {
|
if strings.Contains(config, readonlyField) {
|
||||||
t.Errorf("unexpected result - readonly field: %v, config: %v", readonlyField, config)
|
t.Errorf("unexpected result - readonly field: %v, config: %v", readonlyField, config)
|
||||||
@ -31,8 +31,8 @@ func getFieldsWithReadonlyTag(currentElem reflect.Value, readonlyFields *[]strin
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := currentField.Tag.Lookup(mizu.ReadonlyTag); ok {
|
if _, ok := currentField.Tag.Lookup(config.ReadonlyTag); ok {
|
||||||
fieldNameByTag := strings.Split(currentField.Tag.Get(mizu.FieldNameTag), ",")[0]
|
fieldNameByTag := strings.Split(currentField.Tag.Get(config.FieldNameTag), ",")[0]
|
||||||
*readonlyFields = append(*readonlyFields, fieldNameByTag)
|
*readonlyFields = append(*readonlyFields, fieldNameByTag)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,9 +3,7 @@ package errormessage
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/up9inc/mizu/cli/config"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
|
||||||
|
|
||||||
regexpsyntax "regexp/syntax"
|
regexpsyntax "regexp/syntax"
|
||||||
|
|
||||||
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
@ -20,9 +18,9 @@ func FormatError(err error) error {
|
|||||||
"supply the required permission or control Mizu's access to namespaces by setting %s "+
|
"supply the required permission or control Mizu's access to namespaces by setting %s "+
|
||||||
"in the config file or setting the tapped namespace with --%s %s=<NAMEPSACE>",
|
"in the config file or setting the tapped namespace with --%s %s=<NAMEPSACE>",
|
||||||
err,
|
err,
|
||||||
mizu.MizuResourcesNamespaceConfigName,
|
config.MizuResourcesNamespaceConfigName,
|
||||||
mizu.SetCommandName,
|
config.SetCommandName,
|
||||||
mizu.MizuResourcesNamespaceConfigName)
|
config.MizuResourcesNamespaceConfigName)
|
||||||
} else if syntaxError, isSyntaxError := asRegexSyntaxError(err); isSyntaxError {
|
} else if syntaxError, isSyntaxError := asRegexSyntaxError(err); isSyntaxError {
|
||||||
errorNew = fmt.Errorf("regex %s is invalid: %w", syntaxError.Expr, err)
|
errorNew = fmt.Errorf("regex %s is invalid: %w", syntaxError.Expr, err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,58 +0,0 @@
|
|||||||
package fsUtils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"archive/zip"
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"github.com/up9inc/mizu/cli/kubernetes"
|
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
|
||||||
)
|
|
||||||
|
|
||||||
func DumpLogs(provider *kubernetes.Provider, ctx context.Context, filePath string) error {
|
|
||||||
podExactRegex := regexp.MustCompile("^" + mizu.MizuResourcesPrefix)
|
|
||||||
pods, err := provider.ListAllPodsMatchingRegex(ctx, podExactRegex, []string{mizu.Config.MizuResourcesNamespace})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(pods) == 0 {
|
|
||||||
return fmt.Errorf("no mizu pods found in namespace %s", mizu.Config.MizuResourcesNamespace)
|
|
||||||
}
|
|
||||||
|
|
||||||
newZipFile, err := os.Create(filePath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer newZipFile.Close()
|
|
||||||
zipWriter := zip.NewWriter(newZipFile)
|
|
||||||
defer zipWriter.Close()
|
|
||||||
|
|
||||||
for _, pod := range pods {
|
|
||||||
logs, err := provider.GetPodLogs(pod.Namespace, pod.Name, ctx)
|
|
||||||
if err != nil {
|
|
||||||
mizu.Log.Errorf("Failed to get logs, %v", err)
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
mizu.Log.Debugf("Successfully read log length %d for pod: %s.%s", len(logs), pod.Namespace, pod.Name)
|
|
||||||
}
|
|
||||||
if err := AddStrToZip(zipWriter, logs, fmt.Sprintf("%s.%s.log", pod.Namespace, pod.Name)); err != nil {
|
|
||||||
mizu.Log.Errorf("Failed write logs, %v", err)
|
|
||||||
} else {
|
|
||||||
mizu.Log.Infof("Successfully added log length %d from pod: %s.%s", len(logs), pod.Namespace, pod.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := AddFileToZip(zipWriter, mizu.GetConfigFilePath()); err != nil {
|
|
||||||
mizu.Log.Debugf("Failed write file, %v", err)
|
|
||||||
} else {
|
|
||||||
mizu.Log.Infof("Successfully added file %s", mizu.GetConfigFilePath())
|
|
||||||
}
|
|
||||||
if err := AddFileToZip(zipWriter, mizu.GetLogFilePath()); err != nil {
|
|
||||||
mizu.Log.Debugf("Failed write file, %v", err)
|
|
||||||
} else {
|
|
||||||
mizu.Log.Infof("Successfully added file %s", mizu.GetLogFilePath())
|
|
||||||
}
|
|
||||||
mizu.Log.Infof("You can find the zip with all logs in %s\n", filePath)
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -7,6 +7,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -562,7 +563,7 @@ func (provider *Provider) CreateConfigMap(ctx context.Context, namespace string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (provider *Provider) ApplyMizuTapperDaemonSet(ctx context.Context, namespace string, daemonSetName string, podImage string, tapperPodName string, apiServerPodIp string, nodeToTappedPodIPMap map[string][]string, serviceAccountName string, tapOutgoing bool) error {
|
func (provider *Provider) ApplyMizuTapperDaemonSet(ctx context.Context, namespace string, daemonSetName string, podImage string, tapperPodName string, apiServerPodIp string, nodeToTappedPodIPMap map[string][]string, serviceAccountName string, tapOutgoing bool) error {
|
||||||
mizu.Log.Debugf("Applying %d tapper deamonsets, ns: %s, daemonSetName: %s, podImage: %s, tapperPodName: %s", len(nodeToTappedPodIPMap), namespace, daemonSetName, podImage, tapperPodName)
|
logger.Log.Debugf("Applying %d tapper deamonsets, ns: %s, daemonSetName: %s, podImage: %s, tapperPodName: %s", len(nodeToTappedPodIPMap), namespace, daemonSetName, podImage, tapperPodName)
|
||||||
|
|
||||||
if len(nodeToTappedPodIPMap) == 0 {
|
if len(nodeToTappedPodIPMap) == 0 {
|
||||||
return fmt.Errorf("Daemon set %s must tap at least 1 pod", daemonSetName)
|
return fmt.Errorf("Daemon set %s must tap at least 1 pod", daemonSetName)
|
||||||
@ -745,7 +746,7 @@ func loadKubernetesConfiguration(kubeConfigPath string) clientcmd.ClientConfig {
|
|||||||
kubeConfigPath = filepath.Join(home, ".kube", "config")
|
kubeConfigPath = filepath.Join(home, ".kube", "config")
|
||||||
}
|
}
|
||||||
|
|
||||||
mizu.Log.Debugf("Using kube config %s", kubeConfigPath)
|
logger.Log.Debugf("Using kube config %s", kubeConfigPath)
|
||||||
configPathList := filepath.SplitList(kubeConfigPath)
|
configPathList := filepath.SplitList(kubeConfigPath)
|
||||||
configLoadingRules := &clientcmd.ClientConfigLoadingRules{}
|
configLoadingRules := &clientcmd.ClientConfigLoadingRules{}
|
||||||
if len(configPathList) <= 1 {
|
if len(configPathList) <= 1 {
|
||||||
|
@ -2,7 +2,7 @@ package kubernetes
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
"k8s.io/kubectl/pkg/proxy"
|
"k8s.io/kubectl/pkg/proxy"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -14,7 +14,7 @@ const k8sProxyApiPrefix = "/"
|
|||||||
const mizuServicePort = 80
|
const mizuServicePort = 80
|
||||||
|
|
||||||
func StartProxy(kubernetesProvider *Provider, mizuPort uint16, mizuNamespace string, mizuServiceName string) error {
|
func StartProxy(kubernetesProvider *Provider, mizuPort uint16, mizuNamespace string, mizuServiceName string) error {
|
||||||
mizu.Log.Debugf("Starting proxy. namespace: [%v], service name: [%s], port: [%v]", mizuNamespace, mizuServiceName, mizuPort)
|
logger.Log.Debugf("Starting proxy. namespace: [%v], service name: [%s], port: [%v]", mizuNamespace, mizuServiceName, mizuPort)
|
||||||
filter := &proxy.FilterServer{
|
filter := &proxy.FilterServer{
|
||||||
AcceptPaths: proxy.MakeRegexpArrayOrDie(proxy.DefaultPathAcceptRE),
|
AcceptPaths: proxy.MakeRegexpArrayOrDie(proxy.DefaultPathAcceptRE),
|
||||||
RejectPaths: proxy.MakeRegexpArrayOrDie(proxy.DefaultPathRejectRE),
|
RejectPaths: proxy.MakeRegexpArrayOrDie(proxy.DefaultPathRejectRE),
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
package mizu
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/op/go-logging"
|
"github.com/op/go-logging"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
@ -13,7 +14,7 @@ var format = logging.MustStringFormatter(
|
|||||||
)
|
)
|
||||||
|
|
||||||
func GetLogFilePath() string {
|
func GetLogFilePath() string {
|
||||||
return path.Join(GetMizuFolderPath(), "mizu_cli.log")
|
return path.Join(mizu.GetMizuFolderPath(), "mizu_cli.log")
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitLogger() {
|
func InitLogger() {
|
||||||
@ -34,5 +35,5 @@ func InitLogger() {
|
|||||||
logging.SetBackend(backend1Leveled, backend2Formatter)
|
logging.SetBackend(backend1Leveled, backend2Formatter)
|
||||||
|
|
||||||
Log.Debugf("\n\n\n")
|
Log.Debugf("\n\n\n")
|
||||||
Log.Debugf("Running mizu version %v", SemVer)
|
Log.Debugf("Running mizu version %v", mizu.SemVer)
|
||||||
}
|
}
|
@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/up9inc/mizu/cli/cmd"
|
"github.com/up9inc/mizu/cli/cmd"
|
||||||
"github.com/up9inc/mizu/cli/goUtils"
|
"github.com/up9inc/mizu/cli/mizu/goUtils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
60
cli/mizu/fsUtils/mizuLogsUtils.go
Normal file
60
cli/mizu/fsUtils/mizuLogsUtils.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package fsUtils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/zip"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/up9inc/mizu/cli/config"
|
||||||
|
"github.com/up9inc/mizu/cli/kubernetes"
|
||||||
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DumpLogs(provider *kubernetes.Provider, ctx context.Context, filePath string) error {
|
||||||
|
podExactRegex := regexp.MustCompile("^" + mizu.MizuResourcesPrefix)
|
||||||
|
pods, err := provider.ListAllPodsMatchingRegex(ctx, podExactRegex, []string{config.Config.MizuResourcesNamespace})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(pods) == 0 {
|
||||||
|
return fmt.Errorf("no mizu pods found in namespace %s", config.Config.MizuResourcesNamespace)
|
||||||
|
}
|
||||||
|
|
||||||
|
newZipFile, err := os.Create(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer newZipFile.Close()
|
||||||
|
zipWriter := zip.NewWriter(newZipFile)
|
||||||
|
defer zipWriter.Close()
|
||||||
|
|
||||||
|
for _, pod := range pods {
|
||||||
|
logs, err := provider.GetPodLogs(pod.Namespace, pod.Name, ctx)
|
||||||
|
if err != nil {
|
||||||
|
logger.Log.Errorf("Failed to get logs, %v", err)
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
logger.Log.Debugf("Successfully read log length %d for pod: %s.%s", len(logs), pod.Namespace, pod.Name)
|
||||||
|
}
|
||||||
|
if err := AddStrToZip(zipWriter, logs, fmt.Sprintf("%s.%s.log", pod.Namespace, pod.Name)); err != nil {
|
||||||
|
logger.Log.Errorf("Failed write logs, %v", err)
|
||||||
|
} else {
|
||||||
|
logger.Log.Infof("Successfully added log length %d from pod: %s.%s", len(logs), pod.Namespace, pod.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := AddFileToZip(zipWriter, config.GetConfigFilePath()); err != nil {
|
||||||
|
logger.Log.Debugf("Failed write file, %v", err)
|
||||||
|
} else {
|
||||||
|
logger.Log.Infof("Successfully added file %s", config.GetConfigFilePath())
|
||||||
|
}
|
||||||
|
if err := AddFileToZip(zipWriter, logger.GetLogFilePath()); err != nil {
|
||||||
|
logger.Log.Debugf("Failed write file, %v", err)
|
||||||
|
} else {
|
||||||
|
logger.Log.Infof("Successfully added file %s", logger.GetLogFilePath())
|
||||||
|
}
|
||||||
|
logger.Log.Infof("You can find the zip with all logs in %s\n", filePath)
|
||||||
|
return nil
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package goUtils
|
package goUtils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
)
|
)
|
||||||
@ -10,7 +10,7 @@ func HandleExcWrapper(fn interface{}, params ...interface{}) (result []reflect.V
|
|||||||
defer func() {
|
defer func() {
|
||||||
if panicMessage := recover(); panicMessage != nil {
|
if panicMessage := recover(); panicMessage != nil {
|
||||||
stack := debug.Stack()
|
stack := debug.Stack()
|
||||||
mizu.Log.Fatalf("Unhandled panic: %v\n stack: %s", panicMessage, stack)
|
logger.Log.Fatalf("Unhandled panic: %v\n stack: %s", panicMessage, stack)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
f := reflect.ValueOf(fn)
|
f := reflect.ValueOf(fn)
|
@ -1,9 +1,11 @@
|
|||||||
package mizu
|
package version
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -41,22 +43,22 @@ func CheckVersionCompatibility(port uint16) (bool, error) {
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if semver.SemVersion(apiSemVer).Major() == semver.SemVersion(SemVer).Major() &&
|
if semver.SemVersion(apiSemVer).Major() == semver.SemVersion(mizu.SemVer).Major() &&
|
||||||
semver.SemVersion(apiSemVer).Minor() == semver.SemVersion(SemVer).Minor() {
|
semver.SemVersion(apiSemVer).Minor() == semver.SemVersion(mizu.SemVer).Minor() {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.Errorf(uiUtils.Red, fmt.Sprintf("cli version (%s) is not compatible with api version (%s)", SemVer, apiSemVer))
|
logger.Log.Errorf(uiUtils.Red, fmt.Sprintf("cli version (%s) is not compatible with api version (%s)", mizu.SemVer, apiSemVer))
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckNewerVersion() {
|
func CheckNewerVersion() {
|
||||||
Log.Debugf("Checking for newer version...")
|
logger.Log.Debugf("Checking for newer version...")
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
client := github.NewClient(nil)
|
client := github.NewClient(nil)
|
||||||
latestRelease, _, err := client.Repositories.GetLatestRelease(context.Background(), "up9inc", "mizu")
|
latestRelease, _, err := client.Repositories.GetLatestRelease(context.Background(), "up9inc", "mizu")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Debugf("[ERROR] Failed to get latest release")
|
logger.Log.Debugf("[ERROR] Failed to get latest release")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,26 +70,26 @@ func CheckNewerVersion() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if versionFileUrl == "" {
|
if versionFileUrl == "" {
|
||||||
Log.Debugf("[ERROR] Version file not found in the latest release")
|
logger.Log.Debugf("[ERROR] Version file not found in the latest release")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := http.Get(versionFileUrl)
|
res, err := http.Get(versionFileUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Debugf("[ERROR] Failed to get the version file %v", err)
|
logger.Log.Debugf("[ERROR] Failed to get the version file %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := ioutil.ReadAll(res.Body)
|
data, err := ioutil.ReadAll(res.Body)
|
||||||
res.Body.Close()
|
res.Body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Debugf("[ERROR] Failed to read the version file -> %v", err)
|
logger.Log.Debugf("[ERROR] Failed to read the version file -> %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
gitHubVersion := string(data)
|
gitHubVersion := string(data)
|
||||||
gitHubVersion = gitHubVersion[:len(gitHubVersion)-1]
|
gitHubVersion = gitHubVersion[:len(gitHubVersion)-1]
|
||||||
Log.Debugf("Finished version validation, took %v", time.Since(start))
|
logger.Log.Debugf("Finished version validation, took %v", time.Since(start))
|
||||||
if SemVer < gitHubVersion {
|
if mizu.SemVer < gitHubVersion {
|
||||||
Log.Infof(uiUtils.Yellow, fmt.Sprintf("Update available! %v -> %v (%v)", SemVer, gitHubVersion, *latestRelease.HTMLURL))
|
logger.Log.Infof(uiUtils.Yellow, fmt.Sprintf("Update available! %v -> %v (%v)", mizu.SemVer, gitHubVersion, *latestRelease.HTMLURL))
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,22 +1,25 @@
|
|||||||
package mizu
|
package telemetry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/up9inc/mizu/cli/config"
|
||||||
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
const telemetryUrl = "https://us-east4-up9-prod.cloudfunctions.net/mizu-telemetry"
|
const telemetryUrl = "https://us-east4-up9-prod.cloudfunctions.net/mizu-telemetry"
|
||||||
|
|
||||||
func ReportRun(cmd string, args interface{}) {
|
func ReportRun(cmd string, args interface{}) {
|
||||||
if !Config.Telemetry {
|
if !config.Config.Telemetry {
|
||||||
Log.Debugf("not reporting due to config value")
|
logger.Log.Debugf("not reporting due to config value")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if Branch != "main" && Branch != "develop" {
|
if mizu.Branch != "main" && mizu.Branch != "develop" {
|
||||||
Log.Debugf("not reporting telemetry on private branches")
|
logger.Log.Debugf("not reporting telemetry on private branches")
|
||||||
}
|
}
|
||||||
|
|
||||||
argsBytes, _ := json.Marshal(args)
|
argsBytes, _ := json.Marshal(args)
|
||||||
@ -25,16 +28,16 @@ func ReportRun(cmd string, args interface{}) {
|
|||||||
"cmd": cmd,
|
"cmd": cmd,
|
||||||
"args": string(argsBytes),
|
"args": string(argsBytes),
|
||||||
"component": "mizu_cli",
|
"component": "mizu_cli",
|
||||||
"BuildTimestamp": BuildTimestamp,
|
"BuildTimestamp": mizu.BuildTimestamp,
|
||||||
"Branch": Branch,
|
"Branch": mizu.Branch,
|
||||||
"version": SemVer}
|
"version": mizu.SemVer}
|
||||||
argsMap["message"] = fmt.Sprintf("mizu %v - %v", argsMap["cmd"], string(argsBytes))
|
argsMap["message"] = fmt.Sprintf("mizu %v - %v", argsMap["cmd"], string(argsBytes))
|
||||||
|
|
||||||
jsonValue, _ := json.Marshal(argsMap)
|
jsonValue, _ := json.Marshal(argsMap)
|
||||||
|
|
||||||
if resp, err := http.Post(telemetryUrl, "application/json", bytes.NewBuffer(jsonValue)); err != nil {
|
if resp, err := http.Post(telemetryUrl, "application/json", bytes.NewBuffer(jsonValue)); err != nil {
|
||||||
Log.Debugf("error sending telemetry err: %v, response %v", err, resp)
|
logger.Log.Debugf("error sending telemetry err: %v, response %v", err, resp)
|
||||||
} else {
|
} else {
|
||||||
Log.Debugf("Successfully reported telemetry")
|
logger.Log.Debugf("Successfully reported telemetry")
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user