Skip to content

Commit

Permalink
opt: add some app.kubernetes.io/* labels back (#6047)
Browse files Browse the repository at this point in the history
  • Loading branch information
csuzhangxc authored Jan 17, 2025
1 parent 65c3a1d commit 47d6ecc
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 74 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/pd/tasks/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func newPod(cluster *v1alpha1.Cluster, pd *v1alpha1.PD, configHash string) *core
Labels: maputil.Merge(pd.Labels, map[string]string{
v1alpha1.LabelKeyInstance: pd.Name,
v1alpha1.LabelKeyConfigHash: configHash,
}),
}, k8s.LabelsK8sApp(cluster.Name, v1alpha1.LabelValComponentPD)),
Annotations: anno,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(pd, v1alpha1.SchemeGroupVersion.WithKind("PD")),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/tidb/tasks/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func newPod(cluster *v1alpha1.Cluster,
Labels: maputil.Merge(tidb.Labels, map[string]string{
v1alpha1.LabelKeyInstance: tidb.Name,
v1alpha1.LabelKeyConfigHash: configHash,
}),
}, k8s.LabelsK8sApp(cluster.Name, v1alpha1.LabelValComponentTiDB)),
Annotations: maputil.Merge(tidb.GetAnnotations(), k8s.AnnoProm(tidb.GetStatusPort(), metricsPath)),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(tidb, v1alpha1.SchemeGroupVersion.WithKind("TiDB")),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/tiflash/tasks/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func newPod(cluster *v1alpha1.Cluster, tiflash *v1alpha1.TiFlash, configHash str
Labels: maputil.Merge(tiflash.Labels, map[string]string{
v1alpha1.LabelKeyInstance: tiflash.Name,
v1alpha1.LabelKeyConfigHash: configHash,
}),
}, k8s.LabelsK8sApp(cluster.Name, v1alpha1.LabelValComponentTiFlash)),
Annotations: maputil.Merge(tiflash.GetAnnotations(),
k8s.AnnoProm(tiflash.GetMetricsPort(), metricsPath),
k8s.AnnoAdditionalProm("tiflash.proxy", tiflash.GetProxyStatusPort())),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/tikv/tasks/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func newPod(cluster *v1alpha1.Cluster, tikv *v1alpha1.TiKV, configHash string) *
Labels: maputil.Merge(tikv.Labels, map[string]string{
v1alpha1.LabelKeyInstance: tikv.Name,
v1alpha1.LabelKeyConfigHash: configHash,
}),
}, k8s.LabelsK8sApp(cluster.Name, v1alpha1.LabelValComponentTiKV)),
Annotations: maputil.Merge(tikv.GetAnnotations(), k8s.AnnoProm(tikv.GetStatusPort(), metricsPath)),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(tikv, v1alpha1.SchemeGroupVersion.WithKind("TiKV")),
Expand Down
35 changes: 0 additions & 35 deletions pkg/utils/k8s/annotation.go

This file was deleted.

35 changes: 0 additions & 35 deletions pkg/utils/k8s/annotation_test.go

This file was deleted.

30 changes: 30 additions & 0 deletions pkg/utils/k8s/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,33 @@ func GetResourceRequirements(req v1alpha1.ResourceRequirements) corev1.ResourceR
}
return ret
}

// 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),
}
}

// LabelsForTidbCluster returns some labels with "app.kubernetes.io" prefix.
// NOTE: these labels are deprecated and they are used for TiDB Operator v1 compatibility.
// If you are developing a new feature, please use labels with "pingcap.com" prefix instead.
func LabelsK8sApp(cluster, component string) map[string]string {
return map[string]string{
"app.kubernetes.io/component": component,
"app.kubernetes.io/instance": cluster,
"app.kubernetes.io/managed-by": "tidb-operator",
"app.kubernetes.io/name": "tidb-cluster",
}
}
24 changes: 24 additions & 0 deletions pkg/utils/k8s/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,27 @@ func TestGetResourceRequirements(t *testing.T) {
assert.Equal(t, res.Requests[corev1.ResourceMemory], memory)
assert.Equal(t, res.Limits[corev1.ResourceMemory], memory)
}

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)
}

func TestLabelsK8sApp(t *testing.T) {
labels := LabelsK8sApp("db", "tikv")
require.Equal(t, map[string]string{
"app.kubernetes.io/component": "tikv",
"app.kubernetes.io/instance": "db",
"app.kubernetes.io/managed-by": "tidb-operator",
"app.kubernetes.io/name": "tidb-cluster",
}, labels)
}

0 comments on commit 47d6ecc

Please sign in to comment.