Skip to content

Commit

Permalink
fix(evpn): graceful shutdown and resources not found in db fix
Browse files Browse the repository at this point in the history
Signed-off-by: atulpatel261194 <[email protected]>
  • Loading branch information
atulpatel261194 committed Jun 9, 2024
1 parent 85aaecf commit 43fcf64
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 3 deletions.
35 changes: 32 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions pkg/LinuxCIModule/lci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions pkg/LinuxGeneralModule/lgm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions pkg/LinuxVendorModule/intele2000/intelE2000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions pkg/infradb/infradb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
48 changes: 48 additions & 0 deletions pkg/vendor_plugins/intel-e2000/p4runtime/p4translation/p4trans.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 43fcf64

Please sign in to comment.