diff --git a/pkg/controller/client_go_adapter.go b/pkg/controller/client_go_adapter.go index 74379a923696..7211db793a87 100644 --- a/pkg/controller/client_go_adapter.go +++ b/pkg/controller/client_go_adapter.go @@ -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 ( diff --git a/pkg/controller/external_vpc.go b/pkg/controller/external_vpc.go index ed2b88be7cfe..877d3cf08bb5 100644 --- a/pkg/controller/external_vpc.go +++ b/pkg/controller/external_vpc.go @@ -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 { @@ -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 } @@ -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)