Create Cluster Member @Tags clusters @Summary Create Cluster Member @Description Create Cluster Member @Accept json @Produce json @Param cluster path string true "集群名称" @Param request body Member true "request" @Success 200 {object} Member @Security ApiKeyAuth @Router /clusters/{cluster}/members [
()
| 215 | // @Security ApiKeyAuth |
| 216 | // @Router /clusters/{cluster}/members [post] |
| 217 | func (h *Handler) CreateClusterMember() iris.Handler { |
| 218 | return func(ctx *context.Context) { |
| 219 | name := ctx.Params().GetString("name") |
| 220 | var req Member |
| 221 | err := ctx.ReadJSON(&req) |
| 222 | if err != nil { |
| 223 | ctx.StatusCode(iris.StatusBadRequest) |
| 224 | ctx.Values().Set("message", err.Error()) |
| 225 | return |
| 226 | } |
| 227 | if req.Name == "" { |
| 228 | ctx.StatusCode(iris.StatusBadRequest) |
| 229 | ctx.Values().Set("message", "username can not be none") |
| 230 | return |
| 231 | } |
| 232 | if len(req.ClusterRoles) == 0 && len(req.NamespaceRoles) == 0 { |
| 233 | ctx.StatusCode(iris.StatusBadRequest) |
| 234 | ctx.Values().Set("message", "must select one role") |
| 235 | return |
| 236 | } |
| 237 | u := ctx.Values().Get("profile") |
| 238 | profile := u.(session.UserProfile) |
| 239 | binding := v1Cluster.Binding{ |
| 240 | BaseModel: v1.BaseModel{ |
| 241 | Kind: "ClusterBinding", |
| 242 | CreatedBy: profile.Name, |
| 243 | }, |
| 244 | Metadata: v1.Metadata{ |
| 245 | Name: fmt.Sprintf("%s-%s-cluster-binding", name, req.Name), |
| 246 | }, |
| 247 | UserRef: req.Name, |
| 248 | ClusterRef: name, |
| 249 | } |
| 250 | |
| 251 | tx, _ := server.DB().Begin(true) |
| 252 | c, err := h.clusterService.Get(name, common.DBOptions{DB: tx}) |
| 253 | if err != nil { |
| 254 | ctx.StatusCode(iris.StatusInternalServerError) |
| 255 | ctx.Values().Set("message", fmt.Sprintf("get cluster failed: %s", err.Error())) |
| 256 | return |
| 257 | } |
| 258 | |
| 259 | k := kubernetes.NewKubernetes(c) |
| 260 | cert, err := k.CreateCommonUser(req.Name) |
| 261 | if err != nil { |
| 262 | _ = tx.Rollback() |
| 263 | ctx.StatusCode(iris.StatusInternalServerError) |
| 264 | ctx.Values().Set("message", fmt.Sprintf("create common user failed: %s", err.Error())) |
| 265 | return |
| 266 | } |
| 267 | binding.Certificate = cert |
| 268 | if err := h.clusterBindingService.CreateClusterBinding(&binding, common.DBOptions{DB: tx}); err != nil { |
| 269 | _ = tx.Rollback() |
| 270 | ctx.StatusCode(iris.StatusInternalServerError) |
| 271 | ctx.Values().Set("message", "unable to complete authorization") |
| 272 | return |
| 273 | } |
| 274 | // 创建clusterrolebinding |
no test coverage detected