Skip to content

Commit

Permalink
add kube-ovn-controller switch for EIP and SNAT
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Apr 19, 2022
1 parent ca2ca1a commit a0e2404
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
3 changes: 3 additions & 0 deletions pkg/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Configuration struct {

EnableLb bool
EnableNP bool
EnableEipSnat bool
EnableExternalVpc bool
EnableEcmp bool
EnableKeepVmIP bool
Expand Down Expand Up @@ -100,6 +101,7 @@ func ParseFlags() (*Configuration, error) {
argPodNicType = pflag.String("pod-nic-type", "veth-pair", "The default pod network nic implementation type, default: veth-pair")
argEnableLb = pflag.Bool("enable-lb", true, "Enable load balancer, default: true")
argEnableNP = pflag.Bool("enable-np", true, "Enable network policy support, default: true")
argEnableEipSnat = pflag.Bool("enable-eip-snat", true, "Enable EIP and SNAT")
argEnableExternalVpc = pflag.Bool("enable-external-vpc", true, "Enable external vpc support, default: true")
argEnableEcmp = pflag.Bool("enable-ecmp", false, "Enable ecmp route for centralized subnet")
argKeepVmIP = pflag.Bool("keep-vm-ip", false, "Whether to keep ip for kubevirt pod when pod is rebuild")
Expand Down Expand Up @@ -152,6 +154,7 @@ func ParseFlags() (*Configuration, error) {
PodNicType: *argPodNicType,
EnableLb: *argEnableLb,
EnableNP: *argEnableNP,
EnableEipSnat: *argEnableEipSnat,
EnableExternalVpc: *argEnableExternalVpc,
EnableEcmp: *argEnableEcmp,
EnableKeepVmIP: *argKeepVmIP,
Expand Down
42 changes: 26 additions & 16 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,14 @@ func (c *Controller) processNextDeletePodWorkItem() bool {
utilruntime.HandleError(fmt.Errorf("expected pod in workqueue but got %#v", obj))
return nil
}
klog.Infof("handle delete pod %s", pod.Name)
klog.Infof("handle delete pod %s/%s", pod.Namespace, pod.Name)
if err := c.handleDeletePod(pod); err != nil {
c.deletePodQueue.AddRateLimited(obj)
return fmt.Errorf("error syncing '%s': %s, requeuing", pod.Name, err.Error())
}
c.deletePodQueue.Forget(obj)
last := time.Since(now)
klog.Infof("take %d ms to handle delete pod %s", last.Milliseconds(), pod.Name)
klog.Infof("take %d ms to handle delete pod %s/%s", last.Milliseconds(), pod.Namespace, pod.Name)
return nil
}(obj)

Expand All @@ -326,6 +326,7 @@ func (c *Controller) processNextUpdatePodWorkItem() bool {
return false
}

now := time.Now()
err := func(obj interface{}) error {
defer c.updatePodQueue.Done(obj)
var key string
Expand All @@ -335,11 +336,14 @@ func (c *Controller) processNextUpdatePodWorkItem() bool {
utilruntime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
return nil
}
klog.Infof("handle update pod %s", key)
if err := c.handleUpdatePod(key); err != nil {
c.updatePodQueue.AddRateLimited(key)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
}
c.updatePodQueue.Forget(obj)
last := time.Since(now)
klog.Infof("take %d ms to handle update pod %s", last.Milliseconds(), key)
return nil
}(obj)

Expand Down Expand Up @@ -417,13 +421,15 @@ func (c *Controller) getPodKubeovnNets(pod *v1.Pod) ([]*kubeovnNet, error) {
}

func (c *Controller) handleAddPod(key string) error {
c.podKeyMutex.Lock(key)
defer c.podKeyMutex.Unlock(key)
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
utilruntime.HandleError(fmt.Errorf("invalid resource key: %s", key))
return nil
}

c.podKeyMutex.Lock(key)
defer c.podKeyMutex.Unlock(key)

oripod, err := c.podsLister.Pods(namespace).Get(name)
if err != nil {
if k8serrors.IsNotFound(err) {
Expand All @@ -444,7 +450,7 @@ func (c *Controller) handleAddPod(key string) error {
}

op := "replace"
if pod.Annotations == nil || len(pod.Annotations) == 0 {
if len(pod.Annotations) == 0 {
op = "add"
pod.Annotations = map[string]string{}
}
Expand Down Expand Up @@ -699,13 +705,15 @@ func (c *Controller) handleUpdatePodSecurity(key string) error {
}

func (c *Controller) handleUpdatePod(key string) error {
c.podKeyMutex.Lock(key)
defer c.podKeyMutex.Unlock(key)
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
utilruntime.HandleError(fmt.Errorf("invalid resource key: %s", key))
return nil
}

c.podKeyMutex.Lock(key)
defer c.podKeyMutex.Unlock(key)

oripod, err := c.podsLister.Pods(namespace).Get(name)
if err != nil {
if k8serrors.IsNotFound(err) {
Expand Down Expand Up @@ -742,7 +750,7 @@ func (c *Controller) handleUpdatePod(key string) error {
}

if podIP != "" && subnet.Spec.Vlan == "" && subnet.Spec.Vpc == util.DefaultVpc {
if pod.Annotations[util.EipAnnotation] != "" || pod.Annotations[util.SnatAnnotation] != "" {
if c.config.EnableEipSnat && (pod.Annotations[util.EipAnnotation] != "" || pod.Annotations[util.SnatAnnotation] != "") {
cm, err := c.configMapsLister.ConfigMaps("kube-system").Get(util.ExternalGatewayConfig)
if err != nil {
klog.Errorf("failed to get ex-gateway config, %v", err)
Expand Down Expand Up @@ -794,15 +802,17 @@ func (c *Controller) handleUpdatePod(key string) error {
}
}

for _, ipStr := range strings.Split(podIP, ",") {
if err := c.ovnClient.UpdateNatRule("dnat_and_snat", ipStr, pod.Annotations[util.EipAnnotation], c.config.ClusterRouter, pod.Annotations[util.MacAddressAnnotation], fmt.Sprintf("%s.%s", podName, pod.Namespace)); err != nil {
klog.Errorf("failed to add nat rules, %v", err)
return err
}
if c.config.EnableEipSnat {
for _, ipStr := range strings.Split(podIP, ",") {
if err := c.ovnClient.UpdateNatRule("dnat_and_snat", ipStr, pod.Annotations[util.EipAnnotation], c.config.ClusterRouter, pod.Annotations[util.MacAddressAnnotation], fmt.Sprintf("%s.%s", podName, pod.Namespace)); err != nil {
klog.Errorf("failed to add nat rules, %v", err)
return err
}

if err := c.ovnClient.UpdateNatRule("snat", ipStr, pod.Annotations[util.SnatAnnotation], c.config.ClusterRouter, "", ""); err != nil {
klog.Errorf("failed to add nat rules, %v", err)
return err
if err := c.ovnClient.UpdateNatRule("snat", ipStr, pod.Annotations[util.SnatAnnotation], c.config.ClusterRouter, "", ""); err != nil {
klog.Errorf("failed to add nat rules, %v", err)
return err
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,10 @@ func (c *Controller) reconcileGateway(subnet *kubeovnv1.Subnet) error {
}

for _, pod := range pods {
if !isPodAlive(pod) || pod.Annotations[util.IpAddressAnnotation] == "" || pod.Annotations[util.LogicalSwitchAnnotation] != subnet.Name {
if !isPodAlive(pod) {
continue
}
if c.config.EnableEipSnat && (pod.Annotations[util.EipAnnotation] != "" || pod.Annotations[util.SnatAnnotation] != "") {
continue
}

Expand Down

0 comments on commit a0e2404

Please sign in to comment.