From 84471bbda537dddc0d074b6ae0f982e04cabc29d Mon Sep 17 00:00:00 2001 From: Mathew Merrick Date: Fri, 20 Aug 2021 15:00:20 -0700 Subject: [PATCH 01/12] package case --- .gitignore | 2 +- npm/cmd/{convertIPtable.go => convertiptable.go} | 2 +- npm/cmd/{debugCmd.go => debugcmd.go} | 0 npm/cmd/{getTuples.go => gettuples.go} | 2 +- npm/cmd/{parseIPtable.go => parseiptable.go} | 2 +- npm/{debugTools => debug}/README.md | 0 npm/{debugTools => debug}/dataplane/const.go | 0 npm/{debugTools => debug}/dataplane/converter.go | 6 +++--- npm/{debugTools => debug}/dataplane/converter_test.go | 4 ++-- npm/{debugTools => debug}/dataplane/iptables/iptable.go | 0 npm/{debugTools => debug}/dataplane/parse/parser.go | 2 +- npm/{debugTools => debug}/dataplane/parse/parser_test.go | 2 +- .../dataplane/trafficanalyzer.go} | 2 +- .../dataplane/trafficanalyzer_test.go} | 0 npm/{debugTools => debug}/pb/rule.pb.go | 0 npm/{debugTools => debug}/pb/rule.proto | 0 .../testFiles/iptableSave => debug/testfiles/iptablesave} | 0 .../npmCache.json => debug/testfiles/npmcache.json} | 0 18 files changed, 12 insertions(+), 12 deletions(-) rename npm/cmd/{convertIPtable.go => convertiptable.go} (94%) rename npm/cmd/{debugCmd.go => debugcmd.go} (100%) rename npm/cmd/{getTuples.go => gettuples.go} (95%) rename npm/cmd/{parseIPtable.go => parseiptable.go} (91%) rename npm/{debugTools => debug}/README.md (100%) rename npm/{debugTools => debug}/dataplane/const.go (100%) rename npm/{debugTools => debug}/dataplane/converter.go (98%) rename npm/{debugTools => debug}/dataplane/converter_test.go (99%) rename npm/{debugTools => debug}/dataplane/iptables/iptable.go (100%) rename npm/{debugTools => debug}/dataplane/parse/parser.go (99%) rename npm/{debugTools => debug}/dataplane/parse/parser_test.go (99%) rename npm/{debugTools/dataplane/trafficAnalyzer.go => debug/dataplane/trafficanalyzer.go} (99%) rename npm/{debugTools/dataplane/trafficAnalyzer_test.go => debug/dataplane/trafficanalyzer_test.go} (100%) rename npm/{debugTools => debug}/pb/rule.pb.go (100%) rename npm/{debugTools => debug}/pb/rule.proto (100%) rename npm/{debugTools/testFiles/iptableSave => debug/testfiles/iptablesave} (100%) rename npm/{debugTools/testFiles/npmCache.json => debug/testfiles/npmcache.json} (100%) 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/npm/cmd/convertIPtable.go b/npm/cmd/convertiptable.go similarity index 94% rename from npm/cmd/convertIPtable.go rename to npm/cmd/convertiptable.go index ab150ccce1..445a86d2cc 100644 --- a/npm/cmd/convertIPtable.go +++ b/npm/cmd/convertiptable.go @@ -3,7 +3,7 @@ package cmd import ( "fmt" - "github.com/Azure/azure-container-networking/npm/debugTools/dataplane" + "github.com/Azure/azure-container-networking/npm/debug/dataplane" "github.com/spf13/cobra" ) diff --git a/npm/cmd/debugCmd.go b/npm/cmd/debugcmd.go similarity index 100% rename from npm/cmd/debugCmd.go rename to npm/cmd/debugcmd.go diff --git a/npm/cmd/getTuples.go b/npm/cmd/gettuples.go similarity index 95% rename from npm/cmd/getTuples.go rename to npm/cmd/gettuples.go index 6c41491b1f..23dd696195 100644 --- a/npm/cmd/getTuples.go +++ b/npm/cmd/gettuples.go @@ -3,7 +3,7 @@ package cmd import ( "fmt" - "github.com/Azure/azure-container-networking/npm/debugTools/dataplane" + "github.com/Azure/azure-container-networking/npm/debug/dataplane" "github.com/spf13/cobra" ) diff --git a/npm/cmd/parseIPtable.go b/npm/cmd/parseiptable.go similarity index 91% rename from npm/cmd/parseIPtable.go rename to npm/cmd/parseiptable.go index 8dd6a25db7..42db2abe26 100644 --- a/npm/cmd/parseIPtable.go +++ b/npm/cmd/parseiptable.go @@ -3,7 +3,7 @@ package cmd import ( "fmt" - "github.com/Azure/azure-container-networking/npm/debugTools/dataplane/parse" + "github.com/Azure/azure-container-networking/npm/debug/dataplane/parse" "github.com/spf13/cobra" ) diff --git a/npm/debugTools/README.md b/npm/debug/README.md similarity index 100% rename from npm/debugTools/README.md rename to npm/debug/README.md diff --git a/npm/debugTools/dataplane/const.go b/npm/debug/dataplane/const.go similarity index 100% rename from npm/debugTools/dataplane/const.go rename to npm/debug/dataplane/const.go diff --git a/npm/debugTools/dataplane/converter.go b/npm/debug/dataplane/converter.go similarity index 98% rename from npm/debugTools/dataplane/converter.go rename to npm/debug/dataplane/converter.go index a56ca5c4e5..411f08c796 100644 --- a/npm/debugTools/dataplane/converter.go +++ b/npm/debug/dataplane/converter.go @@ -13,9 +13,9 @@ import ( "strings" "github.com/Azure/azure-container-networking/npm" - 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" + NPMIPtable "github.com/Azure/azure-container-networking/npm/debug/dataplane/iptables" + "github.com/Azure/azure-container-networking/npm/debug/dataplane/parse" + "github.com/Azure/azure-container-networking/npm/debug/pb" "github.com/Azure/azure-container-networking/npm/http/api" "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/debug/dataplane/converter_test.go similarity index 99% rename from npm/debugTools/dataplane/converter_test.go rename to npm/debug/dataplane/converter_test.go index 11c9740fc5..d7a04bd5be 100644 --- a/npm/debugTools/dataplane/converter_test.go +++ b/npm/debug/dataplane/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/debug/dataplane/iptables" + "github.com/Azure/azure-container-networking/npm/debug/pb" "github.com/Azure/azure-container-networking/npm/util" "github.com/google/go-cmp/cmp" ) diff --git a/npm/debugTools/dataplane/iptables/iptable.go b/npm/debug/dataplane/iptables/iptable.go similarity index 100% rename from npm/debugTools/dataplane/iptables/iptable.go rename to npm/debug/dataplane/iptables/iptable.go diff --git a/npm/debugTools/dataplane/parse/parser.go b/npm/debug/dataplane/parse/parser.go similarity index 99% rename from npm/debugTools/dataplane/parse/parser.go rename to npm/debug/dataplane/parse/parser.go index 2bba85dfe3..8d2955dbd0 100644 --- a/npm/debugTools/dataplane/parse/parser.go +++ b/npm/debug/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/debug/dataplane/iptables" "github.com/Azure/azure-container-networking/npm/util" ) diff --git a/npm/debugTools/dataplane/parse/parser_test.go b/npm/debug/dataplane/parse/parser_test.go similarity index 99% rename from npm/debugTools/dataplane/parse/parser_test.go rename to npm/debug/dataplane/parse/parser_test.go index be7d48061d..7dbe7ed172 100644 --- a/npm/debugTools/dataplane/parse/parser_test.go +++ b/npm/debug/dataplane/parse/parser_test.go @@ -6,7 +6,7 @@ import ( "strings" "testing" - NPMIPtable "github.com/Azure/azure-container-networking/npm/debugTools/dataplane/iptables" + NPMIPtable "github.com/Azure/azure-container-networking/npm/debug/dataplane/iptables" "github.com/Azure/azure-container-networking/npm/util" ) diff --git a/npm/debugTools/dataplane/trafficAnalyzer.go b/npm/debug/dataplane/trafficanalyzer.go similarity index 99% rename from npm/debugTools/dataplane/trafficAnalyzer.go rename to npm/debug/dataplane/trafficanalyzer.go index 693185415d..50fcdb3031 100644 --- a/npm/debugTools/dataplane/trafficAnalyzer.go +++ b/npm/debug/dataplane/trafficanalyzer.go @@ -7,7 +7,7 @@ import ( "strings" "github.com/Azure/azure-container-networking/npm" - "github.com/Azure/azure-container-networking/npm/debugTools/pb" + "github.com/Azure/azure-container-networking/npm/debug/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/debug/dataplane/trafficanalyzer_test.go similarity index 100% rename from npm/debugTools/dataplane/trafficAnalyzer_test.go rename to npm/debug/dataplane/trafficanalyzer_test.go diff --git a/npm/debugTools/pb/rule.pb.go b/npm/debug/pb/rule.pb.go similarity index 100% rename from npm/debugTools/pb/rule.pb.go rename to npm/debug/pb/rule.pb.go diff --git a/npm/debugTools/pb/rule.proto b/npm/debug/pb/rule.proto similarity index 100% rename from npm/debugTools/pb/rule.proto rename to npm/debug/pb/rule.proto diff --git a/npm/debugTools/testFiles/iptableSave b/npm/debug/testfiles/iptablesave similarity index 100% rename from npm/debugTools/testFiles/iptableSave rename to npm/debug/testfiles/iptablesave diff --git a/npm/debugTools/testFiles/npmCache.json b/npm/debug/testfiles/npmcache.json similarity index 100% rename from npm/debugTools/testFiles/npmCache.json rename to npm/debug/testfiles/npmcache.json From 284622fb57eb0dabc32a8d5cd6e2fad088bd5d41 Mon Sep 17 00:00:00 2001 From: Mathew Merrick Date: Mon, 23 Aug 2021 14:46:20 -0700 Subject: [PATCH 02/12] package restructure --- Makefile | 2 +- npm/cmd/{ => cli}/convertiptable.go | 4 ++-- npm/cmd/{gettuples.go => cli/debugcmd.go} | 23 +++++++++---------- npm/cmd/cli/gettuples.go | 9 ++++++++ npm/cmd/{ => cli}/parseiptable.go | 4 ++-- npm/cmd/{ => cli}/root.go | 2 +- npm/cmd/const.go | 8 ------- npm/cmd/debugcmd.go | 15 ------------ npm/{plugin => cmd}/main.go | 4 ++-- npm/config/config.go | 7 ++++++ npm/{ => pkg}/debug/README.md | 0 npm/{ => pkg}/debug/dataplane/const.go | 0 npm/{ => pkg}/debug/dataplane/converter.go | 6 ++--- .../debug/dataplane/converter_test.go | 0 .../debug/dataplane/iptables/iptable.go | 0 npm/{ => pkg}/debug/dataplane/parse/parser.go | 2 +- .../debug/dataplane/parse/parser_test.go | 2 +- .../debug/dataplane/trafficanalyzer.go | 2 +- .../debug/dataplane/trafficanalyzer_test.go | 0 npm/{ => pkg}/debug/pb/rule.pb.go | 0 npm/{ => pkg}/debug/pb/rule.proto | 0 npm/{ => pkg}/debug/testfiles/iptablesave | 0 npm/{ => pkg}/debug/testfiles/npmcache.json | 0 npm/util/errors/errors.go | 6 +++++ 24 files changed, 47 insertions(+), 49 deletions(-) rename npm/cmd/{ => cli}/convertiptable.go (93%) rename npm/cmd/{gettuples.go => cli/debugcmd.go} (69%) create mode 100644 npm/cmd/cli/gettuples.go rename npm/cmd/{ => cli}/parseiptable.go (90%) rename npm/cmd/{ => cli}/root.go (99%) delete mode 100644 npm/cmd/const.go delete mode 100644 npm/cmd/debugcmd.go rename npm/{plugin => cmd}/main.go (97%) create mode 100644 npm/config/config.go rename npm/{ => pkg}/debug/README.md (100%) rename npm/{ => pkg}/debug/dataplane/const.go (100%) rename npm/{ => pkg}/debug/dataplane/converter.go (98%) rename npm/{ => pkg}/debug/dataplane/converter_test.go (100%) rename npm/{ => pkg}/debug/dataplane/iptables/iptable.go (100%) rename npm/{ => pkg}/debug/dataplane/parse/parser.go (99%) rename npm/{ => pkg}/debug/dataplane/parse/parser_test.go (98%) rename npm/{ => pkg}/debug/dataplane/trafficanalyzer.go (99%) rename npm/{ => pkg}/debug/dataplane/trafficanalyzer_test.go (100%) rename npm/{ => pkg}/debug/pb/rule.pb.go (100%) rename npm/{ => pkg}/debug/pb/rule.proto (100%) rename npm/{ => pkg}/debug/testfiles/iptablesave (100%) rename npm/{ => pkg}/debug/testfiles/npmcache.json (100%) 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/cmd/convertiptable.go b/npm/cmd/cli/convertiptable.go similarity index 93% rename from npm/cmd/convertiptable.go rename to npm/cmd/cli/convertiptable.go index 445a86d2cc..9b02aef752 100644 --- a/npm/cmd/convertiptable.go +++ b/npm/cmd/cli/convertiptable.go @@ -1,9 +1,9 @@ -package cmd +package cli import ( "fmt" - "github.com/Azure/azure-container-networking/npm/debug/dataplane" + "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane" "github.com/spf13/cobra" ) diff --git a/npm/cmd/gettuples.go b/npm/cmd/cli/debugcmd.go similarity index 69% rename from npm/cmd/gettuples.go rename to npm/cmd/cli/debugcmd.go index 23dd696195..f1a7964d4a 100644 --- a/npm/cmd/gettuples.go +++ b/npm/cmd/cli/debugcmd.go @@ -1,12 +1,19 @@ -package cmd +package cli import ( "fmt" - "github.com/Azure/azure-container-networking/npm/debug/dataplane" + "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane" + "github.com/Azure/azure-container-networking/npm/util/errors" "github.com/spf13/cobra" ) +// convertIptableCmd represents the convertIptable command +var debugCmd = &cobra.Command{ + Use: "debug", + Short: "Debug mode", +} + // getTuplesCmd represents the getTuples command var getTuplesCmd = &cobra.Command{ Use: "gettuples", @@ -14,11 +21,11 @@ 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.SrcNotSpecified) } dst, _ := cmd.Flags().GetString("dst") if dst == "" { - return fmt.Errorf("%w", errDstNotSpecified) + return fmt.Errorf("%w", errors.DstNotSpecified) } npmCacheF, _ := cmd.Flags().GetString("npmF") iptableSaveF, _ := cmd.Flags().GetString("iptF") @@ -47,11 +54,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/cli/gettuples.go b/npm/cmd/cli/gettuples.go new file mode 100644 index 0000000000..2f98217c78 --- /dev/null +++ b/npm/cmd/cli/gettuples.go @@ -0,0 +1,9 @@ +package cli + +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/parseiptable.go b/npm/cmd/cli/parseiptable.go similarity index 90% rename from npm/cmd/parseiptable.go rename to npm/cmd/cli/parseiptable.go index 42db2abe26..a08c9e7ca7 100644 --- a/npm/cmd/parseiptable.go +++ b/npm/cmd/cli/parseiptable.go @@ -1,9 +1,9 @@ -package cmd +package cli import ( "fmt" - "github.com/Azure/azure-container-networking/npm/debug/dataplane/parse" + "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane/parse" "github.com/spf13/cobra" ) diff --git a/npm/cmd/root.go b/npm/cmd/cli/root.go similarity index 99% rename from npm/cmd/root.go rename to npm/cmd/cli/root.go index 3f397258b4..1cb683bb19 100644 --- a/npm/cmd/root.go +++ b/npm/cmd/cli/root.go @@ -1,4 +1,4 @@ -package cmd +package cli import ( "fmt" 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/debugcmd.go b/npm/cmd/debugcmd.go deleted file mode 100644 index 3cef319c46..0000000000 --- a/npm/cmd/debugcmd.go +++ /dev/null @@ -1,15 +0,0 @@ -package cmd - -import ( - "github.com/spf13/cobra" -) - -// convertIptableCmd represents the convertIptable command -var debugCmd = &cobra.Command{ - Use: "debug", - Short: "Debug mode", -} - -func init() { - rootCmd.AddCommand(debugCmd) -} diff --git a/npm/plugin/main.go b/npm/cmd/main.go similarity index 97% rename from npm/plugin/main.go rename to npm/cmd/main.go index 17448b524b..c7ca9f6b49 100644 --- a/npm/plugin/main.go +++ b/npm/cmd/main.go @@ -10,7 +10,7 @@ import ( "github.com/Azure/azure-container-networking/log" "github.com/Azure/azure-container-networking/npm" - "github.com/Azure/azure-container-networking/npm/cmd" + "github.com/Azure/azure-container-networking/npm/cmd/cli" restserver "github.com/Azure/azure-container-networking/npm/http/server" "github.com/Azure/azure-container-networking/npm/metrics" "k8s.io/apimachinery/pkg/util/wait" @@ -43,7 +43,7 @@ func initLogging() error { func main() { if len(os.Args) > 1 { // Cobra will handle invalid command, will prompt user to run `--help` for usage - cmd.Execute() + cli.Execute() return } diff --git a/npm/config/config.go b/npm/config/config.go new file mode 100644 index 0000000000..5951c35d6b --- /dev/null +++ b/npm/config/config.go @@ -0,0 +1,7 @@ +package config + +type NPMConfig struct { + ResyncPeriodInMinutes int `json:"ResyncPeriodInMinutes"` + ListeningPort int `json:"ListeningPort"` + ListeningAddress string `json:"ListeningAddress"` +} diff --git a/npm/debug/README.md b/npm/pkg/debug/README.md similarity index 100% rename from npm/debug/README.md rename to npm/pkg/debug/README.md diff --git a/npm/debug/dataplane/const.go b/npm/pkg/debug/dataplane/const.go similarity index 100% rename from npm/debug/dataplane/const.go rename to npm/pkg/debug/dataplane/const.go diff --git a/npm/debug/dataplane/converter.go b/npm/pkg/debug/dataplane/converter.go similarity index 98% rename from npm/debug/dataplane/converter.go rename to npm/pkg/debug/dataplane/converter.go index 411f08c796..e58917121b 100644 --- a/npm/debug/dataplane/converter.go +++ b/npm/pkg/debug/dataplane/converter.go @@ -13,10 +13,10 @@ import ( "strings" "github.com/Azure/azure-container-networking/npm" - NPMIPtable "github.com/Azure/azure-container-networking/npm/debug/dataplane/iptables" - "github.com/Azure/azure-container-networking/npm/debug/dataplane/parse" - "github.com/Azure/azure-container-networking/npm/debug/pb" "github.com/Azure/azure-container-networking/npm/http/api" + NPMIPtable "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane/iptables" + "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane/parse" + "github.com/Azure/azure-container-networking/npm/pkg/debug/pb" "github.com/Azure/azure-container-networking/npm/util" "google.golang.org/protobuf/encoding/protojson" networkingv1 "k8s.io/api/networking/v1" diff --git a/npm/debug/dataplane/converter_test.go b/npm/pkg/debug/dataplane/converter_test.go similarity index 100% rename from npm/debug/dataplane/converter_test.go rename to npm/pkg/debug/dataplane/converter_test.go diff --git a/npm/debug/dataplane/iptables/iptable.go b/npm/pkg/debug/dataplane/iptables/iptable.go similarity index 100% rename from npm/debug/dataplane/iptables/iptable.go rename to npm/pkg/debug/dataplane/iptables/iptable.go diff --git a/npm/debug/dataplane/parse/parser.go b/npm/pkg/debug/dataplane/parse/parser.go similarity index 99% rename from npm/debug/dataplane/parse/parser.go rename to npm/pkg/debug/dataplane/parse/parser.go index 8d2955dbd0..bcab50c7e5 100644 --- a/npm/debug/dataplane/parse/parser.go +++ b/npm/pkg/debug/dataplane/parse/parser.go @@ -6,7 +6,7 @@ import ( "io/ioutil" "os/exec" - NPMIPtable "github.com/Azure/azure-container-networking/npm/debug/dataplane/iptables" + NPMIPtable "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane/iptables" "github.com/Azure/azure-container-networking/npm/util" ) diff --git a/npm/debug/dataplane/parse/parser_test.go b/npm/pkg/debug/dataplane/parse/parser_test.go similarity index 98% rename from npm/debug/dataplane/parse/parser_test.go rename to npm/pkg/debug/dataplane/parse/parser_test.go index 7dbe7ed172..7a05f576ce 100644 --- a/npm/debug/dataplane/parse/parser_test.go +++ b/npm/pkg/debug/dataplane/parse/parser_test.go @@ -6,7 +6,7 @@ import ( "strings" "testing" - NPMIPtable "github.com/Azure/azure-container-networking/npm/debug/dataplane/iptables" + NPMIPtable "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane/iptables" "github.com/Azure/azure-container-networking/npm/util" ) diff --git a/npm/debug/dataplane/trafficanalyzer.go b/npm/pkg/debug/dataplane/trafficanalyzer.go similarity index 99% rename from npm/debug/dataplane/trafficanalyzer.go rename to npm/pkg/debug/dataplane/trafficanalyzer.go index 50fcdb3031..452df7469c 100644 --- a/npm/debug/dataplane/trafficanalyzer.go +++ b/npm/pkg/debug/dataplane/trafficanalyzer.go @@ -7,7 +7,7 @@ import ( "strings" "github.com/Azure/azure-container-networking/npm" - "github.com/Azure/azure-container-networking/npm/debug/pb" + "github.com/Azure/azure-container-networking/npm/pkg/debug/pb" "github.com/Azure/azure-container-networking/npm/util" "google.golang.org/protobuf/encoding/protojson" ) diff --git a/npm/debug/dataplane/trafficanalyzer_test.go b/npm/pkg/debug/dataplane/trafficanalyzer_test.go similarity index 100% rename from npm/debug/dataplane/trafficanalyzer_test.go rename to npm/pkg/debug/dataplane/trafficanalyzer_test.go diff --git a/npm/debug/pb/rule.pb.go b/npm/pkg/debug/pb/rule.pb.go similarity index 100% rename from npm/debug/pb/rule.pb.go rename to npm/pkg/debug/pb/rule.pb.go diff --git a/npm/debug/pb/rule.proto b/npm/pkg/debug/pb/rule.proto similarity index 100% rename from npm/debug/pb/rule.proto rename to npm/pkg/debug/pb/rule.proto diff --git a/npm/debug/testfiles/iptablesave b/npm/pkg/debug/testfiles/iptablesave similarity index 100% rename from npm/debug/testfiles/iptablesave rename to npm/pkg/debug/testfiles/iptablesave diff --git a/npm/debug/testfiles/npmcache.json b/npm/pkg/debug/testfiles/npmcache.json similarity index 100% rename from npm/debug/testfiles/npmcache.json rename to npm/pkg/debug/testfiles/npmcache.json diff --git a/npm/util/errors/errors.go b/npm/util/errors/errors.go index db4317a27b..bb34be7874 100644 --- a/npm/util/errors/errors.go +++ b/npm/util/errors/errors.go @@ -1,12 +1,18 @@ package errors import ( + "errors" "fmt" "strings" "github.com/Azure/azure-container-networking/npm/util" ) +var ( + SrcNotSpecified = errors.New("source not specified") + DstNotSpecified = 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.] From 648ac297879e3bef1b5a7c96b9a89dfae31970dd Mon Sep 17 00:00:00 2001 From: Mathew Merrick Date: Tue, 24 Aug 2021 11:16:49 -0700 Subject: [PATCH 03/12] add default config and toggles --- npm/cmd/cli/gettuples.go | 9 -- npm/cmd/cli/root.go | 53 ---------- npm/cmd/{cli => }/convertiptable.go | 2 +- npm/cmd/debugcmd.go | 15 +++ npm/cmd/{cli/debugcmd.go => gettuples.go} | 12 ++- npm/cmd/main.go | 118 +++++++++------------- npm/cmd/{cli => }/parseiptable.go | 2 +- npm/cmd/start.go | 75 ++++++++++++++ npm/config/config.go | 28 ++++- npm/config/config_linux.go | 9 ++ npm/config/config_windows.go | 9 ++ npm/http/server/server.go | 53 +++++----- npm/npm.go | 3 +- 13 files changed, 215 insertions(+), 173 deletions(-) delete mode 100644 npm/cmd/cli/gettuples.go delete mode 100644 npm/cmd/cli/root.go rename npm/cmd/{cli => }/convertiptable.go (98%) create mode 100644 npm/cmd/debugcmd.go rename npm/cmd/{cli/debugcmd.go => gettuples.go} (79%) rename npm/cmd/{cli => }/parseiptable.go (98%) create mode 100644 npm/cmd/start.go create mode 100644 npm/config/config_linux.go create mode 100644 npm/config/config_windows.go diff --git a/npm/cmd/cli/gettuples.go b/npm/cmd/cli/gettuples.go deleted file mode 100644 index 2f98217c78..0000000000 --- a/npm/cmd/cli/gettuples.go +++ /dev/null @@ -1,9 +0,0 @@ -package cli - -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/cli/root.go b/npm/cmd/cli/root.go deleted file mode 100644 index 1cb683bb19..0000000000 --- a/npm/cmd/cli/root.go +++ /dev/null @@ -1,53 +0,0 @@ -package cli - -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()) - } -} diff --git a/npm/cmd/cli/convertiptable.go b/npm/cmd/convertiptable.go similarity index 98% rename from npm/cmd/cli/convertiptable.go rename to npm/cmd/convertiptable.go index 9b02aef752..9b961328b1 100644 --- a/npm/cmd/cli/convertiptable.go +++ b/npm/cmd/convertiptable.go @@ -1,4 +1,4 @@ -package cli +package main import ( "fmt" diff --git a/npm/cmd/debugcmd.go b/npm/cmd/debugcmd.go new file mode 100644 index 0000000000..0a4af10c60 --- /dev/null +++ b/npm/cmd/debugcmd.go @@ -0,0 +1,15 @@ +package main + +import ( + "github.com/spf13/cobra" +) + +// convertIptableCmd represents the convertIptable command +var debugCmd = &cobra.Command{ + Use: "debug", + Short: "Debug mode", +} + +func init() { + rootCmd.AddCommand(debugCmd) +} diff --git a/npm/cmd/cli/debugcmd.go b/npm/cmd/gettuples.go similarity index 79% rename from npm/cmd/cli/debugcmd.go rename to npm/cmd/gettuples.go index f1a7964d4a..99a9090848 100644 --- a/npm/cmd/cli/debugcmd.go +++ b/npm/cmd/gettuples.go @@ -1,4 +1,4 @@ -package cli +package main import ( "fmt" @@ -8,10 +8,12 @@ import ( "github.com/spf13/cobra" ) -// convertIptableCmd represents the convertIptable command -var debugCmd = &cobra.Command{ - Use: "debug", - Short: "Debug mode", +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)") } // getTuplesCmd represents the getTuples command diff --git a/npm/cmd/main.go b/npm/cmd/main.go index c7ca9f6b49..39f5210930 100644 --- a/npm/cmd/main.go +++ b/npm/cmd/main.go @@ -3,31 +3,37 @@ 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/cli" - restserver "github.com/Azure/azure-container-networking/npm/http/server" - "github.com/Azure/azure-container-networking/npm/metrics" - "k8s.io/apimachinery/pkg/util/wait" - "k8s.io/client-go/informers" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/rest" + npmconfig "github.com/Azure/azure-container-networking/npm/config" + "github.com/spf13/cobra" + "github.com/spf13/viper" "k8s.io/klog" - "k8s.io/utils/exec" -) - -const ( - // waitForTelemetryInSeconds = 60 unused - resyncPeriodInMinutes = 15 ) // Version is populated by make during build. var version string +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", + + RunE: func(cmd *cobra.Command, args []string) error { + config := &npmconfig.Config{} + err := viper.Unmarshal(config) + if err != nil { + fmt.Printf("unable to decode into config struct, %v", err) + } + + Start(*config) + return nil + }, +} func initLogging() error { log.SetName("azure-npm") @@ -41,64 +47,34 @@ func initLogging() error { } func main() { - if len(os.Args) > 1 { - // Cobra will handle invalid command, will prompt user to run `--help` for usage - cli.Execute() - return - } - klog.Infof("Start NPM version: %s", version) + cobra.CheckErr(rootCmd.Execute()) +} - var err error - defer func() { - if r := recover(); r != nil { - klog.Infof("recovered from error: %v", err) - } - }() - - if err = initLogging(); err != nil { - panic(err.Error()) - } - - metrics.InitializeAll() - - // Creates the in-cluster config - config, err := rest.InClusterConfig() - if err != nil { - panic(err.Error()) - } - - // Creates the clientset - clientset, err := kubernetes.NewForConfig(config) - if err != nil { - klog.Infof("clientset creation failed with error %v.", err) - panic(err.Error()) - } - - // Setting reSyncPeriod to 15 mins - minResyncPeriod := 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) - - klog.Infof("Resync period for NPM pod is set to %d.", int(resyncPeriod/time.Minute)) - factory := informers.NewSharedInformerFactory(clientset, resyncPeriod) +func init() { + cobra.OnInitialize(initConfig) + rootCmd.Flags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.azure-npm-debug-cli.yaml)") +} - npMgr := npm.NewNetworkPolicyManager(clientset, factory, exec.New(), version) - err = metrics.CreateTelemetryHandle(version, npm.GetAIMetadata()) - if err != nil { - klog.Infof("CreateTelemetryHandle failed with error %v.", err) - panic(err.Error()) +// initConfig reads in config file and ENV variables if set. +func initConfig() { + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } else { + viper.SetConfigFile(npmconfig.GetConfigPath()) } - restserver := restserver.NewNpmRestServer(restserver.DefaultHTTPListeningAddress) - go restserver.NPMRestServerListenAndServe(npMgr) - - if err = npMgr.Start(wait.NeverStop); err != nil { - klog.Infof("npm failed with error %v.", err) - panic(err.Error) + viper.AutomaticEnv() // read in environment variables that match + + // If a config file is found, read it in. + if err := viper.ReadInConfig(); err == nil { + klog.Error("Using config file:", viper.ConfigFileUsed()) + } else { + klog.Error(err) + klog.Info("Using default config") + b, _ := json.Marshal(npmconfig.DefaultConfig) + err := viper.ReadConfig(bytes.NewBuffer(b)) + cobra.CheckErr(err) } - - select {} } diff --git a/npm/cmd/cli/parseiptable.go b/npm/cmd/parseiptable.go similarity index 98% rename from npm/cmd/cli/parseiptable.go rename to npm/cmd/parseiptable.go index a08c9e7ca7..8d9b26b1c2 100644 --- a/npm/cmd/cli/parseiptable.go +++ b/npm/cmd/parseiptable.go @@ -1,4 +1,4 @@ -package cli +package main import ( "fmt" diff --git a/npm/cmd/start.go b/npm/cmd/start.go new file mode 100644 index 0000000000..037e55bb5e --- /dev/null +++ b/npm/cmd/start.go @@ -0,0 +1,75 @@ +// Copyright 2018 Microsoft. All rights reserved. +// MIT License +package main + +import ( + "math/rand" + "time" + + "github.com/Azure/azure-container-networking/npm" + 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" + "k8s.io/apimachinery/pkg/util/wait" + "k8s.io/client-go/informers" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" + "k8s.io/klog" + "k8s.io/utils/exec" +) + +func Start(config npmconfig.Config) { + klog.Infof("Using config: %+v", config) + + var err error + defer func() { + if r := recover(); r != nil { + klog.Infof("recovered from error: %v", err) + } + }() + + if err = initLogging(); err != nil { + panic(err.Error()) + } + + metrics.InitializeAll() + + // Creates the in-cluster config + k8sConfig, err := rest.InClusterConfig() + if err != nil { + panic(err.Error()) + } + + // Creates the clientset + clientset, err := kubernetes.NewForConfig(k8sConfig) + if err != nil { + klog.Infof("clientset creation failed with error %v.", err) + panic(err.Error()) + } + + // Setting reSyncPeriod to 15 mins + 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) + + klog.Infof("Resync period for NPM pod is set to %d.", int(resyncPeriod/time.Minute)) + factory := informers.NewSharedInformerFactory(clientset, resyncPeriod) + + npMgr := npm.NewNetworkPolicyManager(clientset, factory, exec.New(), version) + err = metrics.CreateTelemetryHandle(version, npm.GetAIMetadata()) + if err != nil { + klog.Infof("CreateTelemetryHandle failed with error %v.", err) + panic(err.Error()) + } + + go restserver.NPMRestServerListenAndServe(config, npMgr) + + if err = npMgr.Start(config, wait.NeverStop); err != nil { + klog.Infof("npm failed with error %v.", err) + panic(err.Error) + } + + select {} +} diff --git a/npm/config/config.go b/npm/config/config.go index 5951c35d6b..8234f7c9d1 100644 --- a/npm/config/config.go +++ b/npm/config/config.go @@ -1,7 +1,25 @@ -package config +package npmconfig -type NPMConfig struct { - ResyncPeriodInMinutes int `json:"ResyncPeriodInMinutes"` - ListeningPort int `json:"ListeningPort"` - ListeningAddress string `json:"ListeningAddress"` +var DefaultConfig = Config{ + ResyncPeriodInMinutes: 15, + ListeningPort: 10091, + 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..d822accab5 --- /dev/null +++ b/npm/config/config_linux.go @@ -0,0 +1,9 @@ +//go:build !windows +// +build !windows + +package npmconfig + +//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..3a11052f05 --- /dev/null +++ b/npm/config/config_windows.go @@ -0,0 +1,9 @@ +//go:build windows +// +build windows + +package npmconfig + +//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 b233d164f9..c57dc8aaa9 100644 --- a/npm/http/server/server.go +++ b/npm/http/server/server.go @@ -9,6 +9,7 @@ import ( "github.com/Azure/azure-container-networking/log" + 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" @@ -16,53 +17,51 @@ import ( "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(npMgr *npm.NetworkPolicyManager) { - n.router = mux.NewRouter() +func NPMRestServerListenAndServe(config npmconfig.Config, npMgr *npm.NetworkPolicyManager) { + 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.GetNpmMgr(npMgr)).Methods(http.MethodGet) + if config.Toggles.EnableHTTPDebugAPI { + // ACN CLI debug handlerss + rs.router.Handle(api.NPMMgrPath, rs.GetNpmMgr(npMgr)).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.Logf("Starting NPM HTTP API on %s... ", rs.listeningAddress) log.Errorf("Failed to start NPM HTTP Server with error: %+v", srv.ListenAndServe()) } -func NewNpmRestServer(listeningAddress string) *NPMRestServer { - return &NPMRestServer{ - listeningAddress: listeningAddress, - } -} - func (n *NPMRestServer) GetNpmMgr(npMgr *npm.NetworkPolicyManager) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { npMgr.Lock() diff --git a/npm/npm.go b/npm/npm.go index 671224f4ef..2b980cd711 100644 --- a/npm/npm.go +++ b/npm/npm.go @@ -11,6 +11,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/iptm" "github.com/Azure/azure-container-networking/npm/metrics" @@ -138,7 +139,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 { // Starts all informers manufactured by npMgr's informerFactory. npMgr.informerFactory.Start(stopCh) From 1f50705091002045066ae534522ceaed4dd99a21 Mon Sep 17 00:00:00 2001 From: Mathew Merrick Date: Tue, 24 Aug 2021 11:35:44 -0700 Subject: [PATCH 04/12] lint --- npm/cmd/gettuples.go | 4 ++-- npm/cmd/main.go | 6 ++++-- npm/config/config.go | 10 ++++++++-- npm/config/config_linux.go | 2 +- npm/util/errors/errors.go | 7 +++++-- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/npm/cmd/gettuples.go b/npm/cmd/gettuples.go index 99a9090848..6a310a4899 100644 --- a/npm/cmd/gettuples.go +++ b/npm/cmd/gettuples.go @@ -23,11 +23,11 @@ var getTuplesCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { src, _ := cmd.Flags().GetString("src") if src == "" { - return fmt.Errorf("%w", errors.SrcNotSpecified) + return fmt.Errorf("%w", errors.ErrSrcNotSpecified) } dst, _ := cmd.Flags().GetString("dst") if dst == "" { - return fmt.Errorf("%w", errors.DstNotSpecified) + return fmt.Errorf("%w", errors.ErrDstNotSpecified) } npmCacheF, _ := cmd.Flags().GetString("npmF") iptableSaveF, _ := cmd.Flags().GetString("iptF") diff --git a/npm/cmd/main.go b/npm/cmd/main.go index 39f5210930..875d51c2dd 100644 --- a/npm/cmd/main.go +++ b/npm/cmd/main.go @@ -15,8 +15,10 @@ import ( ) // Version is populated by make during build. -var version string -var cfgFile string +var ( + version string + cfgFile string +) // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ diff --git a/npm/config/config.go b/npm/config/config.go index 8234f7c9d1..347cc1ec69 100644 --- a/npm/config/config.go +++ b/npm/config/config.go @@ -1,8 +1,14 @@ package npmconfig +const ( + defaultResyncPeriod = 15 + defaultListeningPort = 10091 +) + +// DefaultConfig is the guaranteed configuration NPM can run in out of the box var DefaultConfig = Config{ - ResyncPeriodInMinutes: 15, - ListeningPort: 10091, + ResyncPeriodInMinutes: defaultResyncPeriod, + ListeningPort: defaultListeningPort, ListeningAddress: "0.0.0.0", Toggles: Toggles{ EnablePrometheusMetrics: true, diff --git a/npm/config/config_linux.go b/npm/config/config_linux.go index d822accab5..d86945c9b3 100644 --- a/npm/config/config_linux.go +++ b/npm/config/config_linux.go @@ -3,7 +3,7 @@ package npmconfig -//TODO: solidify config paths +// TODO: solidify config paths func GetConfigPath() string { return "/etc/azure/azure-vnet/azure-npm.json" } diff --git a/npm/util/errors/errors.go b/npm/util/errors/errors.go index bb34be7874..ad942ace4b 100644 --- a/npm/util/errors/errors.go +++ b/npm/util/errors/errors.go @@ -9,8 +9,11 @@ import ( ) var ( - SrcNotSpecified = errors.New("source not specified") - DstNotSpecified = errors.New("destination not specified") + // 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") ) /* From e06efdef6b2e06262d2855242aa759822b4f45a5 Mon Sep 17 00:00:00 2001 From: Mathew Merrick Date: Tue, 24 Aug 2021 12:02:14 -0700 Subject: [PATCH 05/12] address feedback --- npm/cmd/{debugcmd.go => debug.go} | 0 npm/cmd/main.go | 73 +++++++++---------------------- npm/cmd/root.go | 23 ++++++++++ npm/cmd/start.go | 25 ++++++++--- npm/config/config_linux.go | 2 +- npm/config/config_windows.go | 2 +- 6 files changed, 65 insertions(+), 60 deletions(-) rename npm/cmd/{debugcmd.go => debug.go} (100%) create mode 100644 npm/cmd/root.go diff --git a/npm/cmd/debugcmd.go b/npm/cmd/debug.go similarity index 100% rename from npm/cmd/debugcmd.go rename to npm/cmd/debug.go diff --git a/npm/cmd/main.go b/npm/cmd/main.go index 875d51c2dd..1237f90781 100644 --- a/npm/cmd/main.go +++ b/npm/cmd/main.go @@ -7,7 +7,6 @@ import ( "encoding/json" "fmt" - "github.com/Azure/azure-container-networking/log" npmconfig "github.com/Azure/azure-container-networking/npm/config" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -20,63 +19,33 @@ 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", - - RunE: func(cmd *cobra.Command, args []string) error { - config := &npmconfig.Config{} - err := viper.Unmarshal(config) - if err != nil { - fmt.Printf("unable to decode into config struct, %v", err) - } - - Start(*config) - return nil - }, -} - -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 main() { klog.Infof("Start NPM version: %s", version) 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 { - viper.SetConfigFile(npmconfig.GetConfigPath()) - } + cobra.OnInitialize(func() { + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } else { + viper.SetConfigFile(npmconfig.GetConfigPath()) + } - viper.AutomaticEnv() // read in environment variables that match + viper.AutomaticEnv() // read in environment variables that match + + // If a config file is found, read it in. + if err := viper.ReadInConfig(); err == nil { + klog.Error("Using config file:", viper.ConfigFileUsed()) + } else { + klog.Error(err) + klog.Info("Using default config") + b, _ := json.Marshal(npmconfig.DefaultConfig) + err := viper.ReadConfig(bytes.NewBuffer(b)) + cobra.CheckErr(err) + } + }) - // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { - klog.Error("Using config file:", viper.ConfigFileUsed()) - } else { - klog.Error(err) - klog.Info("Using default config") - b, _ := json.Marshal(npmconfig.DefaultConfig) - err := viper.ReadConfig(bytes.NewBuffer(b)) - cobra.CheckErr(err) - } + rootCmd.Flags().StringVar(&cfgFile, "config", "", fmt.Sprintf("Manually specify config file (default path is %s)", npmconfig.GetConfigPath())) } diff --git a/npm/cmd/root.go b/npm/cmd/root.go new file mode 100644 index 0000000000..f1c41193d9 --- /dev/null +++ b/npm/cmd/root.go @@ -0,0 +1,23 @@ +package main + +import ( + npmconfig "github.com/Azure/azure-container-networking/npm/config" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// 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", + + RunE: func(cmd *cobra.Command, args []string) error { + config := &npmconfig.Config{} + err := viper.Unmarshal(config) + if err != nil { + return err + } + + return Start(*config) + }, +} diff --git a/npm/cmd/start.go b/npm/cmd/start.go index 037e55bb5e..962d755553 100644 --- a/npm/cmd/start.go +++ b/npm/cmd/start.go @@ -3,9 +3,11 @@ package main import ( + "fmt" "math/rand" "time" + "github.com/Azure/azure-container-networking/log" "github.com/Azure/azure-container-networking/npm" npmconfig "github.com/Azure/azure-container-networking/npm/config" restserver "github.com/Azure/azure-container-networking/npm/http/server" @@ -18,7 +20,7 @@ import ( "k8s.io/utils/exec" ) -func Start(config npmconfig.Config) { +func Start(config npmconfig.Config) error { klog.Infof("Using config: %+v", config) var err error @@ -29,7 +31,7 @@ func Start(config npmconfig.Config) { }() if err = initLogging(); err != nil { - panic(err.Error()) + return err } metrics.InitializeAll() @@ -37,14 +39,14 @@ func Start(config npmconfig.Config) { // Creates the in-cluster config 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(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 @@ -61,15 +63,26 @@ func Start(config npmconfig.Config) { err = metrics.CreateTelemetryHandle(version, npm.GetAIMetadata()) if err != nil { klog.Infof("CreateTelemetryHandle failed with error %v.", err) - panic(err.Error()) + return err } go restserver.NPMRestServerListenAndServe(config, npMgr) if err = npMgr.Start(config, wait.NeverStop); err != nil { klog.Infof("npm failed with error %v.", err) - panic(err.Error) + return err } 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 +} diff --git a/npm/config/config_linux.go b/npm/config/config_linux.go index d86945c9b3..ffe0bf205d 100644 --- a/npm/config/config_linux.go +++ b/npm/config/config_linux.go @@ -3,7 +3,7 @@ package npmconfig -// TODO: solidify config paths +//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 index 3a11052f05..f4b8917666 100644 --- a/npm/config/config_windows.go +++ b/npm/config/config_windows.go @@ -3,7 +3,7 @@ package npmconfig -//TODO: solidify config paths +//GetConfigPath TODO: solidify config paths func GetConfigPath() string { return "c:\\k\\azure-npm\\azure-npm.json" } From 5b01880833ca5aab0b86d65ab3b94ede70d7b465 Mon Sep 17 00:00:00 2001 From: Mathew Merrick Date: Tue, 24 Aug 2021 12:17:49 -0700 Subject: [PATCH 06/12] appease the linter --- npm/cmd/root.go | 4 +++- npm/cmd/start.go | 4 ++-- npm/config/config_linux.go | 2 +- npm/config/config_windows.go | 2 +- npm/pkg/debug/dataplane/converter_test.go | 14 +++++++------- npm/pkg/debug/dataplane/parse/parser_test.go | 2 +- npm/pkg/debug/dataplane/trafficanalyzer_test.go | 4 ++-- 7 files changed, 17 insertions(+), 15 deletions(-) diff --git a/npm/cmd/root.go b/npm/cmd/root.go index f1c41193d9..8aeb1d9576 100644 --- a/npm/cmd/root.go +++ b/npm/cmd/root.go @@ -1,6 +1,8 @@ package main import ( + "fmt" + npmconfig "github.com/Azure/azure-container-networking/npm/config" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -15,7 +17,7 @@ var rootCmd = &cobra.Command{ config := &npmconfig.Config{} err := viper.Unmarshal(config) if err != nil { - return err + return fmt.Errorf("failed to load config with error %w", err) } return Start(*config) diff --git a/npm/cmd/start.go b/npm/cmd/start.go index 962d755553..19cb521aad 100644 --- a/npm/cmd/start.go +++ b/npm/cmd/start.go @@ -63,14 +63,14 @@ func Start(config npmconfig.Config) error { err = metrics.CreateTelemetryHandle(version, npm.GetAIMetadata()) if err != nil { klog.Infof("CreateTelemetryHandle failed with error %v.", err) - return err + return fmt.Errorf("CreateTelemetryHandle failed with error %w", err) } go restserver.NPMRestServerListenAndServe(config, npMgr) if err = npMgr.Start(config, wait.NeverStop); err != nil { klog.Infof("npm failed with error %v.", err) - return err + return fmt.Errorf("starting NPM failed with error %w", err) } select {} diff --git a/npm/config/config_linux.go b/npm/config/config_linux.go index ffe0bf205d..47ef83af13 100644 --- a/npm/config/config_linux.go +++ b/npm/config/config_linux.go @@ -3,7 +3,7 @@ package npmconfig -//GetConfigPath TODO: solidify config paths +// 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 index f4b8917666..905745881b 100644 --- a/npm/config/config_windows.go +++ b/npm/config/config_windows.go @@ -3,7 +3,7 @@ package npmconfig -//GetConfigPath TODO: solidify config paths +// GetConfigPath TODO: solidify config paths func GetConfigPath() string { return "c:\\k\\azure-npm\\azure-npm.json" } diff --git a/npm/pkg/debug/dataplane/converter_test.go b/npm/pkg/debug/dataplane/converter_test.go index d7a04bd5be..fd7e9146b2 100644 --- a/npm/pkg/debug/dataplane/converter_test.go +++ b/npm/pkg/debug/dataplane/converter_test.go @@ -14,8 +14,8 @@ func TestGetJSONRulesFromIptableFile(t *testing.T) { c := &Converter{} _, err := c.GetJSONRulesFromIptableFile( util.IptablesFilterTable, - "../testFiles/npmCache.json", - "../testFiles/iptableSave", + "../testfiles/npmCache.json", + "../testfiles/iptableSave", ) if err != nil { t.Errorf("error during TestGetJSONRulesFromIptable : %w", err) @@ -26,8 +26,8 @@ func TestGetProtobufRulesFromIptableFile(t *testing.T) { c := &Converter{} _, err := c.GetProtobufRulesFromIptableFile( util.IptablesFilterTable, - "../testFiles/npmCache.json", - "../testFiles/iptableSave", + "../testfiles/npmCache.json", + "../testfiles/iptableSave", ) if err != nil { t.Errorf("error during TestGetJSONRulesFromIptable : %w", err) @@ -83,7 +83,7 @@ func TestGetSetType(t *testing.T) { } c := &Converter{} - err := c.initConverterFile("../testFiles/npmCache.json") + err := c.initConverterFile("../testfiles/npmCache.json") if err != nil { t.Errorf("error during initilizing converter : %w", err) } @@ -305,7 +305,7 @@ func TestGetRulesFromChain(t *testing.T) { } c := &Converter{} - err := c.initConverterFile("../testFiles/npmCache.json") + err := c.initConverterFile("../testfiles/npmCache.json") if err != nil { t.Errorf("error during initilizing converter : %w", err) } @@ -494,7 +494,7 @@ func TestGetModulesFromRule(t *testing.T) { } c := &Converter{} - err := c.initConverterFile("../testFiles/npmCache.json") + err := c.initConverterFile("../testfiles/npmCache.json") if err != nil { t.Errorf("error during initilizing converter : %w", err) } diff --git a/npm/pkg/debug/dataplane/parse/parser_test.go b/npm/pkg/debug/dataplane/parse/parser_test.go index 7a05f576ce..17a23eddd5 100644 --- a/npm/pkg/debug/dataplane/parse/parser_test.go +++ b/npm/pkg/debug/dataplane/parse/parser_test.go @@ -11,7 +11,7 @@ import ( ) 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/pkg/debug/dataplane/trafficanalyzer_test.go b/npm/pkg/debug/dataplane/trafficanalyzer_test.go index 0ab581d75f..03ddd936eb 100644 --- a/npm/pkg/debug/dataplane/trafficanalyzer_test.go +++ b/npm/pkg/debug/dataplane/trafficanalyzer_test.go @@ -233,8 +233,8 @@ func TestGetNetworkTuple(t *testing.T) { _, actualTupleList, err := GetNetworkTupleFile( test.input.src, test.input.dst, - "../testFiles/npmCache.json", - "../testFiles/iptableSave", + "../testfiles/npmCache.json", + "../testfiles/iptableSave", ) if err != nil { t.Errorf("error during get network tuple : %w", err) From 7a8e10ad50bba3aeb85120b8215155cdd89651b0 Mon Sep 17 00:00:00 2001 From: Mathew Merrick Date: Wed, 25 Aug 2021 11:44:25 -0700 Subject: [PATCH 07/12] add configmap --- npm/azure-npm.yaml | 26 ++++++++++++++++++- npm/cmd/main.go | 12 ++++----- npm/cmd/start.go | 2 +- npm/config/config.go | 3 +++ npm/http/server/server.go | 6 ++--- npm/http/server/server_test.go | 2 +- npm/pkg/debug/dataplane/converter_test.go | 18 ++++++------- .../debug/dataplane/trafficanalyzer_test.go | 4 +-- 8 files changed, 49 insertions(+), 24 deletions(-) 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/main.go b/npm/cmd/main.go index 1237f90781..3473809408 100644 --- a/npm/cmd/main.go +++ b/npm/cmd/main.go @@ -5,7 +5,6 @@ package main import ( "bytes" "encoding/json" - "fmt" npmconfig "github.com/Azure/azure-container-networking/npm/config" "github.com/spf13/cobra" @@ -16,7 +15,6 @@ import ( // Version is populated by make during build. var ( version string - cfgFile string ) func main() { @@ -26,6 +24,10 @@ func main() { func init() { cobra.OnInitialize(func() { + viper.AutomaticEnv() // read in environment variables that match + + cfgFile := viper.GetString(npmconfig.ConfigEnvPath) + if cfgFile != "" { // Use config file from the flag. viper.SetConfigFile(cfgFile) @@ -33,11 +35,9 @@ func init() { viper.SetConfigFile(npmconfig.GetConfigPath()) } - viper.AutomaticEnv() // read in environment variables that match - // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { - klog.Error("Using config file:", viper.ConfigFileUsed()) + klog.Error("Using config file: ", viper.ConfigFileUsed()) } else { klog.Error(err) klog.Info("Using default config") @@ -46,6 +46,4 @@ func init() { cobra.CheckErr(err) } }) - - rootCmd.Flags().StringVar(&cfgFile, "config", "", fmt.Sprintf("Manually specify config file (default path is %s)", npmconfig.GetConfigPath())) } diff --git a/npm/cmd/start.go b/npm/cmd/start.go index 19cb521aad..f2c9ecbed9 100644 --- a/npm/cmd/start.go +++ b/npm/cmd/start.go @@ -49,7 +49,7 @@ func Start(config npmconfig.Config) error { return fmt.Errorf("failed to generate clientset with cluster config: %w", err) } - // Setting reSyncPeriod to 15 mins + // Setting reSyncPeriod minResyncPeriod := time.Duration(config.ResyncPeriodInMinutes) * time.Minute // Adding some randomness so all NPM pods will not request for info at once. diff --git a/npm/config/config.go b/npm/config/config.go index 347cc1ec69..e54f7fc3fe 100644 --- a/npm/config/config.go +++ b/npm/config/config.go @@ -3,6 +3,9 @@ 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 diff --git a/npm/http/server/server.go b/npm/http/server/server.go index c57dc8aaa9..a61b081e0b 100644 --- a/npm/http/server/server.go +++ b/npm/http/server/server.go @@ -7,7 +7,7 @@ import ( "net/http/pprof" _ "net/http/pprof" - "github.com/Azure/azure-container-networking/log" + "k8s.io/klog" npmconfig "github.com/Azure/azure-container-networking/npm/config" "github.com/Azure/azure-container-networking/npm/http/api" @@ -58,8 +58,8 @@ func NPMRestServerListenAndServe(config npmconfig.Config, npMgr *npm.NetworkPoli Addr: rs.listeningAddress, } - log.Logf("Starting NPM HTTP API on %s... ", rs.listeningAddress) - log.Errorf("Failed to start NPM HTTP Server with error: %+v", srv.ListenAndServe()) + 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) GetNpmMgr(npMgr *npm.NetworkPolicyManager) http.Handler { diff --git a/npm/http/server/server_test.go b/npm/http/server/server_test.go index e6f39165ba..a515ba7486 100644 --- a/npm/http/server/server_test.go +++ b/npm/http/server/server_test.go @@ -21,7 +21,7 @@ func TestGetNpmMgrHandler(t *testing.T) { }, }, } - n := NewNpmRestServer("") + n := NPMRestServer{} handler := n.GetNpmMgr(npMgr) req, err := http.NewRequest(http.MethodGet, api.NPMMgrPath, nil) diff --git a/npm/pkg/debug/dataplane/converter_test.go b/npm/pkg/debug/dataplane/converter_test.go index fd7e9146b2..3bc51d254f 100644 --- a/npm/pkg/debug/dataplane/converter_test.go +++ b/npm/pkg/debug/dataplane/converter_test.go @@ -4,8 +4,8 @@ import ( "reflect" "testing" - NPMIPtable "github.com/Azure/azure-container-networking/npm/debug/dataplane/iptables" - "github.com/Azure/azure-container-networking/npm/debug/pb" + NPMIPtable "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane/iptables" + "github.com/Azure/azure-container-networking/npm/pkg/debug/pb" "github.com/Azure/azure-container-networking/npm/util" "github.com/google/go-cmp/cmp" ) @@ -14,8 +14,8 @@ func TestGetJSONRulesFromIptableFile(t *testing.T) { c := &Converter{} _, err := c.GetJSONRulesFromIptableFile( util.IptablesFilterTable, - "../testfiles/npmCache.json", - "../testfiles/iptableSave", + "../testfiles/npmcache.json", + "../testfiles/iptablesave", ) if err != nil { t.Errorf("error during TestGetJSONRulesFromIptable : %w", err) @@ -26,8 +26,8 @@ func TestGetProtobufRulesFromIptableFile(t *testing.T) { c := &Converter{} _, err := c.GetProtobufRulesFromIptableFile( util.IptablesFilterTable, - "../testfiles/npmCache.json", - "../testfiles/iptableSave", + "../testfiles/npmcache.json", + "../testfiles/iptablesave", ) if err != nil { t.Errorf("error during TestGetJSONRulesFromIptable : %w", err) @@ -83,7 +83,7 @@ func TestGetSetType(t *testing.T) { } c := &Converter{} - err := c.initConverterFile("../testfiles/npmCache.json") + err := c.initConverterFile("../testfiles/npmcache.json") if err != nil { t.Errorf("error during initilizing converter : %w", err) } @@ -305,7 +305,7 @@ func TestGetRulesFromChain(t *testing.T) { } c := &Converter{} - err := c.initConverterFile("../testfiles/npmCache.json") + err := c.initConverterFile("../testfiles/npmcache.json") if err != nil { t.Errorf("error during initilizing converter : %w", err) } @@ -494,7 +494,7 @@ func TestGetModulesFromRule(t *testing.T) { } c := &Converter{} - err := c.initConverterFile("../testfiles/npmCache.json") + err := c.initConverterFile("../testfiles/npmcache.json") if err != nil { t.Errorf("error during initilizing converter : %w", err) } diff --git a/npm/pkg/debug/dataplane/trafficanalyzer_test.go b/npm/pkg/debug/dataplane/trafficanalyzer_test.go index 03ddd936eb..16dd482c7b 100644 --- a/npm/pkg/debug/dataplane/trafficanalyzer_test.go +++ b/npm/pkg/debug/dataplane/trafficanalyzer_test.go @@ -233,8 +233,8 @@ func TestGetNetworkTuple(t *testing.T) { _, actualTupleList, err := GetNetworkTupleFile( test.input.src, test.input.dst, - "../testfiles/npmCache.json", - "../testfiles/iptableSave", + "../testfiles/npmcache.json", + "../testfiles/iptablesave", ) if err != nil { t.Errorf("error during get network tuple : %w", err) From 998fb253c839491529c27d42493c804bff6b82b3 Mon Sep 17 00:00:00 2001 From: Mathew Merrick Date: Tue, 31 Aug 2021 10:16:55 -0700 Subject: [PATCH 08/12] fix merge --- npm/cmd/main.go | 4 +--- npm/cmd/start.go | 18 ------------------ npm/http/server/server.go | 11 ++++++----- npm/http/server/server_test.go | 2 +- npm/pkg/debug/dataplane/const.go | 6 +++--- npm/pkg/debug/dataplane/converter.go | 7 ------- 6 files changed, 11 insertions(+), 37 deletions(-) diff --git a/npm/cmd/main.go b/npm/cmd/main.go index 3473809408..58cbe8434c 100644 --- a/npm/cmd/main.go +++ b/npm/cmd/main.go @@ -13,9 +13,7 @@ import ( ) // Version is populated by make during build. -var ( - version string -) +var version string func main() { klog.Infof("Start NPM version: %s", version) diff --git a/npm/cmd/start.go b/npm/cmd/start.go index 7fb2d8d305..f445cffd73 100644 --- a/npm/cmd/start.go +++ b/npm/cmd/start.go @@ -22,24 +22,6 @@ import ( "k8s.io/utils/exec" ) -const ( - // waitForTelemetryInSeconds = 60 unused - resyncPeriodInMinutes = 15 -) - -// Version is populated by make during build. -var version string - -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 diff --git a/npm/http/server/server.go b/npm/http/server/server.go index 96ddc5b163..9fadf7201d 100644 --- a/npm/http/server/server.go +++ b/npm/http/server/server.go @@ -6,12 +6,11 @@ import ( "net/http/pprof" _ "net/http/pprof" - "k8s.io/klog" - "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" @@ -23,8 +22,10 @@ type NPMRestServer struct { router *mux.Router } -func (n *NPMRestServer) NPMRestServerListenAndServe(config npmconfig.Config, npmEncoder npm.NetworkPolicyManagerEncoder) { - n.router = mux.NewRouter() +func NPMRestServerListenAndServe(config npmconfig.Config, npmEncoder npm.NetworkPolicyManagerEncoder) { + rs := NPMRestServer{} + + rs.router = mux.NewRouter() //prometheus handlers if config.Toggles.EnablePrometheusMetrics { @@ -34,7 +35,7 @@ func (n *NPMRestServer) NPMRestServerListenAndServe(config npmconfig.Config, npm if config.Toggles.EnableHTTPDebugAPI { // ACN CLI debug handlerss - n.router.Handle(api.NPMMgrPath, n.npmCacheHandler(npmEncoder)).Methods(http.MethodGet) + rs.router.Handle(api.NPMMgrPath, rs.npmCacheHandler(npmEncoder)).Methods(http.MethodGet) } if config.Toggles.EnablePprof { 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/pkg/debug/dataplane/const.go b/npm/pkg/debug/dataplane/const.go index 09ffce6a0e..bd70776f89 100644 --- a/npm/pkg/debug/dataplane/const.go +++ b/npm/pkg/debug/dataplane/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/pkg/debug/dataplane/converter.go b/npm/pkg/debug/dataplane/converter.go index 665272679f..096227c13c 100644 --- a/npm/pkg/debug/dataplane/converter.go +++ b/npm/pkg/debug/dataplane/converter.go @@ -12,14 +12,7 @@ import ( "strconv" "strings" -<<<<<<< HEAD:npm/pkg/debug/dataplane/converter.go - "github.com/Azure/azure-container-networking/npm" -======= "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" ->>>>>>> master:npm/debugTools/dataplane/converter.go "github.com/Azure/azure-container-networking/npm/http/api" NPMIPtable "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane/iptables" "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane/parse" From 027632ab70ac63c2353a8d1046a1e7a4264da296 Mon Sep 17 00:00:00 2001 From: Mathew Merrick Date: Tue, 31 Aug 2021 11:10:13 -0700 Subject: [PATCH 09/12] directory restructure --- npm/cmd/convertiptable.go | 2 +- npm/cmd/gettuples.go | 2 +- npm/cmd/parseiptable.go | 2 +- npm/pkg/{debug => dataplane}/README.md | 0 npm/pkg/{debug => }/dataplane/const.go | 4 ++-- npm/pkg/{debug => }/dataplane/converter.go | 6 +++--- npm/pkg/{debug => }/dataplane/converter_test.go | 4 ++-- npm/pkg/{debug => }/dataplane/iptables/iptable.go | 0 npm/pkg/{debug => }/dataplane/parse/parser.go | 2 +- npm/pkg/{debug => }/dataplane/parse/parser_test.go | 4 ++-- npm/pkg/{debug => dataplane}/pb/rule.pb.go | 0 npm/pkg/{debug => dataplane}/pb/rule.proto | 0 npm/pkg/{debug => dataplane}/testfiles/iptablesave | 0 .../testfiles/npmCacheWithCustomFormat.json | 0 npm/pkg/{debug => dataplane}/testfiles/npmcache.json | 0 npm/pkg/{debug => }/dataplane/trafficanalyzer.go | 2 +- npm/pkg/{debug => }/dataplane/trafficanalyzer_test.go | 0 17 files changed, 14 insertions(+), 14 deletions(-) rename npm/pkg/{debug => dataplane}/README.md (100%) rename npm/pkg/{debug => }/dataplane/const.go (91%) rename npm/pkg/{debug => }/dataplane/converter.go (98%) rename npm/pkg/{debug => }/dataplane/converter_test.go (99%) rename npm/pkg/{debug => }/dataplane/iptables/iptable.go (100%) rename npm/pkg/{debug => }/dataplane/parse/parser.go (99%) rename npm/pkg/{debug => }/dataplane/parse/parser_test.go (98%) rename npm/pkg/{debug => dataplane}/pb/rule.pb.go (100%) rename npm/pkg/{debug => dataplane}/pb/rule.proto (100%) rename npm/pkg/{debug => dataplane}/testfiles/iptablesave (100%) rename npm/pkg/{debug => dataplane}/testfiles/npmCacheWithCustomFormat.json (100%) rename npm/pkg/{debug => dataplane}/testfiles/npmcache.json (100%) rename npm/pkg/{debug => }/dataplane/trafficanalyzer.go (99%) rename npm/pkg/{debug => }/dataplane/trafficanalyzer_test.go (100%) diff --git a/npm/cmd/convertiptable.go b/npm/cmd/convertiptable.go index 9b961328b1..d2604a99ca 100644 --- a/npm/cmd/convertiptable.go +++ b/npm/cmd/convertiptable.go @@ -3,7 +3,7 @@ package main import ( "fmt" - "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane" + "github.com/Azure/azure-container-networking/npm/pkg/dataplane" "github.com/spf13/cobra" ) diff --git a/npm/cmd/gettuples.go b/npm/cmd/gettuples.go index 6a310a4899..89d75e497c 100644 --- a/npm/cmd/gettuples.go +++ b/npm/cmd/gettuples.go @@ -3,7 +3,7 @@ package main import ( "fmt" - "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane" + "github.com/Azure/azure-container-networking/npm/pkg/dataplane" "github.com/Azure/azure-container-networking/npm/util/errors" "github.com/spf13/cobra" ) diff --git a/npm/cmd/parseiptable.go b/npm/cmd/parseiptable.go index 8d9b26b1c2..1ea14d38f5 100644 --- a/npm/cmd/parseiptable.go +++ b/npm/cmd/parseiptable.go @@ -3,7 +3,7 @@ package main import ( "fmt" - "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane/parse" + "github.com/Azure/azure-container-networking/npm/pkg/dataplane/parse" "github.com/spf13/cobra" ) diff --git a/npm/pkg/debug/README.md b/npm/pkg/dataplane/README.md similarity index 100% rename from npm/pkg/debug/README.md rename to npm/pkg/dataplane/README.md diff --git a/npm/pkg/debug/dataplane/const.go b/npm/pkg/dataplane/const.go similarity index 91% rename from npm/pkg/debug/dataplane/const.go rename to npm/pkg/dataplane/const.go index bd70776f89..58ba30d84a 100644 --- a/npm/pkg/debug/dataplane/const.go +++ b/npm/pkg/dataplane/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" // stored file with custom encoding in Encode function in npmCache.go - npmCacheWithCustomFormatFile = "../testfiles/npmCacheWithCustomFormat.json" + npmCacheWithCustomFormatFile = "./testfiles/npmCacheWithCustomFormat.json" ) diff --git a/npm/pkg/debug/dataplane/converter.go b/npm/pkg/dataplane/converter.go similarity index 98% rename from npm/pkg/debug/dataplane/converter.go rename to npm/pkg/dataplane/converter.go index 096227c13c..81a6ab1151 100644 --- a/npm/pkg/debug/dataplane/converter.go +++ b/npm/pkg/dataplane/converter.go @@ -14,9 +14,9 @@ import ( "github.com/Azure/azure-container-networking/npm/cache" "github.com/Azure/azure-container-networking/npm/http/api" - NPMIPtable "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane/iptables" - "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane/parse" - "github.com/Azure/azure-container-networking/npm/pkg/debug/pb" + 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/pkg/debug/dataplane/converter_test.go b/npm/pkg/dataplane/converter_test.go similarity index 99% rename from npm/pkg/debug/dataplane/converter_test.go rename to npm/pkg/dataplane/converter_test.go index eb377388a7..055ec94b42 100644 --- a/npm/pkg/debug/dataplane/converter_test.go +++ b/npm/pkg/dataplane/converter_test.go @@ -4,8 +4,8 @@ import ( "reflect" "testing" - NPMIPtable "github.com/Azure/azure-container-networking/npm/pkg/debug/dataplane/iptables" - "github.com/Azure/azure-container-networking/npm/pkg/debug/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/pkg/debug/dataplane/iptables/iptable.go b/npm/pkg/dataplane/iptables/iptable.go similarity index 100% rename from npm/pkg/debug/dataplane/iptables/iptable.go rename to npm/pkg/dataplane/iptables/iptable.go diff --git a/npm/pkg/debug/dataplane/parse/parser.go b/npm/pkg/dataplane/parse/parser.go similarity index 99% rename from npm/pkg/debug/dataplane/parse/parser.go rename to npm/pkg/dataplane/parse/parser.go index bcab50c7e5..c4a9b83d5d 100644 --- a/npm/pkg/debug/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/pkg/debug/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/pkg/debug/dataplane/parse/parser_test.go b/npm/pkg/dataplane/parse/parser_test.go similarity index 98% rename from npm/pkg/debug/dataplane/parse/parser_test.go rename to npm/pkg/dataplane/parse/parser_test.go index 17a23eddd5..320a96ef24 100644 --- a/npm/pkg/debug/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/pkg/debug/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/pkg/debug/pb/rule.pb.go b/npm/pkg/dataplane/pb/rule.pb.go similarity index 100% rename from npm/pkg/debug/pb/rule.pb.go rename to npm/pkg/dataplane/pb/rule.pb.go diff --git a/npm/pkg/debug/pb/rule.proto b/npm/pkg/dataplane/pb/rule.proto similarity index 100% rename from npm/pkg/debug/pb/rule.proto rename to npm/pkg/dataplane/pb/rule.proto diff --git a/npm/pkg/debug/testfiles/iptablesave b/npm/pkg/dataplane/testfiles/iptablesave similarity index 100% rename from npm/pkg/debug/testfiles/iptablesave rename to npm/pkg/dataplane/testfiles/iptablesave diff --git a/npm/pkg/debug/testfiles/npmCacheWithCustomFormat.json b/npm/pkg/dataplane/testfiles/npmCacheWithCustomFormat.json similarity index 100% rename from npm/pkg/debug/testfiles/npmCacheWithCustomFormat.json rename to npm/pkg/dataplane/testfiles/npmCacheWithCustomFormat.json diff --git a/npm/pkg/debug/testfiles/npmcache.json b/npm/pkg/dataplane/testfiles/npmcache.json similarity index 100% rename from npm/pkg/debug/testfiles/npmcache.json rename to npm/pkg/dataplane/testfiles/npmcache.json diff --git a/npm/pkg/debug/dataplane/trafficanalyzer.go b/npm/pkg/dataplane/trafficanalyzer.go similarity index 99% rename from npm/pkg/debug/dataplane/trafficanalyzer.go rename to npm/pkg/dataplane/trafficanalyzer.go index c5dbe53861..5e15407624 100644 --- a/npm/pkg/debug/dataplane/trafficanalyzer.go +++ b/npm/pkg/dataplane/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/pkg/debug/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/pkg/debug/dataplane/trafficanalyzer_test.go b/npm/pkg/dataplane/trafficanalyzer_test.go similarity index 100% rename from npm/pkg/debug/dataplane/trafficanalyzer_test.go rename to npm/pkg/dataplane/trafficanalyzer_test.go From 97a4190909bc2f7917f1fae6fd79437d5e9e91c3 Mon Sep 17 00:00:00 2001 From: Mathew Merrick Date: Tue, 31 Aug 2021 14:07:03 -0700 Subject: [PATCH 10/12] debug pkg --- npm/cmd/convertiptable.go | 2 +- npm/cmd/gettuples.go | 2 +- npm/pkg/dataplane/{ => debug}/README.md | 0 npm/pkg/dataplane/{ => debug}/const.go | 0 npm/pkg/dataplane/{ => debug}/converter.go | 0 npm/pkg/dataplane/{ => debug}/converter_test.go | 0 npm/pkg/dataplane/{ => debug}/trafficanalyzer.go | 0 npm/pkg/dataplane/{ => debug}/trafficanalyzer_test.go | 0 8 files changed, 2 insertions(+), 2 deletions(-) rename npm/pkg/dataplane/{ => debug}/README.md (100%) rename npm/pkg/dataplane/{ => debug}/const.go (100%) rename npm/pkg/dataplane/{ => debug}/converter.go (100%) rename npm/pkg/dataplane/{ => debug}/converter_test.go (100%) rename npm/pkg/dataplane/{ => debug}/trafficanalyzer.go (100%) rename npm/pkg/dataplane/{ => debug}/trafficanalyzer_test.go (100%) diff --git a/npm/cmd/convertiptable.go b/npm/cmd/convertiptable.go index d2604a99ca..4809be3e5c 100644 --- a/npm/cmd/convertiptable.go +++ b/npm/cmd/convertiptable.go @@ -3,7 +3,7 @@ package main import ( "fmt" - "github.com/Azure/azure-container-networking/npm/pkg/dataplane" + dataplane "github.com/Azure/azure-container-networking/npm/pkg/dataplane/debug" "github.com/spf13/cobra" ) diff --git a/npm/cmd/gettuples.go b/npm/cmd/gettuples.go index 89d75e497c..eda0a39f35 100644 --- a/npm/cmd/gettuples.go +++ b/npm/cmd/gettuples.go @@ -3,7 +3,7 @@ package main import ( "fmt" - "github.com/Azure/azure-container-networking/npm/pkg/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" ) diff --git a/npm/pkg/dataplane/README.md b/npm/pkg/dataplane/debug/README.md similarity index 100% rename from npm/pkg/dataplane/README.md rename to npm/pkg/dataplane/debug/README.md diff --git a/npm/pkg/dataplane/const.go b/npm/pkg/dataplane/debug/const.go similarity index 100% rename from npm/pkg/dataplane/const.go rename to npm/pkg/dataplane/debug/const.go diff --git a/npm/pkg/dataplane/converter.go b/npm/pkg/dataplane/debug/converter.go similarity index 100% rename from npm/pkg/dataplane/converter.go rename to npm/pkg/dataplane/debug/converter.go diff --git a/npm/pkg/dataplane/converter_test.go b/npm/pkg/dataplane/debug/converter_test.go similarity index 100% rename from npm/pkg/dataplane/converter_test.go rename to npm/pkg/dataplane/debug/converter_test.go diff --git a/npm/pkg/dataplane/trafficanalyzer.go b/npm/pkg/dataplane/debug/trafficanalyzer.go similarity index 100% rename from npm/pkg/dataplane/trafficanalyzer.go rename to npm/pkg/dataplane/debug/trafficanalyzer.go diff --git a/npm/pkg/dataplane/trafficanalyzer_test.go b/npm/pkg/dataplane/debug/trafficanalyzer_test.go similarity index 100% rename from npm/pkg/dataplane/trafficanalyzer_test.go rename to npm/pkg/dataplane/debug/trafficanalyzer_test.go From f4b436c431f6dacce3f47a30505c18521f4de968 Mon Sep 17 00:00:00 2001 From: Mathew Merrick Date: Tue, 31 Aug 2021 15:12:05 -0700 Subject: [PATCH 11/12] move when config is loaded --- npm/cmd/convertiptable.go | 8 ++++---- npm/cmd/gettuples.go | 8 ++++---- npm/cmd/main.go | 33 --------------------------------- npm/cmd/parseiptable.go | 4 ++-- npm/cmd/root.go | 29 +++++++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 43 deletions(-) diff --git a/npm/cmd/convertiptable.go b/npm/cmd/convertiptable.go index 4809be3e5c..297e6e39a0 100644 --- a/npm/cmd/convertiptable.go +++ b/npm/cmd/convertiptable.go @@ -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/gettuples.go b/npm/cmd/gettuples.go index eda0a39f35..d7ab95bd60 100644 --- a/npm/cmd/gettuples.go +++ b/npm/cmd/gettuples.go @@ -12,8 +12,8 @@ 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)") + 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 @@ -29,8 +29,8 @@ var getTuplesCmd = &cobra.Command{ if dst == "" { 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} diff --git a/npm/cmd/main.go b/npm/cmd/main.go index 58cbe8434c..960a24839b 100644 --- a/npm/cmd/main.go +++ b/npm/cmd/main.go @@ -3,45 +3,12 @@ package main import ( - "bytes" - "encoding/json" - - npmconfig "github.com/Azure/azure-container-networking/npm/config" "github.com/spf13/cobra" - "github.com/spf13/viper" - "k8s.io/klog" ) // Version is populated by make during build. var version string func main() { - klog.Infof("Start NPM version: %s", version) cobra.CheckErr(rootCmd.Execute()) } - -func init() { - cobra.OnInitialize(func() { - viper.AutomaticEnv() // read in environment variables that match - - cfgFile := viper.GetString(npmconfig.ConfigEnvPath) - - if cfgFile != "" { - // Use config file from the flag. - viper.SetConfigFile(cfgFile) - } else { - viper.SetConfigFile(npmconfig.GetConfigPath()) - } - - // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { - klog.Error("Using config file: ", viper.ConfigFileUsed()) - } else { - klog.Error(err) - klog.Info("Using default config") - b, _ := json.Marshal(npmconfig.DefaultConfig) - err := viper.ReadConfig(bytes.NewBuffer(b)) - cobra.CheckErr(err) - } - }) -} diff --git a/npm/cmd/parseiptable.go b/npm/cmd/parseiptable.go index 1ea14d38f5..d97d4b2f74 100644 --- a/npm/cmd/parseiptable.go +++ b/npm/cmd/parseiptable.go @@ -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 8aeb1d9576..71f4027d28 100644 --- a/npm/cmd/root.go +++ b/npm/cmd/root.go @@ -1,18 +1,47 @@ package main import ( + "bytes" + "encoding/json" "fmt" npmconfig "github.com/Azure/azure-container-networking/npm/config" "github.com/spf13/cobra" "github.com/spf13/viper" + "k8s.io/klog" ) // 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", + CompletionOptions: cobra.CompletionOptions{ + DisableDefaultCmd: true, + }, + PreRunE: func(cmd *cobra.Command, args []string) error { + viper.AutomaticEnv() // read in environment variables that match + cfgFile := viper.GetString(npmconfig.ConfigEnvPath) + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } else { + viper.SetConfigFile(npmconfig.GetConfigPath()) + } + // If a config file is found, read it in. + if err := viper.ReadInConfig(); err == nil { + klog.Info("Using config file: ", viper.ConfigFileUsed()) + } else { + klog.Info("Using default config") + 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) + } + } + + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { config := &npmconfig.Config{} err := viper.Unmarshal(config) From aa75445c93634eb2f0735649a4aeeef06d8ee94e Mon Sep 17 00:00:00 2001 From: Mathew Merrick Date: Tue, 31 Aug 2021 15:37:35 -0700 Subject: [PATCH 12/12] move to start command --- npm/Dockerfile | 2 +- npm/cmd/root.go | 40 --------------- npm/cmd/start.go | 84 ++++++++++++++++++++++++-------- npm/pkg/dataplane/debug/const.go | 6 +-- 4 files changed, 68 insertions(+), 64 deletions(-) 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/cmd/root.go b/npm/cmd/root.go index 71f4027d28..f1ff870249 100644 --- a/npm/cmd/root.go +++ b/npm/cmd/root.go @@ -1,14 +1,7 @@ package main import ( - "bytes" - "encoding/json" - "fmt" - - npmconfig "github.com/Azure/azure-container-networking/npm/config" "github.com/spf13/cobra" - "github.com/spf13/viper" - "k8s.io/klog" ) // rootCmd represents the base command when called without any subcommands @@ -18,37 +11,4 @@ var rootCmd = &cobra.Command{ CompletionOptions: cobra.CompletionOptions{ DisableDefaultCmd: true, }, - PreRunE: func(cmd *cobra.Command, args []string) error { - viper.AutomaticEnv() // read in environment variables that match - cfgFile := viper.GetString(npmconfig.ConfigEnvPath) - if cfgFile != "" { - // Use config file from the flag. - viper.SetConfigFile(cfgFile) - } else { - viper.SetConfigFile(npmconfig.GetConfigPath()) - } - - // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { - klog.Info("Using config file: ", viper.ConfigFileUsed()) - } else { - klog.Info("Using default config") - 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) - } - } - - return nil - }, - 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) - } - - return Start(*config) - }, } diff --git a/npm/cmd/start.go b/npm/cmd/start.go index f445cffd73..5be2385a29 100644 --- a/npm/cmd/start.go +++ b/npm/cmd/start.go @@ -3,6 +3,8 @@ package main import ( + "bytes" + "encoding/json" "fmt" "math/rand" "time" @@ -13,6 +15,8 @@ import ( 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" @@ -22,31 +26,48 @@ import ( "k8s.io/utils/exec" ) -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 +func init() { + rootCmd.AddCommand(startNPMCmd) +} + +// 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) + } } - } - if err != nil { - metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to retrieving kubernetes version") - panic(err.Error) - } + return nil + }, - if err = util.SetIsNewNwPolicyVerFlag(serverVersion); err != nil { - metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to set IsNewNwPolicyVerFlag") - panic(err.Error) - } - return serverVersion + 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) + } + + return start(*config) + }, } -func Start(config npmconfig.Config) error { - klog.Infof("Using config: %+v", config) +func start(config npmconfig.Config) error { + klog.Infof("loaded config: %+v", config) klog.Infof("Start NPM version: %s", version) var err error @@ -112,3 +133,26 @@ func initLogging() error { 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/pkg/dataplane/debug/const.go b/npm/pkg/dataplane/debug/const.go index 58ba30d84a..9a8a274299 100644 --- a/npm/pkg/dataplane/debug/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" )