Skip to content

Commit

Permalink
Merge pull request #216 from wangyizhi1/main
Browse files Browse the repository at this point in the history
fix: null pointer exception when creating routes
  • Loading branch information
kosmos-robot committed Nov 10, 2023
2 parents 3d210f5 + 1a8ca8a commit 9c38ba1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion deploy/clusterlink-network-manager.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ spec:
imagePullPolicy: IfNotPresent
command:
- clusterlink-network-manager
- v=4
- --v=4
resources:
limits:
memory: 500Mi
Expand Down
38 changes: 38 additions & 0 deletions pkg/clusterlink/network-manager/handlers/pod_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,35 @@ func (h *PodRoutes) Do(c *Context) (err error) {
podCIDRs = cluster.Status.ClusterLinkStatus.PodCIDRs
}

podCIDRs = FilterByIPFamily(podCIDRs, cluster.Spec.ClusterLinkOptions.IPFamily)
podCIDRs = ConvertToGlobalCIDRs(podCIDRs, cluster.Spec.ClusterLinkOptions.GlobalCIDRsMap)
BuildRoutes(c, target, podCIDRs)
}

return nil
}

func convertIPFamilyTypeToIPType(familyType v1alpha1.IPFamilyType) helpers.IPType {
if familyType == v1alpha1.IPFamilyTypeIPV4 {
return helpers.IPV4
}
return helpers.IPV6
}

func FilterByIPFamily(cidrs []string, familyType v1alpha1.IPFamilyType) (results []string) {
if familyType == v1alpha1.IPFamilyTypeALL {
return cidrs
}
specifiedIPType := convertIPFamilyTypeToIPType(familyType)
for _, cidr := range cidrs {
ipType := helpers.GetIPType(cidr)
if ipType == specifiedIPType {
results = append(results, cidr)
}
}
return
}

func ConvertToGlobalCIDRs(cidrs []string, globalCIDRMap map[string]string) []string {
if len(globalCIDRMap) == 0 {
return cidrs
Expand Down Expand Up @@ -64,6 +86,14 @@ func ifCIDRConflictWithSelf(selfCIDRs []string, tarCIDR string) bool {
return false
}

func SupportIPType(cluster *v1alpha1.Cluster, ipType helpers.IPType) bool {
if cluster.Spec.ClusterLinkOptions.IPFamily == v1alpha1.IPFamilyTypeALL {
return true
}
specifiedIPType := convertIPFamilyTypeToIPType(cluster.Spec.ClusterLinkOptions.IPFamily)
return specifiedIPType == ipType
}

func BuildRoutes(ctx *Context, target *v1alpha1.ClusterNode, cidrs []string) {
otherClusterNodes := ctx.Filter.GetAllNodesExceptCluster(target.Spec.ClusterName)

Expand All @@ -81,6 +111,11 @@ func BuildRoutes(ctx *Context, target *v1alpha1.ClusterNode, cidrs []string) {
}

targetDev := ctx.GetDeviceFromResults(target.Name, vxBridge)
if targetDev == nil {
klog.Warning("cannot find the target dev, nodeName: %s, devName: %s", target.Name, vxBridge)
continue
}

targetIP, _, err := net.ParseCIDR(targetDev.Addr)
if err != nil {
klog.Warning("cannot parse target dev addr, nodeName: %s, devName: %s", target.Name, vxBridge)
Expand All @@ -89,6 +124,9 @@ func BuildRoutes(ctx *Context, target *v1alpha1.ClusterNode, cidrs []string) {

for _, n := range otherClusterNodes {
srcCluster := ctx.Filter.GetClusterByName(n.Spec.ClusterName)
if !SupportIPType(srcCluster, ipType) {
continue
}

allCIDRs := append(srcCluster.Status.ClusterLinkStatus.PodCIDRs, srcCluster.Status.ClusterLinkStatus.ServiceCIDRs...)
if ifCIDRConflictWithSelf(allCIDRs, cidr) {
Expand Down
1 change: 1 addition & 0 deletions pkg/clusterlink/network-manager/handlers/svc_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func (h *ServiceRoutes) Do(c *Context) (err error) {
cluster := c.Filter.GetClusterByName(target.Spec.ClusterName)
serviceCIDRs := cluster.Status.ClusterLinkStatus.ServiceCIDRs

serviceCIDRs = FilterByIPFamily(serviceCIDRs, cluster.Spec.ClusterLinkOptions.IPFamily)
serviceCIDRs = ConvertToGlobalCIDRs(serviceCIDRs, cluster.Spec.ClusterLinkOptions.GlobalCIDRsMap)
BuildRoutes(c, target, serviceCIDRs)
}
Expand Down

0 comments on commit 9c38ba1

Please sign in to comment.