/
service.go
80 lines (64 loc) · 2.65 KB
/
service.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) Autovia GmbH
// SPDX-License-Identifier: Apache-2.0
package k8s
import (
"context"
"log"
"net/http"
S "github.com/autovia/flightdeck/api/structs"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
func ServiceHandler(app *S.App, c *S.Client, w http.ResponseWriter, r *http.Request) error {
url := S.GetRequestParams(r, "/api/v1/svc/")
log.Printf("ServiceHandler url: %v", url)
svc, err := c.Clientset.CoreV1().Services(url.Scope).Get(context.TODO(), url.Resource, metav1.GetOptions{})
if err != nil {
return S.RespondError(err)
}
svc.ObjectMeta.ManagedFields = nil
return S.RespondFormat(r, w, http.StatusOK, svc)
}
func NamespaceServiceListHandler(app *S.App, c *S.Client, w http.ResponseWriter, r *http.Request) error {
url := S.GetRequestParams(r, "/api/v1/namespace/svc/")
log.Printf("NamespaceServiceListHandler url: %v", url)
g := S.Graph{Nodes: []S.Node{}, Edges: []S.Edge{}}
ns, err := c.Clientset.CoreV1().Namespaces().Get(context.TODO(), url.Scope, metav1.GetOptions{})
if err != nil {
return S.RespondError(err)
}
node := g.AddNode("ns", string(ns.ObjectMeta.UID), ns.ObjectMeta.Name, S.NodeOptions{Type: "namespace", Group: true})
svcList, err := c.Clientset.CoreV1().Services(url.Scope).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return S.RespondError(err)
}
for _, svc := range svcList.Items {
g.AddNode("svc", string(svc.ObjectMeta.UID), svc.ObjectMeta.Name, S.NodeOptions{Namespace: url.Scope, Type: "svc", ParentNode: node, Extent: "parent"})
}
return S.RespondJSON(w, http.StatusOK, g)
}
func ServicePodListHandler(app *S.App, c *S.Client, w http.ResponseWriter, r *http.Request) error {
url := S.GetRequestParams(r, "/api/v1/graph/svc/")
log.Printf("ServicePodListHandler url: %v", url)
g := S.Graph{Nodes: []S.Node{}, Edges: []S.Edge{}}
svcnode := g.AddNode("svc", url.Resource, url.Resource, S.NodeOptions{Namespace: url.Scope, Type: "svc"})
service, err := c.Clientset.CoreV1().Services(url.Scope).Get(context.TODO(), url.Resource, metav1.GetOptions{})
if err != nil {
return S.RespondError(err)
}
podList, err := c.Clientset.CoreV1().Pods(url.Scope).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return S.RespondError(err)
}
for _, pod := range podList.Items {
if service.Spec.Selector == nil {
continue
}
selector := labels.Set(service.Spec.Selector).AsSelectorPreValidated()
if selector.Matches(labels.Set(pod.Labels)) {
podnode := g.AddNode("pod", string(pod.ObjectMeta.UID), pod.ObjectMeta.Name, S.NodeOptions{Namespace: url.Scope, Type: "pod"})
g.AddEdge(svcnode, podnode)
}
}
return S.RespondJSON(w, http.StatusOK, g)
}