This is an automated email from the ASF dual-hosted git repository.
hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks-controller.git
The following commit(s) were added to refs/heads/unstable by this push:
new 25b77af Add import cluster API for adding existing clusters (#120)
25b77af is described below
commit 25b77afd140eb4b5c61166d6d24966607cb9ad23
Author: hulk <[email protected]>
AuthorDate: Mon Oct 9 19:30:53 2023 +0800
Add import cluster API for adding existing clusters (#120)
This API is used to help users import their existing clusters into the
Kvrocks controller,
the flow is like the create API but cluster node information comes from
existing nodes.
For now, we allow passing the existing node list but only parses it from
the first one,
maybe we can retry other nodes if the first one is not broken.
---
docs/API.md | 46 +++++++++++++++++++++++++++++++++++++++++++++-
server/cluster.go | 38 ++++++++++++++++++++++++++++++++++++++
server/route.go | 1 +
3 files changed, 84 insertions(+), 1 deletion(-)
diff --git a/docs/API.md b/docs/API.md
index ce7673c..3367460 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -168,6 +168,50 @@ GET /api/v1/namespaces/{namespace}/clusters
}
```
+### Import Cluster
+
+This API is used to import the cluster from the existing Kvrocks cluster's
nodes.
+
+```shell
+GET /api/v1/namespaces/{namespace}/clusters/{cluster}/import
+```
+
+#### Request Body
+
+```json
+{
+ "nodes":["127.0.0.1:6666"],
+ "password":""
+}
+```
+
+#### Response JSON Body
+
+* 201
+```json
+{
+ "data": "created"
+}
+```
+
+* 409
+```json
+{
+ "error": {
+ "message": "the entry already existed"
+ }
+}
+```
+
+* 5XX
+```json
+{
+ "error": {
+ "message": "DETAIL ERROR STRING"
+ }
+}
+```
+
### Get Cluster
```shell
@@ -613,7 +657,7 @@ In this case, it only migrates slot distributions between
shards and the data wi
So you MUST ensure that the data is already migrated before you call this API.
```shell
-POST
/api/v1/namespaces/{namespace}/clusters/{cluster}/shards/migration/slot_data
+POST
/api/v1/namespaces/{namespace}/clusters/{cluster}/shards/migration/slot_only
```
#### Request Body
diff --git a/server/cluster.go b/server/cluster.go
index 9685dcd..13d3fbc 100644
--- a/server/cluster.go
+++ b/server/cluster.go
@@ -166,6 +166,44 @@ func (handler *ClusterHandler) Remove(c *gin.Context) {
responseOK(c, "ok")
}
+func (hander *ClusterHandler) Import(c *gin.Context) {
+ namespace := c.Param("namespace")
+ clusterName := c.Param("cluster")
+ var req struct {
+ Nodes []string `json:"nodes"`
+ Password string `json:"password"`
+ }
+ if err := c.BindJSON(&req); err != nil {
+ responseBadRequest(c, err)
+ return
+ }
+ if len(req.Nodes) == 0 {
+ responseBadRequest(c, errors.New("nodes should NOT be empty"))
+ return
+ }
+
+ clusterNodesStr, err := util.ClusterNodesCmd(c, &metadata.NodeInfo{
+ Addr: req.Nodes[0],
+ Password: req.Password,
+ })
+ if err != nil {
+ responseError(c, err)
+ return
+ }
+ clusterInfo, err := metadata.ParseCluster(clusterNodesStr)
+ if err != nil {
+ responseError(c, err)
+ return
+ }
+
+ clusterInfo.Name = clusterName
+ if err := hander.storage.CreateCluster(c, namespace, clusterInfo); err
!= nil {
+ responseError(c, err)
+ return
+ }
+ responseOK(c, "ok")
+}
+
func (handler *ClusterHandler) GetFailOverTasks(c *gin.Context) {
namespace := c.Param("namespace")
cluster := c.Param("cluster")
diff --git a/server/route.go b/server/route.go
index 732acce..c9e9a89 100644
--- a/server/route.go
+++ b/server/route.go
@@ -67,6 +67,7 @@ func (srv *Server) initHandlers() {
clusters.GET("", cluster.List)
clusters.GET("/:cluster", cluster.Get)
clusters.POST("", cluster.Create)
+ clusters.POST("/:cluster/import", cluster.Import)
clusters.DELETE("/:cluster", cluster.Remove)
clusters.GET("/:cluster/failover/:type",
cluster.GetFailOverTasks)
}