MCPcopy
hub / github.com/1Panel-dev/KubePi / SearchClusters

Method SearchClusters

internal/api/v1/cluster/cluster.go:318–388  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

316}
317
318func (h *Handler) SearchClusters() iris.Handler {
319 return func(ctx *context.Context) {
320 pageNum, _ := ctx.Values().GetInt(pkgV1.PageNum)
321 pageSize, _ := ctx.Values().GetInt(pkgV1.PageSize)
322 showExtra := ctx.URLParamExists("showExtra")
323 var conditions commons.SearchConditions
324 if err := ctx.ReadJSON(&conditions); err != nil {
325 ctx.StatusCode(iris.StatusBadRequest)
326 ctx.Values().Set("message", err.Error())
327 return
328 }
329 u := ctx.Values().Get("profile")
330 profile := u.(session.UserProfile)
331 clusters, total, err := h.clusterService.Search(pageNum, pageSize, conditions.Conditions, common.DBOptions{})
332 if err != nil && err != storm.ErrNotFound {
333 ctx.StatusCode(iris.StatusInternalServerError)
334 ctx.Values().Set("message", err.Error())
335 return
336 }
337 result := make([]Cluster, 0)
338 for i := range clusters {
339
340 c := Cluster{Cluster: clusters[i]}
341 if profile.IsAdministrator {
342 c.Accessible = true
343 } else {
344 bs, err := h.clusterBindingService.GetClusterBindingByClusterName(c.Name, common.DBOptions{})
345 if err != nil && !errors.Is(err, storm.ErrNotFound) {
346 ctx.StatusCode(iris.StatusInternalServerError)
347 ctx.Values().Set("message", err.Error())
348 return
349 }
350 c.MemberCount = len(bs)
351 for j := range bs {
352 if bs[j].UserRef == profile.Name {
353 c.Accessible = true
354 }
355 }
356 }
357 result = append(result, c)
358 }
359 if showExtra {
360 ctx1, cancel := goContext.WithTimeout(goContext.Background(), 2*time.Second)
361 defer cancel()
362
363 wg := sync.WaitGroup{}
364 go func(result []Cluster) {
365 for i := range result {
366 if !profile.IsAdministrator && !result[i].Accessible {
367 continue
368 }
369 wg.Add(1)
370 c := kubernetes.NewKubernetes(&result[i].Cluster)
371 go func(i int, ctx1 goContext.Context) {
372 defer wg.Done()
373 info, _ := getExtraClusterInfo(ctx1, c)
374 result[i].ExtraClusterInfo = info
375 }(i, ctx1)

Callers 1

InstallFunction · 0.95

Calls 9

NewKubernetesFunction · 0.92
getExtraClusterInfoFunction · 0.85
sanitizeClusterResponseFunction · 0.85
ErrorMethod · 0.80
GetMethod · 0.65
SearchMethod · 0.65
AddMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected