Skip to content

Commit

Permalink
feat: oeprator 支持自定义 workload labels --story=118965469 (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx authored Aug 2, 2024
1 parent e3145a8 commit 0462fd7
Show file tree
Hide file tree
Showing 15 changed files with 525 additions and 142 deletions.
55 changes: 54 additions & 1 deletion pkg/operator/common/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

package feature

import "strings"
import (
"strings"
)

const (
// labels features
Expand All @@ -26,6 +28,8 @@ const (
keyRelabelIndex = "relabelIndex"
keyMonitorMatchSelector = "monitorMatchSelector"
keyMonitorDropSelector = "monitorDropSelector"
keyLabelJoinMatcher = "labelJoinMatcher"
keySliMonitor = "sliMonitor"
)

func isMapKeyExists(m map[string]string, key string) bool {
Expand All @@ -50,6 +54,47 @@ func parseSelector(s string) map[string]string {
return selector
}

type LabelJoinMatcherSpec struct {
Kind string
Annotations []string
Labels []string
}

// parseLabelJoinMatcher 解析 labeljoin 规则
// Kind://[label:custom_label|annotation:custom_annotation,...]
func parseLabelJoinMatcher(s string) *LabelJoinMatcherSpec {
const (
annotationPrefix = "annotation:"
labelPrefix = "label:"
)

switch {
case strings.HasPrefix(s, "Pod://"): // TODO(mando): 目前仅支持 Pod
s = s[len("Pod://"):]
default:
return nil
}

var annotations []string
var labels []string
parts := strings.Split(s, ",")
for _, part := range parts {
k := strings.TrimSpace(part)
switch {
case strings.HasPrefix(k, annotationPrefix):
annotations = append(annotations, strings.TrimSpace(k[len(annotationPrefix):]))
case strings.HasPrefix(k, labelPrefix):
labels = append(labels, strings.TrimSpace(k[len(labelPrefix):]))
}
}

return &LabelJoinMatcherSpec{
Kind: "Pod",
Annotations: annotations,
Labels: labels,
}
}

// IfCommonResource 检查 DataID 是否为 common 类型
func IfCommonResource(m map[string]string) bool {
return isMapKeyExists(m, keyCommonResource)
Expand Down Expand Up @@ -98,3 +143,11 @@ func MonitorMatchSelector(m map[string]string) map[string]string {
func MonitorDropSelector(m map[string]string) map[string]string {
return parseSelector(m[keyMonitorDropSelector])
}

func LabelJoinMatcher(m map[string]string) *LabelJoinMatcherSpec {
return parseLabelJoinMatcher(m[keyLabelJoinMatcher])
}

func SliMonitor(m map[string]string) string {
return m[keySliMonitor]
}
25 changes: 25 additions & 0 deletions pkg/operator/common/feature/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,28 @@ func TestParseSelector(t *testing.T) {
assert.Equal(t, c.output, parseSelector(c.input))
}
}

func TestParseLabelJoinMatcher(t *testing.T) {
cases := []struct {
input string
annotations []string
labels []string
}{
{
input: "Pod://annotation:biz.service,annotation:biz.set,label:zone.key1,label:zone.key2",
annotations: []string{"biz.service", "biz.set"},
labels: []string{"zone.key1", "zone.key2"},
},
{
input: "Pod:// annotation: biz.service, annotation: biz.set, label: zone.key1, label: zone.key2",
annotations: []string{"biz.service", "biz.set"},
labels: []string{"zone.key1", "zone.key2"},
},
}

for _, c := range cases {
matcher := parseLabelJoinMatcher(c.input)
assert.Equal(t, c.annotations, matcher.Annotations)
assert.Equal(t, c.labels, matcher.Labels)
}
}
24 changes: 24 additions & 0 deletions pkg/operator/common/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Tencent is pleased to support the open source community by making
// 蓝鲸智云 - 监控平台 (BlueKing - Monitor) available.
// Copyright (C) 2022 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 utils

import "strings"

func SplitTrim(s, sep string) []string {
if s == "" {
return nil
}

var ret []string
for _, part := range strings.Split(s, sep) {
ret = append(ret, strings.TrimSpace(part))
}
return ret
}
4 changes: 3 additions & 1 deletion pkg/operator/operator/discover/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

bkv1beta1 "github.com/TencentBlueKing/bkmonitor-datalink/pkg/operator/apis/crd/v1beta1"
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/operator/common/define"
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/operator/common/feature"
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/operator/common/k8sutils"
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/operator/common/labelspool"
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/operator/common/notifier"
Expand Down Expand Up @@ -150,6 +151,7 @@ type BaseParams struct {
MetricRelabelConfigs []yaml.MapSlice
MatchSelector map[string]string
DropSelector map[string]string
LabelJoinMatcher *feature.LabelJoinMatcherSpec
}

type BaseDiscover struct {
Expand Down Expand Up @@ -413,6 +415,7 @@ func (d *BaseDiscover) makeMetricTarget(lbls, origLabels labels.Labels, namespac
metricTarget.RelabelRule = d.RelabelRule
metricTarget.RelabelIndex = d.RelabelIndex
metricTarget.NormalizeMetricName = d.NormalizeMetricName
metricTarget.LabelJoinMatcher = d.LabelJoinMatcher

return metricTarget, nil
}
Expand Down Expand Up @@ -654,7 +657,6 @@ func (d *BaseDiscover) handleTargetGroup(targetGroup *targetgroup.Group) {
continue
}
if childConfig == nil {
d.mm.IncCreatedChildConfigSkippedCounter()
d.cache.Set(namespace, tlset, targetGroup.Labels)
continue
}
Expand Down
26 changes: 0 additions & 26 deletions pkg/operator/operator/discover/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,6 @@ var (
[]string{"name"},
)

discoverCreatedChildConfigSkippedTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: define.MonitorNamespace,
Name: "discover_created_config_skipped_total",
Help: "discover created child config skipped total",
},
[]string{"name"},
)

discoverCreatedChildConfigCachedTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: define.MonitorNamespace,
Expand All @@ -88,15 +79,6 @@ var (
},
[]string{"name"},
)

discoverSkippedTgSourceTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: define.MonitorNamespace,
Name: "discover_skipped_tg_source_total",
Help: "discover skipped tg source total",
},
[]string{"name"},
)
)

func newMetricMonitor(name string) *metricMonitor {
Expand All @@ -123,10 +105,6 @@ func (m *metricMonitor) IncCreatedChildConfigFailedCounter() {
discoverCreatedChildConfigFailedTotal.WithLabelValues(m.name).Inc()
}

func (m *metricMonitor) IncCreatedChildConfigSkippedCounter() {
discoverCreatedChildConfigSkippedTotal.WithLabelValues(m.name).Inc()
}

func (m *metricMonitor) IncCreatedChildConfigCachedCounter() {
discoverCreatedChildConfigCachedTotal.WithLabelValues(m.name).Inc()
}
Expand All @@ -138,7 +116,3 @@ func (m *metricMonitor) IncHandledTgCounter() {
func (m *metricMonitor) IncDeletedTgSourceCounter() {
discoverDeletedTgSourceTotal.WithLabelValues(m.name).Inc()
}

func (m *metricMonitor) IncSkippedTgSourceCounter() {
discoverSkippedTgSourceTotal.WithLabelValues(m.name).Inc()
}
1 change: 0 additions & 1 deletion pkg/operator/operator/discover/shared_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func (sd *sharedDiscovery) start() {
if !ok {
// 第一次记录且没有 targets 则跳过
if tg == nil || len(tg.Targets) == 0 {
sd.mm.IncSkippedTgSourceCounter()
logger.Infof("sharedDiscovery %s skip tg source '%s'", sd.id, tg.Source)
continue
}
Expand Down
29 changes: 16 additions & 13 deletions pkg/operator/operator/objectsref/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ type Object struct {
OwnerRefs []OwnerRef

// Pod 属性
NodeName string
Labels map[string]string
PodIP string
NodeName string
PodIP string
Labels map[string]string
Annotations map[string]string

// Containers
Containers []string
Expand Down Expand Up @@ -359,11 +360,12 @@ func newPodObjects(ctx context.Context, sharedInformer informers.SharedInformerF
Name: pod.Name,
Namespace: pod.Namespace,
},
OwnerRefs: toRefs(pod.OwnerReferences),
NodeName: pod.Spec.NodeName,
Labels: pod.Labels,
PodIP: pod.Status.PodIP,
Containers: toContainers(pod.Spec.Containers),
OwnerRefs: toRefs(pod.OwnerReferences),
NodeName: pod.Spec.NodeName,
Labels: pod.Labels,
Annotations: pod.Annotations,
PodIP: pod.Status.PodIP,
Containers: toContainers(pod.Spec.Containers),
})
},
UpdateFunc: func(_, newObj interface{}) {
Expand All @@ -377,11 +379,12 @@ func newPodObjects(ctx context.Context, sharedInformer informers.SharedInformerF
Name: pod.Name,
Namespace: pod.Namespace,
},
OwnerRefs: toRefs(pod.OwnerReferences),
NodeName: pod.Spec.NodeName,
Labels: pod.Labels,
PodIP: pod.Status.PodIP,
Containers: toContainers(pod.Spec.Containers),
OwnerRefs: toRefs(pod.OwnerReferences),
NodeName: pod.Spec.NodeName,
Labels: pod.Labels,
Annotations: pod.Annotations,
PodIP: pod.Status.PodIP,
Containers: toContainers(pod.Spec.Containers),
})
},
DeleteFunc: func(obj interface{}) {
Expand Down
Loading

0 comments on commit 0462fd7

Please sign in to comment.