diff --git a/.gitignore b/.gitignore index 80360b102b..7463a91833 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,4 @@ ipam-*.xml controller-gen build/tools/bin -npm/debugTools/http +npm/debug/http diff --git a/Makefile b/Makefile index 879fd37396..c5d327c982 100644 --- a/Makefile +++ b/Makefile @@ -87,7 +87,7 @@ ACNCLI_DIR = tools/acncli TELEMETRY_CONF_DIR = telemetry CNS_DIR = cns/service CNMS_DIR = cnms/service -NPM_DIR = npm/plugin +NPM_DIR = npm/cmd OUTPUT_DIR = output BUILD_DIR = $(OUTPUT_DIR)/$(GOOS)_$(GOARCH) IMAGE_DIR = $(OUTPUT_DIR)/images diff --git a/npm/Dockerfile b/npm/Dockerfile index a3286aab69..a5e4a9ac9a 100644 --- a/npm/Dockerfile +++ b/npm/Dockerfile @@ -15,4 +15,4 @@ COPY $NPM_BUILD_DIR/azure-npm /usr/bin WORKDIR /usr/bin # Run the npm command by default when the container starts. -ENTRYPOINT ["/usr/bin/azure-npm"] +ENTRYPOINT ["/usr/bin/azure-npm", "start"] diff --git a/npm/azure-npm.yaml b/npm/azure-npm.yaml index 10f5e5dbac..fb5574f486 100644 --- a/npm/azure-npm.yaml +++ b/npm/azure-npm.yaml @@ -77,7 +77,6 @@ spec: effect: NoSchedule - key: CriticalAddonsOnly operator: Exists - containers: - name: azure-npm image: mcr.microsoft.com/containernetworking/azure-npm:v1.4.1 @@ -95,6 +94,8 @@ spec: fieldRef: apiVersion: v1 fieldPath: spec.nodeName + - name: NPM_CONFIG + value: /etc/azure-npm/azure-npm.json volumeMounts: - name: xtables-lock mountPath: /run/xtables.lock @@ -102,6 +103,8 @@ spec: mountPath: /var/log - name: protocols mountPath: /etc/protocols + - name: azure-npm-config + mountPath: /etc/azure-npm hostNetwork: true volumes: - name: log @@ -116,6 +119,9 @@ spec: hostPath: path: /etc/protocols type: File + - name: azure-npm-config + configMap: + name: azure-npm-config serviceAccountName: azure-npm --- apiVersion: v1 @@ -131,3 +137,21 @@ spec: ports: - port: 9000 targetPort: 10091 +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: azure-npm-config + namespace: kube-system +data: + azure-npm.json: | + { + "ResyncPeriodInMinutes": 15, + "ListeningPort": 10091, + "ListeningAddress": "0.0.0.0", + "Toggles": { + "EnablePrometheusMetrics": true, + "EnablePprof": true, + "EnableHTTPDebugAPI": true + } + } diff --git a/npm/cmd/const.go b/npm/cmd/const.go deleted file mode 100644 index c72144672f..0000000000 --- a/npm/cmd/const.go +++ /dev/null @@ -1,8 +0,0 @@ -package cmd - -import "errors" - -var ( - errSrcNotSpecified = errors.New("source not specified") - errDstNotSpecified = errors.New("destination not specified") -) diff --git a/npm/cmd/convertIPtable.go b/npm/cmd/convertiptable.go similarity index 68% rename from npm/cmd/convertIPtable.go rename to npm/cmd/convertiptable.go index ab150ccce1..297e6e39a0 100644 --- a/npm/cmd/convertIPtable.go +++ b/npm/cmd/convertiptable.go @@ -1,9 +1,9 @@ -package cmd +package main import ( "fmt" - "github.com/Azure/azure-container-networking/npm/debugTools/dataplane" + dataplane "github.com/Azure/azure-container-networking/npm/pkg/dataplane/debug" "github.com/spf13/cobra" ) @@ -16,8 +16,8 @@ var convertIPtableCmd = &cobra.Command{ if iptableName == "" { iptableName = "filter" } - npmCacheF, _ := cmd.Flags().GetString("npmF") - iptableSaveF, _ := cmd.Flags().GetString("iptF") + npmCacheF, _ := cmd.Flags().GetString("cache-file") + iptableSaveF, _ := cmd.Flags().GetString("iptables-file") c := &dataplane.Converter{} if npmCacheF == "" && iptableSaveF == "" { ipTableRulesRes, err := c.GetJSONRulesFromIptables(iptableName) @@ -38,6 +38,6 @@ var convertIPtableCmd = &cobra.Command{ func init() { debugCmd.AddCommand(convertIPtableCmd) - convertIPtableCmd.Flags().StringP("iptF", "i", "", "Set the iptable-save file path (optional)") - convertIPtableCmd.Flags().StringP("npmF", "n", "", "Set the NPM cache file path (optional)") + convertIPtableCmd.Flags().StringP("iptables-file", "i", "", "Set the iptable-save file path (optional)") + convertIPtableCmd.Flags().StringP("cache-file", "c", "", "Set the NPM cache file path (optional)") } diff --git a/npm/cmd/debugCmd.go b/npm/cmd/debug.go similarity index 94% rename from npm/cmd/debugCmd.go rename to npm/cmd/debug.go index 3cef319c46..0a4af10c60 100644 --- a/npm/cmd/debugCmd.go +++ b/npm/cmd/debug.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "github.com/spf13/cobra" diff --git a/npm/cmd/getTuples.go b/npm/cmd/gettuples.go similarity index 69% rename from npm/cmd/getTuples.go rename to npm/cmd/gettuples.go index 6c41491b1f..d7ab95bd60 100644 --- a/npm/cmd/getTuples.go +++ b/npm/cmd/gettuples.go @@ -1,12 +1,21 @@ -package cmd +package main import ( "fmt" - "github.com/Azure/azure-container-networking/npm/debugTools/dataplane" + dataplane "github.com/Azure/azure-container-networking/npm/pkg/dataplane/debug" + "github.com/Azure/azure-container-networking/npm/util/errors" "github.com/spf13/cobra" ) +func init() { + debugCmd.AddCommand(getTuplesCmd) + getTuplesCmd.Flags().StringP("src", "s", "", "set the source") + getTuplesCmd.Flags().StringP("dst", "d", "", "set the destination") + getTuplesCmd.Flags().StringP("iptables-file", "i", "", "Set the iptable-save file path (optional)") + getTuplesCmd.Flags().StringP("cache-file", "c", "", "Set the NPM cache file path (optional)") +} + // getTuplesCmd represents the getTuples command var getTuplesCmd = &cobra.Command{ Use: "gettuples", @@ -14,14 +23,14 @@ var getTuplesCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { src, _ := cmd.Flags().GetString("src") if src == "" { - return fmt.Errorf("%w", errSrcNotSpecified) + return fmt.Errorf("%w", errors.ErrSrcNotSpecified) } dst, _ := cmd.Flags().GetString("dst") if dst == "" { - return fmt.Errorf("%w", errDstNotSpecified) + return fmt.Errorf("%w", errors.ErrDstNotSpecified) } - npmCacheF, _ := cmd.Flags().GetString("npmF") - iptableSaveF, _ := cmd.Flags().GetString("iptF") + npmCacheF, _ := cmd.Flags().GetString("cache-file") + iptableSaveF, _ := cmd.Flags().GetString("iptables-file") srcType := dataplane.GetInputType(src) dstType := dataplane.GetInputType(dst) srcInput := &dataplane.Input{Content: src, Type: srcType} @@ -47,11 +56,3 @@ var getTuplesCmd = &cobra.Command{ return nil }, } - -func init() { - debugCmd.AddCommand(getTuplesCmd) - getTuplesCmd.Flags().StringP("src", "s", "", "set the source") - getTuplesCmd.Flags().StringP("dst", "d", "", "set the destination") - getTuplesCmd.Flags().StringP("iptF", "i", "", "Set the iptable-save file path (optional)") - getTuplesCmd.Flags().StringP("npmF", "n", "", "Set the NPM cache file path (optional)") -} diff --git a/npm/cmd/main.go b/npm/cmd/main.go new file mode 100644 index 0000000000..960a24839b --- /dev/null +++ b/npm/cmd/main.go @@ -0,0 +1,14 @@ +// Copyright 2018 Microsoft. All rights reserved. +// MIT License +package main + +import ( + "github.com/spf13/cobra" +) + +// Version is populated by make during build. +var version string + +func main() { + cobra.CheckErr(rootCmd.Execute()) +} diff --git a/npm/cmd/parseIPtable.go b/npm/cmd/parseiptable.go similarity index 73% rename from npm/cmd/parseIPtable.go rename to npm/cmd/parseiptable.go index 8dd6a25db7..d97d4b2f74 100644 --- a/npm/cmd/parseIPtable.go +++ b/npm/cmd/parseiptable.go @@ -1,9 +1,9 @@ -package cmd +package main import ( "fmt" - "github.com/Azure/azure-container-networking/npm/debugTools/dataplane/parse" + "github.com/Azure/azure-container-networking/npm/pkg/dataplane/parse" "github.com/spf13/cobra" ) @@ -12,7 +12,7 @@ var parseIPtableCmd = &cobra.Command{ Use: "parseiptable", Short: "Parse iptable into Go object, dumping it to the console", RunE: func(cmd *cobra.Command, args []string) error { - iptableSaveF, _ := cmd.Flags().GetString("iptF") + iptableSaveF, _ := cmd.Flags().GetString("iptables-file") if iptableSaveF == "" { iptable, err := parse.Iptables("filter") if err != nil { @@ -33,5 +33,5 @@ var parseIPtableCmd = &cobra.Command{ func init() { debugCmd.AddCommand(parseIPtableCmd) - parseIPtableCmd.Flags().StringP("iptF", "i", "", "Set the iptable-save file path (optional)") + parseIPtableCmd.Flags().StringP("iptables-file", "i", "", "Set the iptable-save file path (optional)") } diff --git a/npm/cmd/root.go b/npm/cmd/root.go index 3f397258b4..f1ff870249 100644 --- a/npm/cmd/root.go +++ b/npm/cmd/root.go @@ -1,53 +1,14 @@ -package cmd +package main import ( - "fmt" - "os" - "github.com/spf13/cobra" - "github.com/spf13/viper" ) -var cfgFile string - // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "azure-npm", Short: "Collection of functions related to Azure NPM's debugging tools", -} - -// Execute adds all child commands to the root command and sets flags appropriately. -// This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute() { - cobra.CheckErr(rootCmd.Execute()) -} - -func init() { - cobra.OnInitialize(initConfig) - - rootCmd.Flags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.azure-npm-debug-cli.yaml)") -} - -// initConfig reads in config file and ENV variables if set. -func initConfig() { - if cfgFile != "" { - // Use config file from the flag. - viper.SetConfigFile(cfgFile) - } else { - // Find home directory. - home, err := os.UserHomeDir() - cobra.CheckErr(err) - - // Search config in home directory with name ".azure-npm-debug-cli" (without extension). - viper.AddConfigPath(home) - viper.SetConfigType("yaml") - viper.SetConfigName(".azure-npm-debug-cli") - } - - viper.AutomaticEnv() // read in environment variables that match - - // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { - fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) - } + CompletionOptions: cobra.CompletionOptions{ + DisableDefaultCmd: true, + }, } diff --git a/npm/plugin/main.go b/npm/cmd/start.go similarity index 58% rename from npm/plugin/main.go rename to npm/cmd/start.go index b15d68215a..5be2385a29 100644 --- a/npm/plugin/main.go +++ b/npm/cmd/start.go @@ -3,17 +3,20 @@ package main import ( + "bytes" + "encoding/json" "fmt" "math/rand" - "os" "time" "github.com/Azure/azure-container-networking/log" "github.com/Azure/azure-container-networking/npm" - "github.com/Azure/azure-container-networking/npm/cmd" + npmconfig "github.com/Azure/azure-container-networking/npm/config" restserver "github.com/Azure/azure-container-networking/npm/http/server" "github.com/Azure/azure-container-networking/npm/metrics" "github.com/Azure/azure-container-networking/npm/util" + "github.com/spf13/cobra" + "github.com/spf13/viper" "k8s.io/apimachinery/pkg/util/wait" k8sversion "k8s.io/apimachinery/pkg/version" "k8s.io/client-go/informers" @@ -23,54 +26,48 @@ import ( "k8s.io/utils/exec" ) -const ( - // waitForTelemetryInSeconds = 60 unused - resyncPeriodInMinutes = 15 -) +func init() { + rootCmd.AddCommand(startNPMCmd) +} -// Version is populated by make during build. -var version string +// getTuplesCmd represents the getTuples command +var startNPMCmd = &cobra.Command{ + Use: "start", + Short: "Starts the Azure NPM process", + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + viper.AutomaticEnv() // read in environment variables that match + viper.SetDefault(npmconfig.ConfigEnvPath, npmconfig.GetConfigPath()) + cfgFile := viper.GetString(npmconfig.ConfigEnvPath) + viper.SetConfigFile(cfgFile) + + // If a config file is found, read it in. + if err := viper.ReadInConfig(); err == nil { + klog.Info("Using config file: ", viper.ConfigFileUsed()) + } else { + klog.Infof("Failed to load config from env %s: %v", npmconfig.ConfigEnvPath, err) + b, _ := json.Marshal(npmconfig.DefaultConfig) + err := viper.ReadConfig(bytes.NewBuffer(b)) + if err != nil { + return fmt.Errorf("failed to read in default with err %w", err) + } + } -func initLogging() error { - log.SetName("azure-npm") - log.SetLevel(log.LevelInfo) - if err := log.SetTargetLogDirectory(log.TargetStdout, ""); err != nil { - log.Logf("Failed to configure logging, err:%v.", err) - return fmt.Errorf("%w", err) - } + return nil + }, - return nil -} -func k8sServerVersion(clientset *kubernetes.Clientset) *k8sversion.Info { - var err error - var serverVersion *k8sversion.Info - for ticker, start := time.NewTicker(1*time.Second).C, time.Now(); time.Since(start) < time.Minute*1; { - <-ticker - serverVersion, err = clientset.ServerVersion() - if err == nil { - break + RunE: func(cmd *cobra.Command, args []string) error { + config := &npmconfig.Config{} + err := viper.Unmarshal(config) + if err != nil { + return fmt.Errorf("failed to load config with error %w", err) } - } - - if err != nil { - metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to retrieving kubernetes version") - panic(err.Error) - } - if err = util.SetIsNewNwPolicyVerFlag(serverVersion); err != nil { - metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to set IsNewNwPolicyVerFlag") - panic(err.Error) - } - return serverVersion + return start(*config) + }, } -func main() { - if len(os.Args) > 1 { - // Cobra will handle invalid command, will prompt user to run `--help` for usage - cmd.Execute() - return - } - +func start(config npmconfig.Config) error { + klog.Infof("loaded config: %+v", config) klog.Infof("Start NPM version: %s", version) var err error @@ -81,26 +78,27 @@ func main() { }() if err = initLogging(); err != nil { - panic(err.Error()) + return err } metrics.InitializeAll() // Creates the in-cluster config - config, err := rest.InClusterConfig() + k8sConfig, err := rest.InClusterConfig() if err != nil { - panic(err.Error()) + return fmt.Errorf("failed to load in cluster config: %w", err) } // Creates the clientset - clientset, err := kubernetes.NewForConfig(config) + clientset, err := kubernetes.NewForConfig(k8sConfig) if err != nil { klog.Infof("clientset creation failed with error %v.", err) - panic(err.Error()) + return fmt.Errorf("failed to generate clientset with cluster config: %w", err) } - // Setting reSyncPeriod to 15 mins - minResyncPeriod := resyncPeriodInMinutes * time.Minute + // Setting reSyncPeriod + minResyncPeriod := time.Duration(config.ResyncPeriodInMinutes) * time.Minute + // Adding some randomness so all NPM pods will not request for info at once. factor := rand.Float64() + 1 resyncPeriod := time.Duration(float64(minResyncPeriod.Nanoseconds()) * factor) @@ -112,16 +110,49 @@ func main() { err = metrics.CreateTelemetryHandle(version, npm.GetAIMetadata()) if err != nil { klog.Infof("CreateTelemetryHandle failed with error %v.", err) - panic(err.Error()) + return fmt.Errorf("CreateTelemetryHandle failed with error %w", err) } - restserver := restserver.NewNpmRestServer(restserver.DefaultHTTPListeningAddress) - go restserver.NPMRestServerListenAndServe(npMgr) + go restserver.NPMRestServerListenAndServe(config, npMgr) - if err = npMgr.Start(wait.NeverStop); err != nil { + if err = npMgr.Start(config, wait.NeverStop); err != nil { metrics.SendErrorLogAndMetric(util.NpmID, "Failed to start NPM due to %s", err) panic(err.Error) } select {} } + +func initLogging() error { + log.SetName("azure-npm") + log.SetLevel(log.LevelInfo) + if err := log.SetTargetLogDirectory(log.TargetStdout, ""); err != nil { + log.Logf("Failed to configure logging, err:%v.", err) + return fmt.Errorf("%w", err) + } + + return nil +} + +func k8sServerVersion(clientset *kubernetes.Clientset) *k8sversion.Info { + var err error + var serverVersion *k8sversion.Info + for ticker, start := time.NewTicker(1*time.Second).C, time.Now(); time.Since(start) < time.Minute*1; { + <-ticker + serverVersion, err = clientset.ServerVersion() + if err == nil { + break + } + } + + if err != nil { + metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to retrieving kubernetes version") + panic(err.Error) + } + + if err = util.SetIsNewNwPolicyVerFlag(serverVersion); err != nil { + metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to set IsNewNwPolicyVerFlag") + panic(err.Error) + } + return serverVersion +} diff --git a/npm/config/config.go b/npm/config/config.go new file mode 100644 index 0000000000..e54f7fc3fe --- /dev/null +++ b/npm/config/config.go @@ -0,0 +1,34 @@ +package npmconfig + +const ( + defaultResyncPeriod = 15 + defaultListeningPort = 10091 + + // ConfigEnvPath is what's used by viper to load config path + ConfigEnvPath = "NPM_CONFIG" +) + +// DefaultConfig is the guaranteed configuration NPM can run in out of the box +var DefaultConfig = Config{ + ResyncPeriodInMinutes: defaultResyncPeriod, + ListeningPort: defaultListeningPort, + ListeningAddress: "0.0.0.0", + Toggles: Toggles{ + EnablePrometheusMetrics: true, + EnablePprof: true, + EnableHTTPDebugAPI: true, + }, +} + +type Config struct { + ResyncPeriodInMinutes int `json:"ResyncPeriodInMinutes"` + ListeningPort int `json:"ListeningPort"` + ListeningAddress string `json:"ListeningAddress"` + Toggles Toggles `json:"Toggles"` +} + +type Toggles struct { + EnablePrometheusMetrics bool + EnablePprof bool + EnableHTTPDebugAPI bool +} diff --git a/npm/config/config_linux.go b/npm/config/config_linux.go new file mode 100644 index 0000000000..47ef83af13 --- /dev/null +++ b/npm/config/config_linux.go @@ -0,0 +1,9 @@ +//go:build !windows +// +build !windows + +package npmconfig + +// GetConfigPath TODO: solidify config paths +func GetConfigPath() string { + return "/etc/azure/azure-vnet/azure-npm.json" +} diff --git a/npm/config/config_windows.go b/npm/config/config_windows.go new file mode 100644 index 0000000000..905745881b --- /dev/null +++ b/npm/config/config_windows.go @@ -0,0 +1,9 @@ +//go:build windows +// +build windows + +package npmconfig + +// GetConfigPath TODO: solidify config paths +func GetConfigPath() string { + return "c:\\k\\azure-npm\\azure-npm.json" +} diff --git a/npm/http/server/server.go b/npm/http/server/server.go index ea11729b13..9fadf7201d 100644 --- a/npm/http/server/server.go +++ b/npm/http/server/server.go @@ -6,61 +6,59 @@ import ( "net/http/pprof" _ "net/http/pprof" - "github.com/Azure/azure-container-networking/log" - "github.com/Azure/azure-container-networking/npm/cache" + npmconfig "github.com/Azure/azure-container-networking/npm/config" "github.com/Azure/azure-container-networking/npm/http/api" "github.com/Azure/azure-container-networking/npm/metrics" + "k8s.io/klog" "github.com/Azure/azure-container-networking/npm" "github.com/gorilla/mux" ) -var ( - DefaultHTTPListeningAddress = fmt.Sprintf("%s:%s", api.DefaultListeningIP, api.DefaultHttpPort) -) - type NPMRestServer struct { listeningAddress string server *http.Server router *mux.Router } -func (n *NPMRestServer) NPMRestServerListenAndServe(npmEncoder npm.NetworkPolicyManagerEncoder) { - n.router = mux.NewRouter() +func NPMRestServerListenAndServe(config npmconfig.Config, npmEncoder npm.NetworkPolicyManagerEncoder) { + rs := NPMRestServer{} + + rs.router = mux.NewRouter() //prometheus handlers - n.router.Handle(api.NodeMetricsPath, metrics.GetHandler(true)) - n.router.Handle(api.ClusterMetricsPath, metrics.GetHandler(false)) + if config.Toggles.EnablePrometheusMetrics { + rs.router.Handle(api.NodeMetricsPath, metrics.GetHandler(true)) + rs.router.Handle(api.ClusterMetricsPath, metrics.GetHandler(false)) + } - // ACN CLI debug handlerss - n.router.Handle(api.NPMMgrPath, n.npmCacheHandler(npmEncoder)).Methods(http.MethodGet) + if config.Toggles.EnableHTTPDebugAPI { + // ACN CLI debug handlerss + rs.router.Handle(api.NPMMgrPath, rs.npmCacheHandler(npmEncoder)).Methods(http.MethodGet) + } - n.router.PathPrefix("/debug/").Handler(http.DefaultServeMux) - n.router.HandleFunc("/debug/pprof/", pprof.Index) - n.router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) - n.router.HandleFunc("/debug/pprof/profile", pprof.Profile) - n.router.HandleFunc("/debug/pprof/symbol", pprof.Symbol) - n.router.HandleFunc("/debug/pprof/trace", pprof.Trace) + if config.Toggles.EnablePprof { + rs.router.PathPrefix("/debug/").Handler(http.DefaultServeMux) + rs.router.HandleFunc("/debug/pprof/", pprof.Index) + rs.router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) + rs.router.HandleFunc("/debug/pprof/profile", pprof.Profile) + rs.router.HandleFunc("/debug/pprof/symbol", pprof.Symbol) + rs.router.HandleFunc("/debug/pprof/trace", pprof.Trace) + } // use default listening address if none is specified - if n.listeningAddress == "" { - n.listeningAddress = DefaultHTTPListeningAddress + if rs.listeningAddress == "" { + rs.listeningAddress = fmt.Sprintf("%s:%d", config.ListeningAddress, config.ListeningPort) } srv := &http.Server{ - Handler: n.router, - Addr: n.listeningAddress, + Handler: rs.router, + Addr: rs.listeningAddress, } - log.Logf("Starting NPM HTTP API on %s... ", n.listeningAddress) - log.Errorf("Failed to start NPM HTTP Server with error: %+v", srv.ListenAndServe()) -} - -func NewNpmRestServer(listeningAddress string) *NPMRestServer { - return &NPMRestServer{ - listeningAddress: listeningAddress, - } + klog.Infof("Starting NPM HTTP API on %s... ", rs.listeningAddress) + klog.Errorf("Failed to start NPM HTTP Server with error: %+v", srv.ListenAndServe()) } func (n *NPMRestServer) npmCacheHandler(npmEncoder npm.NetworkPolicyManagerEncoder) http.Handler { diff --git a/npm/http/server/server_test.go b/npm/http/server/server_test.go index 2e201504e7..33a1a00c39 100644 --- a/npm/http/server/server_test.go +++ b/npm/http/server/server_test.go @@ -37,7 +37,7 @@ func TestGetNPMCacheHandler(t *testing.T) { assert := assert.New(t) npmEncoder := NPMEncoder() - n := NewNpmRestServer("") + n := &NPMRestServer{} handler := n.npmCacheHandler(npmEncoder) req, err := http.NewRequest(http.MethodGet, api.NPMMgrPath, nil) diff --git a/npm/npm.go b/npm/npm.go index 9f863223fd..2264baf65b 100644 --- a/npm/npm.go +++ b/npm/npm.go @@ -13,6 +13,7 @@ import ( "github.com/Azure/azure-container-networking/aitelemetry" + npmconfig "github.com/Azure/azure-container-networking/npm/config" "github.com/Azure/azure-container-networking/npm/ipsm" "github.com/Azure/azure-container-networking/npm/metrics" "github.com/Azure/azure-container-networking/npm/util" @@ -217,7 +218,7 @@ func (npMgr *NetworkPolicyManager) SendClusterMetrics() { } // Start starts shared informers and waits for the shared informer cache to sync. -func (npMgr *NetworkPolicyManager) Start(stopCh <-chan struct{}) error { +func (npMgr *NetworkPolicyManager) Start(config npmconfig.Config, stopCh <-chan struct{}) error { // Do initialization of data plane before starting syncup of each controller to avoid heavy call to api-server if err := npMgr.netPolController.resetDataPlane(); err != nil { return fmt.Errorf("Failed to initialized data plane") diff --git a/npm/debugTools/README.md b/npm/pkg/dataplane/debug/README.md similarity index 100% rename from npm/debugTools/README.md rename to npm/pkg/dataplane/debug/README.md diff --git a/npm/debugTools/dataplane/const.go b/npm/pkg/dataplane/debug/const.go similarity index 88% rename from npm/debugTools/dataplane/const.go rename to npm/pkg/dataplane/debug/const.go index 09ffce6a0e..9a8a274299 100644 --- a/npm/debugTools/dataplane/const.go +++ b/npm/pkg/dataplane/debug/const.go @@ -41,9 +41,9 @@ var ( // To test paser, converter, and trafficAnalyzer with stored files. const ( - iptableSaveFile = "../testFiles/iptableSave" + iptableSaveFile = "../testfiles/iptablesave" // stored file with json compatible form (i.e., can call json.Unmarshal) - // npmCacheFile = "../testFiles/npmCache.json" + // npmCacheFile = ".../testfiles/npmCache.json" // stored file with custom encoding in Encode function in npmCache.go - npmCacheWithCustomFormatFile = "../testFiles/npmCacheWithCustomFormat.json" + npmCacheWithCustomFormatFile = "../testfiles/npmCacheWithCustomFormat.json" ) diff --git a/npm/debugTools/dataplane/converter.go b/npm/pkg/dataplane/debug/converter.go similarity index 97% rename from npm/debugTools/dataplane/converter.go rename to npm/pkg/dataplane/debug/converter.go index e7ec9c6413..81a6ab1151 100644 --- a/npm/debugTools/dataplane/converter.go +++ b/npm/pkg/dataplane/debug/converter.go @@ -13,10 +13,10 @@ import ( "strings" "github.com/Azure/azure-container-networking/npm/cache" - NPMIPtable "github.com/Azure/azure-container-networking/npm/debugTools/dataplane/iptables" - "github.com/Azure/azure-container-networking/npm/debugTools/dataplane/parse" - "github.com/Azure/azure-container-networking/npm/debugTools/pb" "github.com/Azure/azure-container-networking/npm/http/api" + NPMIPtable "github.com/Azure/azure-container-networking/npm/pkg/dataplane/iptables" + "github.com/Azure/azure-container-networking/npm/pkg/dataplane/parse" + "github.com/Azure/azure-container-networking/npm/pkg/dataplane/pb" "github.com/Azure/azure-container-networking/npm/util" "google.golang.org/protobuf/encoding/protojson" ) diff --git a/npm/debugTools/dataplane/converter_test.go b/npm/pkg/dataplane/debug/converter_test.go similarity index 99% rename from npm/debugTools/dataplane/converter_test.go rename to npm/pkg/dataplane/debug/converter_test.go index 9057a9a558..055ec94b42 100644 --- a/npm/debugTools/dataplane/converter_test.go +++ b/npm/pkg/dataplane/debug/converter_test.go @@ -4,8 +4,8 @@ import ( "reflect" "testing" - NPMIPtable "github.com/Azure/azure-container-networking/npm/debugTools/dataplane/iptables" - "github.com/Azure/azure-container-networking/npm/debugTools/pb" + NPMIPtable "github.com/Azure/azure-container-networking/npm/pkg/dataplane/iptables" + "github.com/Azure/azure-container-networking/npm/pkg/dataplane/pb" "github.com/Azure/azure-container-networking/npm/util" "github.com/google/go-cmp/cmp" ) diff --git a/npm/debugTools/dataplane/trafficAnalyzer.go b/npm/pkg/dataplane/debug/trafficanalyzer.go similarity index 99% rename from npm/debugTools/dataplane/trafficAnalyzer.go rename to npm/pkg/dataplane/debug/trafficanalyzer.go index d208f02b94..5e15407624 100644 --- a/npm/debugTools/dataplane/trafficAnalyzer.go +++ b/npm/pkg/dataplane/debug/trafficanalyzer.go @@ -8,7 +8,7 @@ import ( "github.com/Azure/azure-container-networking/npm" "github.com/Azure/azure-container-networking/npm/cache" - "github.com/Azure/azure-container-networking/npm/debugTools/pb" + "github.com/Azure/azure-container-networking/npm/pkg/dataplane/pb" "github.com/Azure/azure-container-networking/npm/util" "google.golang.org/protobuf/encoding/protojson" ) diff --git a/npm/debugTools/dataplane/trafficAnalyzer_test.go b/npm/pkg/dataplane/debug/trafficanalyzer_test.go similarity index 100% rename from npm/debugTools/dataplane/trafficAnalyzer_test.go rename to npm/pkg/dataplane/debug/trafficanalyzer_test.go diff --git a/npm/debugTools/dataplane/iptables/iptable.go b/npm/pkg/dataplane/iptables/iptable.go similarity index 100% rename from npm/debugTools/dataplane/iptables/iptable.go rename to npm/pkg/dataplane/iptables/iptable.go diff --git a/npm/debugTools/dataplane/parse/parser.go b/npm/pkg/dataplane/parse/parser.go similarity index 99% rename from npm/debugTools/dataplane/parse/parser.go rename to npm/pkg/dataplane/parse/parser.go index 2bba85dfe3..c4a9b83d5d 100644 --- a/npm/debugTools/dataplane/parse/parser.go +++ b/npm/pkg/dataplane/parse/parser.go @@ -6,7 +6,7 @@ import ( "io/ioutil" "os/exec" - NPMIPtable "github.com/Azure/azure-container-networking/npm/debugTools/dataplane/iptables" + NPMIPtable "github.com/Azure/azure-container-networking/npm/pkg/dataplane/iptables" "github.com/Azure/azure-container-networking/npm/util" ) diff --git a/npm/debugTools/dataplane/parse/parser_test.go b/npm/pkg/dataplane/parse/parser_test.go similarity index 97% rename from npm/debugTools/dataplane/parse/parser_test.go rename to npm/pkg/dataplane/parse/parser_test.go index be7d48061d..320a96ef24 100644 --- a/npm/debugTools/dataplane/parse/parser_test.go +++ b/npm/pkg/dataplane/parse/parser_test.go @@ -6,12 +6,12 @@ import ( "strings" "testing" - NPMIPtable "github.com/Azure/azure-container-networking/npm/debugTools/dataplane/iptables" + NPMIPtable "github.com/Azure/azure-container-networking/npm/pkg/dataplane/iptables" "github.com/Azure/azure-container-networking/npm/util" ) func TestParseIptablesObjectFile(t *testing.T) { - _, err := IptablesFile(util.IptablesFilterTable, "../../testFiles/iptableSave") + _, err := IptablesFile(util.IptablesFilterTable, "../testfiles/iptablesave") if err != nil { t.Fatal(err) } diff --git a/npm/debugTools/pb/rule.pb.go b/npm/pkg/dataplane/pb/rule.pb.go similarity index 100% rename from npm/debugTools/pb/rule.pb.go rename to npm/pkg/dataplane/pb/rule.pb.go diff --git a/npm/debugTools/pb/rule.proto b/npm/pkg/dataplane/pb/rule.proto similarity index 100% rename from npm/debugTools/pb/rule.proto rename to npm/pkg/dataplane/pb/rule.proto diff --git a/npm/debugTools/testFiles/iptableSave b/npm/pkg/dataplane/testfiles/iptablesave similarity index 100% rename from npm/debugTools/testFiles/iptableSave rename to npm/pkg/dataplane/testfiles/iptablesave diff --git a/npm/debugTools/testFiles/npmCacheWithCustomFormat.json b/npm/pkg/dataplane/testfiles/npmCacheWithCustomFormat.json similarity index 100% rename from npm/debugTools/testFiles/npmCacheWithCustomFormat.json rename to npm/pkg/dataplane/testfiles/npmCacheWithCustomFormat.json diff --git a/npm/debugTools/testFiles/npmCache.json b/npm/pkg/dataplane/testfiles/npmcache.json similarity index 100% rename from npm/debugTools/testFiles/npmCache.json rename to npm/pkg/dataplane/testfiles/npmcache.json diff --git a/npm/util/errors/errors.go b/npm/util/errors/errors.go index db4317a27b..ad942ace4b 100644 --- a/npm/util/errors/errors.go +++ b/npm/util/errors/errors.go @@ -1,12 +1,21 @@ package errors import ( + "errors" "fmt" "strings" "github.com/Azure/azure-container-networking/npm/util" ) +var ( + // ErrSrcNotSpecified thrown during NPM debug cli mode when the source packet is not specified + ErrSrcNotSpecified = errors.New("source not specified") + + // ErrDstNotSpecified thrown during NPM debug cli mode when the source packet is not specified + ErrDstNotSpecified = errors.New("destination not specified") +) + /* ✅ | where Raw !contains "Set cannot be destroyed: it is in use by a kernel component" // Exit status 1 ✅ | where Raw !contains "Elem separator in" // Error: There was an error running command: [ipset -A -exist azure-npm-527074092 10.104.7.252,3000] Stderr: [exit status 1, ipset v7.5: Syntax error: Elem separator in 10.104.7.252,3000, but settype hash:net supports none.]