Skip to content

Commit

Permalink
cleanup: allow ability to add cleanup functions from daemon
Browse files Browse the repository at this point in the history
Only add the cleanup function to cleanup endpoint-related information when
running in tandem with Flannel once the daemon's endpointManager field is
initialized.

Signed-off by: Ian Vernon <ian@cilium.io>
  • Loading branch information
Ian Vernon authored and ianvernon committed Aug 23, 2019
1 parent 353335c commit 34ef1a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
32 changes: 25 additions & 7 deletions daemon/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (
"sync"
"syscall"

"github.com/cilium/cilium/pkg/endpointmanager"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/pidfile"
)

Expand All @@ -31,8 +30,31 @@ var (
cleanUPSig = make(chan struct{})
// cleanUPWg all cleanup operations will be marked as Done() when completed.
cleanUPWg = &sync.WaitGroup{}

cleanupFuncs = &cleanupFuncList{
funcs: make([]func(), 0),
}
)

type cleanupFuncList struct {
funcs []func()
lock lock.Mutex
}

func (c *cleanupFuncList) Add(newFunc func()) {
c.lock.Lock()
c.funcs = append(c.funcs, newFunc)
c.lock.Unlock()
}

func (c *cleanupFuncList) Run() {
c.lock.Lock()
defer c.lock.Unlock()
for k := range c.funcs {
c.funcs[k]()
}
}

func registerSigHandler() <-chan struct{} {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGQUIT, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM)
Expand All @@ -42,11 +64,7 @@ func registerSigHandler() <-chan struct{} {
log.WithField("signal", s).Info("Exiting due to signal")
pidfile.Clean()
Clean()
if option.Config.FlannelUninstallOnExit {
for _, ep := range endpointmanager.GetEndpoints() {
ep.DeleteBPFProgramLocked()
}
}
cleanupFuncs.Run()
break
}
close(interrupt)
Expand Down
13 changes: 12 additions & 1 deletion daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,17 @@ func NewDaemon(dp datapath.Datapath, iptablesManager rulesManager) (*Daemon, *en
// Must be done before calling policy.NewPolicyRepository() below.
identity.InitWellKnownIdentities()

epMgr := endpointmanager.NewEndpointManager(&endpointsynchronizer.EndpointSynchronizer{})

// Cleanup flannel on exit
cleanupFuncs.Add(func() {
if option.Config.FlannelUninstallOnExit {
for _, ep := range epMgr.GetEndpoints() {
ep.DeleteBPFProgramLocked()
}
}
})

d := Daemon{
loadBalancer: loadbalancer.NewLoadBalancer(),
k8sSvcCache: k8s.NewServiceCache(),
Expand All @@ -758,7 +769,7 @@ func NewDaemon(dp datapath.Datapath, iptablesManager rulesManager) (*Daemon, *en
datapath: dp,
nodeDiscovery: nodediscovery.NewNodeDiscovery(nodeMngr, mtuConfig),
iptablesManager: iptablesManager,
endpointManager: endpointmanager.NewEndpointManager(&endpointsynchronizer.EndpointSynchronizer{}),
endpointManager: epMgr,
}

endpointmanager.GlobalEndpointManager = endpointmanager.NewEndpointManager(&endpointsynchronizer.EndpointSynchronizer{})
Expand Down

0 comments on commit 34ef1a4

Please sign in to comment.