|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/go-chi/chi" |
| 11 | + httputil "github.com/goodrain/rainbond/util/http" |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | +) |
| 14 | + |
| 15 | +// KubeBlocksController - |
| 16 | +type KubeBlocksController struct{} |
| 17 | + |
| 18 | +// blockMechanicaBaseURL base URL for block-mechanica service |
| 19 | +const blockMechanicaBaseURL = "http://kb-adapter-rbdplugin.rbd-system.svc:80" |
| 20 | + |
| 21 | +// GetSupportedDatabases get KubeBlocks supported databases list |
| 22 | +func (c *KubeBlocksController) GetSupportedDatabases(w http.ResponseWriter, r *http.Request) { |
| 23 | + c.forwardRequest(w, r, "/v1/addons", "GET") |
| 24 | +} |
| 25 | + |
| 26 | +// GetStorageClasses get KubeBlocks storage classes list |
| 27 | +func (c *KubeBlocksController) GetStorageClasses(w http.ResponseWriter, r *http.Request) { |
| 28 | + c.forwardRequest(w, r, "/v1/storageclasses", "GET") |
| 29 | +} |
| 30 | + |
| 31 | +// GetBackupRepos get KubeBlocks backup repositories list |
| 32 | +func (c *KubeBlocksController) GetBackupRepos(w http.ResponseWriter, r *http.Request) { |
| 33 | + c.forwardRequest(w, r, "/v1/backuprepos", "GET") |
| 34 | +} |
| 35 | + |
| 36 | +// CreateCluster create KubeBlocks database cluster |
| 37 | +func (c *KubeBlocksController) CreateCluster(w http.ResponseWriter, r *http.Request) { |
| 38 | + c.forwardRequest(w, r, "/v1/clusters", "POST") |
| 39 | +} |
| 40 | + |
| 41 | +// GetClusterConnectInfos get KubeBlocks clusters connection infos |
| 42 | +func (c *KubeBlocksController) GetClusterConnectInfos(w http.ResponseWriter, r *http.Request) { |
| 43 | + c.forwardRequest(w, r, "/v1/clusters/connect-infos", "GET") |
| 44 | +} |
| 45 | + |
| 46 | +// GetClusterByID get KubeBlocks cluster by service ID |
| 47 | +func (c *KubeBlocksController) GetClusterByID(w http.ResponseWriter, r *http.Request) { |
| 48 | + serviceID := chi.URLParam(r, "service_id") |
| 49 | + c.forwardRequest(w, r, fmt.Sprintf("/v1/clusters/%s", serviceID), "GET") |
| 50 | +} |
| 51 | + |
| 52 | +// ExpansionCluster update KubeBlocks cluster |
| 53 | +func (c *KubeBlocksController) ExpansionCluster(w http.ResponseWriter, r *http.Request) { |
| 54 | + serviceID := chi.URLParam(r, "service_id") |
| 55 | + c.forwardRequest(w, r, fmt.Sprintf("/v1/clusters/%s", serviceID), "PUT") |
| 56 | +} |
| 57 | + |
| 58 | +// UpdateClusterBackupSchedules update KubeBlocks cluster backup schedules |
| 59 | +func (c *KubeBlocksController) UpdateClusterBackupSchedules(w http.ResponseWriter, r *http.Request) { |
| 60 | + serviceID := chi.URLParam(r, "service_id") |
| 61 | + c.forwardRequest(w, r, fmt.Sprintf("/v1/clusters/%s/backup-schedules", serviceID), "PUT") |
| 62 | +} |
| 63 | + |
| 64 | +// CreateClusterBackup create KubeBlocks cluster backup |
| 65 | +func (c *KubeBlocksController) CreateClusterBackup(w http.ResponseWriter, r *http.Request) { |
| 66 | + serviceID := chi.URLParam(r, "service_id") |
| 67 | + c.forwardRequest(w, r, fmt.Sprintf("/v1/clusters/%s/backups", serviceID), "POST") |
| 68 | +} |
| 69 | + |
| 70 | +// GetClusterBackups get KubeBlocks cluster backups |
| 71 | +func (c *KubeBlocksController) GetClusterBackups(w http.ResponseWriter, r *http.Request) { |
| 72 | + serviceID := chi.URLParam(r, "service_id") |
| 73 | + c.forwardRequest(w, r, fmt.Sprintf("/v1/clusters/%s/backups", serviceID), "GET") |
| 74 | +} |
| 75 | + |
| 76 | +// DeleteClusters delete KubeBlocks clusters |
| 77 | +func (c *KubeBlocksController) DeleteClusters(w http.ResponseWriter, r *http.Request) { |
| 78 | + c.forwardRequest(w, r, "/v1/clusters", "DELETE") |
| 79 | +} |
| 80 | + |
| 81 | +// DeleteClusterBackup delete KubeBlocks cluster backup |
| 82 | +func (c *KubeBlocksController) DeleteClusterBackup(w http.ResponseWriter, r *http.Request) { |
| 83 | + serviceID := chi.URLParam(r, "service_id") |
| 84 | + c.forwardRequest(w, r, fmt.Sprintf("/v1/clusters/%s/backups", serviceID), "DELETE") |
| 85 | +} |
| 86 | + |
| 87 | +// ManageCluster forwards to block-mechanica to manage cluster lifecycle |
| 88 | +func (c *KubeBlocksController) ManageCluster(w http.ResponseWriter, r *http.Request) { |
| 89 | + logrus.Infof("ManageCluster request: %v", r.Body) |
| 90 | + c.forwardRequest(w, r, "/v1/clusters/actions", "POST") |
| 91 | +} |
| 92 | + |
| 93 | +// GetClusterPodDetail forwards to block-mechanica to get pod details managed by Cluster |
| 94 | +func (c *KubeBlocksController) GetClusterPodDetail(w http.ResponseWriter, r *http.Request) { |
| 95 | + serviceID := chi.URLParam(r, "service_id") |
| 96 | + podName := chi.URLParam(r, "pod_name") |
| 97 | + c.forwardRequest(w, r, fmt.Sprintf("/v1/clusters/%s/pods/%s/details", serviceID, podName), "GET") |
| 98 | +} |
| 99 | + |
| 100 | +// GetClusterEvents forwards to block-mechanica to get cluster's events |
| 101 | +func (c *KubeBlocksController) GetClusterEvents(w http.ResponseWriter, r *http.Request) { |
| 102 | + serviceID := chi.URLParam(r, "service_id") |
| 103 | + c.forwardRequest(w, r, fmt.Sprintf("/v1/clusters/%s/events", serviceID), "GET") |
| 104 | +} |
| 105 | + |
| 106 | +func (c *KubeBlocksController) GetClusterParameters(w http.ResponseWriter, r *http.Request) { |
| 107 | + serviceID := chi.URLParam(r, "service_id") |
| 108 | + c.forwardRequest(w, r, fmt.Sprintf("/v1/clusters/%s/parameters", serviceID), "GET") |
| 109 | +} |
| 110 | + |
| 111 | +func (c *KubeBlocksController) ChangeClusterParameters(w http.ResponseWriter, r *http.Request) { |
| 112 | + serviceID := chi.URLParam(r, "service_id") |
| 113 | + c.forwardRequest(w, r, fmt.Sprintf("/v1/clusters/%s/parameters", serviceID), "POST") |
| 114 | +} |
| 115 | + |
| 116 | +func (c *KubeBlocksController) RestoreClusterFromBackup(w http.ResponseWriter, r *http.Request) { |
| 117 | + serviceID := chi.URLParam(r, "service_id") |
| 118 | + c.forwardRequest(w, r, fmt.Sprintf("/v1/clusters/%s/restores", serviceID), "POST") // TODO |
| 119 | +} |
| 120 | + |
| 121 | +// forwardRequest helper function to forward requests to Block Mechanica |
| 122 | +func (c *KubeBlocksController) forwardRequest(w http.ResponseWriter, r *http.Request, api, method string) { |
| 123 | + // 构建URL |
| 124 | + targetURL := blockMechanicaBaseURL + api |
| 125 | + if r.URL.RawQuery != "" { |
| 126 | + targetURL += "?" + r.URL.RawQuery |
| 127 | + } |
| 128 | + logrus.Debugf("request block-mechanica service: %s %s", method, targetURL) |
| 129 | + |
| 130 | + var req *http.Request |
| 131 | + var err error |
| 132 | + |
| 133 | + // 读取请求体 |
| 134 | + var body io.Reader |
| 135 | + requestBody, readErr := io.ReadAll(r.Body) |
| 136 | + if readErr != nil { |
| 137 | + logrus.Errorf("read request body failed: %v", readErr) |
| 138 | + httputil.ReturnError(r, w, 400, "read request body failed: "+readErr.Error()) |
| 139 | + return |
| 140 | + } |
| 141 | + if len(requestBody) > 0 { |
| 142 | + body = strings.NewReader(string(requestBody)) |
| 143 | + } |
| 144 | + req, err = http.NewRequest(method, targetURL, body) |
| 145 | + |
| 146 | + if err != nil { |
| 147 | + logrus.Errorf("create request to block-mechanica failed: %v", err) |
| 148 | + httputil.ReturnError(r, w, 500, "create request to block-mechanica failed: "+err.Error()) |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + // Set headers |
| 153 | + for key, values := range r.Header { |
| 154 | + for _, value := range values { |
| 155 | + req.Header.Set(key, value) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + if req.Header.Get("Content-Type") == "" && (method == "POST" || method == "PUT" || method == "DELETE") { |
| 160 | + req.Header.Set("Content-Type", "application/json") |
| 161 | + } |
| 162 | + |
| 163 | + client := &http.Client{} |
| 164 | + resp, err := client.Do(req) |
| 165 | + if err != nil { |
| 166 | + logrus.Errorf("request block-mechanica service failed: %v", err) |
| 167 | + httputil.ReturnError(r, w, 500, "request block-mechanica service failed: "+err.Error()) |
| 168 | + return |
| 169 | + } |
| 170 | + defer resp.Body.Close() |
| 171 | + |
| 172 | + if resp.StatusCode != http.StatusOK { |
| 173 | + body, _ := io.ReadAll(resp.Body) |
| 174 | + logrus.Errorf("block-mechanica service returned error status code %d: %s", resp.StatusCode, string(body)) |
| 175 | + httputil.ReturnError(r, w, resp.StatusCode, "block-mechanica service returned error: "+string(body)) |
| 176 | + return |
| 177 | + } |
| 178 | + |
| 179 | + var result map[string]interface{} |
| 180 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 181 | + logrus.Errorf("parse block-mechanica response failed: %v", err) |
| 182 | + httputil.ReturnError(r, w, 500, "parse block-mechanica response failed: "+err.Error()) |
| 183 | + return |
| 184 | + } |
| 185 | + |
| 186 | + if list, exists := result["list"]; exists { |
| 187 | + // 尝试提取分页信息,存在分页信息则 ReturnList |
| 188 | + if total, pageNum, ok := extractPaginationInfo(result); ok { |
| 189 | + httputil.ReturnList(r, w, total, pageNum, list) |
| 190 | + return |
| 191 | + } |
| 192 | + |
| 193 | + httputil.ReturnSuccess(r, w, list) |
| 194 | + } else if field, exists := result["bean"]; exists { |
| 195 | + httputil.ReturnSuccess(r, w, field) |
| 196 | + } else { |
| 197 | + httputil.ReturnSuccess(r, w, result) |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +// extractPaginationInfo 从结果中提取分页信息 |
| 202 | +func extractPaginationInfo(result map[string]interface{}) (total, pageNum int, ok bool) { |
| 203 | + number, hasNumber := result["number"] |
| 204 | + if !hasNumber { |
| 205 | + return 0, 0, false |
| 206 | + } |
| 207 | + |
| 208 | + page, hasPage := result["page"] |
| 209 | + if !hasPage { |
| 210 | + return 0, 0, false |
| 211 | + } |
| 212 | + |
| 213 | + total, totalOk := safeToInt(number) |
| 214 | + if !totalOk { |
| 215 | + return 0, 0, false |
| 216 | + } |
| 217 | + |
| 218 | + pageNum, pageOk := safeToInt(page) |
| 219 | + if !pageOk { |
| 220 | + return 0, 0, false |
| 221 | + } |
| 222 | + |
| 223 | + return total, pageNum, true |
| 224 | +} |
| 225 | + |
| 226 | +// safeToInt 安全地将 interface{} 转换为 int,支持多种数字类型 |
| 227 | +func safeToInt(value interface{}) (int, bool) { |
| 228 | + switch v := value.(type) { |
| 229 | + case int: |
| 230 | + return v, true |
| 231 | + case int32: |
| 232 | + return int(v), true |
| 233 | + case int64: |
| 234 | + return int(v), true |
| 235 | + case float32: |
| 236 | + return int(v), true |
| 237 | + case float64: |
| 238 | + return int(v), true |
| 239 | + case json.Number: |
| 240 | + if i, err := v.Int64(); err == nil { |
| 241 | + return int(i), true |
| 242 | + } |
| 243 | + } |
| 244 | + logrus.Warnf("Failed to convert %T(%v) to int", value, value) |
| 245 | + return 0, false |
| 246 | +} |
0 commit comments