Skip to content

Commit

Permalink
feat: add annotations for Prometheus (#6037)
Browse files Browse the repository at this point in the history
  • Loading branch information
csuzhangxc authored Jan 16, 2025
1 parent d0fa980 commit bb7a088
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 4 deletions.
4 changes: 3 additions & 1 deletion pkg/controllers/pd/tasks/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (

const (
defaultReadinessProbeInitialDelaySeconds = 5

metricsPath = "/metrics"
)

func TaskPod(state *ReconcileContext, c client.Client) task.Task {
Expand Down Expand Up @@ -182,7 +184,7 @@ func newPod(cluster *v1alpha1.Cluster, pd *v1alpha1.PD, configHash string) *core
})
}

anno := maputil.Copy(pd.GetAnnotations())
anno := maputil.Merge(pd.GetAnnotations(), k8s.AnnoProm(pd.GetClientPort(), metricsPath))
// TODO: should not inherit all labels and annotations into pod
delete(anno, v1alpha1.AnnoKeyInitialClusterNum)
pod := &corev1.Pod{
Expand Down
4 changes: 3 additions & 1 deletion pkg/controllers/tidb/tasks/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const (
// defaultReadinessProbeInitialDelaySeconds is the default initial delay seconds for readiness probe.
// This is the same value as TiDB Operator v1.
defaultReadinessProbeInitialDelaySeconds = 10

metricsPath = "/metrics"
)

func TaskPod(state *ReconcileContext, c client.Client) task.Task {
Expand Down Expand Up @@ -225,7 +227,7 @@ func newPod(cluster *v1alpha1.Cluster,
v1alpha1.LabelKeyInstance: tidb.Name,
v1alpha1.LabelKeyConfigHash: configHash,
}),
Annotations: maputil.Copy(tidb.GetAnnotations()),
Annotations: maputil.Merge(tidb.GetAnnotations(), k8s.AnnoProm(tidb.GetStatusPort(), metricsPath)),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(tidb, v1alpha1.SchemeGroupVersion.WithKind("TiDB")),
},
Expand Down
8 changes: 7 additions & 1 deletion pkg/controllers/tiflash/tasks/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import (
"github.com/pingcap/tidb-operator/pkg/utils/task/v3"
)

const (
metricsPath = "/metrics"
)

func TaskPod(state *ReconcileContext, c client.Client) task.Task {
return task.NameTaskFunc("Pod", func(ctx context.Context) task.Result {
logger := logr.FromContextOrDiscard(ctx)
Expand Down Expand Up @@ -142,7 +146,9 @@ func newPod(cluster *v1alpha1.Cluster, tiflash *v1alpha1.TiFlash, configHash str
v1alpha1.LabelKeyInstance: tiflash.Name,
v1alpha1.LabelKeyConfigHash: configHash,
}),
Annotations: maputil.Copy(tiflash.GetAnnotations()),
Annotations: maputil.Merge(tiflash.GetAnnotations(),
k8s.AnnoProm(tiflash.GetMetricsPort(), metricsPath),
k8s.AnnoAdditionalProm("tiflash.proxy", tiflash.GetProxyStatusPort())),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(tiflash, v1alpha1.SchemeGroupVersion.WithKind("TiFlash")),
},
Expand Down
4 changes: 3 additions & 1 deletion pkg/controllers/tikv/tasks/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (
MinGracePeriodSeconds = 30
// Assume that approximately 200 regions are transferred for 1s
RegionsPerSecond = 200

metricsPath = "/metrics"
)

func TaskSuspendPod(state *ReconcileContext, c client.Client) task.Task {
Expand Down Expand Up @@ -195,7 +197,7 @@ func newPod(cluster *v1alpha1.Cluster, tikv *v1alpha1.TiKV, configHash string) *
v1alpha1.LabelKeyInstance: tikv.Name,
v1alpha1.LabelKeyConfigHash: configHash,
}),
Annotations: maputil.Copy(tikv.GetAnnotations()),
Annotations: maputil.Merge(tikv.GetAnnotations(), k8s.AnnoProm(tikv.GetStatusPort(), metricsPath)),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(tikv, v1alpha1.SchemeGroupVersion.WithKind("TiKV")),
},
Expand Down
35 changes: 35 additions & 0 deletions pkg/utils/k8s/annotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 k8s

import "fmt"

// AnnoProm returns the prometheus annotations for a pod.
func AnnoProm(port int32, path string) map[string]string {
return map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": fmt.Sprintf("%d", port),
"prometheus.io/path": path,
}
}

// AnnoAdditionalProm returns the additional prometheus annotations for a pod.
// Some pods may have multiple prometheus endpoints.
// We assume the same path is used for all endpoints.
func AnnoAdditionalProm(name string, port int32) map[string]string {
return map[string]string{
fmt.Sprintf("%s.prometheus.io/port", name): fmt.Sprintf("%d", port),
}
}
35 changes: 35 additions & 0 deletions pkg/utils/k8s/annotation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 k8s

import (
"testing"

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

func TestPromAnno(t *testing.T) {
anno := AnnoProm(8234, "/metrics")
require.Equal(t, map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": "8234",
"prometheus.io/path": "/metrics",
}, anno)

annoAdditional := AnnoAdditionalProm("tiflash.proxy", 20292)
require.Equal(t, map[string]string{
"tiflash.proxy.prometheus.io/port": "20292",
}, annoAdditional)
}

0 comments on commit bb7a088

Please sign in to comment.