mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-06-26 00:04:33 +00:00
Extracted tap config to consistent volume (#617)
This commit is contained in:
parent
f102079e3c
commit
a55f51f0e7
@ -11,18 +11,18 @@ import (
|
|||||||
"mizuserver/pkg/config"
|
"mizuserver/pkg/config"
|
||||||
"mizuserver/pkg/models"
|
"mizuserver/pkg/models"
|
||||||
"mizuserver/pkg/providers"
|
"mizuserver/pkg/providers"
|
||||||
|
"mizuserver/pkg/providers/tapConfig"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var globalTapConfig = &models.TapConfig{TappedNamespaces: make(map[string]bool)}
|
|
||||||
var cancelTapperSyncer context.CancelFunc
|
var cancelTapperSyncer context.CancelFunc
|
||||||
|
|
||||||
func PostTapConfig(c *gin.Context) {
|
func PostTapConfig(c *gin.Context) {
|
||||||
tapConfig := &models.TapConfig{}
|
requestTapConfig := &models.TapConfig{}
|
||||||
|
|
||||||
if err := c.Bind(tapConfig); err != nil {
|
if err := c.Bind(requestTapConfig); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, err)
|
c.JSON(http.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -37,7 +37,7 @@ func PostTapConfig(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var tappedNamespaces []string
|
var tappedNamespaces []string
|
||||||
for namespace, tapped := range tapConfig.TappedNamespaces {
|
for namespace, tapped := range requestTapConfig.TappedNamespaces {
|
||||||
if tapped {
|
if tapped {
|
||||||
tappedNamespaces = append(tappedNamespaces, namespace)
|
tappedNamespaces = append(tappedNamespaces, namespace)
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ func PostTapConfig(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cancelTapperSyncer = cancel
|
cancelTapperSyncer = cancel
|
||||||
globalTapConfig = tapConfig
|
tapConfig.Save(requestTapConfig)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, "OK")
|
c.JSON(http.StatusOK, "OK")
|
||||||
}
|
}
|
||||||
@ -81,17 +81,19 @@ func GetTapConfig(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
savedTapConfig := tapConfig.Get()
|
||||||
|
|
||||||
tappedNamespaces := make(map[string]bool)
|
tappedNamespaces := make(map[string]bool)
|
||||||
for _, namespace := range namespaces {
|
for _, namespace := range namespaces {
|
||||||
if namespace.Name == config.Config.MizuResourcesNamespace {
|
if namespace.Name == config.Config.MizuResourcesNamespace {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
tappedNamespaces[namespace.Name] = globalTapConfig.TappedNamespaces[namespace.Name]
|
tappedNamespaces[namespace.Name] = savedTapConfig.TappedNamespaces[namespace.Name]
|
||||||
}
|
}
|
||||||
|
|
||||||
tapConfig := models.TapConfig{TappedNamespaces: tappedNamespaces}
|
tapConfigToReturn := models.TapConfig{TappedNamespaces: tappedNamespaces}
|
||||||
c.JSON(http.StatusOK, tapConfig)
|
c.JSON(http.StatusOK, tapConfigToReturn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func startMizuTapperSyncer(ctx context.Context, provider *kubernetes.Provider, targetNamespaces []string, podFilterRegex regexp.Regexp, ignoredUserAgents []string, mizuApiFilteringOptions tapApi.TrafficFilteringOptions, serviceMesh bool) (*kubernetes.MizuTapperSyncer, error) {
|
func startMizuTapperSyncer(ctx context.Context, provider *kubernetes.Provider, targetNamespaces []string, podFilterRegex regexp.Regexp, ignoredUserAgents []string, mizuApiFilteringOptions tapApi.TrafficFilteringOptions, serviceMesh bool) (*kubernetes.MizuTapperSyncer, error) {
|
||||||
|
54
agent/pkg/providers/tapConfig/tap_config_provider.go
Normal file
54
agent/pkg/providers/tapConfig/tap_config_provider.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package tapConfig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/up9inc/mizu/shared"
|
||||||
|
"github.com/up9inc/mizu/shared/logger"
|
||||||
|
"io/ioutil"
|
||||||
|
"mizuserver/pkg/models"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
const FilePath = shared.DataDirPath + "tap-config.json"
|
||||||
|
|
||||||
|
var lock = &sync.Mutex{}
|
||||||
|
|
||||||
|
var config *models.TapConfig
|
||||||
|
|
||||||
|
func Get() *models.TapConfig {
|
||||||
|
if config == nil {
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
if config == nil {
|
||||||
|
if content, err := ioutil.ReadFile(FilePath); err != nil {
|
||||||
|
config = &models.TapConfig{TappedNamespaces: make(map[string]bool)}
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
logger.Log.Errorf("Error loading tap config from file, err: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err = json.Unmarshal(content, &config); err != nil {
|
||||||
|
config = &models.TapConfig{TappedNamespaces: make(map[string]bool)}
|
||||||
|
logger.Log.Errorf("Error while unmarshal tap config, err: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
func Save(tapConfigToSave *models.TapConfig) {
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
config = tapConfigToSave
|
||||||
|
if data, err := json.Marshal(config); err != nil {
|
||||||
|
logger.Log.Errorf("Error while marshal tap config, err: %v", err)
|
||||||
|
} else {
|
||||||
|
if err := ioutil.WriteFile(FilePath, data, 0644); err != nil {
|
||||||
|
logger.Log.Errorf("Error writing tap config to file, err: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user