| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- package api
- import (
- "context"
- "fmt"
- "net/http"
- "net/url"
- "github.com/porter-dev/porter/server/api"
- v1 "k8s.io/api/core/v1"
- )
- // GetK8sNamespacesResponse is the list of namespaces returned when a
- // user has successfully authenticated
- type GetK8sNamespacesResponse v1.NamespaceList
- // GetK8sNamespaces gets a namespaces list in a k8s cluster
- func (c *Client) GetK8sNamespaces(
- ctx context.Context,
- projectID uint,
- clusterID uint,
- ) (*GetK8sNamespacesResponse, error) {
- cl := fmt.Sprintf("%d", clusterID)
- req, err := http.NewRequest(
- "GET",
- fmt.Sprintf("%s/projects/%d/k8s/namespaces?"+url.Values{
- "cluster_id": []string{cl},
- }.Encode(), c.BaseURL, projectID),
- nil,
- )
- if err != nil {
- return nil, err
- }
- req = req.WithContext(ctx)
- bodyResp := &GetK8sNamespacesResponse{}
- if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
- if httpErr != nil {
- return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
- }
- return nil, err
- }
- return bodyResp, nil
- }
- // GetKubeconfigResponse is the list of namespaces returned when a
- // user has successfully authenticated
- type GetKubeconfigResponse struct {
- Kubeconfig []byte `json:"kubeconfig"`
- }
- // GetK8sNamespaces gets a namespaces list in a k8s cluster
- func (c *Client) GetKubeconfig(
- ctx context.Context,
- projectID uint,
- clusterID uint,
- ) (*GetKubeconfigResponse, error) {
- cl := fmt.Sprintf("%d", clusterID)
- req, err := http.NewRequest(
- "GET",
- fmt.Sprintf("%s/projects/%d/k8s/kubeconfig?"+url.Values{
- "cluster_id": []string{cl},
- }.Encode(), c.BaseURL, projectID),
- nil,
- )
- if err != nil {
- return nil, err
- }
- req = req.WithContext(ctx)
- bodyResp := &GetKubeconfigResponse{}
- if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
- if httpErr != nil {
- return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
- }
- return nil, err
- }
- return bodyResp, nil
- }
- // GetReleaseLatestRevision gets the latest revision of a Helm release
- type GetReleaseResponse api.PorterRelease
- // GetK8sAllPods gets all pods for a given release
- func (c *Client) GetRelease(
- ctx context.Context,
- projectID, clusterID uint,
- namespace, name string,
- ) (*GetReleaseResponse, error) {
- cl := fmt.Sprintf("%d", clusterID)
- req, err := http.NewRequest(
- "GET",
- fmt.Sprintf("%s/projects/%d/releases/%s/0?"+url.Values{
- "cluster_id": []string{cl},
- "namespace": []string{namespace},
- "storage": []string{"secret"},
- }.Encode(), c.BaseURL, projectID, name),
- nil,
- )
- if err != nil {
- return nil, err
- }
- req = req.WithContext(ctx)
- bodyResp := &GetReleaseResponse{}
- if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
- if httpErr != nil {
- return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
- }
- return nil, err
- }
- return bodyResp, nil
- }
- // GetReleaseAllPodsResponse is the list of all pods for a given Helm release
- type GetReleaseAllPodsResponse []v1.Pod
- // GetK8sAllPods gets all pods for a given release
- func (c *Client) GetK8sAllPods(
- ctx context.Context,
- projectID, clusterID uint,
- namespace, name string,
- ) (GetReleaseAllPodsResponse, error) {
- cl := fmt.Sprintf("%d", clusterID)
- req, err := http.NewRequest(
- "GET",
- fmt.Sprintf("%s/projects/%d/releases/%s/0/pods/all?"+url.Values{
- "cluster_id": []string{cl},
- "namespace": []string{namespace},
- "storage": []string{"secret"},
- }.Encode(), c.BaseURL, projectID, name),
- nil,
- )
- if err != nil {
- return nil, err
- }
- req = req.WithContext(ctx)
- bodyResp := &GetReleaseAllPodsResponse{}
- if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
- if httpErr != nil {
- return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
- }
- return nil, err
- }
- return *bodyResp, nil
- }
|