k8s.go 1000 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. v1 "k8s.io/api/core/v1"
  8. )
  9. // GetK8sNamespacesResponse is the list of namespaces returned when a
  10. // user has successfully authenticated
  11. type GetK8sNamespacesResponse v1.NamespaceList
  12. // GetK8sNamespaces gets a namespaces list in a k8s cluster
  13. func (c *Client) GetK8sNamespaces(
  14. ctx context.Context,
  15. projectID uint,
  16. clusterID uint,
  17. ) (*GetK8sNamespacesResponse, error) {
  18. cl := fmt.Sprintf("%d", clusterID)
  19. req, err := http.NewRequest(
  20. "GET",
  21. fmt.Sprintf("%s/projects/%d/k8s/namespaces?"+url.Values{
  22. "cluster_id": []string{cl},
  23. }.Encode(), c.BaseURL, projectID),
  24. nil,
  25. )
  26. if err != nil {
  27. return nil, err
  28. }
  29. req = req.WithContext(ctx)
  30. bodyResp := &GetK8sNamespacesResponse{}
  31. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  32. if httpErr != nil {
  33. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  34. }
  35. return nil, err
  36. }
  37. return bodyResp, nil
  38. }