mirror of
				https://github.com/kubenetworks/kubevpn.git
				synced 2025-11-01 03:04:02 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			86 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			86 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package handler
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"net"
 | |
| 	"os"
 | |
| 	"os/signal"
 | |
| 	"syscall"
 | |
| 	"time"
 | |
| 
 | |
| 	"k8s.io/apimachinery/pkg/apis/meta/v1"
 | |
| 	"k8s.io/utils/pointer"
 | |
| 
 | |
| 	"github.com/wencaiwulue/kubevpn/v2/pkg/config"
 | |
| 	plog "github.com/wencaiwulue/kubevpn/v2/pkg/log"
 | |
| )
 | |
| 
 | |
| func (c *ConnectOptions) setupSignalHandler() {
 | |
| 	var stopChan = make(chan os.Signal)
 | |
| 	signal.Notify(stopChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
 | |
| 	select {
 | |
| 	case <-stopChan:
 | |
| 		c.Cleanup(context.Background())
 | |
| 	case <-c.ctx.Done():
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (c *ConnectOptions) Cleanup(ctx context.Context) {
 | |
| 	if c == nil {
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	var inUserDaemon bool
 | |
| 	if c.ctx != nil {
 | |
| 		inUserDaemon = true
 | |
| 	}
 | |
| 
 | |
| 	c.once.Do(func() {
 | |
| 		if inUserDaemon {
 | |
| 			plog.G(ctx).Info("Performing cleanup operations")
 | |
| 		}
 | |
| 		ctx2, cancel := context.WithTimeout(context.Background(), time.Second*10)
 | |
| 		defer cancel()
 | |
| 		var ips []net.IP
 | |
| 		if c.localTunIPv4 != nil && c.localTunIPv4.IP != nil {
 | |
| 			ips = append(ips, c.localTunIPv4.IP)
 | |
| 		}
 | |
| 		if c.localTunIPv6 != nil && c.localTunIPv6.IP != nil {
 | |
| 			ips = append(ips, c.localTunIPv6.IP)
 | |
| 		}
 | |
| 		if c.dhcp != nil {
 | |
| 			err := c.dhcp.ReleaseIP(ctx2, ips...)
 | |
| 			if err != nil {
 | |
| 				plog.G(ctx).Errorf("Failed to release IP to DHCP server: %v", err)
 | |
| 			} else {
 | |
| 				plog.G(ctx).Infof("Releaseed IP %v to DHCP server", ips)
 | |
| 			}
 | |
| 		}
 | |
| 		if c.clientset != nil {
 | |
| 			_ = c.clientset.CoreV1().Pods(c.Namespace).Delete(ctx2, config.CniNetName, v1.DeleteOptions{GracePeriodSeconds: pointer.Int64(0)})
 | |
| 		}
 | |
| 		// leave proxy resources
 | |
| 		err := c.LeaveAllProxyResources(ctx2)
 | |
| 		if err != nil {
 | |
| 			plog.G(ctx).Errorf("Leave proxy resources error: %v", err)
 | |
| 		}
 | |
| 		if c.cancel != nil {
 | |
| 			c.cancel()
 | |
| 		}
 | |
| 
 | |
| 		for _, function := range c.getRolloutFunc() {
 | |
| 			if function != nil {
 | |
| 				if err = function(); err != nil {
 | |
| 					plog.G(ctx).Warnf("Rollout function error: %v", err)
 | |
| 				}
 | |
| 			}
 | |
| 		}
 | |
| 		if c.dnsConfig != nil {
 | |
| 			if inUserDaemon {
 | |
| 				plog.G(ctx2).Infof("Clearing DNS settings")
 | |
| 			}
 | |
| 			c.dnsConfig.CancelDNS()
 | |
| 		}
 | |
| 	})
 | |
| }
 | 
