Skip to content

Commit 1f59638

Browse files
authored
Merge pull request #618 from oceanbase/ds_0.3.2_release
2 parents b826a0b + 34b1a35 commit 1f59638

File tree

11 files changed

+69
-62
lines changed

11 files changed

+69
-62
lines changed

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/oceanbase-dashboard/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ type: application
1515
# This is the chart version. This version number should be incremented each time you make changes
1616
# to the chart and its templates, including the app version.
1717
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18-
version: 0.3.1
18+
version: 0.3.2
1919

2020
# This is the version number of the application being deployed. This version number should be
2121
# incremented each time you make changes to the application. Versions are not expected to
2222
# follow Semantic Versioning. They should reflect the version the application is using.
2323
# It is recommended to use it with quotes.
24-
appVersion: "0.3.1"
24+
appVersion: "0.3.2"

charts/oceanbase-dashboard/templates/cluster-role.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ rules:
4242
- endpoints
4343
- pods
4444
verbs: ["get", "list", "watch"]
45+
- apiGroups: ["metrics.k8s.io"]
46+
resources:
47+
- nodes
48+
- nodes/proxy
49+
- services
50+
- endpoints
51+
- pods
52+
verbs: ["get", "list", "watch"]
4553
- apiGroups:
4654
- extensions
4755
resources:

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ require (
4444
k8s.io/apimachinery v0.29.3
4545
k8s.io/client-go v0.29.3
4646
k8s.io/kubernetes v1.27.2
47+
k8s.io/metrics v0.0.0
4748
sigs.k8s.io/controller-runtime v0.15.0
4849
)
4950

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,8 @@ k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 h1:BZqlfIlq5YbRMFko6/PM7F
10041004
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340/go.mod h1:yD4MZYeKMBwQKVht279WycxKyM84kkAx2DPrTXaeb98=
10051005
k8s.io/kubernetes v1.27.2 h1:g4v9oY6u7vBUDEuq4FvC50Bbw2K7GZuvM00IIESWVf4=
10061006
k8s.io/kubernetes v1.27.2/go.mod h1:U8ZXeKBAPxeb4J4/HOaxjw1A9K6WfSH+fY2SS7CR6IM=
1007+
k8s.io/metrics v0.29.3 h1:nN+eavbMQ7Kuif2tIdTr2/F2ec2E/SIAWSruTZ+Ye6U=
1008+
k8s.io/metrics v0.29.3/go.mod h1:kb3tGGC4ZcIDIuvXyUE291RwJ5WmDu0tB4wAVZM6h2I=
10071009
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
10081010
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
10091011
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=

internal/dashboard/business/k8s/k8s.go

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
storagev1 "k8s.io/api/storage/v1"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
"k8s.io/apimachinery/pkg/runtime/schema"
25+
metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
2526

2627
"github.com/oceanbase/ob-operator/internal/dashboard/business/common"
2728
"github.com/oceanbase/ob-operator/internal/dashboard/business/constant"
@@ -105,34 +106,21 @@ func extractNodeConditions(node *corev1.Node) []response.K8sNodeCondition {
105106
return conditions
106107
}
107108

108-
func extractNodeResource(ctx context.Context, node *corev1.Node) *response.K8sNodeResource {
109+
func extractNodeResource(metricsMap map[string]metricsv1beta1.NodeMetrics, node *corev1.Node) *response.K8sNodeResource {
109110
nodeResource := &response.K8sNodeResource{}
111+
metrics, ok := metricsMap[node.Name]
110112
nodeResource.CpuTotal = node.Status.Capacity.Cpu().AsApproximateFloat64()
111113
nodeResource.MemoryTotal = node.Status.Capacity.Memory().AsApproximateFloat64() / constant.GB
112-
podList, err := resource.ListAllPods(ctx)
113-
if err == nil {
114-
cpuRequested := 0.0
115-
memoryRequested := 0.0
116-
for _, pod := range podList.Items {
117-
if !strings.Contains(pod.Spec.NodeName, node.Name) {
118-
continue
119-
}
120-
for _, container := range pod.Spec.Containers {
121-
cpuRequest, found := container.Resources.Requests[ResourceCpu]
122-
if found {
123-
cpuRequested += cpuRequest.AsApproximateFloat64()
124-
}
125-
memoryRequest, found := container.Resources.Requests[ResourceMemory]
126-
if found {
127-
memoryRequested += memoryRequest.AsApproximateFloat64() / constant.GB
128-
}
129-
}
114+
if ok {
115+
if cpuUsed, ok := metrics.Usage[corev1.ResourceCPU]; ok {
116+
nodeResource.CpuUsed = cpuUsed.AsApproximateFloat64()
117+
}
118+
if memoryUsed, ok := metrics.Usage[corev1.ResourceMemory]; ok {
119+
nodeResource.MemoryUsed = memoryUsed.AsApproximateFloat64() / constant.GB
130120
}
131-
nodeResource.CpuUsed = cpuRequested
132-
nodeResource.MemoryUsed = memoryRequested
121+
nodeResource.CpuFree = nodeResource.CpuTotal - nodeResource.CpuUsed
122+
nodeResource.MemoryFree = nodeResource.MemoryTotal - nodeResource.MemoryUsed
133123
}
134-
nodeResource.CpuFree = nodeResource.CpuTotal - nodeResource.CpuUsed
135-
nodeResource.MemoryFree = nodeResource.MemoryTotal - nodeResource.MemoryUsed
136124
return nodeResource
137125
}
138126

@@ -222,6 +210,7 @@ func ListEvents(ctx context.Context, queryEventParam *param.QueryEventParam) ([]
222210
func ListNodes(ctx context.Context) ([]response.K8sNode, error) {
223211
nodes := make([]response.K8sNode, 0)
224212
nodeList, err := resource.ListNodes(ctx)
213+
nodeMetricsMap, metricsErr := resource.ListNodeMetrics(ctx)
225214
if err == nil {
226215
for _, node := range nodeList.Items {
227216
internalAddress, externalAddress := extractNodeAddress(&node)
@@ -240,28 +229,21 @@ func ListNodes(ctx context.Context) ([]response.K8sNode, error) {
240229
CRI: node.Status.NodeInfo.ContainerRuntimeVersion,
241230
}
242231

232+
nodeResource := &response.K8sNodeResource{}
233+
if metricsErr == nil {
234+
nodeResource = extractNodeResource(nodeMetricsMap, &node)
235+
} else {
236+
logger.Errorf("Got error when list node metrics, err: %v", metricsErr)
237+
}
243238
nodes = append(nodes, response.K8sNode{
244239
Info: nodeInfo,
245-
Resource: extractNodeResource(ctx, &node),
240+
Resource: nodeResource,
246241
})
247242
}
248243
}
249244
return nodes, err
250245
}
251246

252-
func ListNodeResources(ctx context.Context) ([]response.K8sNodeResource, error) {
253-
nodeList, err := resource.ListNodes(ctx)
254-
if err != nil {
255-
return nil, err
256-
}
257-
nodeResources := make([]response.K8sNodeResource, 0, len(nodeList.Items))
258-
for _, node := range nodeList.Items {
259-
nodeResource := extractNodeResource(ctx, &node)
260-
nodeResources = append(nodeResources, *nodeResource)
261-
}
262-
return nodeResources, nil
263-
}
264-
265247
func ListStorageClasses(ctx context.Context) ([]response.StorageClass, error) {
266248
storageClasses := make([]response.StorageClass, 0)
267249
storageClassList, err := resource.ListStorageClasses(ctx)

internal/dashboard/business/metric/constant/metric_constant.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ See the Mulan PSL v2 for more details.
1313
package constant
1414

1515
const (
16-
MetricConfigFile = "internal/assets/metric.yaml"
17-
MetricConfigFileEnUS = "internal/assets/metric_en_US.yaml"
18-
MetricConfigFileZhCN = "internal/assets/metric_zh_CN.yaml"
19-
MetricExprConfigFile = "internal/assets/metric_expr.yaml"
16+
MetricConfigFile = "internal/assets/dashboard/metric.yaml"
17+
MetricConfigFileEnUS = "internal/assets/dashboard/metric_en_US.yaml"
18+
MetricConfigFileZhCN = "internal/assets/dashboard/metric_zh_CN.yaml"
19+
MetricExprConfigFile = "internal/assets/dashboard/metric_expr.yaml"
2020
)
2121

2222
const (

make/dashboard.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PROJECT=oceanbase-dashboard
44
PROCESSOR=4
55
PWD ?= $(shell pwd)
66

7-
DASHBOARD_VERSION ?= 0.3.1
7+
DASHBOARD_VERSION ?= 0.3.2
88
DASHBOARD_IMG ?= quay.io/oceanbase/oceanbase-dashboard:${DASHBOARD_VERSION}
99
COMMIT_HASH ?= $(shell git rev-parse --short HEAD)
1010
BUILD_TIMESTAMP ?= $(shell date '+%Y%m%d%H%M%S')
@@ -31,7 +31,7 @@ dashboard-build: dashboard-bindata-gen dashboard-doc-gen ## Build oceanbase-dash
3131

3232
.PHONY: dashboard-bindata-gen
3333
dashboard-bindata-gen: dashboard-dep-install ## Generate bindata
34-
go-bindata -o internal/dashboard/generated/bindata/bindata.go -pkg bindata internal/assets/...
34+
go-bindata -o internal/dashboard/generated/bindata/bindata.go -pkg bindata internal/assets/dashboard/...
3535

3636
.PHONY: dashboard-clean
3737
dashboard-clean: ## Clean build

pkg/k8s/client/client.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ import (
2121
"k8s.io/client-go/kubernetes"
2222
"k8s.io/client-go/metadata"
2323
"k8s.io/client-go/rest"
24+
"k8s.io/metrics/pkg/client/clientset/versioned"
2425
)
2526

2627
type Client struct {
27-
ClientSet *kubernetes.Clientset
28-
DynamicClient dynamic.Interface
29-
MetaClient metadata.Interface
30-
config *rest.Config
28+
ClientSet *kubernetes.Clientset
29+
DynamicClient dynamic.Interface
30+
MetaClient metadata.Interface
31+
MetricsClientset *versioned.Clientset
32+
config *rest.Config
3133
}
3234

3335
var client *Client
@@ -75,11 +77,16 @@ func getClientFromConfig(config *rest.Config) (*Client, error) {
7577
if err != nil {
7678
return nil, errors.Wrap(err, "failed to create meta client")
7779
}
80+
metricsClientset, err := versioned.NewForConfig(config)
81+
if err != nil {
82+
return nil, errors.Wrap(err, "failed to create metrics client")
83+
}
7884
return &Client{
79-
ClientSet: clientset,
80-
DynamicClient: dynamicClient,
81-
MetaClient: metaClient,
82-
config: config,
85+
ClientSet: clientset,
86+
DynamicClient: dynamicClient,
87+
MetaClient: metaClient,
88+
MetricsClientset: metricsClientset,
89+
config: config,
8390
}, nil
8491
}
8592

pkg/k8s/resource/node.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
corev1 "k8s.io/api/core/v1"
1919
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
2021

2122
"github.com/oceanbase/ob-operator/pkg/k8s/client"
2223
k8sconst "github.com/oceanbase/ob-operator/pkg/k8s/constants"
@@ -30,3 +31,17 @@ func ListNodes(ctx context.Context) (*corev1.NodeList, error) {
3031
TimeoutSeconds: &timeout,
3132
})
3233
}
34+
35+
func ListNodeMetrics(ctx context.Context) (map[string]metricsv1beta1.NodeMetrics, error) {
36+
client := client.GetClient()
37+
nodeMetricsMap := make(map[string]metricsv1beta1.NodeMetrics)
38+
metricsList, err := client.MetricsClientset.MetricsV1beta1().NodeMetricses().List(ctx, metav1.ListOptions{
39+
TimeoutSeconds: &timeout,
40+
})
41+
if err == nil {
42+
for _, metrics := range metricsList.Items {
43+
nodeMetricsMap[metrics.Name] = metrics
44+
}
45+
}
46+
return nodeMetricsMap, err
47+
}

0 commit comments

Comments
 (0)