Skip to content

Commit

Permalink
feat: get machineid as client uid (TencentBlueKing#54)
Browse files Browse the repository at this point in the history
* feat: get machineid as client uid

* feat: get machineid as client uid

* feat: get fringerprint from /proc if can not get machineid
  • Loading branch information
AlkaidChan authored Dec 27, 2023
1 parent 78d6b08 commit 742d111
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
24 changes: 11 additions & 13 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,21 @@ type Client interface {

// Client is the bscp client
type client struct {
pairs map[string]string
opts options
fingerPrint sfs.FingerPrint
watcher *watcher
upstream upstream.Upstream
pairs map[string]string
opts options
watcher *watcher
upstream upstream.Upstream
}

// New return a bscp client instance
func New(opts ...Option) (Client, error) {
clientOpt := &options{}
fp, err := sfs.GetFingerPrint()
fp, err := util.GenerateFingerPrint()
if err != nil {
return nil, fmt.Errorf("get instance fingerprint failed, err: %s", err.Error())
return nil, fmt.Errorf("generate instance fingerprint failed, err: %s", err.Error())
}
logger.Info("instance fingerprint", slog.String("fingerprint", fp.Encode()))
clientOpt.fingerprint = fp.Encode()
logger.Info("instance fingerprint", slog.String("fingerprint", fp))
clientOpt.fingerprint = fp
clientOpt.uid = clientOpt.fingerprint
for _, opt := range opts {
if e := opt(clientOpt); e != nil {
Expand Down Expand Up @@ -102,10 +101,9 @@ func New(opts ...Option) (Client, error) {
return nil, fmt.Errorf("init upstream client failed, err: %s", err.Error())
}
c := &client{
opts: *clientOpt,
fingerPrint: fp,
upstream: u,
pairs: pairs,
opts: *clientOpt,
upstream: u,
pairs: pairs,
}
// handshake
vas, _ := c.buildVas()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/Tencent/bk-bcs/bcs-common v0.0.0-20230912015319-acb7495967f5
github.com/TencentBlueking/bk-bcs/bcs-services/bcs-bscp v0.0.0-20231226133937-7be33dc8c268
github.com/denisbrodbeck/machineid v1.0.1
github.com/dustin/go-humanize v1.0.1
github.com/fsnotify/fsnotify v1.7.0
github.com/olekukonko/tablewriter v0.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/denisbrodbeck/machineid v1.0.1 h1:geKr9qtkB876mXguW2X6TU4ZynleN6ezuMSRhl4D7AQ=
github.com/denisbrodbeck/machineid v1.0.1/go.mod h1:dJUwb7PTidGDeYyUBmXZ2GphQBbjJCrnectwCyxcUSI=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
Expand Down
36 changes: 36 additions & 0 deletions internal/util/fingerprint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 defines the common util function.
package util

import (
"fmt"

sfs "github.com/TencentBlueking/bk-bcs/bcs-services/bcs-bscp/pkg/sf-share"
"github.com/denisbrodbeck/machineid"
)

// GenerateFingerPrint generate finger print
func GenerateFingerPrint() (string, error) {
// 1. try to get machine id
machineID, err := machineid.ID()
if err == nil {
return machineID, nil
}
// 2. try to get id from
fp, err := sfs.GetFingerPrint()
if err == nil {
return fp.Encode(), nil
}
return "", fmt.Errorf("failed to generate fingerprint, err %s", err.Error())
}

0 comments on commit 742d111

Please sign in to comment.