-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespaces.go
44 lines (32 loc) · 921 Bytes
/
namespaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package kube
import (
"context"
"net/http"
"github.com/mperkins808/log-based-metric-exporter/server/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func ReqListNamespaces(w http.ResponseWriter, r *http.Request) {
client, err := GenClient()
if err != nil {
util.ErrResponse(w, http.StatusInternalServerError, err, err.Error())
return
}
ns, err := ListNamespaces(r.Context(), client)
if err != nil {
util.ErrResponse(w, http.StatusInternalServerError, err, err.Error())
return
}
util.JsonResponse(w, http.StatusOK, ns)
}
func ListNamespaces(ctx context.Context, client *kubernetes.Clientset) ([]string, error) {
ns, err := client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
var namespaces []string
for _, ns := range ns.Items {
namespaces = append(namespaces, ns.Name)
}
return namespaces, nil
}