Skip to content

refactor Name() and ExtendedName(c) methods #865

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

Merged
merged 18 commits into from
Sep 29, 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
6 changes: 3 additions & 3 deletions pkg/awsvpc/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,15 @@ func printConfig(c *vpcmodel.VPCConfig) {
if n.IsExternal() {
continue
}
logging.Debug(strings.Join([]string{n.Kind(), n.CidrOrAddress(), n.Name(), n.UID()}, separator))
logging.Debug(strings.Join([]string{n.Kind(), n.CidrOrAddress(), n.NameForAnalyzerOut(c), n.UID()}, separator))
}
logging.Debug("Subnets:")
for _, n := range c.Subnets {
logging.Debug(strings.Join([]string{n.Kind(), n.CIDR(), n.Name(), n.UID()}, separator))
logging.Debug(strings.Join([]string{n.Kind(), n.CIDR(), n.NameForAnalyzerOut(c), n.UID()}, separator))
}
logging.Debug("NodeSets:")
for _, n := range c.NodeSets {
logging.Debug(strings.Join([]string{n.Kind(), n.AddressRange().ToIPRanges(), n.Name(), n.UID()}, separator))
logging.Debug(strings.Join([]string{n.Kind(), n.AddressRange().ToIPRanges(), n.NameForAnalyzerOut(c), n.UID()}, separator))
}
logging.Debug("FilterResources:")
for _, f := range c.FilterResources {
Expand Down
16 changes: 10 additions & 6 deletions pkg/commonvpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,8 @@ func (ni *NetworkInterface) VsiName() string {
return ni.Vsi
}

func (ni *NetworkInterface) Name() string {
return nameWithBracketsInfo(ni.Vsi, ni.Address())
}

func (ni *NetworkInterface) ExtendedName(c *vpcmodel.VPCConfig) string {
return ni.ExtendedPrefix(c) + ni.Name()
func (ni *NetworkInterface) NameForAnalyzerOut(c *vpcmodel.VPCConfig) string {
return MultipleVPCsConfigPrefix(c, &ni.VPCResource) + nameWithBracketsInfo(ni.Vsi, ni.Address())
}

func nameWithBracketsInfo(name, inBrackets string) string {
Expand Down Expand Up @@ -737,3 +733,11 @@ func getTableConnEffect(connQuery, conn *connection.Set) (*connection.Set, vpcmo
return conn.Intersect(connQuery), vpcmodel.PartlyAllow
}
}

// MultipleVPCsConfigPrefix returns the vpcName of the passed resource when config is multi-vpc
func MultipleVPCsConfigPrefix(c *vpcmodel.VPCConfig, resource *vpcmodel.VPCResource) string {
if c != nil && resource.VPC() != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when is resource.VPC() expected to be nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got some tests failed before I added this condition, it seems like there are some cases we do not init config file in tests

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only unit tests or end-to-end tests?
this is not about initializing config file, but having the resource a pointer to the VPC it is contained in.

return c.MultipleVPCsConfigPrefix(resource.VPC().Name())
}
return ""
}
100 changes: 50 additions & 50 deletions pkg/ibmvpc/examples/out/synthesis_out/acl_testing3_all_vpcs_.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,36 @@
"external-1": "161.26.0.0/16"
},
"required-connections": [
{
"allowed-protocols": [
{
"protocol": "ANY"
}
],
"dst": {
"name": "test-vpc1-ky/vsi1-ky",
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/vsi2-ky",
"type": "instance"
}
},
{
"allowed-protocols": [
{
"protocol": "ICMP"
}
],
"dst": {
"name": "external-0",
"type": "external"
},
"src": {
"name": "test-vpc1-ky/vsi2-ky",
"type": "instance"
}
},
{
"allowed-protocols": [
{
Expand All @@ -16,8 +46,8 @@
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/vsi3c-ky",
"type": "instance"
"name": "test-vpc1-ky/db-endpoint-gateway-ky",
"type": "vpe"
}
},
{
Expand All @@ -32,8 +62,8 @@
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/vsi3c-ky",
"type": "instance"
"name": "test-vpc1-ky/db-endpoint-gateway-ky",
"type": "vpe"
}
},
{
Expand All @@ -47,8 +77,8 @@
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/vsi3c-ky",
"type": "instance"
"name": "test-vpc1-ky/db-endpoint-gateway-ky",
"type": "vpe"
}
},
{
Expand All @@ -65,8 +95,8 @@
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/vsi3c-ky",
"type": "instance"
"name": "test-vpc1-ky/db-endpoint-gateway-ky",
"type": "vpe"
}
},
{
Expand All @@ -77,12 +107,12 @@
],
"bidirectional": true,
"dst": {
"name": "test-vpc1-ky/db-endpoint-gateway-ky",
"type": "vpe"
},
"src": {
"name": "test-vpc1-ky/vsi3c-ky",
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/db-endpoint-gateway-ky",
"type": "vpe"
}
},
{
Expand Down Expand Up @@ -142,8 +172,8 @@
],
"bidirectional": true,
"dst": {
"name": "test-vpc1-ky/db-endpoint-gateway-ky",
"type": "vpe"
"name": "test-vpc1-ky/vsi3c-ky",
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/vsi3b-ky",
Expand Down Expand Up @@ -191,41 +221,11 @@
],
"bidirectional": true,
"dst": {
"name": "test-vpc1-ky/db-endpoint-gateway-ky",
"type": "vpe"
},
"src": {
"name": "test-vpc1-ky/vsi3a-ky",
"type": "instance"
}
},
{
"allowed-protocols": [
{
"protocol": "ANY"
}
],
"dst": {
"name": "test-vpc1-ky/vsi1-ky",
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/vsi2-ky",
"name": "test-vpc1-ky/vsi3c-ky",
"type": "instance"
}
},
{
"allowed-protocols": [
{
"protocol": "ICMP"
}
],
"dst": {
"name": "external-0",
"type": "external"
},
"src": {
"name": "test-vpc1-ky/vsi2-ky",
"name": "test-vpc1-ky/vsi3a-ky",
"type": "instance"
}
},
Expand Down Expand Up @@ -273,8 +273,8 @@
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/db-endpoint-gateway-ky",
"type": "vpe"
"name": "test-vpc1-ky/vsi3c-ky",
"type": "instance"
}
},
{
Expand All @@ -291,8 +291,8 @@
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/db-endpoint-gateway-ky",
"type": "vpe"
"name": "test-vpc1-ky/vsi3c-ky",
"type": "instance"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
],
"bidirectional": true,
"dst": {
"name": "test-vpc1-ky/vsi1-ky",
"name": "test-vpc1-ky/vsi2-ky",
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/vsi2-ky",
"name": "test-vpc1-ky/vsi1-ky",
"type": "instance"
}
},
Expand All @@ -31,7 +31,7 @@
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/vsi2-ky",
"name": "test-vpc1-ky/vsi1-ky",
"type": "instance"
}
},
Expand All @@ -43,8 +43,8 @@
],
"bidirectional": true,
"dst": {
"name": "external-0",
"type": "external"
"name": "test-vpc1-ky/vsi0-ky",
"type": "instance"
},
"src": {
"name": "test-vpc1-ky/vsi2-ky",
Expand All @@ -59,11 +59,11 @@
],
"bidirectional": true,
"dst": {
"name": "test-vpc1-ky/vsi0-ky",
"type": "instance"
"name": "external-0",
"type": "external"
},
"src": {
"name": "test-vpc1-ky/vsi1-ky",
"name": "test-vpc1-ky/vsi2-ky",
"type": "instance"
}
},
Expand Down
3 changes: 1 addition & 2 deletions pkg/ibmvpc/ibmDrawioGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ func (lb *LoadBalancer) GenerateDrawioTreeNode(gen *vpcmodel.DrawioGenerator) dr
}
}
vpcTn := gen.TreeNode(lb.VPC()).(drawio.SquareTreeNodeInterface)
// here we do not call lb.Name() because lb.Name() add the kind to the name
return drawio.GroupPrivateIPsWithLoadBalancer(vpcTn, lb.ResourceName, privateIPs)
return drawio.GroupPrivateIPsWithLoadBalancer(vpcTn, lb.Name(), privateIPs)
}
func (pip *PrivateIP) GenerateDrawioTreeNode(gen *vpcmodel.DrawioGenerator) drawio.TreeNodeInterface {
if gen.LBAbstraction() {
Expand Down
3 changes: 2 additions & 1 deletion pkg/ibmvpc/implicit_routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ func (rt *systemImplicitRT) getIngressPath(dest *ipblock.IPBlock) (vpcmodel.Path
// traffic from some source is by default simply routed to dest node
path := rt.destAsPath(dest)
if len(path) == 0 {
return nil, fmt.Errorf("getIngressPath: failed to find path to dest resource address %s in VPC %s", dest.String(), rt.vpc.Name())
return nil, fmt.Errorf("getIngressPath: failed to find path to dest resource address %s in VPC %s",
dest.String(), rt.vpc.Name())
}
return path, nil
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/ibmvpc/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1517,19 +1517,19 @@ func printConfig(c *vpcmodel.VPCConfig) {
if n.IsExternal() {
continue
}
logging.Debug(strings.Join([]string{n.Kind(), n.CidrOrAddress(), n.Name(), n.UID()}, separator))
logging.Debug(strings.Join([]string{n.Kind(), n.CidrOrAddress(), n.NameForAnalyzerOut(c), n.UID()}, separator))
}
logging.Debug("Subnets:")
for _, n := range c.Subnets {
logging.Debug(strings.Join([]string{n.Kind(), n.CIDR(), n.Name(), n.UID()}, separator))
logging.Debug(strings.Join([]string{n.Kind(), n.CIDR(), n.NameForAnalyzerOut(c), n.UID()}, separator))
}
logging.Debug("LoadBalancers:")
for _, lb := range c.LoadBalancers {
logging.Debug(strings.Join([]string{lb.Kind(), lb.Name(), lb.AddressRange().ToIPRanges(), lb.UID()}, separator))
logging.Debug(strings.Join([]string{lb.Kind(), lb.NameForAnalyzerOut(c), lb.AddressRange().ToIPRanges(), lb.UID()}, separator))
}
logging.Debug("NodeSets:")
for _, n := range c.NodeSets {
logging.Debug(strings.Join([]string{n.Kind(), n.AddressRange().ToIPRanges(), n.Name(), n.UID()}, separator))
logging.Debug(strings.Join([]string{n.Kind(), n.AddressRange().ToIPRanges(), n.NameForAnalyzerOut(c), n.UID()}, separator))
}
logging.Debug("FilterResources:")
for _, f := range c.FilterResources {
Expand All @@ -1554,14 +1554,14 @@ func printConfig(c *vpcmodel.VPCConfig) {
}
logging.Debug("RoutingResources:")
for _, r := range c.RoutingResources {
logging.Debug(strings.Join([]string{r.Kind(), r.Name(), r.UID()}, separator))
logging.Debug(strings.Join([]string{r.Kind(), r.NameForAnalyzerOut(c), r.UID()}, separator))
if tgw, ok := r.(*TransitGateway); ok {
printTGWAvailableRoutes(tgw)
}
}
logging.Debug("RoutingTables:")
for _, r := range c.RoutingTables {
logging.Debug(strings.Join([]string{r.Kind(), r.Name(), r.UID(), "vpc:", r.VPC().UID()}, separator))
logging.Debug(strings.Join([]string{r.Kind(), r.NameForAnalyzerOut(c), r.UID(), "vpc:", r.VPC().UID()}, separator))
if rt, ok := r.(*ingressRoutingTable); ok {
logging.Debug("ingress routing table")
logging.Debug(rt.string())
Expand All @@ -1572,7 +1572,7 @@ func printConfig(c *vpcmodel.VPCConfig) {
logging.Debug("subnets:")
subnetsList := make([]string, len(rt.subnets))
for i := range rt.subnets {
subnetsList[i] = rt.subnets[i].Name()
subnetsList[i] = rt.subnets[i].NameForAnalyzerOut(c)
}
logging.Debug(strings.Join(subnetsList, ","))
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/ibmvpc/routing_tables_analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ func (irt *ingressRoutingTable) advertiseRoutes(vpcConfig *vpcmodel.VPCConfig) {
var tgwAB *TransitGateway
for _, tgw := range tgws {
for _, vpc := range tgw.vpcs {
logging.Debugf("check tgw %s with vpc %s, AP %s", tgw.Name(), vpc.Name(), vpc.AddressPrefixesIPBlock.ToCidrListString())
logging.Debugf("check tgw %s with vpc %s, AP %s", tgw.Name(),
vpc.Name(), vpc.AddressPrefixesIPBlock.ToCidrListString())
// TODO: shouldn't be containment rather than intersection?? (works with intersection on hub-n-spoke config object)
if vpc.UID() != irt.vpc.UID() && routeCidr.Overlap(vpc.AddressPrefixesIPBlock) {
vpcB = vpc
Expand Down
Loading