diff --git a/build/nodemanPlugin/main.go b/build/nodemanPlugin/main.go index 54256d6c6..9ad1611f5 100644 --- a/build/nodemanPlugin/main.go +++ b/build/nodemanPlugin/main.go @@ -17,7 +17,6 @@ import ( "errors" "fmt" "io" - "net" "net/http" "os" "path/filepath" @@ -36,6 +35,7 @@ import ( "github.com/TencentBlueKing/bscp-go/client" "github.com/TencentBlueKing/bscp-go/internal/config" + "github.com/TencentBlueKing/bscp-go/internal/util" "github.com/TencentBlueKing/bscp-go/pkg/logger" "github.com/TencentBlueKing/bscp-go/pkg/metrics" ) @@ -43,7 +43,6 @@ import ( const ( defaultConfigPath = "../etc/bkbscp.conf" pidFile = "bkbscp.pid" - unitSocketFile = "bkbscp.sock" logFile = "bkbscp.log" defaultLogMaxSize = 500 // megabytes defaultLogMaxBackups = 3 @@ -204,7 +203,7 @@ func serveHttp() error { return err } - listen, err := netListen() + listen, err := util.ListenLocal(conf.PidPath) if err != nil { logger.Error("start http server failed", logger.ErrAttr(err)) return err @@ -218,15 +217,6 @@ func serveHttp() error { return nil } -// netListen 默认监听 unix_socket, 可以支持多个实例 -func netListen() (net.Listener, error) { - // 强制清理老的sock文件 - unitSocketPath := filepath.Join(conf.PidPath, unitSocketFile) - _ = os.Remove(unitSocketPath) - - return net.Listen("unix", unitSocketPath) -} - func checkProcess(pidPath string) error { data, err := os.ReadFile(pidPath) if err != nil { diff --git a/build/nodemanPlugin/main_windows.go b/build/nodemanPlugin/main_windows.go deleted file mode 100644 index d4d7a00d5..000000000 --- a/build/nodemanPlugin/main_windows.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import ( - "fmt" - "net" - - "github.com/TencentBlueKing/bscp-go/internal/constant" -) - -// netListen windows 2008/2012/2016 不支持 unix_socket, 使用监听 tcp 方式, 只允许一个实例 -func netListen() (net.Listener, error) { - return net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", constant.DefaultHttpPort)) -} diff --git a/internal/util/net.go b/internal/util/net.go new file mode 100644 index 000000000..6d9c5132a --- /dev/null +++ b/internal/util/net.go @@ -0,0 +1,34 @@ +//go:build !windows + +/* + * Tencent is pleased to support the open source community by making Blueking Container Service available. + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * http://opensource.org/licenses/MIT + * 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 util + +import ( + "net" + "os" + "path/filepath" +) + +const ( + unitSocketFile = "bkbscp.sock" +) + +// ListenLocal 默认监听 unix_socket, 可以支持多个实例 +func ListenLocal(pidPath string) (net.Listener, error) { + // 强制清理老的sock文件 + unitSocketPath := filepath.Join(pidPath, unitSocketFile) + _ = os.Remove(unitSocketPath) + + return net.Listen("unix", unitSocketPath) +} diff --git a/internal/util/net_windows.go b/internal/util/net_windows.go new file mode 100644 index 000000000..df8dbaa5f --- /dev/null +++ b/internal/util/net_windows.go @@ -0,0 +1,25 @@ +/* + * Tencent is pleased to support the open source community by making Blueking Container Service available. + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * http://opensource.org/licenses/MIT + * 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 util + +import ( + "fmt" + "net" + + "github.com/TencentBlueKing/bscp-go/internal/constant" +) + +// ListenLocal windows 2008/2012/2016 不支持 unix_socket, 使用监听 tcp 方式, 只允许一个实例 +func ListenLocal(pidPath string) (net.Listener, error) { + return net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", constant.DefaultHttpPort)) +}