Skip to content

Commit

Permalink
feat: add default labels for client request --tech=118414071 (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
fireyun authored Aug 19, 2024
1 parent 31c23ba commit dd43b24
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 32 deletions.
31 changes: 16 additions & 15 deletions cmd/bscp/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/TencentBlueKing/bscp-go/internal/config"
"github.com/TencentBlueKing/bscp-go/internal/constant"
"github.com/TencentBlueKing/bscp-go/pkg/env"
"github.com/TencentBlueKing/bscp-go/pkg/logger"
)

Expand Down Expand Up @@ -57,28 +58,28 @@ var (

// rootEnvs variable definition
rootEnvs = map[string]string{
"config_file": "BSCP_CONFIG",
"log_level": "log_level",
"config_file": env.BscpConfig,
"log_level": env.LogLevel,
}

// commonEnvs variable definition, viper key => envName
commonEnvs = map[string]string{
"biz": "biz",
"app": "app",
"labels_str": "labels",
"labels_file": "labels_file",
"feed_addrs": "feed_addrs",
"token": "token",
"temp_dir": "temp_dir",
"enable_p2p_download": "enable_p2p_download",
"bk_agent_id": "bk_agent_id",
"cluster_id": "cluster_id",
"pod_id": "pod_id",
"container_name": "container_name",
"biz": env.Biz,
"app": env.App,
"labels_str": env.Labels,
"labels_file": env.LabelsFile,
"feed_addrs": env.FeedAddrs,
"token": env.Token,
"temp_dir": env.TempDir,
"enable_p2p_download": env.EnableP2PDownload,
"bk_agent_id": env.BkAgentID,
"cluster_id": env.ClusterID,
"pod_id": env.PodID,
"container_name": env.ContainerName,
}

watchEnvs = map[string]string{
"port": "port",
"port": env.Port,
}
)

Expand Down
30 changes: 30 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/TencentBlueKing/bscp-go/internal/constant"
"github.com/TencentBlueKing/bscp-go/internal/util"
"github.com/TencentBlueKing/bscp-go/pkg/env"
)

// ClientConfig config for bscp-go when run as daemon
Expand Down Expand Up @@ -142,6 +143,8 @@ func (c *ClientConfig) updateConfLabels() error {
c.Labels = util.MergeLabels(c.Labels, labels)
}

c.setDefaultLabels()

return nil
}

Expand Down Expand Up @@ -186,6 +189,33 @@ func readLabelsFile(path string) (map[string]string, error) {
return fileLabels, nil
}

var defaultLabels = []string{
env.IP,
env.PodName,
env.PodID,
env.NodeName,
env.Namespace,
}

// setDefaultLabels sets default labels
// 不会覆盖已有同名标签,当默认标签没有被显示设置过且存在对应环境变量,则设置默认标签
func (c *ClientConfig) setDefaultLabels() {
for _, label := range defaultLabels {
// 同名标签已存在,不再设置默认标签
if _, ok := c.Labels[label]; ok {
continue
}

if label == env.IP {
c.Labels[label] = util.GetClientIP()
}

if e := os.Getenv(label); e != "" {
c.Labels[label] = e
}
}
}

// ValidateBase validate the watch config
func (c *ClientConfig) ValidateBase() error {
if len(c.FeedAddrs) == 0 {
Expand Down
23 changes: 6 additions & 17 deletions internal/util/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
sfs "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/sf-share"
"golang.org/x/exp/slog"

"github.com/TencentBlueKing/bscp-go/pkg/env"
"github.com/TencentBlueKing/bscp-go/pkg/logger"
)

Expand All @@ -32,30 +33,18 @@ const (
executeShellCmd = "bash"
// executePythonCmd python script executor
executePythonCmd = "python3"

// EnvAppTempDir bscp app temp dir env
// !important: promise of compatibility
EnvAppTempDir = "bk_bscp_app_temp_dir"
// EnvTempDir bscp temp dir env
EnvTempDir = "bk_bscp_temp_dir"
// EnvBiz bscp biz id env
EnvBiz = "bk_bscp_biz"
// EnvApp bscp app name env
EnvApp = "bk_bscp_app"
// EnvRelName bscp release name env
EnvRelName = "bk_bscp_current_version_name"
)

// ExecuteHook executes the hook.
func ExecuteHook(hook *pbhook.HookSpec, hookType table.HookType,
tempDir string, biz uint32, app string, relName string) error {
appTempDir := path.Join(tempDir, fmt.Sprintf("%d/%s", biz, app))
hookEnvs := []string{
fmt.Sprintf("%s=%s", EnvAppTempDir, appTempDir),
fmt.Sprintf("%s=%s", EnvTempDir, tempDir),
fmt.Sprintf("%s=%d", EnvBiz, biz),
fmt.Sprintf("%s=%s", EnvApp, app),
fmt.Sprintf("%s=%s", EnvRelName, relName),
fmt.Sprintf("%s=%s", env.HookAppTempDir, appTempDir),
fmt.Sprintf("%s=%s", env.HookTempDir, tempDir),
fmt.Sprintf("%s=%d", env.HookBiz, biz),
fmt.Sprintf("%s=%s", env.HookApp, app),
fmt.Sprintf("%s=%s", env.HookRelName, relName),
}

hookPath, err := saveContentToFile(appTempDir, hook, hookType, hookEnvs)
Expand Down
67 changes: 67 additions & 0 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 env defines the environment variables.
package env

// !important: promise of compatibility
const (
// HookAppTempDir is environment variable for bk_bscp_app_temp_dir
HookAppTempDir = "bk_bscp_app_temp_dir"
// HookTempDir is environment variable for bk_bscp_temp_dir
HookTempDir = "bk_bscp_temp_dir"
// HookBiz is environment variable for bk_bscp_biz
HookBiz = "bk_bscp_biz"
// HookApp is environment variable for bk_bscp_app
HookApp = "bk_bscp_app"
// HookRelName is environment variable for bk_bscp_current_version_name
HookRelName = "bk_bscp_current_version_name"

// BscpConfig is environment variable for BSCP_CONFIG, keep uppercase for compatibility
BscpConfig = "BSCP_CONFIG"
// LogLevel is environment variable for log_level
LogLevel = "log_level"
// Biz is environment variable for biz
Biz = "biz"
// App is environment variable for app
App = "app"
// Labels is environment variable for labels
Labels = "labels"
// LabelsFile is environment variable for labels_file
LabelsFile = "labels_file"
// FeedAddrs is environment variable for feed_addrs
FeedAddrs = "feed_addrs"
// Token is environment variable for token
Token = "token"
// TempDir is environment variable for temp_dir
TempDir = "temp_dir"
// EnableP2PDownload is environment variable for enable_p2p_download
EnableP2PDownload = "enable_p2p_download"
// BkAgentID is environment variable for bk_agent_id
BkAgentID = "bk_agent_id"
// ClusterID is environment variable for cluster_id
ClusterID = "cluster_id"
// PodID is environment variable for pod_id
PodID = "pod_id"
// PodName is environment variable for pod_name
PodName = "pod_name"
// ContainerName is environment variable for container_name
ContainerName = "container_name"
// NodeName is environment variable for node_name
NodeName = "node_name"
// Namespace is environment variable for name_space
Namespace = "namespace"
// IP is environment variable for ip
IP = "ip"
// Port is environment variable for port
Port = "port"
)

0 comments on commit dd43b24

Please sign in to comment.