Skip to content

Commit a0cc8bf

Browse files
committed
feat(k8s): 初始化 k8s-dbs 存储集群纳管模块 #10302
1 parent 0222657 commit a0cc8bf

File tree

355 files changed

+30387
-54
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

355 files changed

+30387
-54
lines changed

.gtmproject.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ project:
3333
- sqlserver
3434
- dbm-services
3535
- helm-charts
36+
- k8s-dbs
3637
- other

dbm-services/go.work

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
go 1.24.2
22

33
use (
4+
k8s-dbs
45
bigdata/db-tools/dbactuator
56
common/bkdata-kafka-consumer
67
common/celery-service

dbm-services/go.work.sum

+527-54
Large diffs are not rendered by default.

dbm-services/k8s-dbs/.golangci.yaml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
linters-settings:
2+
lll:
3+
line-length: 120
4+
funlen:
5+
lines: 80
6+
statements: 80
7+
gocyclo:
8+
min-complexity: 20
9+
gocritic:
10+
enabled-checks:
11+
- nestingReduce
12+
13+
run:
14+
# default concurrency is a available CPU number
15+
concurrency: 4
16+
# timeout for analysis, e.g. 30s, 5m, default is 1m
17+
timeout: 2m
18+
# exit code when at least one issue was found, default is 1
19+
issues-exit-code: 1
20+
# include test files or not, default is true
21+
tests: false
22+
23+
issues:
24+
exclude-use-default: true
25+
exclude-files:
26+
- ".*/mock/.*.go"
27+
- ".*testing.go"
28+
29+
linters:
30+
disable-all: true
31+
enable:
32+
- funlen
33+
- goconst
34+
- gocyclo
35+
- gofmt
36+
- ineffassign
37+
- staticcheck
38+
- typecheck
39+
- goimports
40+
- revive
41+
- gosimple
42+
- govet
43+
- lll
44+
- rowserrcheck
45+
- errcheck
46+
- unused
47+
- sqlclosecheck
48+
- gocritic
49+
- bodyclose
50+
51+
fast: false

dbm-services/k8s-dbs/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# k8s-dbs
2+
3+
k8s-dbs 是一个基于 Go 客户端和 KubeBlocks API 构建的数据库管控服务,使用 Gin 框架开发。 该服务提供了一系列 RESTful API,允许用户在 Kubernetes 集群中基于 KubeBlocks 组件轻松部署、 管理和操作数据库集群。主要功能包括数据库集群的创建、删除、缩放、启动、停止、重启和升级等。
4+
[说明文档](README.md)
5+
6+
## 快速开始
7+
### 环境要求
8+
- Go 语言环境(版本 >= 1.18)
9+
- Kubernetes 集群(版本 >= 1.20)
10+
- KubeBlocks 已安装到 Kubernetes 集群——[KubeBlocks安装说明](https://cn.kubeblocks.io/docs/preview/user-docs/installation/install-kubeblocks)

dbm-services/k8s-dbs/cmd/server.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
TencentBlueKing is pleased to support the open source community by making
3+
蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
4+
5+
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
6+
7+
Licensed under the MIT License (the "License");
8+
you may not use this file except in compliance with the License.
9+
10+
You may obtain a copy of the License at
11+
https://opensource.org/licenses/MIT
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package main
21+
22+
import (
23+
"context"
24+
"errors"
25+
"fmt"
26+
"k8s-dbs/core"
27+
"k8s-dbs/core/client"
28+
"k8s-dbs/router"
29+
"log"
30+
"log/slog"
31+
"net/http"
32+
"os"
33+
"os/signal"
34+
"syscall"
35+
"time"
36+
37+
"github.com/gin-gonic/gin"
38+
)
39+
40+
// main 函数是程序的入口点,执行以下步骤:
41+
// 1. 初始化系统核心配置
42+
// 2. 创建并配置 Gin 路由引擎
43+
// 3. 启动 HTTP 服务并监听终止信号
44+
// 4. 在接收到终止信号时优雅关闭服务器
45+
func main() {
46+
slog.Info("Start initial configuration...")
47+
48+
if err := core.Init(); err != nil {
49+
log.Fatalf("Failed to initialize core: %v", err)
50+
}
51+
52+
r := router.NewRouter(client.Db.GormDb)
53+
54+
slog.Info("Finish initial configuration...")
55+
56+
startServer(r.Engine)
57+
}
58+
59+
// startServer 启动 HTTP 服务并处理优雅关闭
60+
func startServer(r *gin.Engine) {
61+
server := &http.Server{
62+
Addr: ":8000",
63+
Handler: r,
64+
}
65+
66+
go func() {
67+
slog.Info("Start server...")
68+
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
69+
slog.Error("Failed to start server", "error", err)
70+
}
71+
}()
72+
73+
quit := make(chan os.Signal, 1)
74+
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
75+
<-quit
76+
77+
slog.Info("Shutdown Server ...")
78+
79+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
80+
defer cancel()
81+
if err := server.Shutdown(ctx); err != nil {
82+
slog.Error("Server forced to shutdown", "error", err)
83+
panic(fmt.Errorf("fatal error: %w", err)) // 触发 panic
84+
}
85+
86+
slog.Info("Server exited properly")
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
TencentBlueKing is pleased to support the open source community by making
3+
蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
4+
5+
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
6+
7+
Licensed under the MIT License (the "License");
8+
you may not use this file except in compliance with the License.
9+
10+
You may obtain a copy of the License at
11+
https://opensource.org/licenses/MIT
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package api
21+
22+
import (
23+
"k8s-dbs/core/entity"
24+
25+
"github.com/gin-gonic/gin"
26+
)
27+
28+
const HealthCheckURL = "/common/health"
29+
30+
// HealthCheck 集群健康检查 API
31+
func HealthCheck(ctx *gin.Context) {
32+
entity.SuccessResponse(ctx, nil, "OK")
33+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
TencentBlueKing is pleased to support the open source community by making
3+
蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
4+
5+
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
6+
7+
Licensed under the MIT License (the "License");
8+
you may not use this file except in compliance with the License.
9+
10+
You may obtain a copy of the License at
11+
https://opensource.org/licenses/MIT
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package api
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
TencentBlueKing is pleased to support the open source community by making
3+
蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
4+
5+
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
6+
7+
Licensed under the MIT License (the "License");
8+
you may not use this file except in compliance with the License.
9+
10+
You may obtain a copy of the License at
11+
https://opensource.org/licenses/MIT
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package utils
21+
22+
import (
23+
"fmt"
24+
"time"
25+
)
26+
27+
// 常量定义常用的日期时间格式
28+
const (
29+
LayoutRFC3339 = time.RFC3339 // RFC3339 格式
30+
LayoutISO8601 = "2006-01-02T15:04:05Z07:00" // ISO8601 格式
31+
LayoutYYYYMMDD = "2006-01-02" // 年-月-日 格式
32+
LayoutYYYYMMDDHHMM = "2006-01-02 15:04" // 年-月-日 时:分 格式
33+
LayoutYYYYMMDDHHMMSS = "2006-01-02 15:04:05" // 年-月-日 时:分:秒 格式
34+
LayoutYYYYMMDDHHMMSSZ = "2006-01-02 15:04:05Z" // 年-月-日 时:分:秒 Z 格式
35+
LayoutUnixTimestamp = "1136239445" // Unix 时间戳(秒)
36+
)
37+
38+
// Now 返回当前时间的字符串表示,如果 format 为空,则使用 RFC3339 格式
39+
func Now(format string) string {
40+
if format == "" {
41+
return time.Now().Format(LayoutRFC3339)
42+
}
43+
return time.Now().Format(format)
44+
}
45+
46+
// Parse 将时间字符串解析为 time.Time 对象,如果 format 为空,则使用 RFC3339 格式
47+
// 如果 timeStr 为空,返回错误
48+
func Parse(timeStr, format string) (time.Time, error) {
49+
if timeStr == "" {
50+
return time.Time{}, fmt.Errorf("空的时间字符串")
51+
}
52+
if format == "" {
53+
return time.Parse(LayoutRFC3339, timeStr)
54+
}
55+
return time.Parse(format, timeStr)
56+
}
57+
58+
// Format 将 time.Time 对象格式化为字符串,如果 format 为空,则使用 RFC3339 格式
59+
func Format(t time.Time, format string) string {
60+
if format == "" {
61+
return t.Format(LayoutRFC3339)
62+
}
63+
return t.Format(format)
64+
}
65+
66+
// Diff 计算两个时间之间的差值,并返回秒、分钟、小时和天的差值
67+
// 确保 t1 <= t2
68+
func Diff(t1, t2 time.Time) (seconds, minutes, hours, days int64) {
69+
if t1.After(t2) {
70+
t1, t2 = t2, t1 // 确保 t1 <= t2
71+
}
72+
diff := t2.Sub(t1)
73+
seconds = int64(diff.Seconds())
74+
minutes = seconds / 60
75+
hours = minutes / 60
76+
days = hours / 24
77+
return seconds, minutes, hours, days
78+
}
79+
80+
// Add 给 time.Time 对象添加一个时间间隔,并返回新的时间
81+
func Add(t time.Time, duration time.Duration) time.Time {
82+
return t.Add(duration)
83+
}
84+
85+
// Sub 从 time.Time 对象中减去一个时间间隔,并返回新的时间
86+
func Sub(t time.Time, duration time.Duration) time.Time {
87+
return t.Add(-duration)
88+
}
89+
90+
// UnixTimestamp 返回当前时间的 Unix 时间戳(秒)
91+
func UnixTimestamp() int64 {
92+
return time.Now().Unix()
93+
}
94+
95+
// UnixTimestampMillis 返回当前时间的 Unix 时间戳(毫秒)
96+
func UnixTimestampMillis() int64 {
97+
return time.Now().UnixMilli()
98+
}
99+
100+
// UnixTimestampMicro 返回当前时间的 Unix 时间戳(微秒)
101+
func UnixTimestampMicro() int64 {
102+
return time.Now().UnixMicro()
103+
}
104+
105+
// FromUnixTimestamp 将 Unix 时间戳(秒)转换为 time.Time 对象
106+
func FromUnixTimestamp(timestamp int64) time.Time {
107+
return time.Unix(timestamp, 0)
108+
}
109+
110+
// FromUnixTimestampMillis 将 Unix 时间戳(毫秒)转换为 time.Time 对象
111+
func FromUnixTimestampMillis(timestamp int64) time.Time {
112+
return time.UnixMilli(timestamp)
113+
}
114+
115+
// FromUnixTimestampMicro 将 Unix 时间戳(微秒)转换为 time.Time 对象
116+
func FromUnixTimestampMicro(timestamp int64) time.Time {
117+
return time.UnixMicro(timestamp)
118+
}
119+
120+
// IsZero 检查 time.Time 对象是否为零值(即未设置)
121+
func IsZero(t time.Time) bool {
122+
return t.IsZero()
123+
}
124+
125+
// StartOfDay 返回当天的开始时间(00:00:00)
126+
func StartOfDay(t time.Time) time.Time {
127+
year, month, day := t.Date()
128+
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
129+
}
130+
131+
// EndOfDay 返回当天的结束时间(23:59:59.999999999)
132+
func EndOfDay(t time.Time) time.Time {
133+
year, month, day := t.Date()
134+
return time.Date(year, month, day, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
135+
}
136+
137+
// IsWeekend 判断给定时间是否是周末(周六或周日)
138+
func IsWeekend(t time.Time) bool {
139+
weekday := t.Weekday()
140+
return weekday == time.Saturday || weekday == time.Sunday
141+
}
142+
143+
// NextWorkday 返回当前时间的下一个工作日(跳过周末)
144+
func NextWorkday(t time.Time) time.Time {
145+
for {
146+
t = t.AddDate(0, 0, 1)
147+
if !IsWeekend(t) {
148+
return t
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)