Skip to content

Commit

Permalink
Support ip creation (#3667)
Browse files Browse the repository at this point in the history
* support ip creation
* merge node gateway static routes handling
* fix webhook
---------

Signed-off-by: bobz965 <[email protected]>
  • Loading branch information
zbb88888 authored Jan 31, 2024
1 parent 38db6c3 commit 6db8c84
Show file tree
Hide file tree
Showing 23 changed files with 553 additions and 313 deletions.
15 changes: 6 additions & 9 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,27 +798,24 @@ func (c *Controller) Run(ctx context.Context) {
}

// sync ip crd before initIPAM since ip crd will be used to restore vm and statefulset pod in initIPAM
if err := c.initSyncCrdIPs(); err != nil {
if err := c.syncIPCR(); err != nil {
util.LogFatalAndExit(err, "failed to sync crd ips")
}

if err := c.InitIPAM(); err != nil {
util.LogFatalAndExit(err, "failed to initialize ipam")
}

if err := c.initNodeRoutes(); err != nil {
if err := c.syncNodeRoutes(); err != nil {
util.LogFatalAndExit(err, "failed to initialize node routes")
}

if err := c.initSyncCrdSubnets(); err != nil {
if err := c.syncSubnetCR(); err != nil {
util.LogFatalAndExit(err, "failed to sync crd subnets")
}
if err := c.initSyncCrdVlans(); err != nil {
util.LogFatalAndExit(err, "failed to sync crd vlans")
}

if err := c.addNodeGwStaticRoute(); err != nil {
util.LogFatalAndExit(err, "failed to add static route for node gateway")
if err := c.syncVlanCR(); err != nil {
util.LogFatalAndExit(err, "failed to sync crd vlans")
}

// start workers to do all the network operations
Expand Down Expand Up @@ -1167,7 +1164,7 @@ func (c *Controller) initResourceOnce() {
}

if c.config.PodDefaultFipType == util.IptablesFip {
if err := c.initSyncCrdVpcNatGw(); err != nil {
if err := c.syncVpcNatGatewayCR(); err != nil {
util.LogFatalAndExit(err, "failed to sync crd vpc nat gateways")
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/external_gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (c *Controller) createDefaultVpcLrpEip() (string, string, error) {
klog.Errorf("failed to acquire ip address for default vpc lrp %s, %v", lrpEipName, err)
return "", "", err
}
if err := c.createOrUpdateCrdOvnEip(lrpEipName, c.config.ExternalGatewaySwitch, v4ip, v6ip, mac, util.OvnEipTypeLRP); err != nil {
if err := c.createOrUpdateOvnEipCR(lrpEipName, c.config.ExternalGatewaySwitch, v4ip, v6ip, mac, util.OvnEipTypeLRP); err != nil {
klog.Errorf("failed to create ovn eip cr for lrp %s, %v", lrpEipName, err)
return "", "", err
}
Expand Down
35 changes: 19 additions & 16 deletions pkg/controller/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,7 @@ func (c *Controller) markAndCleanLSP() error {
klog.Errorf("failed to delete lsp %s: %v", lsp.Name, err)
return err
}
klog.Infof("gc ip %s", lsp.Name)
ipCr, err := c.config.KubeOvnClient.KubeovnV1().IPs().Get(context.Background(), lsp.Name, metav1.GetOptions{})
ipCR, err := c.config.KubeOvnClient.KubeovnV1().IPs().Get(context.Background(), lsp.Name, metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
// ip cr not found, skip lsp gc
Expand All @@ -368,22 +367,26 @@ func (c *Controller) markAndCleanLSP() error {
klog.Errorf("failed to get ip %s, %v", lsp.Name, err)
return err
}
klog.Infof("gc ip %s", ipCr.Name)
if err := c.config.KubeOvnClient.KubeovnV1().IPs().Delete(context.Background(), ipCr.Name, metav1.DeleteOptions{}); err != nil {
if k8serrors.IsNotFound(err) {
// ip cr not found, skip lsp gc
if ipCR.Labels[util.IPReservedLabel] != "true" {
klog.Infof("gc ip %s", ipCR.Name)
if err := c.config.KubeOvnClient.KubeovnV1().IPs().Delete(context.Background(), ipCR.Name, metav1.DeleteOptions{}); err != nil {
if k8serrors.IsNotFound(err) {
// ip cr not found, skip lsp gc
continue
}
klog.Errorf("failed to delete ip %s, %v", ipCR.Name, err)
return err
}
if ipCR.Spec.Subnet == "" {
klog.Errorf("ip %s has no subnet", ipCR.Name)
// ip cr no subnet, skip lsp gc
continue
}
klog.Errorf("failed to delete ip %s, %v", ipCr.Name, err)
return err
}
if ipCr.Spec.Subnet == "" {
klog.Errorf("ip %s has no subnet", ipCr.Name)
// ip cr no subnet, skip lsp gc
continue
}
if key := lsp.ExternalIDs["pod"]; key != "" {
c.ipam.ReleaseAddressByPod(key, ipCr.Spec.Subnet)
if key := lsp.ExternalIDs["pod"]; key != "" {
c.ipam.ReleaseAddressByPod(key, ipCR.Spec.Subnet)
}
} else {
klog.Infof("gc skip reserved ip %s", ipCR.Name)
}
}
lastNoPodLSP = noPodLSP
Expand Down
25 changes: 15 additions & 10 deletions pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (c *Controller) InitIPAM() error {

for _, ip := range ips {
// recover sts and kubevirt vm ip, other ip recover in later pod loop
if ip.Spec.PodType != "StatefulSet" && ip.Spec.PodType != util.VM {
if ip.Spec.PodType != util.StatefulSet && ip.Spec.PodType != util.VM {
continue
}

Expand Down Expand Up @@ -365,7 +365,7 @@ func (c *Controller) InitIPAM() error {
if err != nil {
klog.Errorf("failed to init pod %s.%s address %s: %v", podName, pod.Namespace, pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, podNet.ProviderName)], err)
} else {
err = c.createOrUpdateCrdIPs(podName, ip, mac, podNet.Subnet.Name, pod.Namespace, pod.Spec.NodeName, podNet.ProviderName, podType)
err = c.createOrUpdateIPCR(portName, podName, ip, mac, podNet.Subnet.Name, pod.Namespace, pod.Spec.NodeName, podType)
if err != nil {
klog.Errorf("failed to create/update ips CR %s.%s with ip address %s: %v", podName, pod.Namespace, ip, err)
}
Expand Down Expand Up @@ -563,7 +563,7 @@ func (c *Controller) initDefaultVlan() error {
return nil
}

func (c *Controller) initSyncCrdIPs() error {
func (c *Controller) syncIPCR() error {
klog.Info("start to sync ips")
ips, err := c.ipsLister.List(labels.Everything())
if err != nil {
Expand All @@ -575,8 +575,8 @@ func (c *Controller) initSyncCrdIPs() error {

ipMap := strset.New(c.getVMLsps()...)

for _, ipCr := range ips {
ip := ipCr.DeepCopy()
for _, ipCR := range ips {
ip := ipCR.DeepCopy()
changed := false
if ipMap.Has(ip.Name) && ip.Spec.PodType == "" {
ip.Spec.PodType = util.VM
Expand All @@ -587,9 +587,9 @@ func (c *Controller) initSyncCrdIPs() error {
if ip.Spec.V4IPAddress == v4IP && ip.Spec.V6IPAddress == v6IP && !changed {
continue
}

ip.Spec.V4IPAddress = v4IP
ip.Spec.V6IPAddress = v6IP

_, err := c.config.KubeOvnClient.KubeovnV1().IPs().Update(context.Background(), ip, metav1.UpdateOptions{})
if err != nil {
klog.Errorf("failed to sync crd ip %s: %v", ip.Spec.IPAddress, err)
Expand All @@ -599,7 +599,7 @@ func (c *Controller) initSyncCrdIPs() error {
return nil
}

func (c *Controller) initSyncCrdSubnets() error {
func (c *Controller) syncSubnetCR() error {
klog.Info("start to sync subnets")
subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
Expand Down Expand Up @@ -639,7 +639,7 @@ func (c *Controller) initSyncCrdSubnets() error {
return nil
}

func (c *Controller) initSyncCrdVpcNatGw() error {
func (c *Controller) syncVpcNatGatewayCR() error {
klog.Info("start to sync crd vpc nat gw")
// get vpc nat gateway enable state
cm, err := c.configMapsLister.ConfigMaps(c.config.PodNamespace).Get(util.VpcNatGatewayConfig)
Expand Down Expand Up @@ -681,7 +681,7 @@ func (c *Controller) initSyncCrdVpcNatGw() error {
return nil
}

func (c *Controller) initSyncCrdVlans() error {
func (c *Controller) syncVlanCR() error {
klog.Info("start to sync vlans")
vlans, err := c.vlansLister.List(labels.Everything())
if err != nil {
Expand Down Expand Up @@ -717,6 +717,7 @@ func (c *Controller) initSyncCrdVlans() error {
}

func (c *Controller) migrateNodeRoute(af int, node, ip, nexthop string) error {
// default vpc use static route in old version, migrate to policy route
var (
match = fmt.Sprintf("ip%d.dst == %s", af, ip)
action = kubeovnv1.PolicyRouteActionReroute
Expand Down Expand Up @@ -768,7 +769,7 @@ func (c *Controller) migrateNodeRoute(af int, node, ip, nexthop string) error {
return nil
}

func (c *Controller) initNodeRoutes() error {
func (c *Controller) syncNodeRoutes() error {
nodes, err := c.nodesLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list nodes: %v", err)
Expand All @@ -792,6 +793,10 @@ func (c *Controller) initNodeRoutes() error {
}
}

if err := c.addNodeGatewayStaticRoute(); err != nil {
klog.Errorf("failed to add static route for node gateway")
return err
}
return nil
}

Expand Down
Loading

0 comments on commit 6db8c84

Please sign in to comment.