From 817b6ce22481c33dff07dc13bd48ce753d5f1168 Mon Sep 17 00:00:00 2001 From: wencaiwulue <895703375@qq.com> Date: Tue, 21 Nov 2023 23:52:01 +0800 Subject: [PATCH] feat: optimize code --- .github/workflows/test.yml | 11 ++++++++++- README.md | 3 +++ README_ZH.md | 2 ++ pkg/dev/main.go | 2 +- pkg/driver/openvpn/other.go | 1 - pkg/driver/openvpn/windows.go | 3 +-- pkg/driver/wintun/amd64.go | 1 - pkg/driver/wintun/arm.go | 1 - pkg/driver/wintun/arm64.go | 1 - pkg/driver/wintun/others.go | 1 - pkg/driver/wintun/x86.go | 2 -- pkg/test/local.go | 6 +++++- pkg/util/elevate_others.go | 1 - pkg/util/elevate_windows.go | 1 - pkg/util/elevatecheck_others.go | 1 - pkg/util/elevatecheck_windows.go | 1 - pkg/util/getcidr.go | 5 +++-- pkg/util/image.go | 2 +- pkg/util/upgarde.go | 4 ++-- 19 files changed, 28 insertions(+), 21 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 264ddd663..e7363e831 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -166,4 +166,13 @@ jobs: choco install make - name: Build - run: make kubevpn-windows-amd64 \ No newline at end of file + run: make kubevpn-windows-amd64 + upload-coverage-reports-to-codecov: + - runs-on: ubuntu-latest + if: ${{ always() }} + needs: [ "linux", "macos", "windows" ] + steps: + - name: Upload coverage reports to Codecov + uses: codecov/codecov-action@v3 + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} \ No newline at end of file diff --git a/README.md b/README.md index 892a2cfab..ced12703d 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,9 @@ [![GitHub License][5]](https://github.com/KubeNetworks/kubevpn/blob/main/LICENSE) [![Docker Pulls][6]](https://hub.docker.com/r/naison/kubevpn) [![Releases][7]](https://github.com/KubeNetworks/kubevpn/releases) +[![GoDoc](https://godoc.org/github.com/KubeNetworks/kubevpn?status.png)](https://godoc.org/github.com/KubeNetworks/kubevpn) +[![Test coverage](http://gocover.io/_badge/github.com/KubeNetworks/kubevpn)](https://gocover.io/github.com/KubeNetworks/kubevpn) + [1]: https://img.shields.io/github/actions/workflow/status/KubeNetworks/kubevpn/release.yml?logo=github diff --git a/README_ZH.md b/README_ZH.md index 12446c9ff..c44cfdee1 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -7,6 +7,8 @@ [![GitHub License][5]](https://github.com/KubeNetworks/kubevpn/blob/main/LICENSE) [![Docker Pulls][6]](https://hub.docker.com/r/naison/kubevpn) [![Releases][7]](https://github.com/KubeNetworks/kubevpn/releases) +[![GoDoc](https://godoc.org/github.com/KubeNetworks/kubevpn?status.png)](https://godoc.org/github.com/KubeNetworks/kubevpn) +[![Test coverage](http://gocover.io/_badge/github.com/KubeNetworks/kubevpn)](https://gocover.io/github.com/KubeNetworks/kubevpn) [1]: https://img.shields.io/github/actions/workflow/status/KubeNetworks/kubevpn/release.yml?logo=github diff --git a/pkg/dev/main.go b/pkg/dev/main.go index 78169df8c..56b7d19a2 100644 --- a/pkg/dev/main.go +++ b/pkg/dev/main.go @@ -398,7 +398,7 @@ func DoDev(ctx context.Context, devOption *Options, conf *util.SshConfig, flags return err } if inspect.State == nil { - return fmt.Errorf("can not get container status, please make contianer name is valid") + return fmt.Errorf("can not get container status, please make container name is valid") } if !inspect.State.Running { return fmt.Errorf("container %s status is %s, expect is running, please make sure your outer docker name is correct", mode.ConnectedContainer(), inspect.State.Status) diff --git a/pkg/driver/openvpn/other.go b/pkg/driver/openvpn/other.go index e79a2718a..897864250 100644 --- a/pkg/driver/openvpn/other.go +++ b/pkg/driver/openvpn/other.go @@ -1,5 +1,4 @@ //go:build !windows -// +build !windows package openvpn diff --git a/pkg/driver/openvpn/windows.go b/pkg/driver/openvpn/windows.go index ac66879a5..da9f4e794 100644 --- a/pkg/driver/openvpn/windows.go +++ b/pkg/driver/openvpn/windows.go @@ -1,5 +1,4 @@ //go:build windows -// +build windows package openvpn @@ -13,7 +12,7 @@ import ( //go:embed exe/tap-windows-9.21.2.exe var fs embed.FS -// driver download from https://build.openvpn.net/downloads/releases/ +// driver download from https://build.openvpn.net/downloads/releases/ func Install() error { bytes, err := fs.ReadFile("exe/tap-windows-9.21.2.exe") if err != nil { diff --git a/pkg/driver/wintun/amd64.go b/pkg/driver/wintun/amd64.go index 01bf925ba..840598367 100644 --- a/pkg/driver/wintun/amd64.go +++ b/pkg/driver/wintun/amd64.go @@ -1,5 +1,4 @@ //go:build windows && amd64 -// +build windows,amd64 package wintun diff --git a/pkg/driver/wintun/arm.go b/pkg/driver/wintun/arm.go index 2d38dc0b7..a579755e7 100644 --- a/pkg/driver/wintun/arm.go +++ b/pkg/driver/wintun/arm.go @@ -1,5 +1,4 @@ //go:build windows && arm -// +build windows,arm package wintun diff --git a/pkg/driver/wintun/arm64.go b/pkg/driver/wintun/arm64.go index 1ee8ff2c1..e715a90a8 100644 --- a/pkg/driver/wintun/arm64.go +++ b/pkg/driver/wintun/arm64.go @@ -1,5 +1,4 @@ //go:build windows && arm64 -// +build windows,arm64 package wintun diff --git a/pkg/driver/wintun/others.go b/pkg/driver/wintun/others.go index 652befd94..220cf0bcc 100644 --- a/pkg/driver/wintun/others.go +++ b/pkg/driver/wintun/others.go @@ -1,5 +1,4 @@ //go:build !windows -// +build !windows package wintun diff --git a/pkg/driver/wintun/x86.go b/pkg/driver/wintun/x86.go index 555ee167f..6fdc709c3 100644 --- a/pkg/driver/wintun/x86.go +++ b/pkg/driver/wintun/x86.go @@ -1,6 +1,4 @@ //go:build windows && (x86 || 386) -// +build windows -// +build x86 386 package wintun diff --git a/pkg/test/local.go b/pkg/test/local.go index a9115635a..9490fae99 100644 --- a/pkg/test/local.go +++ b/pkg/test/local.go @@ -32,7 +32,11 @@ func main() { if err != nil { panic(err) } - tunConn, err := listener.Accept() + var tunConn net.Conn + tunConn, err = listener.Accept() + if err != nil { + log.Fatal(err) + } defer tunConn.Close() tcpConn, err := net.Dial("tcp", ":1080") if err != nil { diff --git a/pkg/util/elevate_others.go b/pkg/util/elevate_others.go index 585f68590..0596b5955 100644 --- a/pkg/util/elevate_others.go +++ b/pkg/util/elevate_others.go @@ -1,5 +1,4 @@ //go:build !windows -// +build !windows package util diff --git a/pkg/util/elevate_windows.go b/pkg/util/elevate_windows.go index ab68bc371..117db5545 100644 --- a/pkg/util/elevate_windows.go +++ b/pkg/util/elevate_windows.go @@ -1,5 +1,4 @@ //go:build windows -// +build windows package util diff --git a/pkg/util/elevatecheck_others.go b/pkg/util/elevatecheck_others.go index 0ae6dfe71..653eca994 100644 --- a/pkg/util/elevatecheck_others.go +++ b/pkg/util/elevatecheck_others.go @@ -1,5 +1,4 @@ //go:build !windows -// +build !windows package util diff --git a/pkg/util/elevatecheck_windows.go b/pkg/util/elevatecheck_windows.go index 3032ae583..dd8d54065 100644 --- a/pkg/util/elevatecheck_windows.go +++ b/pkg/util/elevatecheck_windows.go @@ -1,5 +1,4 @@ //go:build windows -// +build windows package util diff --git a/pkg/util/getcidr.go b/pkg/util/getcidr.go index e51dae932..73ce836f8 100644 --- a/pkg/util/getcidr.go +++ b/pkg/util/getcidr.go @@ -56,12 +56,13 @@ func getCIDRFromCNI(clientset *kubernetes.Clientset, restclient *rest.RESTClient var cmd = `grep -a -R "service-cluster-ip-range\|cluster-cidr" /etc/cni/proc/*/cmdline | grep -a -v grep | tr "\0" "\n"` - var result []*net.IPNet - content, err := Shell(clientset, restclient, restconfig, pod.Name, "", pod.Namespace, []string{"sh", "-c", cmd}) + var content string + content, err = Shell(clientset, restclient, restconfig, pod.Name, "", pod.Namespace, []string{"sh", "-c", cmd}) if err != nil { return nil, err } + var result []*net.IPNet for _, s := range strings.Split(content, "\n") { result = Deduplicate(append(result, parseCIDRFromString(s)...)) } diff --git a/pkg/util/image.go b/pkg/util/image.go index b29ff2814..f289debd9 100644 --- a/pkg/util/image.go +++ b/pkg/util/image.go @@ -129,7 +129,7 @@ func TransferImage(ctx context.Context, conf *SshConfig, imageSource, imageTarge } defer os.Remove(file.Name()) - logrus.Infof("Transfering image %s", imageTarget) + logrus.Infof("Transferring image %s", imageTarget) filename := filepath.Base(file.Name()) cmd := fmt.Sprintf( "(docker load image -i ~/.kubevpn/%s && docker push %s) || (nerdctl image load -i ~/.kubevpn/%s && nerdctl image push %s)", diff --git a/pkg/util/upgarde.go b/pkg/util/upgarde.go index 902079732..9a00c28fb 100644 --- a/pkg/util/upgarde.go +++ b/pkg/util/upgarde.go @@ -40,13 +40,13 @@ func GetManifest(httpCli *http.Client, os string, arch string) (version string, var all []byte all, err = io.ReadAll(resp.Body) if err != nil { - err = fmt.Errorf("failed to read all reponse from github api, err: %v", err) + err = fmt.Errorf("failed to read all response from github api, err: %v", err) return } var m RootEntity err = json.Unmarshal(all, &m) if err != nil { - err = fmt.Errorf("failed to unmarshal reponse, err: %v", err) + err = fmt.Errorf("failed to unmarshal response, err: %v", err) return } version = m.TagName