get.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package cluster
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/authz"
  5. "github.com/porter-dev/porter/api/server/handlers"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/server/shared/apierrors"
  8. "github.com/porter-dev/porter/api/server/shared/config"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/internal/kubernetes"
  11. "github.com/porter-dev/porter/internal/kubernetes/domain"
  12. "github.com/porter-dev/porter/internal/models"
  13. )
  14. type ClusterGetHandler struct {
  15. handlers.PorterHandlerWriter
  16. authz.KubernetesAgentGetter
  17. }
  18. func NewClusterGetHandler(
  19. config *config.Config,
  20. writer shared.ResultWriter,
  21. ) *ClusterGetHandler {
  22. return &ClusterGetHandler{
  23. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  24. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  25. }
  26. }
  27. func (c *ClusterGetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  29. res := &types.ClusterGetResponse{
  30. Cluster: cluster.ToClusterType(),
  31. }
  32. agent, err := c.GetAgent(r, cluster, "")
  33. if err != nil {
  34. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  35. return
  36. }
  37. endpoint, found, ingressErr := domain.GetNGINXIngressServiceIP(agent.Clientset)
  38. if found {
  39. res.IngressIP = endpoint
  40. }
  41. if !found && ingressErr != nil {
  42. res.IngressError = kubernetes.CatchK8sConnectionError(ingressErr).Externalize()
  43. }
  44. c.WriteResult(w, r, res)
  45. }