github.phpd.cn/cilium/cilium@v1.6.12/daemon/cleanup.go (about)

     1  // Copyright 2018-2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"os"
    19  	"os/signal"
    20  	"sync"
    21  	"syscall"
    22  
    23  	"github.com/cilium/cilium/pkg/endpointmanager"
    24  	"github.com/cilium/cilium/pkg/option"
    25  	"github.com/cilium/cilium/pkg/pidfile"
    26  
    27  	gops "github.com/google/gops/agent"
    28  )
    29  
    30  var (
    31  	// cleanUPSig channel that is closed when the daemon agent should be
    32  	// terminated.
    33  	cleanUPSig = make(chan struct{})
    34  	// cleanUPWg all cleanup operations will be marked as Done() when completed.
    35  	cleanUPWg = &sync.WaitGroup{}
    36  )
    37  
    38  func registerSigHandler() <-chan struct{} {
    39  	sig := make(chan os.Signal, 1)
    40  	signal.Notify(sig, syscall.SIGQUIT, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM)
    41  	interrupt := make(chan struct{})
    42  	go func() {
    43  		for s := range sig {
    44  			log.WithField("signal", s).Info("Exiting due to signal")
    45  			pidfile.Clean()
    46  			Clean()
    47  			if option.Config.FlannelUninstallOnExit {
    48  				for _, ep := range endpointmanager.GetEndpoints() {
    49  					ep.DeleteBPFProgramLocked()
    50  				}
    51  			}
    52  			break
    53  		}
    54  		close(interrupt)
    55  	}()
    56  	return interrupt
    57  }
    58  
    59  // Clean cleans up everything created by this package.
    60  func Clean() {
    61  	gops.Close()
    62  	close(cleanUPSig)
    63  	cleanUPWg.Wait()
    64  }