From 43fcf64702e707a4b6c6b7ae10d472c624b5489e Mon Sep 17 00:00:00 2001 From: atulpatel261194 Date: Sun, 9 Jun 2024 12:09:22 -0700 Subject: [PATCH] fix(evpn): graceful shutdown and resources not found in db fix Signed-off-by: atulpatel261194 --- cmd/main.go | 35 ++++++++++++-- pkg/LinuxCIModule/lci.go | 13 +++++ pkg/LinuxGeneralModule/lgm.go | 39 +++++++++++++++ .../intele2000/intelE2000.go | 26 ++++++++++ pkg/infradb/infradb.go | 35 ++++++++++++++ .../p4runtime/p4translation/p4trans.go | 48 +++++++++++++++++++ 6 files changed, 193 insertions(+), 3 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index db6b9e34..058bf379 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -14,7 +14,9 @@ import ( "net" "net/http" "os" + "os/signal" "path/filepath" + "syscall" "time" pc "github.com/opiproject/opi-api/inventory/v1/gen/go" @@ -71,6 +73,7 @@ var rootCmd = &cobra.Command{ intel_e2000_linux.Initialize() frr.Initialize() ipu_vendor.Initialize() + netlink.DeInitialize() case "ci": gen_linux.Initialize() ci_linux.Initialize() @@ -135,15 +138,15 @@ func cleanUp() { if err := deleteGrdVrf(); err != nil { log.Println("Failed to delete GRD vrf") } - + if err := infradb.DeleteAllResources(); err != nil { + log.Println("Failed to delete all the resources: ", err) + } switch config.GlobalConfig.Buildenv { case intelStr: - gen_linux.DeInitialize() intel_e2000_linux.DeInitialize() frr.DeInitialize() ipu_vendor.DeInitialize() - netlink.DeInitialize() case "ci": gen_linux.DeInitialize() ci_linux.DeInitialize() @@ -167,6 +170,32 @@ func main() { // log.Println(err) log.Panicf("Error in initialize(): %v", err) } + + sigChan := make(chan os.Signal, 1) + // Notify sigChan on SIGINT or SIGTERM. + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) + + // This goroutine executes a blocking receive for signals. + // When it gets one it will then exit the program. + go func() { + sig := <-sigChan + switch sig { + case syscall.SIGINT: + cleanUp() + fmt.Println("Received SIGINT, shutting down.") + case syscall.SIGTERM: + cleanUp() + fmt.Println("Received SIGTERM, shutting down.") + default: + fmt.Println("Received unknown signal.") + } + // Perform any cleanup tasks here. + // ... + + // Exit the program. + os.Exit(0) + }() + // start the main cmd if err := rootCmd.Execute(); err != nil { log.Panicf("Error in Execute(): %v", err) diff --git a/pkg/LinuxCIModule/lci.go b/pkg/LinuxCIModule/lci.go index 07eac125..a72c2288 100644 --- a/pkg/LinuxCIModule/lci.go +++ b/pkg/LinuxCIModule/lci.go @@ -39,11 +39,24 @@ func (h *ModulelciHandler) HandleEvent(eventType string, objectData *eventbus.Ob } // handlebp handle the bridge port functionality +// +//gocognit:ignore func handlebp(objectData *eventbus.ObjectData) { var comp common.Component BP, err := infradb.GetBP(objectData.Name) if err != nil { log.Printf("LCI : GetBP error: %s\n", err) + comp.Name = lciComp + comp.CompStatus = common.ComponentStatusError + if comp.Timer == 0 { + comp.Timer = 2 * time.Second + } else { + comp.Timer *= 2 + } + err := infradb.UpdateBPStatus(objectData.Name, objectData.ResourceVersion, objectData.NotificationID, nil, comp) + if err != nil { + log.Printf("error in updating bp status: %s\n", err) + } return } if objectData.ResourceVersion != BP.ResourceVersion { diff --git a/pkg/LinuxGeneralModule/lgm.go b/pkg/LinuxGeneralModule/lgm.go index ed9f532f..28d51758 100644 --- a/pkg/LinuxGeneralModule/lgm.go +++ b/pkg/LinuxGeneralModule/lgm.go @@ -85,11 +85,24 @@ func (h *ModulelgmHandler) HandleEvent(eventType string, objectData *eventbus.Ob } // handleLB handles the logical Bridge +// +//gocognit:ignore func handleLB(objectData *eventbus.ObjectData) { var comp common.Component lb, err := infradb.GetLB(objectData.Name) if err != nil { log.Printf("LGM: GetLB error: %s %s\n", err, objectData.Name) + comp.Name = lgmComp + comp.CompStatus = common.ComponentStatusError + if comp.Timer == 0 { + comp.Timer = 2 * time.Second + } else { + comp.Timer *= 2 + } + err := infradb.UpdateLBStatus(objectData.Name, objectData.ResourceVersion, objectData.NotificationID, nil, comp) + if err != nil { + log.Printf("error in updating lb status: %s\n", err) + } return } if objectData.ResourceVersion != lb.ResourceVersion { @@ -157,11 +170,24 @@ func handleLB(objectData *eventbus.ObjectData) { } // handlesvi handles the svi functionality +// +//gocognit:ignore func handlesvi(objectData *eventbus.ObjectData) { var comp common.Component svi, err := infradb.GetSvi(objectData.Name) if err != nil { log.Printf("LGM: GetSvi error: %s %s\n", err, objectData.Name) + comp.Name = lgmComp + comp.CompStatus = common.ComponentStatusError + if comp.Timer == 0 { + comp.Timer = 2 * time.Second + } else { + comp.Timer *= 2 + } + err := infradb.UpdateSviStatus(objectData.Name, objectData.ResourceVersion, objectData.NotificationID, nil, comp) + if err != nil { + log.Printf("error in updating svi status: %s\n", err) + } return } if objectData.ResourceVersion != svi.ResourceVersion { @@ -228,11 +254,24 @@ func handlesvi(objectData *eventbus.ObjectData) { } // handlevrf handles the vrf functionality +// +//gocognit:ignore func handlevrf(objectData *eventbus.ObjectData) { var comp common.Component vrf, err := infradb.GetVrf(objectData.Name) if err != nil { log.Printf("LGM: GetVRF error: %s %s\n", err, objectData.Name) + comp.Name = lgmComp + comp.CompStatus = common.ComponentStatusError + if comp.Timer == 0 { // wait timer is 2 powerof natural numbers ex : 1,2,3... + comp.Timer = 2 * time.Second + } else { + comp.Timer *= 2 + } + err := infradb.UpdateVrfStatus(objectData.Name, objectData.ResourceVersion, objectData.NotificationID, nil, comp) + if err != nil { + log.Printf("error in updating vrf status: %s\n", err) + } return } if objectData.ResourceVersion != vrf.ResourceVersion { diff --git a/pkg/LinuxVendorModule/intele2000/intelE2000.go b/pkg/LinuxVendorModule/intele2000/intelE2000.go index 60e2f4b6..d9c26674 100644 --- a/pkg/LinuxVendorModule/intele2000/intelE2000.go +++ b/pkg/LinuxVendorModule/intele2000/intelE2000.go @@ -69,11 +69,24 @@ func (h *ModulelvmHandler) HandleEvent(eventType string, objectData *eventbus.Ob } // handlebp handles the bridge port functionality +// +//gocognit:ignore func handlebp(objectData *eventbus.ObjectData) { var comp common.Component bp, err := infradb.GetBP(objectData.Name) if err != nil { log.Printf("LVM : GetBP error: %s\n", err) + comp.Name = lvmComp + comp.CompStatus = common.ComponentStatusError + if comp.Timer == 0 { + comp.Timer = 2 * time.Second + } else { + comp.Timer *= 2 + } + err := infradb.UpdateBPStatus(objectData.Name, objectData.ResourceVersion, objectData.NotificationID, nil, comp) + if err != nil { + log.Printf("error in updating bp status: %s\n", err) + } return } if objectData.ResourceVersion != bp.ResourceVersion { @@ -227,11 +240,24 @@ func tearDownBp(bp *infradb.BridgePort) bool { } // handlevrf handles the vrf functionality +// +//gocognit:ignore func handlevrf(objectData *eventbus.ObjectData) { var comp common.Component vrf, err := infradb.GetVrf(objectData.Name) if err != nil { log.Printf("LVM : GetVrf error: %s\n", err) + comp.Name = lvmComp + comp.CompStatus = common.ComponentStatusError + if comp.Timer == 0 { // wait timer is 2 powerof natural numbers ex : 1,2,3... + comp.Timer = 2 * time.Second + } else { + comp.Timer *= 2 + } + err := infradb.UpdateVrfStatus(objectData.Name, objectData.ResourceVersion, objectData.NotificationID, nil, comp) + if err != nil { + log.Printf("error updaing vrf status %s\n", err) + } return } if objectData.ResourceVersion != vrf.ResourceVersion { diff --git a/pkg/infradb/infradb.go b/pkg/infradb/infradb.go index e16a5328..e113a79e 100644 --- a/pkg/infradb/infradb.go +++ b/pkg/infradb/infradb.go @@ -1215,6 +1215,41 @@ func GetAllSvis() ([]*Svi, error) { return svis, nil } +// DeleteAllResources deletes all components from infradb +func DeleteAllResources() error { + svis, _ := GetAllSvis() + for _, svi := range svis { + err := DeleteSvi(svi.Name) + if err != nil { + return err + } + } + + vrfs, _ := GetAllVrfs() + for _, vrf := range vrfs { + err := DeleteVrf(vrf.Name) + if err != nil { + return err + } + } + + bps, _ := GetAllBPs() + for _, bp := range bps { + err := DeleteBP(bp.Name) + if err != nil { + return err + } + } + lbs, _ := GetAllLBs() + for _, lb := range lbs { + err := DeleteLB(lb.Name) + if err != nil { + return err + } + } + return nil +} + // UpdateSvi updates a svi infradb object func UpdateSvi(svi *Svi) error { globalLock.Lock() diff --git a/pkg/vendor_plugins/intel-e2000/p4runtime/p4translation/p4trans.go b/pkg/vendor_plugins/intel-e2000/p4runtime/p4translation/p4trans.go index 054bcaa2..149a830f 100644 --- a/pkg/vendor_plugins/intel-e2000/p4runtime/p4translation/p4trans.go +++ b/pkg/vendor_plugins/intel-e2000/p4runtime/p4translation/p4trans.go @@ -553,11 +553,24 @@ func (h *ModuleipuHandler) HandleEvent(eventType string, objectData *eventbus.Ob } // handlevrf handles the vrf events +// +//gocognit:ignore func handlevrf(objectData *eventbus.ObjectData) { var comp common.Component vrf, err := infradb.GetVrf(objectData.Name) if err != nil { log.Printf("intel-e2000: GetVRF error: %s %s\n", err, objectData.Name) + comp.Name = intele2000Str + comp.CompStatus = common.ComponentStatusError + if comp.Timer == 0 { // wait timer is 2 powerof natural numbers ex : 1,2,3... + comp.Timer = 2 * time.Second + } else { + comp.Timer *= 2 + } + err = infradb.UpdateVrfStatus(objectData.Name, objectData.ResourceVersion, objectData.NotificationID, nil, comp) + if err != nil { + log.Printf("error in updating vrf status: %s\n", err) + } return } @@ -637,6 +650,17 @@ func handlelb(objectData *eventbus.ObjectData) { lb, err := infradb.GetLB(objectData.Name) if err != nil { log.Printf("intel-e2000: GetLB error: %s %s\n", err, objectData.Name) + comp.Name = intele2000Str + comp.CompStatus = common.ComponentStatusError + if comp.Timer == 0 { + comp.Timer = 2 * time.Second + } else { + comp.Timer *= 2 + } + err = infradb.UpdateLBStatus(objectData.Name, objectData.ResourceVersion, objectData.NotificationID, nil, comp) + if err != nil { + log.Printf("error in updating lb status: %s\n", err) + } return } @@ -697,6 +721,17 @@ func handlebp(objectData *eventbus.ObjectData) { bp, err := infradb.GetBP(objectData.Name) if err != nil { log.Printf("intel-e2000: GetBP error: %s\n", err) + comp.Name = intele2000Str + comp.CompStatus = common.ComponentStatusError + if comp.Timer == 0 { + comp.Timer = 2 * time.Second + } else { + comp.Timer *= 2 + } + err = infradb.UpdateBPStatus(objectData.Name, objectData.ResourceVersion, objectData.NotificationID, nil, comp) + if err != nil { + log.Printf("error in updating lb status: %s\n", err) + } return } @@ -752,11 +787,24 @@ func handlebp(objectData *eventbus.ObjectData) { } // handlesvi handles the svi events +// +//gocognit:ignore func handlesvi(objectData *eventbus.ObjectData) { var comp common.Component svi, err := infradb.GetSvi(objectData.Name) if err != nil { log.Printf("intel-e2000: GetSvi error: %s %s\n", err, objectData.Name) + comp.Name = intele2000Str + comp.CompStatus = common.ComponentStatusError + if comp.Timer == 0 { + comp.Timer = 2 * time.Second + } else { + comp.Timer *= 2 + } + err = infradb.UpdateSviStatus(objectData.Name, objectData.ResourceVersion, objectData.NotificationID, nil, comp) + if err != nil { + log.Printf("error in updating lb status: %s\n", err) + } return }