Skip to content
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

[Antrea-IPAM] Support pre-allocating continuous IPs for StatefulSet #3281

Merged
merged 2 commits into from Mar 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions pkg/agent/cniserver/ipam/antrea_ipam_controller.go
Expand Up @@ -32,6 +32,7 @@ import (
"antrea.io/antrea/pkg/client/informers/externalversions"
crdinformers "antrea.io/antrea/pkg/client/informers/externalversions/crd/v1alpha2"
crdlisters "antrea.io/antrea/pkg/client/listers/crd/v1alpha2"
annotation "antrea.io/antrea/pkg/ipam"
"antrea.io/antrea/pkg/ipam/poolallocator"
"antrea.io/antrea/pkg/util/k8s"
)
Expand Down Expand Up @@ -148,25 +149,25 @@ func (c *AntreaIPAMController) getIPPoolsByPod(namespace, name string) ([]string
return nil, nil, nil, err
}

annotations, exists := pod.Annotations[AntreaIPAMAnnotationKey]
annotations, exists := pod.Annotations[annotation.AntreaIPAMAnnotationKey]
if !exists {
// Find IPPool by Namespace
ns, err := c.namespaceLister.Get(namespace)
if err != nil {
return nil, nil, nil, nil
}
annotations, exists = ns.Annotations[AntreaIPAMAnnotationKey]
annotations, exists = ns.Annotations[annotation.AntreaIPAMAnnotationKey]
if !exists {
return nil, nil, nil, nil
}
}

// Collect specified IPs if exist
ipStrings, _ := pod.Annotations[AntreaIPAMPodIPAnnotationKey]
ipStrings, _ := pod.Annotations[annotation.AntreaIPAMPodIPAnnotationKey]
ipStrings = strings.ReplaceAll(ipStrings, " ", "")
var ipErr error
if ipStrings != "" {
splittedIPStrings := strings.Split(ipStrings, AntreaIPAMAnnotationDelimiter)
splittedIPStrings := strings.Split(ipStrings, annotation.AntreaIPAMAnnotationDelimiter)
for _, ipString := range splittedIPStrings {
ip := net.ParseIP(ipString)
if ipString != "" && ip == nil {
Expand Down Expand Up @@ -200,7 +201,7 @@ ownerReferenceLoop:
}
}

return strings.Split(annotations, AntreaIPAMAnnotationDelimiter), ips, reservedOwner, ipErr
return strings.Split(annotations, annotation.AntreaIPAMAnnotationDelimiter), ips, reservedOwner, ipErr
}

func (c *AntreaIPAMController) getPoolAllocatorByPod(namespace, podName string) (mineType, *poolallocator.IPPoolAllocator, []net.IP, *crdv1a2.IPAddressOwner, error) {
Expand Down
23 changes: 12 additions & 11 deletions pkg/agent/cniserver/ipam/antrea_ipam_test.go
Expand Up @@ -39,6 +39,7 @@ import (
argtypes "antrea.io/antrea/pkg/agent/cniserver/types"
crdv1a2 "antrea.io/antrea/pkg/apis/crd/v1alpha2"
crdinformers "antrea.io/antrea/pkg/client/informers/externalversions"
annotations "antrea.io/antrea/pkg/ipam"
fakepoolclient "antrea.io/antrea/pkg/ipam/poolallocator/testing"
)

Expand Down Expand Up @@ -154,13 +155,13 @@ func initTestClients() (*fake.Clientset, *fakepoolclient.IPPoolClientset) {
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: testApple,
Annotations: map[string]string{AntreaIPAMAnnotationKey: testApple, "junk": "garbage"},
Annotations: map[string]string{annotations.AntreaIPAMAnnotationKey: testApple, "junk": "garbage"},
},
},
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: testOrange,
Annotations: map[string]string{"junk": "garbage", AntreaIPAMAnnotationKey: testOrange},
Annotations: map[string]string{"junk": "garbage", annotations.AntreaIPAMAnnotationKey: testOrange},
},
},
&corev1.Namespace{
Expand All @@ -172,7 +173,7 @@ func initTestClients() (*fake.Clientset, *fakepoolclient.IPPoolClientset) {
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: testJunkAnnotation,
Annotations: map[string]string{AntreaIPAMAnnotationKey: testJunkAnnotation},
Annotations: map[string]string{annotations.AntreaIPAMAnnotationKey: testJunkAnnotation},
},
},
&corev1.Namespace{
Expand Down Expand Up @@ -220,23 +221,23 @@ func initTestClients() (*fake.Clientset, *fakepoolclient.IPPoolClientset) {
ObjectMeta: metav1.ObjectMeta{
Name: "pear1",
Namespace: testPear,
Annotations: map[string]string{"junk": "garbage", AntreaIPAMAnnotationKey: testPear},
Annotations: map[string]string{"junk": "garbage", annotations.AntreaIPAMAnnotationKey: testPear},
},
Spec: corev1.PodSpec{NodeName: "fakeNode"},
},
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pear2",
Namespace: testPear,
Annotations: map[string]string{"junk": "garbage", AntreaIPAMAnnotationKey: testPear, AntreaIPAMPodIPAnnotationKey: " "},
Annotations: map[string]string{"junk": "garbage", annotations.AntreaIPAMAnnotationKey: testPear, annotations.AntreaIPAMPodIPAnnotationKey: " "},
},
Spec: corev1.PodSpec{NodeName: "fakeNode"},
},
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pear3",
Namespace: testPear,
Annotations: map[string]string{"junk": "garbage", AntreaIPAMAnnotationKey: testPear, AntreaIPAMPodIPAnnotationKey: "10.2.3.199"},
Annotations: map[string]string{"junk": "garbage", annotations.AntreaIPAMAnnotationKey: testPear, annotations.AntreaIPAMPodIPAnnotationKey: "10.2.3.199"},
},
Spec: corev1.PodSpec{NodeName: "fakeNode"},
},
Expand All @@ -245,7 +246,7 @@ func initTestClients() (*fake.Clientset, *fakepoolclient.IPPoolClientset) {
// conflict
Name: "pear4",
Namespace: testPear,
Annotations: map[string]string{"junk": "garbage", AntreaIPAMAnnotationKey: testPear, AntreaIPAMPodIPAnnotationKey: "10.2.3.199"},
Annotations: map[string]string{"junk": "garbage", annotations.AntreaIPAMAnnotationKey: testPear, annotations.AntreaIPAMPodIPAnnotationKey: "10.2.3.199"},
},
Spec: corev1.PodSpec{NodeName: "fakeNode"},
},
Expand All @@ -254,7 +255,7 @@ func initTestClients() (*fake.Clientset, *fakepoolclient.IPPoolClientset) {
// out of range
Name: "pear5",
Namespace: testPear,
Annotations: map[string]string{"junk": "garbage", AntreaIPAMAnnotationKey: testPear, AntreaIPAMPodIPAnnotationKey: "10.2.4.199"},
Annotations: map[string]string{"junk": "garbage", annotations.AntreaIPAMAnnotationKey: testPear, annotations.AntreaIPAMPodIPAnnotationKey: "10.2.4.199"},
},
Spec: corev1.PodSpec{NodeName: "fakeNode"},
},
Expand All @@ -263,7 +264,7 @@ func initTestClients() (*fake.Clientset, *fakepoolclient.IPPoolClientset) {
// invalid IP
Name: "pear6",
Namespace: testPear,
Annotations: map[string]string{"junk": "garbage", AntreaIPAMAnnotationKey: testPear, AntreaIPAMPodIPAnnotationKey: "junk"},
Annotations: map[string]string{"junk": "garbage", annotations.AntreaIPAMAnnotationKey: testPear, annotations.AntreaIPAMPodIPAnnotationKey: "junk"},
},
Spec: corev1.PodSpec{NodeName: "fakeNode"},
},
Expand All @@ -272,15 +273,15 @@ func initTestClients() (*fake.Clientset, *fakepoolclient.IPPoolClientset) {
// invalid IPPool
Name: "pear7",
Namespace: testPear,
Annotations: map[string]string{"junk": "garbage", AntreaIPAMAnnotationKey: testJunkAnnotation},
Annotations: map[string]string{"junk": "garbage", annotations.AntreaIPAMAnnotationKey: testJunkAnnotation},
},
Spec: corev1.PodSpec{NodeName: "fakeNode"},
},
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pear-sts-8",
Namespace: testPear,
Annotations: map[string]string{AntreaIPAMAnnotationKey: testPear},
Annotations: map[string]string{annotations.AntreaIPAMAnnotationKey: testPear},
OwnerReferences: []metav1.OwnerReference{{Controller: &bTrue, Kind: "StatefulSet"}},
},
Spec: corev1.PodSpec{NodeName: "fakeNode"},
Expand Down