Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: operator 支持上报 helmcharts 版本 --story=121174631 #665

Merged
4 changes: 1 addition & 3 deletions pkg/collector/example/parsepprof/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Shopify/sarama v1.32.0 h1:P+RUjEaRU0GMMbYexGMDyrMkLhbbBVUVISDywi+IlFU=
github.com/Shopify/sarama v1.32.0/go.mod h1:+EmJJKZWVT/faR9RcOxJerP+LId4iWdQPBGLy1Y1Njs=
Expand Down Expand Up @@ -334,8 +333,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU=
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c=
Expand Down
44 changes: 44 additions & 0 deletions pkg/operator/common/promfmt/promfmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 promfmt

import "io"

type Metric struct {
Name string
Labels []Label
}

type Label struct {
Name string
Value string
}

func FmtBytes(w io.Writer, metrics ...Metric) {
for _, metric := range metrics {
w.Write([]byte(metric.Name))
w.Write([]byte(`{`))

var n int
for _, label := range metric.Labels {
if n > 0 {
w.Write([]byte(`,`))
}
n++
w.Write([]byte(label.Name))
w.Write([]byte(`="`))
w.Write([]byte(label.Value))
w.Write([]byte(`"`))
}

w.Write([]byte("} 1"))
w.Write([]byte("\n"))
}
}
71 changes: 71 additions & 0 deletions pkg/operator/common/promfmt/promfmt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 promfmt

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMetricsToPrometheusFormat(t *testing.T) {
t.Run("Labels/Count=2", func(t *testing.T) {
rows := []Metric{
{
Name: "usage",
Labels: []Label{
{Name: "cpu", Value: "1"},
{Name: "biz", Value: "0"},
},
},
{
Name: "usage",
Labels: []Label{
{Name: "cpu", Value: "2"},
{Name: "biz", Value: "0"},
},
},
}

buf := &bytes.Buffer{}
FmtBytes(buf, rows...)

expected := `usage{cpu="1",biz="0"} 1
usage{cpu="2",biz="0"} 1
`
assert.Equal(t, expected, buf.String())
})

t.Run("Labels/Count=1", func(t *testing.T) {
rows := []Metric{
{
Name: "usage",
Labels: []Label{
{Name: "cpu", Value: "1"},
},
},
{
Name: "usage",
Labels: []Label{
{Name: "cpu", Value: "2"},
},
},
}

buf := &bytes.Buffer{}
FmtBytes(buf, rows...)

expected := `usage{cpu="1"} 1
usage{cpu="2"} 1
`
assert.Equal(t, expected, buf.String())
})
}
99 changes: 99 additions & 0 deletions pkg/operator/operator/helmcharts/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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 helmcharts

import (
"context"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"

"github.com/TencentBlueKing/bkmonitor-datalink/pkg/operator/common/define"
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/operator/configs"
)

var helmchartsRevision = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: define.MonitorNamespace,
Name: "helm_charts_revision",
Help: "helm charts revision",
},
[]string{"name", "namespace", "updated", "status", "chart", "app_version"},
)

func newMetricMonitor() *metricMonitor {
return &metricMonitor{}
}

type metricMonitor struct{}

func (m *metricMonitor) SetHelmChartsRevision(element ReleaseElement) {
helmchartsRevision.WithLabelValues(
element.Name,
element.Namespace,
element.Updated,
element.Status,
element.Chart,
element.AppVersion,
).Set(float64(element.Revision))
}

type Controller struct {
ctx context.Context
cancel context.CancelFunc
objects *Objects
mm *metricMonitor
}

func NewController(ctx context.Context, client kubernetes.Interface) (*Controller, error) {
labelOptions := informers.WithTweakListOptions(func(opts *metav1.ListOptions) {
opts.LabelSelector = "owner=helm"
})

namespace := informers.WithNamespace(configs.G().MonitorNamespace) // TODO(mando): 目前仅监听 operator 组件所处 namespace
sharedInformer := informers.NewSharedInformerFactoryWithOptions(client, define.ReSyncPeriod, namespace, labelOptions)

ctx, cancel := context.WithCancel(ctx)
objs, err := newHelmChartsObjects(ctx, sharedInformer)
if err != nil {
cancel()
return nil, err
}

return &Controller{
ctx: ctx,
cancel: cancel,
objects: objs,
mm: newMetricMonitor(),
}, nil
}

func (c *Controller) UpdateMetrics() {
c.objects.Range(func(ele ReleaseElement) {
c.mm.SetHelmChartsRevision(ele)
})
}

func (c *Controller) Stop() {
c.cancel()
}

func (c *Controller) GetByNamespace(namespace string) []ReleaseElement {
var eles []ReleaseElement
c.objects.Range(func(ele ReleaseElement) {
if ele.Namespace == namespace {
eles = append(eles, ele)
}
})
return eles
}
Loading