Skip to content

Commit

Permalink
Support preallocate continuous IPs for StatefulSet
Browse files Browse the repository at this point in the history
In order to provide better user experience, AtreamIPAM will try to
preallocate continuous IP range for StatefulSet. If unsuccesful,
IPs for the StatfulSet will be allocated on the fly, as before.

Signed-off-by: Anna Khmelnitsky <akhmelnitsky@vmware.com>
  • Loading branch information
annakhm committed Feb 16, 2022
1 parent 12f20a7 commit de41bf4
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 179 deletions.
13 changes: 7 additions & 6 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 @@ -149,11 +150,11 @@ func (c *AntreaIPAMController) getIPPoolsByPod(namespace, name string) ([]string
return nil, nil, nil, err
}
// 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 @@ -187,21 +188,21 @@ ownerReferenceLoop:
}
}

annotations, exists := pod.Annotations[AntreaIPAMAnnotationKey]
annotations, exists := pod.Annotations[annotation.AntreaIPAMAnnotationKey]
if exists {
return strings.Split(annotations, AntreaIPAMAnnotationDelimiter), ips, reservedOwner, ipErr
return strings.Split(annotations, annotation.AntreaIPAMAnnotationDelimiter), ips, reservedOwner, ipErr
}

// 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
}
return strings.Split(annotations, AntreaIPAMAnnotationDelimiter), ips, reservedOwner, ipErr
return strings.Split(annotations, annotation.AntreaIPAMAnnotationDelimiter), ips, reservedOwner, ipErr
}

func (c *AntreaIPAMController) getPoolAllocatorByPod(namespace, podName string) (*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 @@ -153,13 +154,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 @@ -171,7 +172,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 @@ -219,23 +220,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 @@ -244,7 +245,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 @@ -253,7 +254,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 @@ -262,7 +263,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 @@ -271,15 +272,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

0 comments on commit de41bf4

Please sign in to comment.