k8s.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. serviceAccountID uint,
  17. clusterID uint,
  18. ) (*GetK8sNamespacesResponse, error) {
  19. sa := fmt.Sprintf("%d", serviceAccountID)
  20. cl := fmt.Sprintf("%d", clusterID)
  21. req, err := http.NewRequest(
  22. "GET",
  23. fmt.Sprintf("%s/projects/%d/k8s/namespaces?"+url.Values{
  24. "service_account_id": []string{sa},
  25. "cluster_id": []string{cl},
  26. }.Encode(), c.BaseURL, projectID),
  27. nil,
  28. )
  29. if err != nil {
  30. return nil, err
  31. }
  32. req = req.WithContext(ctx)
  33. bodyResp := &GetK8sNamespacesResponse{}
  34. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  35. if httpErr != nil {
  36. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  37. }
  38. return nil, err
  39. }
  40. return bodyResp, nil
  41. }