Skip to content

Commit

Permalink
support dual-stack
Browse files Browse the repository at this point in the history
  • Loading branch information
DockToFuture committed Nov 6, 2024
1 parent 9ab7017 commit 60a5df6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
19 changes: 18 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package main

import (
"fmt"
"net"
"os"
"strings"
"time"

"github.com/gardener/aws-custom-route-controller/pkg/controller"
Expand Down Expand Up @@ -121,7 +123,7 @@ func main() {
log.Error(err, "could not create AWS EC2 interface")
os.Exit(1)
}
customRoutes, err := updater.NewCustomRoutes(log.WithName("updater"), ec2Routes, *clusterName, *podNetworkCidr)
customRoutes, err := updater.NewCustomRoutes(log.WithName("updater"), ec2Routes, *clusterName, getIPv4PodCIDR(log, *podNetworkCidr))
if err != nil {
log.Error(err, "could not create AWS custom routes updater")
os.Exit(1)
Expand All @@ -142,3 +144,18 @@ func checkRequiredFlag(log logr.Logger, name, value string) {
os.Exit(1)
}
}

func getIPv4PodCIDR(log logr.Logger, podCIDRs string) string {
addresses := strings.Split(podCIDRs, ",")
for _, address := range addresses {
_, ipNet, err := net.ParseCIDR(address)
if err != nil {
log.Error(err, "could not parse pod-network-cidr")
os.Exit(1)
}
if ipNet.IP.To4() != nil {
return address
}
}
return ""
}
19 changes: 17 additions & 2 deletions pkg/updater/node_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ func extractNodeRoute(node *corev1.Node) *NodeRoute {
return nil
}
_, instanceID, _ := decodeRegionAndInstanceID(node.Spec.ProviderID)
return NewNodeRoute(instanceID, node.Spec.PodCIDR)
podCIDR, _ := getIPv4CIDR(node.Spec.PodCIDRs)
return NewNodeRoute(instanceID, podCIDR)
}

// decodeRegionAndInstanceID extracts region and instanceID
Expand All @@ -125,8 +126,22 @@ func decodeRegionAndInstanceID(providerID string) (string, string, error) {
}
splitProviderID := strings.Split(providerID, "/")
if len(splitProviderID) < 2 {
err := fmt.Errorf("Unable to decode provider-ID")
err := fmt.Errorf("unable to decode provider-ID")
return "", "", err
}
return splitProviderID[len(splitProviderID)-2], splitProviderID[len(splitProviderID)-1], nil
}

func getIPv4CIDR(podCIDRs []string) (string, error) {
for _, podCIDR := range podCIDRs {
_, ipNet, err := net.ParseCIDR(podCIDR)
if err != nil {
err := fmt.Errorf("unable to parse podCIDR: %s", podCIDR)
return "", err
}
if ipNet.IP.To4() != nil {
return podCIDR, nil
}
}
return "", nil
}
18 changes: 9 additions & 9 deletions pkg/updater/node_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,50 @@ import (

var _ = Describe("NamedNodeRoutes", func() {
var (
podCIDR1 = "10.0.1.0/24"
podCIDRs1 = []string{"10.0.1.0/24"}
node1InstanceID = "i-0001"
node1 = &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: corev1.NodeSpec{
PodCIDR: podCIDR1,
PodCIDRs: podCIDRs1,
ProviderID: makeProviderID(node1InstanceID),
},
}
podCIDR2 = "10.0.7.0/24"
podCIDRs2 = []string{"10.0.7.0/24"}
node2InstanceID = "i-0001"
node2 = &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node2",
},
Spec: corev1.NodeSpec{
PodCIDR: podCIDR2,
PodCIDRs: podCIDRs2,
ProviderID: makeProviderID(node2InstanceID),
},
}
podCIDR3 = "10.0.33.0/24"
node3 = &corev1.Node{
podCIDRs3 = []string{"10.0.33.0/24"}
node3 = &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node3",
},
Spec: corev1.NodeSpec{
PodCIDR: podCIDR3,
PodCIDRs: podCIDRs3,
},
}
)

It("should extract node data", func() {
routes := updater.NewNamedNodeRoutes()
route1, changed1 := routes.AddNodeRoute(node1)
Expect(route1).To(Equal(updater.NewNodeRoute(node1InstanceID, podCIDR1)))
Expect(route1).To(Equal(updater.NewNodeRoute(node1InstanceID, podCIDRs1[0])))
Expect(changed1).To(BeTrue())
route1b, changed1b := routes.AddNodeRoute(node1)
Expect(route1b).NotTo(BeNil())
Expect(changed1b).To(BeFalse())

route2, changed2 := routes.AddNodeRoute(node2)
Expect(route2).To(Equal(updater.NewNodeRoute(node2InstanceID, podCIDR2)))
Expect(route2).To(Equal(updater.NewNodeRoute(node2InstanceID, podCIDRs2[0])))
Expect(changed2).To(BeTrue())

route3, changed3 := routes.AddNodeRoute(node3)
Expand Down

0 comments on commit 60a5df6

Please sign in to comment.