mirror of
				https://github.com/kubenetworks/kubevpn.git
				synced 2025-10-31 18:52:50 +08:00 
			
		
		
		
	fix: remove svc from hosts if svc deleted (#107)
This commit is contained in:
		| @@ -50,6 +50,8 @@ func CmdReset(factory cmdutil.Factory) *cobra.Command { | ||||
| 			if err := connect.InitClient(factory); err != nil { | ||||
| 				log.Fatal(err) | ||||
| 			} | ||||
| 			_ = quit(cmd.Context(), true) | ||||
| 			_ = quit(cmd.Context(), false) | ||||
| 			err := connect.Reset(cmd.Context()) | ||||
| 			if err != nil { | ||||
| 				log.Fatal(err) | ||||
|   | ||||
| @@ -83,6 +83,27 @@ func (c *Config) AddServiceNameToHosts(ctx context.Context, serviceInterface v13 | ||||
| 						if !rateLimiter.TryAccept() { | ||||
| 							return | ||||
| 						} | ||||
| 						if event.Type == watch.Deleted { | ||||
| 							svc, ok := event.Object.(*v12.Service) | ||||
| 							if !ok { | ||||
| 								continue | ||||
| 							} | ||||
| 							var list []Entry | ||||
| 							for _, p := range sets.New[string](svc.Spec.ClusterIPs...).Insert(svc.Spec.ClusterIP).UnsortedList() { | ||||
| 								if net.ParseIP(p) == nil { | ||||
| 									continue | ||||
| 								} | ||||
| 								list = append(list, Entry{ | ||||
| 									IP:     p, | ||||
| 									Domain: svc.Name, | ||||
| 								}) | ||||
| 							} | ||||
| 							err = c.removeHosts(list) | ||||
| 							if err != nil { | ||||
| 								log.Errorf("failed to remove hosts(%s) to hosts: %v", entryList2String(list), err) | ||||
| 							} | ||||
| 							continue | ||||
| 						} | ||||
| 						list, err := serviceInterface.List(ctx, v1.ListOptions{}) | ||||
| 						if err != nil { | ||||
| 							return | ||||
| @@ -129,14 +150,23 @@ func (c *Config) addHosts(entryList []Entry) error { | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (c *Config) removeHosts() error { | ||||
| func (c *Config) removeHosts(entryList []Entry) error { | ||||
| 	c.Lock.Lock() | ||||
| 	defer c.Lock.Unlock() | ||||
|  | ||||
| 	if len(c.Hosts) == 0 { | ||||
| 	if len(entryList) == 0 { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	for _, entry := range entryList { | ||||
| 		for i := 0; i < len(c.Hosts); i++ { | ||||
| 			if entry == c.Hosts[i] { | ||||
| 				c.Hosts = append(c.Hosts[:i], c.Hosts[i+1:]...) | ||||
| 				i-- | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	hostFile := GetHostFile() | ||||
| 	f, err := os.OpenFile(hostFile, os.O_RDWR, 0644) | ||||
| 	if err != nil { | ||||
| @@ -153,7 +183,7 @@ func (c *Config) removeHosts() error { | ||||
| 		} | ||||
| 		var needsRemove bool | ||||
| 		if strings.Contains(line, config.HostsKeyWord) { | ||||
| 			for _, host := range c.Hosts { | ||||
| 			for _, host := range entryList { | ||||
| 				if strings.Contains(line, host.IP) && strings.Contains(line, host.Domain) { | ||||
| 					needsRemove = true | ||||
| 				} | ||||
|   | ||||
| @@ -116,7 +116,7 @@ func SetupLocalDNS(clientConfig *miekgdns.ClientConfig, existNameservers []strin | ||||
| } | ||||
|  | ||||
| func (c *Config) CancelDNS() { | ||||
| 	c.removeHosts() | ||||
| 	c.removeHosts(c.Hosts) | ||||
|  | ||||
| 	if !c.Lite { | ||||
| 		filename := filepath.Join("/", "etc", "resolv.conf") | ||||
|   | ||||
| @@ -194,7 +194,7 @@ func (c *Config) CancelDNS() { | ||||
| 		_ = os.RemoveAll(filepath.Join("/", "etc", "resolver")) | ||||
| 	} | ||||
| 	//networkCancel() | ||||
| 	c.removeHosts() | ||||
| 	c.removeHosts(c.Hosts) | ||||
| } | ||||
|  | ||||
| /* | ||||
|   | ||||
| @@ -46,7 +46,7 @@ func (c *Config) SetupDNS() error { | ||||
| } | ||||
|  | ||||
| func (c *Config) CancelDNS() { | ||||
| 	c.removeHosts() | ||||
| 	c.removeHosts(c.Hosts) | ||||
| 	tun, err := net.InterfaceByName(c.TunName) | ||||
| 	if err != nil { | ||||
| 		return | ||||
|   | ||||
| @@ -60,7 +60,7 @@ func (c *ConnectOptions) Cleanup() { | ||||
| 		if err == nil && count <= 0 { | ||||
| 			deployment, errs := c.clientset.AppsV1().Deployments(c.Namespace).Get(ctx, config.ConfigMapPodTrafficManager, v1.GetOptions{}) | ||||
| 			if errs == nil && deployment.Status.UnavailableReplicas != 0 { | ||||
| 				cleanup(ctx, c.clientset, c.Namespace, config.ConfigMapPodTrafficManager, true) | ||||
| 				cleanupK8sResource(ctx, c.clientset, c.Namespace, config.ConfigMapPodTrafficManager, true) | ||||
| 			} | ||||
| 		} | ||||
| 		if err != nil { | ||||
| @@ -149,7 +149,7 @@ func updateRefCount(ctx context.Context, configMapInterface v12.ConfigMapInterfa | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func cleanup(ctx context.Context, clientset *kubernetes.Clientset, namespace, name string, keepCIDR bool) { | ||||
| func cleanupK8sResource(ctx context.Context, clientset *kubernetes.Clientset, namespace, name string, keepCIDR bool) { | ||||
| 	options := v1.DeleteOptions{GracePeriodSeconds: pointer.Int64(0)} | ||||
|  | ||||
| 	if keepCIDR { | ||||
|   | ||||
| @@ -14,32 +14,45 @@ import ( | ||||
|  | ||||
| 	"github.com/wencaiwulue/kubevpn/pkg/config" | ||||
| 	"github.com/wencaiwulue/kubevpn/pkg/controlplane" | ||||
| 	"github.com/wencaiwulue/kubevpn/pkg/dns" | ||||
| ) | ||||
|  | ||||
| // Reset | ||||
| // 1, get all proxy-resources from configmap | ||||
| // 2, cleanup all containers | ||||
| // 1) quit daemon | ||||
| // 2) get all proxy-resources from configmap | ||||
| // 3) cleanup all containers | ||||
| // 4) cleanup hosts | ||||
| func (c *ConnectOptions) Reset(ctx context.Context) error { | ||||
| 	err := c.LeaveProxyResources(ctx) | ||||
| 	if err != nil { | ||||
| 		log.Errorf("leave proxy resources error: %v", err) | ||||
| 	} else { | ||||
| 		log.Infof("leave proxy resources success") | ||||
| 	} | ||||
|  | ||||
| 	cleanup(ctx, c.clientset, c.Namespace, config.ConfigMapPodTrafficManager, false) | ||||
| 	var cli *client.Client | ||||
| 	cli, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) | ||||
| 	log.Infof("cleanup k8s resource") | ||||
| 	cleanupK8sResource(ctx, c.clientset, c.Namespace, config.ConfigMapPodTrafficManager, false) | ||||
|  | ||||
| 	_ = c.CleanupLocalContainer(ctx) | ||||
|  | ||||
| 	_ = dns.CleanupHosts() | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (c *ConnectOptions) CleanupLocalContainer(ctx context.Context) error { | ||||
| 	cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) | ||||
| 	if err != nil { | ||||
| 		return nil | ||||
| 		return err | ||||
| 	} | ||||
| 	var networkResource types.NetworkResource | ||||
| 	networkResource, err = cli.NetworkInspect(ctx, config.ConfigMapPodTrafficManager, types.NetworkInspectOptions{}) | ||||
| 	if err != nil { | ||||
| 		return nil | ||||
| 		return err | ||||
| 	} | ||||
| 	if len(networkResource.Containers) == 0 { | ||||
| 		return cli.NetworkRemove(ctx, config.ConfigMapPodTrafficManager) | ||||
| 		err = cli.NetworkRemove(ctx, config.ConfigMapPodTrafficManager) | ||||
| 	} | ||||
| 	return nil | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (c *ConnectOptions) LeaveProxyResources(ctx context.Context) (err error) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 naison
					naison