mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-09-14 13:49:42 +00:00
added proxy logs, added events logs (#254)
This commit is contained in:
@@ -5,6 +5,7 @@ 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/logger"
|
||||||
"github.com/up9inc/mizu/cli/uiUtils"
|
"github.com/up9inc/mizu/cli/uiUtils"
|
||||||
"github.com/up9inc/mizu/shared"
|
"github.com/up9inc/mizu/shared"
|
||||||
@@ -18,18 +19,27 @@ import (
|
|||||||
type apiServerProvider struct {
|
type apiServerProvider struct {
|
||||||
url string
|
url string
|
||||||
isReady bool
|
isReady bool
|
||||||
|
retries int
|
||||||
}
|
}
|
||||||
|
|
||||||
var Provider = apiServerProvider{}
|
var Provider = apiServerProvider{retries: config.GetIntEnvConfig(config.ApiServerRetries, 20)}
|
||||||
|
|
||||||
func (provider *apiServerProvider) InitAndTestConnection(url string, retries int) error {
|
func (provider *apiServerProvider) InitAndTestConnection(url string) error {
|
||||||
healthUrl := fmt.Sprintf("%s/", url)
|
healthUrl := fmt.Sprintf("%s/", url)
|
||||||
retriesLeft := retries
|
retriesLeft := provider.retries
|
||||||
for retriesLeft > 0 {
|
for retriesLeft > 0 {
|
||||||
if response, err := http.Get(healthUrl); err != nil {
|
if response, err := http.Get(healthUrl); err != nil {
|
||||||
logger.Log.Debugf("[ERROR] failed connecting to api server %v", err)
|
logger.Log.Debugf("[ERROR] failed connecting to api server %v", err)
|
||||||
} else if response.StatusCode != 200 {
|
} else if response.StatusCode != 200 {
|
||||||
logger.Log.Debugf("can't connect to api server yet, response status code %v", response.StatusCode)
|
responseBody := ""
|
||||||
|
data, readErr := ioutil.ReadAll(response.Body)
|
||||||
|
if readErr == nil {
|
||||||
|
responseBody = string(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Log.Debugf("can't connect to api server yet, response status code: %v, body: %v", response.StatusCode, responseBody)
|
||||||
|
|
||||||
|
response.Body.Close()
|
||||||
} else {
|
} else {
|
||||||
logger.Log.Debugf("connection test to api server passed successfully")
|
logger.Log.Debugf("connection test to api server passed successfully")
|
||||||
break
|
break
|
||||||
@@ -40,7 +50,7 @@ func (provider *apiServerProvider) InitAndTestConnection(url string, retries int
|
|||||||
|
|
||||||
if retriesLeft == 0 {
|
if retriesLeft == 0 {
|
||||||
provider.isReady = false
|
provider.isReady = false
|
||||||
return fmt.Errorf("couldn't reach the api server after %v retries", retries)
|
return fmt.Errorf("couldn't reach the api server after %v retries", provider.retries)
|
||||||
}
|
}
|
||||||
provider.url = url
|
provider.url = url
|
||||||
provider.isReady = true
|
provider.isReady = true
|
||||||
|
@@ -26,6 +26,8 @@ func startProxyReportErrorIfAny(kubernetesProvider *kubernetes.Provider, cancel
|
|||||||
"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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Log.Debugf("proxy ended")
|
||||||
}
|
}
|
||||||
|
|
||||||
func waitForFinish(ctx context.Context, cancel context.CancelFunc) {
|
func waitForFinish(ctx context.Context, cancel context.CancelFunc) {
|
||||||
|
@@ -18,7 +18,7 @@ var fetchCmd = &cobra.Command{
|
|||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
go telemetry.ReportRun("fetch", config.Config.Fetch)
|
go telemetry.ReportRun("fetch", config.Config.Fetch)
|
||||||
|
|
||||||
if err := apiserver.Provider.InitAndTestConnection(GetApiServerUrl(), 1); err != nil {
|
if err := apiserver.Provider.InitAndTestConnection(GetApiServerUrl()); err != nil {
|
||||||
logger.Log.Errorf(uiUtils.Error, "Couldn't connect to API server, make sure one running")
|
logger.Log.Errorf(uiUtils.Error, "Couldn't connect to API server, make sure one running")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func RunMizuFetch() {
|
func RunMizuFetch() {
|
||||||
if err := apiserver.Provider.InitAndTestConnection(GetApiServerUrl(), 5); err != nil {
|
if err := apiserver.Provider.InitAndTestConnection(GetApiServerUrl()); err != nil {
|
||||||
logger.Log.Errorf(uiUtils.Error, "Couldn't connect to API server, check logs")
|
logger.Log.Errorf(uiUtils.Error, "Couldn't connect to API server, check logs")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -497,7 +497,7 @@ func watchApiServerPod(ctx context.Context, kubernetesProvider *kubernetes.Provi
|
|||||||
isPodReady = true
|
isPodReady = true
|
||||||
go startProxyReportErrorIfAny(kubernetesProvider, cancel)
|
go startProxyReportErrorIfAny(kubernetesProvider, cancel)
|
||||||
|
|
||||||
if err := apiserver.Provider.InitAndTestConnection(GetApiServerUrl(), 20); err != nil {
|
if err := apiserver.Provider.InitAndTestConnection(GetApiServerUrl()); err != nil {
|
||||||
logger.Log.Errorf(uiUtils.Error, "Couldn't connect to API server, check logs")
|
logger.Log.Errorf(uiUtils.Error, "Couldn't connect to API server, check logs")
|
||||||
cancel()
|
cancel()
|
||||||
break
|
break
|
||||||
|
@@ -43,7 +43,7 @@ func runMizuView() {
|
|||||||
logger.Log.Infof("Establishing connection to k8s cluster...")
|
logger.Log.Infof("Establishing connection to k8s cluster...")
|
||||||
go startProxyReportErrorIfAny(kubernetesProvider, cancel)
|
go startProxyReportErrorIfAny(kubernetesProvider, cancel)
|
||||||
|
|
||||||
if err := apiserver.Provider.InitAndTestConnection(GetApiServerUrl(), 10); err != nil {
|
if err := apiserver.Provider.InitAndTestConnection(GetApiServerUrl()); err != nil {
|
||||||
logger.Log.Errorf(uiUtils.Error, "Couldn't connect to API server, check logs")
|
logger.Log.Errorf(uiUtils.Error, "Couldn't connect to API server, check logs")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
24
cli/config/envConfig.go
Normal file
24
cli/config/envConfig.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ApiServerRetries = "API_SERVER_RETRIES"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetIntEnvConfig(key string, defaultValue int) int {
|
||||||
|
value := os.Getenv(key)
|
||||||
|
if value == "" {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
intValue, err := strconv.Atoi(value)
|
||||||
|
if err != nil {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
return intValue
|
||||||
|
}
|
@@ -739,6 +739,16 @@ func (provider *Provider) GetPodLogs(namespace string, podName string, ctx conte
|
|||||||
return str, nil
|
return str, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (provider *Provider) GetNamespaceEvents(namespace string, ctx context.Context) (string, error) {
|
||||||
|
eventsOpts := metav1.ListOptions{}
|
||||||
|
eventList, err := provider.clientSet.CoreV1().Events(namespace).List(ctx, eventsOpts)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error getting events on ns: %s, %w", namespace, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return eventList.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func getClientSet(config *restclient.Config) (*kubernetes.Clientset, error) {
|
func getClientSet(config *restclient.Config) (*kubernetes.Clientset, error) {
|
||||||
clientSet, err := kubernetes.NewForConfig(config)
|
clientSet, err := kubernetes.NewForConfig(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -39,6 +39,7 @@ func StartProxy(kubernetesProvider *Provider, mizuPort uint16, mizuNamespace str
|
|||||||
server := http.Server{
|
server := http.Server{
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
}
|
}
|
||||||
|
|
||||||
return server.Serve(l)
|
return server.Serve(l)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -39,22 +39,39 @@ func DumpLogs(provider *kubernetes.Provider, ctx context.Context, filePath strin
|
|||||||
} else {
|
} else {
|
||||||
logger.Log.Debugf("Successfully read log length %d for pod: %s.%s", len(logs), pod.Namespace, pod.Name)
|
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 {
|
if err := AddStrToZip(zipWriter, logs, fmt.Sprintf("%s.%s.log", pod.Namespace, pod.Name)); err != nil {
|
||||||
logger.Log.Errorf("Failed write logs, %v", err)
|
logger.Log.Errorf("Failed write logs, %v", err)
|
||||||
} else {
|
} else {
|
||||||
logger.Log.Debugf("Successfully added log length %d from pod: %s.%s", len(logs), pod.Namespace, pod.Name)
|
logger.Log.Debugf("Successfully added log length %d from pod: %s.%s", len(logs), pod.Namespace, pod.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
events, err := provider.GetNamespaceEvents(config.Config.MizuResourcesNamespace, ctx)
|
||||||
|
if err != nil {
|
||||||
|
logger.Log.Debugf("Failed to get k8b events, %v", err)
|
||||||
|
} else {
|
||||||
|
logger.Log.Debugf("Successfully read events for k8b namespace: %s", config.Config.MizuResourcesNamespace)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := AddStrToZip(zipWriter, events, fmt.Sprintf("%s_events.log", config.Config.MizuResourcesNamespace)); err != nil {
|
||||||
|
logger.Log.Debugf("Failed write logs, %v", err)
|
||||||
|
} else {
|
||||||
|
logger.Log.Debugf("Successfully added events for k8b namespace: %s", config.Config.MizuResourcesNamespace)
|
||||||
|
}
|
||||||
|
|
||||||
if err := AddFileToZip(zipWriter, config.Config.ConfigFilePath); err != nil {
|
if err := AddFileToZip(zipWriter, config.Config.ConfigFilePath); err != nil {
|
||||||
logger.Log.Debugf("Failed write file, %v", err)
|
logger.Log.Debugf("Failed write file, %v", err)
|
||||||
} else {
|
} else {
|
||||||
logger.Log.Debugf("Successfully added file %s", config.Config.ConfigFilePath)
|
logger.Log.Debugf("Successfully added file %s", config.Config.ConfigFilePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := AddFileToZip(zipWriter, logger.GetLogFilePath()); err != nil {
|
if err := AddFileToZip(zipWriter, logger.GetLogFilePath()); err != nil {
|
||||||
logger.Log.Debugf("Failed write file, %v", err)
|
logger.Log.Debugf("Failed write file, %v", err)
|
||||||
} else {
|
} else {
|
||||||
logger.Log.Debugf("Successfully added file %s", logger.GetLogFilePath())
|
logger.Log.Debugf("Successfully added file %s", logger.GetLogFilePath())
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log.Infof("You can find the zip file with all logs in %s\n", filePath)
|
logger.Log.Infof("You can find the zip file with all logs in %s\n", filePath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user