Skip to content

Commit

Permalink
feat: windows pluign use tcp listen
Browse files Browse the repository at this point in the history
  • Loading branch information
ifooth committed Oct 15, 2024
1 parent 1fe7fff commit 0daf15f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
14 changes: 2 additions & 12 deletions build/nodemanPlugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
Expand All @@ -36,14 +35,14 @@ 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"
)

const (
defaultConfigPath = "../etc/bkbscp.conf"
pidFile = "bkbscp.pid"
unitSocketFile = "bkbscp.sock"
logFile = "bkbscp.log"
defaultLogMaxSize = 500 // megabytes
defaultLogMaxBackups = 3
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
13 changes: 0 additions & 13 deletions build/nodemanPlugin/main_windows.go

This file was deleted.

34 changes: 34 additions & 0 deletions internal/util/net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build !windows

Check failure on line 1 in internal/util/net.go

View workflow job for this annotation

GitHub Actions / lint

Missed header for check (goheader)

/*
* 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)
}
25 changes: 25 additions & 0 deletions internal/util/net_windows.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 0daf15f

Please sign in to comment.