Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routing analysis wip - support config object input #604

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions cmd/analyzer/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,6 @@ func TestMain(t *testing.T) {
args: "--output-file version.txt --version",
},

// read from account // need to export api-key first
/*{
name: "read_from_account_mode",
args: "report endpoints --output-file account.txt --provider ibm --resource-group ola",
},
{
name: "read_from_account_mode_dump_resources",
args: "report endpoints --output-file account.txt --provider ibm --dump-resources account_resources_file.json",
},*/

// resource group and region filter
{
name: "txt_resource_group_filter_multi_resource_groups",
Expand All @@ -152,6 +142,20 @@ func TestMain(t *testing.T) {
name: "diff_with_different_uid",
args: "diff endpoints --quiet --vpc-config ../../pkg/ibmvpc/examples/input/input_sg_testing_default.json --vpc-config-second ../../pkg/ibmvpc/examples/input/input_sg_testing_3.json",
},
{
name: "test_routing_cmd",
args: "report routing --vpc-config ../../pkg/ibmvpc/examples/input/input_hub_n_spoke_1.json",
},

// read from account // need to export api-key first
/*{
name: "read_from_account_mode",
args: "report endpoints --output-file account.txt --provider ibm --resource-group ola",
},
{
name: "read_from_account_mode_dump_resources",
args: "report endpoints --output-file account.txt --provider ibm --dump-resources account_resources_file.json",
},*/
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
75 changes: 75 additions & 0 deletions cmd/analyzer/subcmds/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,84 @@ import (

"github.com/spf13/cobra"

"github.com/np-guard/vpc-network-config-analyzer/pkg/ibmvpc"
"github.com/np-guard/vpc-network-config-analyzer/pkg/vpcmodel"
)

//nolint:gocritic // temporary version, wip
func routingAnalysis(inArgs *inArgs) error {
vpcConfigs, err := buildConfigs(inArgs)
if err != nil {
return err
}

analyzer := ibmvpc.NewGlobalRTAnalyzer(vpcConfigs)
pairs := vpcConfigs.GetInternalNodePairs()
for _, pair := range pairs {
path, err := analyzer.GetRoutingPath(pair.Src.(vpcmodel.InternalNodeIntf), pair.Dst.IPBlock())
if err != nil {
return err
}
fmt.Printf("path for src %s, dst %s:\n", pair.Src.CidrOrAddress(), pair.Dst.CidrOrAddress())
fmt.Println(path.String())
fmt.Println("")
}
return nil

/*
current output:

path for src 10.1.15.4, dst 192.168.0.4:
NetworkInterface - tvpc-transit-z1-worker[10.1.15.4] -> TGW - tvpc-tgw-link -> NetworkInterface - tvpc-enterprise-z1-worker[192.168.0.4]

path for src 192.168.0.4, dst 10.1.15.4:
NetworkInterface - tvpc-enterprise-z1-worker[192.168.0.4] -> TGW - tvpc-tgw-link -> NetworkInterface - tvpc-transit-z1-worker[10.1.15.4]

path for src 10.1.0.4, dst 192.168.0.4:
NetworkInterface - tvpc-spoke0-z1-worker[10.1.0.4] -> TGW - tvpc-tgw -> nextHop: 10.3.15.196 [origDest: 192.168.0.4]

path for src 10.3.15.196, dst 192.168.0.4:
NetworkInterface - tvpc-fw-z3-s3-0[10.3.15.196] -> TGW - tvpc-tgw-link -> NetworkInterface - tvpc-enterprise-z1-worker[192.168.0.4]

*/
/*srcDstPairs := []struct {
src string
dst string
}{
{
src: "10.1.15.4",
dst: "192.168.0.4",
},
{
dst: "10.1.15.4",
src: "192.168.0.4",
},
{
src: "10.1.0.4", // spoke vpc
dst: "192.168.0.4", // enterprise vpc
},
{
src: "10.3.15.196",
dst: "192.168.0.4",
},

}
for _, pair := range srcDstPairs {
srcNode, err1 := vpcConfigs.GetInternalNodeFromAddress(pair.src)
dstIPBlock, err2 := ipblock.FromIPAddress(pair.dst)
path, err3 := analyzer.GetRoutingPath(srcNode, dstIPBlock)
if err := errors.Join(err1, err2, err3); err != nil {
fmt.Printf("err: %s", err.Error())
return err
}
fmt.Printf("path for src %s, dst %s:\n", pair.src, pair.dst)
fmt.Println(path.String())
fmt.Println("")
}

return nil*/
}

func analysisVPCConfigs(cmd *cobra.Command, inArgs *inArgs, analysisType vpcmodel.OutputUseCase) error {
cmd.SilenceUsage = true // if we got this far, flags are syntactically correct, so no need to print usage
cmd.SilenceErrors = true // also, error will be printed to logger in main(), so no need for cobra to also print it
Expand Down
13 changes: 13 additions & 0 deletions cmd/analyzer/subcmds/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewReportCommand(args *inArgs) *cobra.Command {
cmd.AddCommand(newReportEndpointsCommand(args))
cmd.AddCommand(newReportSubnetsCommand(args))
cmd.AddCommand(newReportSingleSubnetCommand(args))
cmd.AddCommand(newReportRoutingCommand(args))

return cmd
}
Expand Down Expand Up @@ -78,3 +79,15 @@ func newReportSingleSubnetCommand(args *inArgs) *cobra.Command {
},
}
}

func newReportRoutingCommand(args *inArgs) *cobra.Command {
return &cobra.Command{
Use: "routing",
Short: "Report VPC routing paths between given endpoints",
Long: `reports VPC routing paths between given endpoints as implied by the given cloud configuration`,
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
return routingAnalysis(args)
},
}
}
12 changes: 12 additions & 0 deletions pkg/common/pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Copyright 2023- IBM Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package common

type Pair[T any] struct {
Src T
Dst T
}
2 changes: 1 addition & 1 deletion pkg/ibmvpc/egress_routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func newEgressRTFromRoutes(rps *routesPerSubnets, config *vpcmodel.VPCConfig, vp
for subnetsKey, routes := range rps.routesMap {
egressRT := &egressRoutingTable{}
implicitRT := &systemImplicitRT{vpc: vpc, config: systemRTConfigFromVPCConfig(config), vpcConfig: config}
if rt, err := newRoutingTable(routes, implicitRT); err == nil {
if rt, err := newRoutingTable(routes, implicitRT, &vpcmodel.VPCResource{}); err == nil {
egressRT.routingTable = *rt
}
egressRT.vpc = vpc
Expand Down
Loading