Skip to content

Commit

Permalink
optimize external vpc
Browse files Browse the repository at this point in the history
Signed-off-by: bobz965 <[email protected]>
  • Loading branch information
bobz965 committed May 20, 2024
1 parent bcc8d16 commit de1ead0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/client_go_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

// this file contains setup logic to initialize the myriad of places
// that client-go registers metrics. We copy the names and formats
// that client-go registers metrics. we copy the names and formats
// from Kubernetes so that we match the core controllers.

var (
Expand Down
56 changes: 27 additions & 29 deletions pkg/controller/external_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,40 @@ import (
"github.com/kubeovn/kube-ovn/pkg/util"
)

// syncExternalVpc syncs the non kubeovn created ovn vpc, such as neutron ovn vpc
func (c *Controller) syncExternalVpc() {
logicalRouters, err := c.getRouterStatus()
klog.V(4).Infof("sync over with %s", logicalRouters)
logicalRouters, err := c.getNonKubeovnRouterStatus()
if err != nil {
klog.Error("list lr failed", err)
klog.Error("failed to list ovn logical routers", err)
return
}
klog.V(3).Infof("sync external vpcs with ovn non kube-ovn created vpc")
vpcs, err := c.vpcsLister.List(labels.SelectorFromSet(labels.Set{util.VpcExternalLabel: "true"}))
if err != nil {
klog.Errorf("failed to list vpc, %v", err)
return
}
vpcMaps := make(map[string]*v1.Vpc)
for _, vpc := range vpcs {
vpcMaps[vpc.Name] = vpc.DeepCopy()
}
for vpcName, vpc := range vpcMaps {
if _, ok := logicalRouters[vpcName]; ok {
for _, cachedVpc := range vpcs {
vpc := cachedVpc.DeepCopy()
if _, ok := logicalRouters[vpc.Name]; ok {
// update vpc status subnet list
vpc.Status.Subnets = []string{}
for _, asw := range logicalRouters[vpcName].LogicalSwitches {
vpc.Status.Subnets = append(vpc.Status.Subnets, asw.Name)
for _, ls := range logicalRouters[vpc.Name].LogicalSwitches {
vpc.Status.Subnets = append(vpc.Status.Subnets, ls.Name)
}
_, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().UpdateStatus(context.Background(), vpc, metav1.UpdateOptions{})
if err != nil {
klog.Errorf("update vpc %s status failed: %v", vpcName, err)
klog.Errorf("failed to update vpc %s status: %v", vpc.Name, err)
continue
}
delete(logicalRouters, vpcName)
klog.V(4).Infof("patch vpc %s", vpcName)
delete(logicalRouters, vpc.Name)
} else {
err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Delete(context.Background(), vpcName, metav1.DeleteOptions{})
klog.Infof("external vpc %s has no ovn logical router, delete it", vpc.Name)
err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Delete(context.Background(), vpc.Name, metav1.DeleteOptions{})
if err != nil {
klog.Errorf("delete vpc %s failed: %v", vpcName, err)
klog.Errorf("failed to delete vpc %s: %v", vpc.Name, err)
continue
}
klog.Infof("deleted vpc %s", vpcName)
}
}
if len(logicalRouters) != 0 {
Expand All @@ -59,7 +57,7 @@ func (c *Controller) syncExternalVpc() {
vpc.Labels = map[string]string{util.VpcExternalLabel: "true"}
vpc, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Create(context.Background(), vpc, metav1.CreateOptions{})
if err != nil {
klog.Errorf("init vpc %s failed %v", routerName, err)
klog.Errorf("failed init external vpc %s, %v", routerName, err)
return
}

Expand All @@ -74,34 +72,34 @@ func (c *Controller) syncExternalVpc() {

_, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().UpdateStatus(context.Background(), vpc, metav1.UpdateOptions{})
if err != nil {
klog.Errorf("update vpc status failed %v", err)
klog.Errorf("failed to update vpc %s status, %v", routerName, err)
return
}
klog.V(4).Infof("add vpc %s", routerName)
klog.V(4).Infof("added external vpc %s", routerName)
}
}
}

func (c *Controller) getRouterStatus() (logicalRouters map[string]util.LogicalRouter, err error) {
func (c *Controller) getNonKubeovnRouterStatus() (logicalRouters map[string]util.LogicalRouter, err error) {
logicalRouters = make(map[string]util.LogicalRouter)
externalOvnRouters, err := c.OVNNbClient.ListLogicalRouter(false, func(lr *ovnnb.LogicalRouter) bool {
nonKubeovnRouters, err := c.OVNNbClient.ListLogicalRouter(false, func(lr *ovnnb.LogicalRouter) bool {
return len(lr.ExternalIDs) == 0 || lr.ExternalIDs["vendor"] != util.CniTypeName
})
if err != nil {
klog.Errorf("failed to list external logical router, %v", err)
klog.Errorf("failed to list non kubeovn logical router, %v", err)
return logicalRouters, err
}
if len(externalOvnRouters) == 0 {
klog.V(4).Info("sync over, no external vpc")
if len(nonKubeovnRouters) == 0 {
klog.V(4).Info("no non kubeovn logical router")
return logicalRouters, nil
}

for _, externalLR := range externalOvnRouters {
for _, router := range nonKubeovnRouters {
lr := util.LogicalRouter{
Name: externalLR.Name,
Ports: make([]util.Port, 0, len(externalLR.Ports)),
Name: router.Name,
Ports: make([]util.Port, 0, len(router.Ports)),
}
for _, uuid := range externalLR.Ports {
for _, uuid := range router.Ports {
lrp, err := c.OVNNbClient.GetLogicalRouterPortByUUID(uuid)
if err != nil {
klog.Warningf("failed to get LRP by UUID %s: %v", uuid, err)
Expand Down

0 comments on commit de1ead0

Please sign in to comment.