mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-10-05 23:36:59 +08:00
feat: optimize code
This commit is contained in:
29
.github/workflows/release.yml
vendored
29
.github/workflows/release.yml
vendored
@@ -70,6 +70,35 @@ jobs:
|
||||
token: ${{ secrets.REPOSITORYDISPATCH }}
|
||||
inputs: '{"url": "${{ steps.create_release.outputs.upload_url }}", "tag": "${{ github.ref_name }}"}'
|
||||
|
||||
- name: Make changes to pull request
|
||||
run: make version
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
uses: peter-evans/create-pull-request@v4
|
||||
with:
|
||||
add-paths: |
|
||||
*.yaml
|
||||
token: ${{ secrets.REPOSITORYDISPATCH }}
|
||||
commit-message: "feat: update krew index version to ${{ github.ref }}"
|
||||
committer: GitHub <noreply@github.com>
|
||||
author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
|
||||
signoff: false
|
||||
branch: feat/update-krew-index-version
|
||||
base: master
|
||||
delete-branch: true
|
||||
title: 'feat: update krew index version to ${{ github.ref }}'
|
||||
body: |
|
||||
update report
|
||||
- update with *today's* date
|
||||
- update krew index version to ${{ github.ref }}
|
||||
labels: |
|
||||
report
|
||||
automated pr
|
||||
# team-reviewers: |
|
||||
# owners
|
||||
# maintainers
|
||||
draft: false
|
||||
|
||||
# - name: Update new version in krew-index
|
||||
# uses: rajatjindal/krew-release-bot@v0.0.43
|
||||
# with:
|
||||
|
@@ -16,8 +16,8 @@ func CmdReset(factory cmdutil.Factory) *cobra.Command {
|
||||
var sshConf = &util.SshConfig{}
|
||||
cmd := &cobra.Command{
|
||||
Use: "reset",
|
||||
Short: "Reset KubeVPN",
|
||||
Long: `Reset KubeVPN if any error occurs`,
|
||||
Short: "Reset all changes made by KubeVPN",
|
||||
Long: `Reset all changes made by KubeVPN`,
|
||||
Example: templates.Examples(i18n.T(`
|
||||
# Reset default namespace
|
||||
kubevpn reset
|
||||
|
@@ -35,10 +35,10 @@ func NewKubeVPNCommand() *cobra.Command {
|
||||
CmdProxy(factory),
|
||||
CmdDev(factory),
|
||||
CmdDuplicate(factory),
|
||||
CmdReset(factory),
|
||||
CmdUpgrade(factory),
|
||||
CmdVersion(factory),
|
||||
CmdCp(factory),
|
||||
CmdUpgrade(factory),
|
||||
CmdReset(factory),
|
||||
CmdVersion(factory),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@@ -85,8 +85,15 @@ func GetDNSIPFromDnsPod(clientset *kubernetes.Clientset) (ips []string, err erro
|
||||
func AddServiceNameToHosts(ctx context.Context, serviceInterface v13.ServiceInterface) {
|
||||
rateLimiter := flowcontrol.NewTokenBucketRateLimiter(0.2, 1)
|
||||
defer rateLimiter.Stop()
|
||||
|
||||
var last string
|
||||
|
||||
serviceList, err := serviceInterface.List(ctx, v1.ListOptions{})
|
||||
if err == nil && len(serviceList.Items) != 0 {
|
||||
entry := generateHostsEntry(serviceList.Items)
|
||||
if err = updateHosts(entry); err == nil {
|
||||
last = entry
|
||||
}
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -94,7 +101,7 @@ func AddServiceNameToHosts(ctx context.Context, serviceInterface v13.ServiceInte
|
||||
default:
|
||||
func() {
|
||||
w, err := serviceInterface.Watch(ctx, v1.ListOptions{
|
||||
Watch: true, TimeoutSeconds: pointer.Int64(30),
|
||||
Watch: true, TimeoutSeconds: pointer.Int64(30), ResourceVersion: serviceList.ResourceVersion,
|
||||
})
|
||||
if err != nil {
|
||||
if utilnet.IsConnectionRefused(err) || apierrors.IsTooManyRequests(err) {
|
||||
@@ -109,7 +116,7 @@ func AddServiceNameToHosts(ctx context.Context, serviceInterface v13.ServiceInte
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if watch.Deleted == c.Type || watch.Error == c.Type {
|
||||
if watch.Error == c.Type || watch.Bookmark == c.Type {
|
||||
continue
|
||||
}
|
||||
if !rateLimiter.TryAccept() {
|
||||
@@ -123,8 +130,7 @@ func AddServiceNameToHosts(ctx context.Context, serviceInterface v13.ServiceInte
|
||||
if entry == last {
|
||||
continue
|
||||
}
|
||||
err = updateHosts(entry)
|
||||
if err != nil {
|
||||
if err = updateHosts(entry); err != nil {
|
||||
return
|
||||
}
|
||||
last = entry
|
||||
|
@@ -3,7 +3,6 @@ package handler
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
@@ -28,26 +27,33 @@ var stopChan = make(chan os.Signal)
|
||||
var RollbackFuncList = make([]func(), 2)
|
||||
var ctx, cancel = context.WithCancel(context.Background())
|
||||
|
||||
func AddCleanUpResourceHandler(clientset *kubernetes.Clientset, ns string, dhcp *DHCPManager, usedIPs ...*net.IPNet) {
|
||||
func (c *ConnectOptions) addCleanUpResourceHandler() {
|
||||
signal.Notify(stopChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
|
||||
go func() {
|
||||
<-stopChan
|
||||
log.Info("prepare to exit, cleaning up")
|
||||
if dhcp != nil {
|
||||
err := dhcp.ReleaseIpToDHCP(usedIPs...)
|
||||
err := c.dhcp.ReleaseIpToDHCP(append(c.usedIPs, c.localTunIP)...)
|
||||
if err != nil {
|
||||
log.Errorf("failed to release ip to dhcp, err: %v", err)
|
||||
}
|
||||
}
|
||||
for _, function := range RollbackFuncList {
|
||||
if function != nil {
|
||||
function()
|
||||
}
|
||||
}
|
||||
_ = clientset.CoreV1().Pods(ns).Delete(context.Background(), config.CniNetName, v1.DeleteOptions{GracePeriodSeconds: pointer.Int64(0)})
|
||||
_, err := updateRefCount(clientset.CoreV1().ConfigMaps(ns), config.ConfigMapPodTrafficManager, -1)
|
||||
_ = c.clientset.CoreV1().Pods(c.Namespace).Delete(context.Background(), config.CniNetName, v1.DeleteOptions{GracePeriodSeconds: pointer.Int64(0)})
|
||||
count, err := updateRefCount(c.clientset.CoreV1().ConfigMaps(c.Namespace), config.ConfigMapPodTrafficManager, -1)
|
||||
if err == nil {
|
||||
// only if ref is zero and deployment is not ready, needs to clean up
|
||||
if count <= 0 {
|
||||
deployment, errs := c.clientset.AppsV1().Deployments(c.Namespace).Get(context.Background(), config.ConfigMapPodTrafficManager, v1.GetOptions{})
|
||||
if errs == nil && deployment.Status.UnavailableReplicas != 0 {
|
||||
cleanup(c.clientset, c.Namespace, config.ConfigMapPodTrafficManager, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
log.Errorf("can not update ref-count: %v", err)
|
||||
}
|
||||
dns.CancelDNS()
|
||||
cancel()
|
||||
|
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
@@ -15,6 +16,7 @@ import (
|
||||
"github.com/containernetworking/cni/pkg/types"
|
||||
"github.com/docker/distribution/reference"
|
||||
"github.com/google/gopacket/routing"
|
||||
goversion "github.com/hashicorp/go-version"
|
||||
netroute "github.com/libp2p/go-netroute"
|
||||
miekgdns "github.com/miekg/dns"
|
||||
"github.com/pkg/errors"
|
||||
@@ -131,7 +133,7 @@ func (c *ConnectOptions) DoConnect() (err error) {
|
||||
if err = c.dhcp.InitDHCP(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
AddCleanUpResourceHandler(c.clientset, c.Namespace, c.dhcp, c.usedIPs...)
|
||||
c.addCleanUpResourceHandler()
|
||||
err = c.GetCIDR(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -786,6 +788,7 @@ func (c *ConnectOptions) heartbeats() {
|
||||
|
||||
err := c.dhcp.ForEach(func(ip net.IP) {
|
||||
go func() {
|
||||
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000)))
|
||||
_, _ = util.Ping(ip.String())
|
||||
}()
|
||||
})
|
||||
@@ -938,7 +941,16 @@ func (c *ConnectOptions) UpdateImage(ctx context.Context) error {
|
||||
if reference.Domain(newImg) != reference.Domain(oldImg) {
|
||||
return nil
|
||||
}
|
||||
if oldTag.Tag() >= newTag.Tag() {
|
||||
var oldVersion, newVersion *goversion.Version
|
||||
oldVersion, err = goversion.NewVersion(oldTag.Tag())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
newVersion, err = goversion.NewVersion(newTag.Tag())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if oldVersion.GreaterThanOrEqual(newVersion) {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -985,10 +997,18 @@ func (c *ConnectOptions) SetImage(ctx context.Context) error {
|
||||
if reference.Domain(newImg) != reference.Domain(oldImg) {
|
||||
return nil
|
||||
}
|
||||
if oldTag.Tag() >= newTag.Tag() {
|
||||
var oldVersion, newVersion *goversion.Version
|
||||
oldVersion, err = goversion.NewVersion(oldTag.Tag())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
newVersion, err = goversion.NewVersion(newTag.Tag())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if oldVersion.GreaterThanOrEqual(newVersion) {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Infof("found newer image %s, set image from %s to it...", config.Image, deployment.Spec.Template.Spec.Containers[0].Image)
|
||||
|
||||
r := c.factory.NewBuilder().
|
||||
|
@@ -106,8 +106,7 @@ func (d *DHCPManager) ReleaseIpToDHCP(ips ...*net.IPNet) error {
|
||||
func (d *DHCPManager) updateDHCPConfigMap(f func(*ipallocator.Range) error) error {
|
||||
cm, err := d.client.Get(context.Background(), config.ConfigMapPodTrafficManager, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.Errorf("failed to get cm DHCP server, err: %v", err)
|
||||
return err
|
||||
return fmt.Errorf("failed to get cm DHCP server, err: %v", err)
|
||||
}
|
||||
if cm.Data == nil {
|
||||
cm.Data = make(map[string]string)
|
||||
@@ -175,8 +174,7 @@ func (d *DHCPManager) Get(ctx2 context.Context, key string) (string, error) {
|
||||
func (d *DHCPManager) ForEach(fn func(net.IP)) error {
|
||||
cm, err := d.client.Get(context.Background(), config.ConfigMapPodTrafficManager, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.Errorf("failed to get cm DHCP server, err: %v", err)
|
||||
return err
|
||||
return fmt.Errorf("failed to get cm DHCP server, err: %v", err)
|
||||
}
|
||||
if cm.Data == nil {
|
||||
cm.Data = make(map[string]string)
|
||||
|
@@ -167,7 +167,7 @@ func fullDomain(t *testing.T) {
|
||||
|
||||
func dialUDP(t *testing.T) {
|
||||
port := util.GetAvailableUDPPortOrDie()
|
||||
go server(port)
|
||||
go UDPServer(port)
|
||||
|
||||
list, err := clientset.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{
|
||||
LabelSelector: fields.OneTermEqualSelector("app", "reviews").String(),
|
||||
@@ -191,13 +191,13 @@ func dialUDP(t *testing.T) {
|
||||
func(err error) bool {
|
||||
return err != nil
|
||||
}, func() error {
|
||||
return client(ip, port)
|
||||
return UDPClient(ip, port)
|
||||
}); err != nil {
|
||||
t.Errorf("can not access pod ip: %s, port: %v", ip, port)
|
||||
}
|
||||
}
|
||||
|
||||
func client(ip string, port int) error {
|
||||
func UDPClient(ip string, port int) error {
|
||||
udpConn, err := net.DialUDP("udp4", nil, &net.UDPAddr{
|
||||
IP: net.ParseIP(ip),
|
||||
Port: port,
|
||||
@@ -233,7 +233,7 @@ func client(ip string, port int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func server(port int) {
|
||||
func UDPServer(port int) {
|
||||
// 创建监听
|
||||
udpConn, err := net.ListenUDP("udp4", &net.UDPAddr{
|
||||
IP: net.IPv4(0, 0, 0, 0),
|
||||
|
@@ -3,7 +3,7 @@ kind: Plugin
|
||||
metadata:
|
||||
name: kubevpn
|
||||
spec:
|
||||
version: v1.1.28
|
||||
version: v1.1.29
|
||||
homepage: https://github.com/wencaiwulue/kubevpn
|
||||
shortDescription: "A vpn tunnel tools which can connect to kubernetes cluster network"
|
||||
description: |
|
||||
@@ -17,8 +17,8 @@ spec:
|
||||
matchLabels:
|
||||
os: windows
|
||||
arch: amd64
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.28/kubevpn_v1.1.28_windows_amd64.zip
|
||||
sha256: c1292f94523bc51ef3c68b7c01c1c21beb8d3ca6be48a4b05938c139092cd0ed
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.29/kubevpn_v1.1.29_windows_amd64.zip
|
||||
sha256: 2ddc6e8bb3affa4e4b1d359c1953d680cc92172c9396851bc271254db2f36b45
|
||||
files:
|
||||
- from: ./bin/kubevpn.exe
|
||||
to: .
|
||||
@@ -29,8 +29,8 @@ spec:
|
||||
matchLabels:
|
||||
os: windows
|
||||
arch: arm64
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.28/kubevpn_v1.1.28_windows_arm64.zip
|
||||
sha256: b10b16769a791f229ac24a0aa642f59150fcb73d449e2bb83921239f7e77fb48
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.29/kubevpn_v1.1.29_windows_arm64.zip
|
||||
sha256: 7452da85e3b8660cdb0496fa3627f8b5e47839cc8c09c1744a8b5b28ce824ebc
|
||||
files:
|
||||
- from: ./bin/kubevpn.exe
|
||||
to: .
|
||||
@@ -41,8 +41,8 @@ spec:
|
||||
matchLabels:
|
||||
os: windows
|
||||
arch: 386
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.28/kubevpn_v1.1.28_windows_386.zip
|
||||
sha256: 03819683d26210669a9240e6943fc20af6a8a62b91df7aa92b9ee38be01ffd07
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.29/kubevpn_v1.1.29_windows_386.zip
|
||||
sha256: 56ce06200b89e11929cba5ae45e27eb7d136601267caa484e32855d1a9d6eff0
|
||||
files:
|
||||
- from: ./bin/kubevpn.exe
|
||||
to: .
|
||||
@@ -53,8 +53,8 @@ spec:
|
||||
matchLabels:
|
||||
os: linux
|
||||
arch: amd64
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.28/kubevpn_v1.1.28_linux_amd64.zip
|
||||
sha256: 544b2c53de9fdb1ade5a7b4487d08526596f0fc8e540e76510409b62aedeeeb4
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.29/kubevpn_v1.1.29_linux_amd64.zip
|
||||
sha256: b375d503e4db93513df559677418073a56af13195b0bcd259f959df5e8a29528
|
||||
files:
|
||||
- from: ./bin/kubevpn
|
||||
to: .
|
||||
@@ -65,8 +65,8 @@ spec:
|
||||
matchLabels:
|
||||
os: linux
|
||||
arch: arm64
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.28/kubevpn_v1.1.28_linux_arm64.zip
|
||||
sha256: c99df8f999e151b448b2f48f3d07eaf4f9bc722542ba7f4a20904412a25bcbfd
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.29/kubevpn_v1.1.29_linux_arm64.zip
|
||||
sha256: 4dafdfa181b1ae2809ddbc79afbf411efef62ceaed12457b89df5428922183a7
|
||||
files:
|
||||
- from: ./bin/kubevpn
|
||||
to: .
|
||||
@@ -77,8 +77,8 @@ spec:
|
||||
matchLabels:
|
||||
os: linux
|
||||
arch: 386
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.28/kubevpn_v1.1.28_linux_386.zip
|
||||
sha256: f623dcf8c84ce9d662f5516dbe2272d288d550277139adc105dcea23da643778
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.29/kubevpn_v1.1.29_linux_386.zip
|
||||
sha256: da57db38558c0787edd435ebc8323a0441d91071484c01c64906a6802a7a4803
|
||||
files:
|
||||
- from: ./bin/kubevpn
|
||||
to: .
|
||||
@@ -89,8 +89,8 @@ spec:
|
||||
matchLabels:
|
||||
os: darwin
|
||||
arch: amd64
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.28/kubevpn_v1.1.28_darwin_amd64.zip
|
||||
sha256: 917fe6b1bc757ceca3ecb1bebbe5b08e7b1a96aebcff5da54fe29f270b89805c
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.29/kubevpn_v1.1.29_darwin_amd64.zip
|
||||
sha256: 10bd12df6fab7fb6c426a069515ce57b64d9e8b8aec31f106a2cf78a21faafb4
|
||||
files:
|
||||
- from: ./bin/kubevpn
|
||||
to: .
|
||||
@@ -101,8 +101,8 @@ spec:
|
||||
matchLabels:
|
||||
os: darwin
|
||||
arch: arm64
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.28/kubevpn_v1.1.28_darwin_arm64.zip
|
||||
sha256: c86532abfdd0b582c001a994921157abbbb3fcc5f12f28ec76a8e6afbc357916
|
||||
uri: https://github.com/wencaiwulue/kubevpn/releases/download/v1.1.29/kubevpn_v1.1.29_darwin_arm64.zip
|
||||
sha256: 15a1f9abecd08583aedca2f18a40a89e8703a68389acb2cffae4637013ba381d
|
||||
files:
|
||||
- from: ./bin/kubevpn
|
||||
to: .
|
||||
|
Reference in New Issue
Block a user