-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (118 loc) · 3.54 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"context"
"flag"
"fmt"
"github.com/cccfs/util"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/fatih/color"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"os"
"os/exec"
"path/filepath"
)
type Info struct {
pvClaim string
pvName string
pvVolumeID string
pvVolumePath string
pvStatus string
pvSCName string
pvSize string
pvSource string
}
func main() {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
if ok, _ := util.Exists(filepath.Join(home, ".kube", "config")); ok {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
outfile, err := os.Create("/tmp/config")
if err != nil {
panic(err)
}
defer outfile.Close()
cmd := exec.Command("bash", "-c", "kubectl config view --merge --flatten")
cmd.Stdout = outfile
err = cmd.Start(); if err != nil {
panic(err)
}
cmd.Wait()
kubeconfig = flag.String("kubeconfig", filepath.Join("/tmp/config"), "using $KUBECONFIG variables to the kubeconfig file")
}
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
pvList, _ := clientset.CoreV1().PersistentVolumes().List(context.TODO(), v1.ListOptions{})
info := Info{}
fmt.Println("### Print pv info in console")
for _, pv := range pvList.Items {
info.pvClaim = pv.Spec.ClaimRef.Namespace + "/" + pv.Spec.ClaimRef.Name
info.pvName = pv.Name
info.pvVolumeID = pv.Annotations["gluster.kubernetes.io/heketi-volume-id"]
info.pvSCName = pv.Spec.StorageClassName
info.pvSize = pv.Spec.Capacity.Storage().String()
info.pvStatus = string(pv.Status.Phase)
if pv.Spec.Glusterfs != nil {
info.pvVolumePath = pv.Spec.Glusterfs.Path
}
commaind := fmt.Sprintf(`
Claim: %s
PV: %s
VolumeID: %s
Size: %s
StorageClass: %s
Status: %s
VolumePath: %s
`, color.RedString(info.pvClaim), color.BlueString(info.pvName), color.HiWhiteString(info.pvVolumeID), color.CyanString(info.pvSize), color.CyanString(info.pvSCName), color.CyanString(info.pvStatus), color.CyanString(info.pvVolumePath))
fmt.Print("*****")
fmt.Println(commaind)
//con := func (field string, val reflect.Value) color.Color {
// switch field {
// case "Claim":
// return color.BLUEBG
// case "PV":
// return color.BLUE
// case "VolumeID":
// return color.GREENBG
// case "VolumePath":
// return color.BLUE
// case "Size":
// return color.BLUE
// case "StorageClass":
// return color.BLUE
// case "Status":
// return color.BLUE
// default:
// return ""
// }
//}
//headers := []string{"PV", "VolumeID", "Size", "StorageClass", "Status", "VolumePath", "Claim"}
//tb, err := gotable.CreateTable(headers, gotable.WithColorController(con))
//if err != nil {
// fmt.Println("Create table failed: ", err.Error())
// return
//}
//value := gotable.CreateEmptyValueMap()
//value["Claim"] = gotable.CreateValue(info.pvClaim)
//value["PV"] = gotable.CreateValue(info.pvName)
//value["VolumeID"] = gotable.CreateValue(info.pvVolumeID)
//value["VolumePath"] = gotable.CreateValue(info.pvVolumePath)
//value["Size"] = gotable.CreateValue(info.pvSize)
//value["StorageClass"] = gotable.CreateValue(info.pvSCName)
//value["Status"] = gotable.CreateValue(info.pvStatus)
//tb.AddValue(value)
//tb.PrintTable()
}
}