From 3452a71126ec97aae0c91dbf5529325d127354d3 Mon Sep 17 00:00:00 2001 From: wencaiwulue <895703375@qq.com> Date: Sun, 25 Dec 2022 16:14:37 +0800 Subject: [PATCH] feat: optimize get cidr from pod logic --- go.mod | 132 ++++++++++++++++++++++---------------------- pkg/util/getcidr.go | 19 ++++++- 2 files changed, 81 insertions(+), 70 deletions(-) diff --git a/go.mod b/go.mod index ffa11172..683f3977 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/docker/libcontainer v2.2.1+incompatible github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1 github.com/fsnotify/fsnotify v1.5.4 - github.com/go-openapi/jsonpointer v0.19.5 // indirect + github.com/go-openapi/jsonpointer v0.19.5 github.com/miekg/dns v1.0.14 github.com/milosgajdos/tenus v0.0.3 github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 @@ -20,10 +20,10 @@ require ( golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f golang.zx2c4.com/wireguard v0.0.0-20211209221555-9c9e7e272434 golang.zx2c4.com/wireguard/windows v0.4.10 - google.golang.org/appengine v1.6.7 // indirect + google.golang.org/appengine v1.6.7 google.golang.org/grpc v1.47.0 google.golang.org/protobuf v1.28.0 - gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v2 v2.4.0 k8s.io/api v0.25.0 k8s.io/apimachinery v0.25.0 k8s.io/cli-runtime v0.24.2 @@ -33,74 +33,72 @@ require ( ) require ( + github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 + github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd + github.com/PuerkitoBio/purell v1.1.1 + github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 + github.com/census-instrumentation/opencensus-proto v0.2.1 + github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5 + github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1 github.com/containernetworking/cni v1.1.2 + github.com/davecgh/go-spew v1.1.1 + github.com/emicklei/go-restful/v3 v3.8.0 + github.com/envoyproxy/protoc-gen-validate v0.1.0 + github.com/evanphx/json-patch v4.12.0+incompatible + github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d + github.com/fatih/camelcase v1.0.0 + github.com/fvbommel/sortorder v1.0.1 + github.com/go-errors/errors v1.0.1 + github.com/go-logr/logr v1.2.3 + github.com/go-openapi/jsonreference v0.19.5 + github.com/go-openapi/swag v0.19.14 + github.com/gogo/protobuf v1.3.2 github.com/golang/protobuf v1.5.2 + github.com/google/btree v1.0.1 + github.com/google/gnostic v0.5.7-v3refs + github.com/google/go-cmp v0.5.8 + github.com/google/gofuzz v1.1.0 + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 + github.com/google/uuid v1.2.0 + github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 + github.com/imdario/mergo v0.3.12 + github.com/inconshreveable/mousetrap v1.0.0 + github.com/josharian/intern v1.0.0 + github.com/json-iterator/go v1.1.12 + github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de + github.com/mailru/easyjson v0.7.6 github.com/mattbaird/jsonpatch v0.0.0-20200820163806-098863c1fc24 + github.com/mitchellh/go-wordwrap v1.0.0 + github.com/moby/spdystream v0.2.0 + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd + github.com/modern-go/reflect2 v1.0.2 + github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 + github.com/peterbourgon/diskv v2.0.1+incompatible + github.com/pmezard/go-difflib v1.0.0 + github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 + github.com/russross/blackfriday v1.5.2 + github.com/spf13/pflag v1.0.5 + github.com/stretchr/testify v1.7.0 + github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca + go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 + golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd + golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 + golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 + golang.org/x/text v0.3.7 + golang.org/x/time v0.0.0-20220609170525-579cf78fd858 + golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 + google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 + gopkg.in/inf.v0 v0.9.1 + gopkg.in/yaml.v3 v3.0.1 + k8s.io/component-base v0.25.0 + k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed + sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 + sigs.k8s.io/kustomize/api v0.11.4 + sigs.k8s.io/kustomize/kyaml v0.13.6 + sigs.k8s.io/structured-merge-diff/v4 v4.2.3 sigs.k8s.io/yaml v1.3.0 ) -require ( - github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect - github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect - github.com/PuerkitoBio/purell v1.1.1 // indirect - github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect - github.com/census-instrumentation/opencensus-proto v0.2.1 // indirect - github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5 // indirect - github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/emicklei/go-restful/v3 v3.8.0 // indirect - github.com/envoyproxy/protoc-gen-validate v0.1.0 // indirect - github.com/evanphx/json-patch v4.12.0+incompatible // indirect - github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect - github.com/fatih/camelcase v1.0.0 // indirect - github.com/fvbommel/sortorder v1.0.1 // indirect - github.com/go-errors/errors v1.0.1 // indirect - github.com/go-logr/logr v1.2.3 // indirect - github.com/go-openapi/jsonreference v0.19.5 // indirect - github.com/go-openapi/swag v0.19.14 // indirect - github.com/gogo/protobuf v1.3.2 // indirect - github.com/google/btree v1.0.1 // indirect - github.com/google/gnostic v0.5.7-v3refs // indirect - github.com/google/go-cmp v0.5.8 // indirect - github.com/google/gofuzz v1.1.0 // indirect - github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect - github.com/google/uuid v1.2.0 // indirect - github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect - github.com/imdario/mergo v0.3.12 // indirect - github.com/inconshreveable/mousetrap v1.0.0 // indirect - github.com/josharian/intern v1.0.0 // indirect - github.com/json-iterator/go v1.1.12 // indirect - github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect - github.com/mailru/easyjson v0.7.6 // indirect - github.com/mitchellh/go-wordwrap v1.0.0 // indirect - github.com/moby/spdystream v0.2.0 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect - github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/onsi/ginkgo v1.16.5 // indirect - github.com/peterbourgon/diskv v2.0.1+incompatible // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 // indirect - github.com/russross/blackfriday v1.5.2 // indirect - github.com/spf13/pflag v1.0.5 // indirect - github.com/stretchr/testify v1.7.0 // indirect - github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect - go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect - golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect - golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect - golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect - golang.org/x/text v0.3.7 // indirect - golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect - golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 // indirect - google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect - gopkg.in/inf.v0 v0.9.1 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect - k8s.io/component-base v0.25.0 // indirect - k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect - sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect - sigs.k8s.io/kustomize/api v0.11.4 // indirect - sigs.k8s.io/kustomize/kyaml v0.13.6 // indirect - sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect -) +require github.com/onsi/ginkgo v1.16.5 // indirect diff --git a/pkg/util/getcidr.go b/pkg/util/getcidr.go index d1f1799e..d28fd7eb 100644 --- a/pkg/util/getcidr.go +++ b/pkg/util/getcidr.go @@ -258,12 +258,25 @@ func createCIDRPod(clientset *kubernetes.Clientset, namespace string) (*v12.Pod, } func getPodCIDRFromPod(clientset *kubernetes.Clientset, namespace string, svc *net.IPNet) ([]*net.IPNet, error) { - get, err := clientset.CoreV1().Pods(namespace).Get(context.Background(), config.CniNetName, v1.GetOptions{}) + podList, err := clientset.CoreV1().Pods(namespace).List(context.Background(), v1.ListOptions{}) if err != nil { return nil, err } - ip := net.ParseIP(get.Status.PodIP) - return []*net.IPNet{svc, {IP: ip, Mask: svc.Mask}}, nil + for i := 0; i < len(podList.Items); i++ { + if podList.Items[i].Spec.HostNetwork || net.ParseIP(podList.Items[i].Status.PodIP) == nil { + podList.Items = append(podList.Items[:i], podList.Items[i+1:]...) + i-- + } + } + for _, item := range podList.Items { + if item.Name == config.CniNetName { + return []*net.IPNet{svc, {IP: net.ParseIP(item.Status.PodIP), Mask: svc.Mask}}, nil + } + } + for _, item := range podList.Items { + return []*net.IPNet{svc, {IP: net.ParseIP(item.Status.PodIP), Mask: svc.Mask}}, nil + } + return nil, fmt.Errorf("can not found pod cidr from pod list") } func parseCIDRFromString(content string) (result []*net.IPNet) {