Skip to content

Commit

Permalink
[flexible-ipam] Wait for controller ready when processing CNI request
Browse files Browse the repository at this point in the history
Signed-off-by: gran <gran@vmware.com>
  • Loading branch information
gran-vmv committed Mar 8, 2022
1 parent ffb4025 commit 248b98d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 10 additions & 4 deletions pkg/agent/cniserver/ipam/antrea_ipam.go
Expand Up @@ -17,6 +17,7 @@ package ipam
import (
"fmt"
"net"
"sync"

"github.com/containernetworking/cni/pkg/invoke"
cnitypes "github.com/containernetworking/cni/pkg/types"
Expand All @@ -37,7 +38,8 @@ const (
// if present. If annotation is not present, the driver will delegate functionality
// to traditional IPAM driver.
type AntreaIPAM struct {
controller *AntreaIPAMController
controller *AntreaIPAMController
controllerMutex sync.RWMutex
}

// Global variable is needed to work around order of initialization
Expand Down Expand Up @@ -97,6 +99,8 @@ func generateIPConfig(ip net.IP, prefixLength int, gwIP net.IP) (*current.IPConf
}

func (d *AntreaIPAM) setController(controller *AntreaIPAMController) {
d.controllerMutex.Lock()
defer d.controllerMutex.Unlock()
d.controller = controller
}

Expand Down Expand Up @@ -197,9 +201,11 @@ func (d *AntreaIPAM) Check(args *invoke.Args, k8sArgs *argtypes.K8sArgs, network
// of today). If annotation is not present, or annotated IP Pool not found, the driver
// will not own the request and fall back to next IPAM driver.
func (d *AntreaIPAM) owns(k8sArgs *argtypes.K8sArgs, errFunc func(error) error) (bool, *poolallocator.IPPoolAllocator, []net.IP, *crdv1a2.IPAddressOwner, error) {
if d.controller == nil {
klog.Warningf("Antrea IPAM driver failed to initialize due to inconsistent configuration. Falling back to default IPAM")
return false, nil, nil, nil, nil
// Wait controller ready to avoid inappropriate behavior on CNI request
d.controllerMutex.RLock()
defer d.controllerMutex.RUnlock()
if err := d.controller.waitControllerReadyForCNIRequest(); err != nil {
return false, nil, nil, nil, err
}

// As of today, only Namespace annotation is supported
Expand Down
15 changes: 13 additions & 2 deletions pkg/agent/cniserver/ipam/antrea_ipam_controller.go
Expand Up @@ -18,8 +18,10 @@ import (
"fmt"
"net"
"strings"
"time"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/informers"
coreinformers "k8s.io/client-go/informers/core/v1"
clientset "k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -105,8 +107,6 @@ func InitializeAntreaIPAMController(kubeClient clientset.Interface, crdClient cl
return nil, fmt.Errorf("Antrea IPAM driver failed to initialize")
}

antreaIPAMDriver.setController(antreaIPAMController)

return antreaIPAMController, nil
}

Expand All @@ -121,10 +121,21 @@ func (c *AntreaIPAMController) Run(stopCh <-chan struct{}) {
if !cache.WaitForNamedCacheSync(controllerName, stopCh, c.namespaceInformer.Informer().HasSynced, c.ipPoolInformer.Informer().HasSynced, c.podInformer.HasSynced) {
return
}
antreaIPAMDriver.setController(c)

<-stopCh
}

func (c *AntreaIPAMController) waitControllerReadyForCNIRequest() error {
return wait.PollImmediate(500*time.Millisecond, 5*time.Second, func() (bool, error) {
if c == nil {
klog.Warningf("Antrea IPAM driver is not ready.")
return false, nil
}
return true, nil
})
}

func (c *AntreaIPAMController) getIPPoolsByPod(namespace, name string) ([]string, []net.IP, *crdv1a2.IPAddressOwner, error) {
// Find IPPool by Pod
var ips []net.IP
Expand Down

0 comments on commit 248b98d

Please sign in to comment.