Skip to content

Commit

Permalink
issue #256 : change NameToResource to be UIDToResource (#299)
Browse files Browse the repository at this point in the history
Signed-off-by: adisos <[email protected]>
  • Loading branch information
adisos authored Jan 1, 2024
1 parent 294f54e commit 5e8359e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
1 change: 1 addition & 0 deletions pkg/ibmvpc/analysis_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ var formatsAvoidComparison = map[vpcmodel.OutFormat]bool{vpcmodel.ARCHDRAWIO: tr

// uncomment the function below to run for updating the expected output
/*var formatsAvoidOutputGeneration = map[vpcmodel.OutFormat]bool{vpcmodel.ARCHDRAWIO: true, vpcmodel.DRAWIO: true}
func TestAllWithGeneration(t *testing.T) {
// tests is the list of tests to run
for testIdx := range tests {
Expand Down
48 changes: 22 additions & 26 deletions pkg/ibmvpc/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ func addZone(zoneName, vpcUID string, res map[string]*vpcmodel.VPCConfig) error
func getInstancesConfig(
instanceList []*vpc1.Instance,
subnetNameToNetIntf map[string][]*NetworkInterface,
intfNameToIntf map[string]*NetworkInterface,
res map[string]*vpcmodel.VPCConfig,
skipByVPC func(string) bool,
) error {
Expand Down Expand Up @@ -431,7 +430,7 @@ func getInstancesConfig(
return err
}
res[vpcUID].NodeSets = append(res[vpcUID].NodeSets, vsiNode)
res[vpcUID].NameToResource[vsiNode.Name()] = vsiNode
res[vpcUID].UIDToResource[vsiNode.ResourceUID] = vsiNode
for j := range instance.NetworkInterfaces {
netintf := instance.NetworkInterfaces[j]
// netintf has no CRN, thus using its ID for ResourceUID
Expand All @@ -445,9 +444,8 @@ func getInstancesConfig(
},
address: *netintf.PrimaryIP.Address, vsi: *instance.Name}
res[vpcUID].Nodes = append(res[vpcUID].Nodes, intfNode)
res[vpcUID].NameToResource[intfNode.Name()] = intfNode
res[vpcUID].UIDToResource[intfNode.ResourceUID] = intfNode
vsiNode.nodes = append(vsiNode.nodes, intfNode)
intfNameToIntf[intfNode.ResourceName] = intfNode
subnetName := *netintf.Subnet.Name
if _, ok := subnetNameToNetIntf[subnetName]; !ok {
subnetNameToNetIntf[subnetName] = []*NetworkInterface{}
Expand Down Expand Up @@ -501,7 +499,7 @@ func getSubnetsConfig(
if err := addZone(*subnet.Zone.Name, vpcUID, res); err != nil {
return nil, err
}
res[vpcUID].NameToResource[subnetNode.Name()] = subnetNode
res[vpcUID].UIDToResource[subnetNode.ResourceUID] = subnetNode
subnetNameToSubnet[*subnet.Name] = subnetNode
if subnet.PublicGateway != nil {
if _, ok := pgwToSubnet[*subnet.PublicGateway.Name]; !ok {
Expand Down Expand Up @@ -572,7 +570,7 @@ func getPgwConfig(
vpc: vpc,
} // TODO: get cidr from fip of the pgw
res[vpcUID].RoutingResources = append(res[vpcUID].RoutingResources, routerPgw)
res[vpcUID].NameToResource[routerPgw.Name()] = routerPgw
res[vpcUID].UIDToResource[routerPgw.ResourceUID] = routerPgw
err = addZone(*pgw.Zone.Name, vpcUID, res)
if err != nil {
return err
Expand Down Expand Up @@ -646,7 +644,7 @@ func getFipConfig(
},
cidr: *fip.Address, src: srcNodes}
res[vpcUID].RoutingResources = append(res[vpcUID].RoutingResources, routerFip)
res[vpcUID].NameToResource[routerFip.Name()] = routerFip
res[vpcUID].UIDToResource[routerFip.ResourceUID] = routerFip

// node with fip should not have pgw
for _, r := range res[vpcUID].RoutingResources {
Expand Down Expand Up @@ -681,7 +679,7 @@ func getVPCconfig(rc *ResourcesContainer, res map[string]*vpcmodel.VPCConfig, sk
vpcNodeSet.VPCRef = vpcNodeSet
newVPCConfig := NewEmptyVPCConfig()
newVPCConfig.NodeSets = []vpcmodel.NodeSet{vpcNodeSet}
newVPCConfig.NameToResource[vpcNodeSet.Name()] = vpcNodeSet
newVPCConfig.UIDToResource[vpcNodeSet.ResourceUID] = vpcNodeSet
newVPCConfig.VPC = vpcNodeSet
res[vpcNodeSet.ResourceUID] = newVPCConfig
}
Expand All @@ -693,21 +691,21 @@ func getVPCconfig(rc *ResourcesContainer, res map[string]*vpcmodel.VPCConfig, sk

func parseSGTargets(sgResource *SecurityGroup,
sg *vpc1.SecurityGroup,
c *vpcmodel.VPCConfig,
intfNameToIntf map[string]*NetworkInterface) {
c *vpcmodel.VPCConfig) {
targets := sg.Targets // *SecurityGroupTargetReference
// type SecurityGroupTargetReference struct
for _, target := range targets {
if targetIntfRef, ok := target.(*vpc1.SecurityGroupTargetReference); ok {
// get from target name + resource type -> find the address of the target
targetType := *targetIntfRef.ResourceType
targetName := *targetIntfRef.Name
if targetType == networkInterfaceResourceType {
if intfNode, ok := intfNameToIntf[targetName]; ok {
sgResource.members[intfNode.address] = intfNode
if intfNode, ok := c.UIDToResource[*targetIntfRef.ID]; ok {
if intfNodeObj, ok := intfNode.(*NetworkInterface); ok {
sgResource.members[intfNodeObj.address] = intfNodeObj
}
}
} else if targetType == vpeResourceType {
if vpe, ok := c.NameToResource[targetName]; ok {
if vpe, ok := c.UIDToResource[*targetIntfRef.CRN]; ok {
vpeObj := vpe.(*Vpe)
for _, n := range vpeObj.nodes {
nIP := n.(*ReservedIP)
Expand All @@ -721,7 +719,6 @@ func parseSGTargets(sgResource *SecurityGroup,

func getSGconfig(rc *ResourcesContainer,
res map[string]*vpcmodel.VPCConfig,
intfNameToIntf map[string]*NetworkInterface,
skipByVPC func(string) bool,
) error {
sgMap := map[string]map[string]*SecurityGroup{} // map from vpc uid to map from sg name to its sg object
Expand Down Expand Up @@ -749,7 +746,7 @@ func getSGconfig(rc *ResourcesContainer,
sgMap[vpcUID] = map[string]*SecurityGroup{}
}
sgMap[vpcUID][*sg.Name] = sgResource
parseSGTargets(sgResource, sg, res[vpcUID], intfNameToIntf)
parseSGTargets(sgResource, sg, res[vpcUID])
sgLists[vpcUID] = append(sgLists[vpcUID], sgResource)
}
for vpcUID, sgListInstance := range sgLists {
Expand Down Expand Up @@ -873,7 +870,7 @@ func addTGWbasedConfigs(tgws map[string]*TransitGateway, res map[string]*vpcmode
continue
}
newConfig := &vpcmodel.VPCConfig{
NameToResource: map[string]vpcmodel.VPCResourceIntf{},
UIDToResource: map[string]vpcmodel.VPCResourceIntf{},
IsMultipleVPCsConfig: true,
}
var vpcsAddressRanges *common.IPBlock // collect all internal address ranges of involved VPCs
Expand Down Expand Up @@ -904,8 +901,8 @@ func addTGWbasedConfigs(tgws map[string]*TransitGateway, res map[string]*vpcmode
// TODO: is there a scenario of connectivity from one vpc's vsi to external entity through another vpc's pgw/fip ?

// simple union for NameToResource map
for n, r := range vpcConfig.NameToResource {
newConfig.NameToResource[n] = r
for n, r := range vpcConfig.UIDToResource {
newConfig.UIDToResource[n] = r
}
if vpcsAddressRanges == nil {
vpcsAddressRanges = vpcConfig.VPC.(*VPC).internalAddressRange
Expand Down Expand Up @@ -1009,10 +1006,10 @@ func getVPEconfig(rc *ResourcesContainer,
res[vpcUID].Nodes = append(res[vpcUID].Nodes, rIPNode)
// TODO: make sure the address is in the subnet's reserved ips list?
subnet.nodes = append(subnet.nodes, rIPNode)
res[vpcUID].NameToResource[rIPNode.Name()] = rIPNode
res[vpcUID].UIDToResource[rIPNode.ResourceUID] = rIPNode
vpeResource.nodes = append(vpeResource.nodes, rIPNode)
}
res[vpcUID].NameToResource[vpeResource.ResourceName] = vpeResource
res[vpcUID].UIDToResource[vpeResource.ResourceUID] = vpeResource
// TODO: verify that vpe.SecurityGroups contain the reserved-ips as members? (not at this stage)
// sgList := vpe.SecurityGroups
}
Expand Down Expand Up @@ -1065,7 +1062,7 @@ func NewEmptyVPCConfig() *vpcmodel.VPCConfig {
NodeSets: []vpcmodel.NodeSet{},
FilterResources: []vpcmodel.FilterTrafficResource{},
RoutingResources: []vpcmodel.RoutingResource{},
NameToResource: map[string]vpcmodel.VPCResourceIntf{},
UIDToResource: map[string]vpcmodel.VPCResourceIntf{},
CloudName: "IBM Cloud",
}
}
Expand All @@ -1089,8 +1086,7 @@ func VPCConfigsFromResources(rc *ResourcesContainer, vpcID string, debug bool) (
var vpcInternalAddressRange map[string]*common.IPBlock // map from vpc name to its internal address range

subnetNameToNetIntf := map[string][]*NetworkInterface{}
intfNameToIntf := map[string]*NetworkInterface{}
err = getInstancesConfig(rc.instanceList, subnetNameToNetIntf, intfNameToIntf, res, shouldSkipByVPC)
err = getInstancesConfig(rc.instanceList, subnetNameToNetIntf, res, shouldSkipByVPC)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1124,7 +1120,7 @@ func VPCConfigsFromResources(rc *ResourcesContainer, vpcID string, debug bool) (
return nil, err
}

err = getSGconfig(rc, res, intfNameToIntf, shouldSkipByVPC)
err = getSGconfig(rc, res, shouldSkipByVPC)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1227,7 +1223,7 @@ func addExternalNodes(config *vpcmodel.VPCConfig, vpcInternalAddressRange *commo
}
config.Nodes = append(config.Nodes, externalNodes...)
for _, n := range externalNodes {
config.NameToResource[n.Name()] = n
config.UIDToResource[n.UID()] = n
}
return externalNodes, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/vpcmodel/externalNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ExternalNetwork struct {
isPublicInternet bool
}

func (exn *ExternalNetwork) UID() string { return "" }
func (exn *ExternalNetwork) UID() string { return exn.Name() }
func (exn *ExternalNetwork) ZoneName() string { return "" }
func (exn *ExternalNetwork) IsExternal() bool { return true }

Expand Down
6 changes: 3 additions & 3 deletions pkg/vpcmodel/subnetsConnectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (v *VPCsubnetConnectivity) computeAllowedConnsCombined() error {
}
var combinedConns *common.ConnectionSet
// peerNode kind is expected to be Subnet or External
peerNodeObj := v.VPCConfig.NameToResource[peerNode.Name()]
peerNodeObj := v.VPCConfig.UIDToResource[peerNode.UID()]
switch concPeerNode := peerNodeObj.(type) {
case NodeSet:
egressConns := v.AllowedConns[concPeerNode].EgressAllowedConns[subnetNodeSet]
Expand All @@ -257,7 +257,7 @@ func (v *VPCsubnetConnectivity) computeAllowedConnsCombined() error {
combinedConns := conns

// peerNode kind is expected to be Subnet or External
peerNodeObj := v.VPCConfig.NameToResource[peerNode.Name()]
peerNodeObj := v.VPCConfig.UIDToResource[peerNode.UID()]
switch peerNodeObj.(type) {
case NodeSet:
continue
Expand All @@ -281,7 +281,7 @@ func (v *VPCsubnetConnectivity) computeStatefulConnections() {
if conn.IsEmpty() {
continue
}
dstObj := v.VPCConfig.NameToResource[dst.Name()]
dstObj := v.VPCConfig.UIDToResource[dst.UID()]
var otherDirectionConn *common.ConnectionSet
switch dstObj.(type) {
case NodeSet:
Expand Down
6 changes: 3 additions & 3 deletions pkg/vpcmodel/vpcConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type VPCConfig struct {
FilterResources []FilterTrafficResource
// RoutingResources is the list of resources that enable certain types of connectivity, such as PGW, FIP
RoutingResources []RoutingResource
// NameToResource is a map from resource UID to its object in the VPC
NameToResource map[string]VPCResourceIntf
CloudName string
// UIDToResource is a map from resource UID to its object in the VPC
UIDToResource map[string]VPCResourceIntf
CloudName string
// VPC is a reference to the relevant VPC object for which this config belongs
VPC VPCResourceIntf
// IsMultipleVPCsConfig is a bool indicator, when set true, it means that the VPCConfig contains resources from
Expand Down

0 comments on commit 5e8359e

Please sign in to comment.