-
Notifications
You must be signed in to change notification settings - Fork 2
/
get-pvcs.go
74 lines (64 loc) · 1.71 KB
/
get-pvcs.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
package main
import (
"html/template"
"log"
"net/http"
"os"
"path/filepath"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
type Pvc struct {
Name string
Status string
Storage string
}
type PageVars struct {
Pvcs []Pvc
}
func pvcHandler(w http.ResponseWriter, r *http.Request) {
kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
log.Println("Using kubeconfig file: ", kubeconfig)
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
log.Fatal(err)
}
// Create an rest client not targeting specific API version
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
}
var ns, label, field string
api := clientset.CoreV1()
// setup list options
listOptions := metav1.ListOptions{
LabelSelector: label,
FieldSelector: field,
}
pvcs, err := api.PersistentVolumeClaims(ns).List(listOptions)
if err != nil {
log.Fatal(err)
}
var pvcsList []Pvc
for _, pvc := range pvcs.Items {
quant := pvc.Spec.Resources.Requests[v1.ResourceStorage]
pvcObj := Pvc{
pvc.Name,
string(pvc.Status.Phase),
quant.String()}
pvcsList = append(pvcsList, pvcObj)
}
pvcVars := PageVars{
Pvcs: pvcsList,
}
t, err := template.ParseFiles("./assets/pvc.html") //parse the html file homepage.html
if err != nil { // if there is an error
log.Print("template parsing error: ", err) // log it
}
err = t.Execute(w, pvcVars) //execute the template and pass it the HomePageVars struct to fill in the gaps
if err != nil { // if there is an error
log.Print("template executing error: ", err) //log it
}
}