mirror of
https://github.com/cloudnativelabs/kube-router.git
synced 2025-10-10 17:31:04 +02:00
allow pod CIDR to be set using node annotations (#345)
This commit is contained in:
parent
25923bead8
commit
6a8e707cc3
@ -13,7 +13,6 @@ import (
|
|||||||
|
|
||||||
// GetNodeObject returns the node API object for the node
|
// GetNodeObject returns the node API object for the node
|
||||||
func GetNodeObject(clientset kubernetes.Interface, hostnameOverride string) (*apiv1.Node, error) {
|
func GetNodeObject(clientset kubernetes.Interface, hostnameOverride string) (*apiv1.Node, error) {
|
||||||
|
|
||||||
// assuming kube-router is running as pod, first check env NODE_NAME
|
// assuming kube-router is running as pod, first check env NODE_NAME
|
||||||
nodeName := os.Getenv("NODE_NAME")
|
nodeName := os.Getenv("NODE_NAME")
|
||||||
if nodeName != "" {
|
if nodeName != "" {
|
||||||
|
@ -14,6 +14,10 @@ import (
|
|||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
podCIDRAnnotation = "kube-router.io/pod-cidr"
|
||||||
|
)
|
||||||
|
|
||||||
// GetPodCidrFromCniSpec gets pod CIDR allocated to the node from CNI spec file and returns it
|
// GetPodCidrFromCniSpec gets pod CIDR allocated to the node from CNI spec file and returns it
|
||||||
func GetPodCidrFromCniSpec(cniConfFilePath string) (net.IPNet, error) {
|
func GetPodCidrFromCniSpec(cniConfFilePath string) (net.IPNet, error) {
|
||||||
var podCidr net.IPNet
|
var podCidr net.IPNet
|
||||||
@ -112,6 +116,15 @@ func GetPodCidrFromNodeSpec(clientset kubernetes.Interface, hostnameOverride str
|
|||||||
return "", fmt.Errorf("Failed to get pod CIDR allocated for the node due to: " + err.Error())
|
return "", fmt.Errorf("Failed to get pod CIDR allocated for the node due to: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cidr, ok := node.Annotations[podCIDRAnnotation]; ok {
|
||||||
|
_, _, err = net.ParseCIDR(cidr)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error parsing pod CIDR in node annotation: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cidr, nil
|
||||||
|
}
|
||||||
|
|
||||||
if node.Spec.PodCIDR == "" {
|
if node.Spec.PodCIDR == "" {
|
||||||
return "", fmt.Errorf("node.Spec.PodCIDR not set for node: %v", node.Name)
|
return "", fmt.Errorf("node.Spec.PodCIDR not set for node: %v", node.Name)
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,10 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
apiv1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_GetPodCidrFromCniSpec(t *testing.T) {
|
func Test_GetPodCidrFromCniSpec(t *testing.T) {
|
||||||
@ -118,6 +122,82 @@ func Test_InsertPodCidrInCniSpec(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_GetPodCidrFromNodeSpec(t *testing.T) {
|
||||||
|
testcases := []struct {
|
||||||
|
name string
|
||||||
|
hostnameOverride string
|
||||||
|
existingNode *apiv1.Node
|
||||||
|
podCIDR string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"node with node.Spec.PoodCIDR",
|
||||||
|
"test-node",
|
||||||
|
&apiv1.Node{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-node",
|
||||||
|
},
|
||||||
|
Spec: apiv1.NodeSpec{
|
||||||
|
PodCIDR: "172.17.0.0/24",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"172.17.0.0/24",
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"node with node.Annotations['kube-router.io/pod-cidr']",
|
||||||
|
"test-node",
|
||||||
|
&apiv1.Node{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-node",
|
||||||
|
Annotations: map[string]string{
|
||||||
|
podCIDRAnnotation: "172.17.0.0/24",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"172.17.0.0/24",
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"node with invalid pod cidr in node.Annotations['kube-router.io/pod-cidr']",
|
||||||
|
"test-node",
|
||||||
|
&apiv1.Node{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-node",
|
||||||
|
Annotations: map[string]string{
|
||||||
|
podCIDRAnnotation: "172.17.0.0",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"",
|
||||||
|
errors.New("error parsing pod CIDR in node annotation: invalid CIDR address: 172.17.0.0"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testcase := range testcases {
|
||||||
|
t.Run(testcase.name, func(t *testing.T) {
|
||||||
|
clientset := fake.NewSimpleClientset()
|
||||||
|
_, err := clientset.Core().Nodes().Create(testcase.existingNode)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create existing nodes for test: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
podCIDR, err := GetPodCidrFromNodeSpec(clientset, testcase.hostnameOverride)
|
||||||
|
if !reflect.DeepEqual(err, testcase.err) {
|
||||||
|
t.Logf("actual error: %v", err)
|
||||||
|
t.Logf("expected error: %v", testcase.err)
|
||||||
|
t.Error("did not get expected error")
|
||||||
|
}
|
||||||
|
|
||||||
|
if podCIDR != testcase.podCIDR {
|
||||||
|
t.Logf("actual podCIDR: %q", podCIDR)
|
||||||
|
t.Logf("expected podCIDR: %q", testcase.podCIDR)
|
||||||
|
t.Error("did not get expected podCIDR")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func createFile(content, filename string) (*os.File, error) {
|
func createFile(content, filename string) (*os.File, error) {
|
||||||
file, err := os.Create(filename)
|
file, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user