From f5460f0ac824ebbd29589ec661921269eae09966 Mon Sep 17 00:00:00 2001 From: Mengxin Liu Date: Mon, 29 Jul 2024 22:04:00 +0800 Subject: [PATCH] move initDefaultVpc into initOVN (#4344) Signed-off-by: oilbeater --- pkg/controller/controller.go | 4 ---- pkg/controller/init.go | 37 ++++++++++++++++++------------------ 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index b91cae974d3..2bd294d0ea5 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -770,10 +770,6 @@ func (c *Controller) Run(ctx context.Context) { util.LogFatalAndExit(err, "failed to initialize ovn resources") } - if err := c.InitDefaultVpc(); err != nil { - util.LogFatalAndExit(err, "failed to initialize default vpc") - } - // sync ip crd before initIPAM since ip crd will be used to restore vm and statefulset pod in initIPAM if err := c.syncIPCR(); err != nil { util.LogFatalAndExit(err, "failed to sync crd ips") diff --git a/pkg/controller/init.go b/pkg/controller/init.go index 1bfdb51e001..120a4d118cd 100644 --- a/pkg/controller/init.go +++ b/pkg/controller/init.go @@ -29,6 +29,11 @@ import ( func (c *Controller) InitOVN() error { var err error + if err := c.InitDefaultVpc(); err != nil { + klog.Errorf("init default vpc failed: %v", err) + return err + } + if err = c.initClusterRouter(); err != nil { klog.Errorf("init cluster router failed: %v", err) return err @@ -61,39 +66,33 @@ func (c *Controller) InitOVN() error { func (c *Controller) InitDefaultVpc() error { cachedVpc, err := c.vpcsLister.Get(c.config.ClusterRouter) - if err != nil { - cachedVpc = &kubeovnv1.Vpc{} - cachedVpc.Name = c.config.ClusterRouter - cachedVpc, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Create(context.Background(), cachedVpc, metav1.CreateOptions{}) + if !k8serrors.IsNotFound(err) { + klog.Errorf("get default vpc %s failed: %v", c.config.DefaultLogicalSwitch, err) + return err + } + + if k8serrors.IsNotFound(err) { + vpc := &kubeovnv1.Vpc{ + ObjectMeta: metav1.ObjectMeta{Name: c.config.ClusterRouter}, + } + cachedVpc, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Create(context.Background(), vpc, metav1.CreateOptions{}) if err != nil { klog.Errorf("init default vpc failed: %v", err) return err } } + vpc := cachedVpc.DeepCopy() vpc.Status.DefaultLogicalSwitch = c.config.DefaultLogicalSwitch vpc.Status.Router = c.config.ClusterRouter - if c.config.EnableLb { - vpc.Status.TCPLoadBalancer = c.config.ClusterTCPLoadBalancer - vpc.Status.TCPSessionLoadBalancer = c.config.ClusterTCPSessionLoadBalancer - vpc.Status.UDPLoadBalancer = c.config.ClusterUDPLoadBalancer - vpc.Status.UDPSessionLoadBalancer = c.config.ClusterUDPSessionLoadBalancer - vpc.Status.SctpLoadBalancer = c.config.ClusterSctpLoadBalancer - vpc.Status.SctpSessionLoadBalancer = c.config.ClusterSctpSessionLoadBalancer - } vpc.Status.Standby = true vpc.Status.Default = true - bytes, err := vpc.Status.Bytes() - if err != nil { - klog.Error(err) - return err - } - _, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Patch(context.Background(), vpc.Name, types.MergePatchType, bytes, metav1.PatchOptions{}, "status") - if err != nil { + if _, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().UpdateStatus(context.Background(), vpc, metav1.UpdateOptions{}); err != nil { klog.Errorf("init default vpc failed: %v", err) return err } + return nil }