From f37c58ed241849f6faceec5dd5d012e6972c6b6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Wed, 24 Apr 2024 15:24:26 +0800 Subject: [PATCH] fix(climc): enable build windows climc (#20121) --- cmd/climc/shell/compute/sshkeypairs.go | 43 +++++++++-- cmd/climc/shell/misc/pprof.go | 4 +- go.mod | 4 +- go.sum | 9 ++- pkg/mcclient/aksk.go | 4 +- pkg/util/netutils2/http.go | 21 +----- .../{cmdsetenv.go => cmdsetenv_others.go} | 0 pkg/util/procutils/cmdsetenv_windows.go | 30 ++++++++ .../vishvananda/netns/.golangci.yml | 26 +++++++ .../vishvananda/netns/.yamllint.yml | 9 +++ vendor/github.com/vishvananda/netns/README.md | 12 +--- vendor/github.com/vishvananda/netns/doc.go | 9 +++ .../vishvananda/netns/netns_linux.go | 71 ++++++++++--------- .../vishvananda/netns/netns_others.go | 56 +++++++++++++++ .../vishvananda/netns/netns_unspecified.go | 43 ----------- .../netns/{netns.go => nshandle_linux.go} | 12 +--- .../vishvananda/netns/nshandle_others.go | 45 ++++++++++++ vendor/modules.txt | 6 +- vendor/yunion.io/x/pkg/util/netutils/http.go | 40 +++++++++++ 19 files changed, 309 insertions(+), 135 deletions(-) rename pkg/util/procutils/{cmdsetenv.go => cmdsetenv_others.go} (100%) create mode 100644 pkg/util/procutils/cmdsetenv_windows.go create mode 100644 vendor/github.com/vishvananda/netns/.golangci.yml create mode 100644 vendor/github.com/vishvananda/netns/.yamllint.yml create mode 100644 vendor/github.com/vishvananda/netns/doc.go create mode 100644 vendor/github.com/vishvananda/netns/netns_others.go delete mode 100644 vendor/github.com/vishvananda/netns/netns_unspecified.go rename vendor/github.com/vishvananda/netns/{netns.go => nshandle_linux.go} (75%) create mode 100644 vendor/github.com/vishvananda/netns/nshandle_others.go create mode 100644 vendor/yunion.io/x/pkg/util/netutils/http.go diff --git a/cmd/climc/shell/compute/sshkeypairs.go b/cmd/climc/shell/compute/sshkeypairs.go index 7a0f6a13cd..8dc86bf985 100644 --- a/cmd/climc/shell/compute/sshkeypairs.go +++ b/cmd/climc/shell/compute/sshkeypairs.go @@ -18,12 +18,11 @@ import ( "fmt" "os" "path" + "strings" "yunion.io/x/jsonutils" "yunion.io/x/pkg/errors" - "yunion.io/x/onecloud/pkg/hostman/guestfs/fsdriver" - deployapi "yunion.io/x/onecloud/pkg/hostman/hostdeployer/apis" "yunion.io/x/onecloud/pkg/mcclient" modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute" "yunion.io/x/onecloud/pkg/util/procutils" @@ -101,8 +100,44 @@ func init() { } oldKeys = string(output) } - pubKeys := &deployapi.SSHKeys{AdminPublicKey: pubKey} - newKeys := fsdriver.MergeAuthorizedKeys(oldKeys, pubKeys, true) + var MergeAuthorizedKeys = func(oldKeys string, pubKey string) string { + const sshKeySignature = "@yunioncloudpods" + var allkeys = make(map[string]string) + if len(oldKeys) > 0 { + for _, line := range strings.Split(oldKeys, "\n") { + line = strings.TrimSpace(line) + dat := strings.Split(line, " ") + if len(dat) > 1 { + if len(dat) > 2 && dat[2] == sshKeySignature { + // skip ssh keys with signature + continue + } + if _, ok := allkeys[dat[1]]; !ok { + allkeys[dat[1]] = line + } + } + } + } + candiateKeys := []string{pubKey} + for _, k := range candiateKeys { + if len(k) > 0 { + k = strings.TrimSpace(k) + dat := strings.Split(k, " ") + if len(dat) > 1 { + if _, ok := allkeys[dat[1]]; !ok { + allkeys[dat[1]] = strings.Join([]string{dat[0], dat[1], sshKeySignature}, " ") + } + } + } + } + var keys = make([]string, 0) + for _, val := range allkeys { + keys = append(keys, val) + } + return strings.Join(keys, "\n") + "\n" + } + + newKeys := MergeAuthorizedKeys(oldKeys, pubKey) if output, err := procutils.NewCommand( "sh", "-c", fmt.Sprintf("echo '%s' > %s", newKeys, authFile)).Output(); err != nil { return errors.Wrapf(err, "write public keys: %s", output) diff --git a/cmd/climc/shell/misc/pprof.go b/cmd/climc/shell/misc/pprof.go index f89e64d49d..26f4c796b7 100644 --- a/cmd/climc/shell/misc/pprof.go +++ b/cmd/climc/shell/misc/pprof.go @@ -23,11 +23,11 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/util/netutils" "yunion.io/x/pkg/util/signalutils" "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/modules" - "yunion.io/x/onecloud/pkg/util/netutils2" "yunion.io/x/onecloud/pkg/util/procutils" ) @@ -125,7 +125,7 @@ func init() { } { pType := kind R(&TraceOptions{}, fmt.Sprintf("pprof-%s", pType), fmt.Sprintf("pprof %s of backend service", pType), func(s *mcclient.ClientSession, args *TraceOptions) error { - port, err := netutils2.GetFreePort() + port, err := netutils.GetFreePort() if err != nil { return err } diff --git a/go.mod b/go.mod index 09814ac76f..9b15821ccd 100644 --- a/go.mod +++ b/go.mod @@ -63,7 +63,7 @@ require ( github.com/tjfoc/gmsm v1.4.1 github.com/tredoe/osutil/v2 v2.0.0-rc.16 github.com/vishvananda/netlink v1.1.0 - github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 + github.com/vishvananda/netns v0.0.5-0.20240412164733-9469873f4601 github.com/vmihailenco/msgpack v4.0.4+incompatible github.com/xuri/excelize/v2 v2.7.1 github.com/zexi/influxql-to-metricsql v0.0.6 @@ -94,7 +94,7 @@ require ( yunion.io/x/jsonutils v1.0.1-0.20240203102553-4096f103b401 yunion.io/x/log v1.0.1-0.20240305175729-7cf2d6cd5a91 yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900 - yunion.io/x/pkg v1.10.1-0.20240416041628-5e17b8728132 + yunion.io/x/pkg v1.10.1-0.20240424071144-ff6b95d2046d yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e yunion.io/x/sqlchemy v1.1.3-0.20240417115142-4f4271cd8305 yunion.io/x/structarg v0.0.0-20231017124457-df4d5009457c diff --git a/go.sum b/go.sum index 53aa148b38..8f18e73e92 100644 --- a/go.sum +++ b/go.sum @@ -723,8 +723,8 @@ github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLY github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= -github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 h1:gga7acRE695APm9hlsSMoOoE65U4/TcqNj90mc69Rlg= -github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= +github.com/vishvananda/netns v0.0.5-0.20240412164733-9469873f4601 h1:eELqkILRCR0kkoFujWv/hGyn+KfpGjHY01Rl1p42CBg= +github.com/vishvananda/netns v0.0.5-0.20240412164733-9469873f4601/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM= github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmware/govmomi v0.20.1 h1:7b/SeTUB3tER8ZLGLLLH3xcnB2xeuLULXmfPFqPSRZA= @@ -928,7 +928,6 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -1225,8 +1224,8 @@ yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900 h1:Hu/4ERvoWaN6aiFs4h4/yvVB yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900/go.mod h1:0vLkNEhlmA64HViPBAnSTUMrx5QP1CLsxXmxDKQ80tc= yunion.io/x/pkg v0.0.0-20190620104149-945c25821dbf/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= yunion.io/x/pkg v0.0.0-20190628082551-f4033ba2ea30/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= -yunion.io/x/pkg v1.10.1-0.20240416041628-5e17b8728132 h1:+7rO5MAfU4BNvYPim0MhgN1/zO6KTyUk37HC4pYcqPE= -yunion.io/x/pkg v1.10.1-0.20240416041628-5e17b8728132/go.mod h1:ksCJVQ+DwKrJ5QBEoU8pzrDFfDaZVAFH/iJ6yQCYxJk= +yunion.io/x/pkg v1.10.1-0.20240424071144-ff6b95d2046d h1:kl78atYnRcYjVeo/TjMXNMJL5F5N9rnqVHwpQXrG/yc= +yunion.io/x/pkg v1.10.1-0.20240424071144-ff6b95d2046d/go.mod h1:ksCJVQ+DwKrJ5QBEoU8pzrDFfDaZVAFH/iJ6yQCYxJk= yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e h1:v+EzIadodSwkdZ/7bremd7J8J50Cise/HCylsOJngmo= yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e/go.mod h1:0iFKpOs1y4lbCxeOmq3Xx/0AcQoewVPwj62eRluioEo= yunion.io/x/sqlchemy v1.1.3-0.20240417115142-4f4271cd8305 h1:TIXzc634+YSAs2+3A+TyPmKAGd6HtAYU3M4cKwzUuo0= diff --git a/pkg/mcclient/aksk.go b/pkg/mcclient/aksk.go index 48a03f2a0a..d5ff97d14a 100644 --- a/pkg/mcclient/aksk.go +++ b/pkg/mcclient/aksk.go @@ -22,10 +22,10 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/util/netutils" "yunion.io/x/pkg/util/s3auth" api "yunion.io/x/onecloud/pkg/apis/identity" - "yunion.io/x/onecloud/pkg/util/netutils2" ) type SAkskTokenCredential struct { @@ -59,7 +59,7 @@ func (this *Client) _verifyKeySecret(aksk s3auth.IAccessKeySecretRequest, aCtx S } func (this *Client) VerifyRequest(req http.Request, aksk s3auth.IAccessKeySecretRequest, virtualHost bool) (*SAkskTokenCredential, error) { - cliIp := netutils2.GetHttpRequestIp(&req) + cliIp := netutils.GetHttpRequestIp(&req) aCtx := SAuthContext{ Source: AuthSourceSrv, Ip: cliIp, diff --git a/pkg/util/netutils2/http.go b/pkg/util/netutils2/http.go index e30843cad7..6ccd8d913c 100644 --- a/pkg/util/netutils2/http.go +++ b/pkg/util/netutils2/http.go @@ -16,25 +16,10 @@ package netutils2 import ( "net/http" - "strings" + + "yunion.io/x/pkg/util/netutils" ) func GetHttpRequestIp(r *http.Request) string { - ipStr := r.Header.Get("X-Forwarded-For") - if len(ipStr) > 0 { - ipList := strings.Split(ipStr, ",") - if len(ipList) > 0 { - return ipList[0] - } - } - ipStr = r.Header.Get("X-Real-Ip") - if len(ipStr) > 0 { - return ipStr - } - ipStr = r.RemoteAddr - colonPos := strings.Index(ipStr, ":") - if colonPos > 0 { - ipStr = ipStr[:colonPos] - } - return ipStr + return netutils.GetHttpRequestIp(r) } diff --git a/pkg/util/procutils/cmdsetenv.go b/pkg/util/procutils/cmdsetenv_others.go similarity index 100% rename from pkg/util/procutils/cmdsetenv.go rename to pkg/util/procutils/cmdsetenv_others.go diff --git a/pkg/util/procutils/cmdsetenv_windows.go b/pkg/util/procutils/cmdsetenv_windows.go new file mode 100644 index 0000000000..0d10b5e7e5 --- /dev/null +++ b/pkg/util/procutils/cmdsetenv_windows.go @@ -0,0 +1,30 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build windows +// +build windows + +package procutils + +import ( + "os/exec" + + "yunion.io/x/executor/client" +) + +func cmdSetEnv(cmd *exec.Cmd) { +} + +func remoteCmdSetEnv(cmd *client.Cmd) { +} diff --git a/vendor/github.com/vishvananda/netns/.golangci.yml b/vendor/github.com/vishvananda/netns/.golangci.yml new file mode 100644 index 0000000000..2b6988f286 --- /dev/null +++ b/vendor/github.com/vishvananda/netns/.golangci.yml @@ -0,0 +1,26 @@ +linters: + enable: + - errcheck + - errorlint + - gocritic + - gosec + - gosimple + - govet + - gci + - misspell + - nonamedreturns + - staticcheck + - unconvert + - unparam + - unused + - whitespace + +linters-settings: + gci: + sections: + - standard + - default + - prefix(github.com/vishvananda) + +run: + timeout: 5m diff --git a/vendor/github.com/vishvananda/netns/.yamllint.yml b/vendor/github.com/vishvananda/netns/.yamllint.yml new file mode 100644 index 0000000000..1b2830cc99 --- /dev/null +++ b/vendor/github.com/vishvananda/netns/.yamllint.yml @@ -0,0 +1,9 @@ +--- +extends: default + +rules: + document-start: disable + line-length: disable + truthy: + ignore: | + .github/workflows/*.yml diff --git a/vendor/github.com/vishvananda/netns/README.md b/vendor/github.com/vishvananda/netns/README.md index 1fdb2d3e4a..bdfedbe81f 100644 --- a/vendor/github.com/vishvananda/netns/README.md +++ b/vendor/github.com/vishvananda/netns/README.md @@ -23,6 +23,7 @@ import ( "fmt" "net" "runtime" + "github.com/vishvananda/netns" ) @@ -48,14 +49,3 @@ func main() { } ``` - -## NOTE - -The library can be safely used only with Go >= 1.10 due to [golang/go#20676](https://github.com/golang/go/issues/20676). - -After locking a goroutine to its current OS thread with `runtime.LockOSThread()` -and changing its network namespace, any new subsequent goroutine won't be -scheduled on that thread while it's locked. Therefore, the new goroutine -will run in a different namespace leading to unexpected results. - -See [here](https://www.weave.works/blog/linux-namespaces-golang-followup) for more details. diff --git a/vendor/github.com/vishvananda/netns/doc.go b/vendor/github.com/vishvananda/netns/doc.go new file mode 100644 index 0000000000..cd4093a4d7 --- /dev/null +++ b/vendor/github.com/vishvananda/netns/doc.go @@ -0,0 +1,9 @@ +// Package netns allows ultra-simple network namespace handling. NsHandles +// can be retrieved and set. Note that the current namespace is thread +// local so actions that set and reset namespaces should use LockOSThread +// to make sure the namespace doesn't change due to a goroutine switch. +// It is best to close NsHandles when you are done with them. This can be +// accomplished via a `defer ns.Close()` on the handle. Changing namespaces +// requires elevated privileges, so in most cases this code needs to be run +// as root. +package netns diff --git a/vendor/github.com/vishvananda/netns/netns_linux.go b/vendor/github.com/vishvananda/netns/netns_linux.go index 6be5c55c16..8c9b177883 100644 --- a/vendor/github.com/vishvananda/netns/netns_linux.go +++ b/vendor/github.com/vishvananda/netns/netns_linux.go @@ -1,57 +1,55 @@ -//go:build linux && go1.10 -// +build linux,go1.10 - package netns import ( "fmt" - "io/ioutil" "os" "path" "path/filepath" "strconv" "strings" - "syscall" "golang.org/x/sys/unix" ) -// Deprecated: use syscall pkg instead (go >= 1.5 needed). +// Deprecated: use golang.org/x/sys/unix pkg instead. const ( - CLONE_NEWUTS = 0x04000000 /* New utsname group? */ - CLONE_NEWIPC = 0x08000000 /* New ipcs */ - CLONE_NEWUSER = 0x10000000 /* New user namespace */ - CLONE_NEWPID = 0x20000000 /* New pid namespace */ - CLONE_NEWNET = 0x40000000 /* New network namespace */ - CLONE_IO = 0x80000000 /* Get io context */ - bindMountPath = "/run/netns" /* Bind mount path for named netns */ + CLONE_NEWUTS = unix.CLONE_NEWUTS /* New utsname group? */ + CLONE_NEWIPC = unix.CLONE_NEWIPC /* New ipcs */ + CLONE_NEWUSER = unix.CLONE_NEWUSER /* New user namespace */ + CLONE_NEWPID = unix.CLONE_NEWPID /* New pid namespace */ + CLONE_NEWNET = unix.CLONE_NEWNET /* New network namespace */ + CLONE_IO = unix.CLONE_IO /* Get io context */ ) -// Setns sets namespace using syscall. Note that this should be a method -// in syscall but it has not been added. -func Setns(ns NsHandle, nstype int) (err error) { +const bindMountPath = "/run/netns" /* Bind mount path for named netns */ + +// Setns sets namespace using golang.org/x/sys/unix.Setns. +// +// Deprecated: Use golang.org/x/sys/unix.Setns instead. +func Setns(ns NsHandle, nstype int) error { return unix.Setns(int(ns), nstype) } // Set sets the current network namespace to the namespace represented // by NsHandle. -func Set(ns NsHandle) (err error) { - return Setns(ns, CLONE_NEWNET) +func Set(ns NsHandle) error { + return unix.Setns(int(ns), unix.CLONE_NEWNET) } // New creates a new network namespace, sets it as current and returns // a handle to it. -func New() (ns NsHandle, err error) { - if err := unix.Unshare(CLONE_NEWNET); err != nil { +func New() (NsHandle, error) { + if err := unix.Unshare(unix.CLONE_NEWNET); err != nil { return -1, err } return Get() } -// NewNamed creates a new named network namespace and returns a handle to it +// NewNamed creates a new named network namespace, sets it as current, +// and returns a handle to it func NewNamed(name string) (NsHandle, error) { if _, err := os.Stat(bindMountPath); os.IsNotExist(err) { - err = os.MkdirAll(bindMountPath, 0755) + err = os.MkdirAll(bindMountPath, 0o755) if err != nil { return None(), err } @@ -64,15 +62,17 @@ func NewNamed(name string) (NsHandle, error) { namedPath := path.Join(bindMountPath, name) - f, err := os.OpenFile(namedPath, os.O_CREATE|os.O_EXCL, 0444) + f, err := os.OpenFile(namedPath, os.O_CREATE|os.O_EXCL, 0o444) if err != nil { + newNs.Close() return None(), err } f.Close() - nsPath := fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid()) - err = syscall.Mount(nsPath, namedPath, "bind", syscall.MS_BIND, "") + nsPath := fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), unix.Gettid()) + err = unix.Mount(nsPath, namedPath, "bind", unix.MS_BIND, "") if err != nil { + newNs.Close() return None(), err } @@ -83,7 +83,7 @@ func NewNamed(name string) (NsHandle, error) { func DeleteNamed(name string) error { namedPath := path.Join(bindMountPath, name) - err := syscall.Unmount(namedPath, syscall.MNT_DETACH) + err := unix.Unmount(namedPath, unix.MNT_DETACH) if err != nil { return err } @@ -109,7 +109,7 @@ func GetFromPath(path string) (NsHandle, error) { // GetFromName gets a handle to a named network namespace such as one // created by `ip netns add`. func GetFromName(name string) (NsHandle, error) { - return GetFromPath(fmt.Sprintf("/var/run/netns/%s", name)) + return GetFromPath(filepath.Join(bindMountPath, name)) } // GetFromPid gets a handle to the network namespace of a given pid. @@ -135,7 +135,7 @@ func GetFromDocker(id string) (NsHandle, error) { // borrowed from docker/utils/utils.go func findCgroupMountpoint(cgroupType string) (int, string, error) { - output, err := ioutil.ReadFile("/proc/mounts") + output, err := os.ReadFile("/proc/mounts") if err != nil { return -1, "", err } @@ -165,7 +165,7 @@ func findCgroupMountpoint(cgroupType string) (int, string, error) { // borrowed from docker/utils/utils.go // modified to get the docker pid instead of using /proc/self func getDockerCgroup(cgroupVer int, cgroupType string) (string, error) { - dockerpid, err := ioutil.ReadFile("/var/run/docker.pid") + dockerpid, err := os.ReadFile("/var/run/docker.pid") if err != nil { return "", err } @@ -177,7 +177,7 @@ func getDockerCgroup(cgroupVer int, cgroupType string) (string, error) { if err != nil { return "", err } - output, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cgroup", pid)) + output, err := os.ReadFile(fmt.Sprintf("/proc/%d/cgroup", pid)) if err != nil { return "", err } @@ -217,11 +217,12 @@ func getPidForContainer(id string) (int, error) { id += "*" var pidFile string - if cgroupVer == 1 { + switch cgroupVer { + case 1: pidFile = "tasks" - } else if cgroupVer == 2 { + case 2: pidFile = "cgroup.procs" - } else { + default: return -1, fmt.Errorf("Invalid cgroup version '%d'", cgroupVer) } @@ -264,7 +265,7 @@ func getPidForContainer(id string) (int, error) { return pid, fmt.Errorf("Unable to find container: %v", id[:len(id)-1]) } - output, err := ioutil.ReadFile(filename) + output, err := os.ReadFile(filename) if err != nil { return pid, err } @@ -276,7 +277,7 @@ func getPidForContainer(id string) (int, error) { pid, err = strconv.Atoi(result[0]) if err != nil { - return pid, fmt.Errorf("Invalid pid '%s': %s", result[0], err) + return pid, fmt.Errorf("Invalid pid '%s': %w", result[0], err) } return pid, nil diff --git a/vendor/github.com/vishvananda/netns/netns_others.go b/vendor/github.com/vishvananda/netns/netns_others.go new file mode 100644 index 0000000000..f444f6e77f --- /dev/null +++ b/vendor/github.com/vishvananda/netns/netns_others.go @@ -0,0 +1,56 @@ +//go:build !linux +// +build !linux + +package netns + +import "errors" + +var ErrNotImplemented = errors.New("not implemented") + +// Setns sets namespace using golang.org/x/sys/unix.Setns on Linux. It +// is not implemented on other platforms. +// +// Deprecated: Use golang.org/x/sys/unix.Setns instead. +func Setns(ns NsHandle, nstype int) error { + return ErrNotImplemented +} + +func Set(ns NsHandle) error { + return ErrNotImplemented +} + +func New() (NsHandle, error) { + return -1, ErrNotImplemented +} + +func NewNamed(name string) (NsHandle, error) { + return -1, ErrNotImplemented +} + +func DeleteNamed(name string) error { + return ErrNotImplemented +} + +func Get() (NsHandle, error) { + return -1, ErrNotImplemented +} + +func GetFromPath(path string) (NsHandle, error) { + return -1, ErrNotImplemented +} + +func GetFromName(name string) (NsHandle, error) { + return -1, ErrNotImplemented +} + +func GetFromPid(pid int) (NsHandle, error) { + return -1, ErrNotImplemented +} + +func GetFromThread(pid int, tid int) (NsHandle, error) { + return -1, ErrNotImplemented +} + +func GetFromDocker(id string) (NsHandle, error) { + return -1, ErrNotImplemented +} diff --git a/vendor/github.com/vishvananda/netns/netns_unspecified.go b/vendor/github.com/vishvananda/netns/netns_unspecified.go deleted file mode 100644 index d06af62b68..0000000000 --- a/vendor/github.com/vishvananda/netns/netns_unspecified.go +++ /dev/null @@ -1,43 +0,0 @@ -// +build !linux - -package netns - -import ( - "errors" -) - -var ( - ErrNotImplemented = errors.New("not implemented") -) - -func Set(ns NsHandle) (err error) { - return ErrNotImplemented -} - -func New() (ns NsHandle, err error) { - return -1, ErrNotImplemented -} - -func Get() (NsHandle, error) { - return -1, ErrNotImplemented -} - -func GetFromPath(path string) (NsHandle, error) { - return -1, ErrNotImplemented -} - -func GetFromName(name string) (NsHandle, error) { - return -1, ErrNotImplemented -} - -func GetFromPid(pid int) (NsHandle, error) { - return -1, ErrNotImplemented -} - -func GetFromThread(pid, tid int) (NsHandle, error) { - return -1, ErrNotImplemented -} - -func GetFromDocker(id string) (NsHandle, error) { - return -1, ErrNotImplemented -} diff --git a/vendor/github.com/vishvananda/netns/netns.go b/vendor/github.com/vishvananda/netns/nshandle_linux.go similarity index 75% rename from vendor/github.com/vishvananda/netns/netns.go rename to vendor/github.com/vishvananda/netns/nshandle_linux.go index 116befd548..1baffb66ac 100644 --- a/vendor/github.com/vishvananda/netns/netns.go +++ b/vendor/github.com/vishvananda/netns/nshandle_linux.go @@ -1,11 +1,3 @@ -// Package netns allows ultra-simple network namespace handling. NsHandles -// can be retrieved and set. Note that the current namespace is thread -// local so actions that set and reset namespaces should use LockOSThread -// to make sure the namespace doesn't change due to a goroutine switch. -// It is best to close NsHandles when you are done with them. This can be -// accomplished via a `defer ns.Close()` on the handle. Changing namespaces -// requires elevated privileges, so in most cases this code needs to be run -// as root. package netns import ( @@ -38,7 +30,7 @@ func (ns NsHandle) Equal(other NsHandle) bool { // String shows the file descriptor number and its dev and inode. func (ns NsHandle) String() string { if ns == -1 { - return "NS(None)" + return "NS(none)" } var s unix.Stat_t if err := unix.Fstat(int(ns), &s); err != nil { @@ -71,7 +63,7 @@ func (ns *NsHandle) Close() error { if err := unix.Close(int(*ns)); err != nil { return err } - (*ns) = -1 + *ns = -1 return nil } diff --git a/vendor/github.com/vishvananda/netns/nshandle_others.go b/vendor/github.com/vishvananda/netns/nshandle_others.go new file mode 100644 index 0000000000..af727bc091 --- /dev/null +++ b/vendor/github.com/vishvananda/netns/nshandle_others.go @@ -0,0 +1,45 @@ +//go:build !linux +// +build !linux + +package netns + +// NsHandle is a handle to a network namespace. It can only be used on Linux, +// but provides stub methods on other platforms. +type NsHandle int + +// Equal determines if two network handles refer to the same network +// namespace. It is only implemented on Linux. +func (ns NsHandle) Equal(_ NsHandle) bool { + return false +} + +// String shows the file descriptor number and its dev and inode. +// It is only implemented on Linux, and returns "NS(none)" on other +// platforms. +func (ns NsHandle) String() string { + return "NS(none)" +} + +// UniqueId returns a string which uniquely identifies the namespace +// associated with the network handle. It is only implemented on Linux, +// and returns "NS(none)" on other platforms. +func (ns NsHandle) UniqueId() string { + return "NS(none)" +} + +// IsOpen returns true if Close() has not been called. It is only implemented +// on Linux and always returns false on other platforms. +func (ns NsHandle) IsOpen() bool { + return false +} + +// Close closes the NsHandle and resets its file descriptor to -1. +// It is only implemented on Linux. +func (ns *NsHandle) Close() error { + return nil +} + +// None gets an empty (closed) NsHandle. +func None() NsHandle { + return NsHandle(-1) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 4415252529..d4c711c2f7 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -919,8 +919,8 @@ github.com/ugorji/go/codec ## explicit; go 1.12 github.com/vishvananda/netlink github.com/vishvananda/netlink/nl -# github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 -## explicit; go 1.12 +# github.com/vishvananda/netns v0.0.5-0.20240412164733-9469873f4601 +## explicit; go 1.17 github.com/vishvananda/netns # github.com/vmihailenco/msgpack v4.0.4+incompatible ## explicit @@ -1573,7 +1573,7 @@ yunion.io/x/log/hooks yunion.io/x/ovsdb/cli_util yunion.io/x/ovsdb/schema/ovn_nb yunion.io/x/ovsdb/types -# yunion.io/x/pkg v1.10.1-0.20240416041628-5e17b8728132 +# yunion.io/x/pkg v1.10.1-0.20240424071144-ff6b95d2046d ## explicit; go 1.18 yunion.io/x/pkg/appctx yunion.io/x/pkg/errors diff --git a/vendor/yunion.io/x/pkg/util/netutils/http.go b/vendor/yunion.io/x/pkg/util/netutils/http.go new file mode 100644 index 0000000000..cc9feb6435 --- /dev/null +++ b/vendor/yunion.io/x/pkg/util/netutils/http.go @@ -0,0 +1,40 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package netutils + +import ( + "net/http" + "strings" +) + +func GetHttpRequestIp(r *http.Request) string { + ipStr := r.Header.Get("X-Forwarded-For") + if len(ipStr) > 0 { + ipList := strings.Split(ipStr, ",") + if len(ipList) > 0 { + return ipList[0] + } + } + ipStr = r.Header.Get("X-Real-Ip") + if len(ipStr) > 0 { + return ipStr + } + ipStr = r.RemoteAddr + colonPos := strings.Index(ipStr, ":") + if colonPos > 0 { + ipStr = ipStr[:colonPos] + } + return ipStr +}