forked from cjimti/go-vol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vol.go
85 lines (68 loc) · 1.68 KB
/
vol.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
package main
import (
"io/ioutil"
"log"
"time"
"os"
"fmt"
"github.com/gin-gonic/gin"
"github.com/nu7hatch/gouuid"
)
func main() {
count := 1
callUuidV4, _ := uuid.NewV4()
nodeName := os.Getenv("NODE_NAME")
podName := os.Getenv("POD_NAME")
podNamespace := os.Getenv("POD_NAMESPACE")
podIP := os.Getenv("POD_IP")
serviceAccountName := os.Getenv("SERVICE_ACCOUNT")
path := os.Getenv("VOL_PATH")
if path == "" {
path = "./"
}
filePath := fmt.Sprintf("%s/gv_%s_%s.txt", path, nodeName, podName)
statusMessage := "OK"
// touch a file
_, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, 0666)
if err != nil {
statusMessage = err.Error()
}
// web API endpoint
r := gin.Default()
r.GET("/", func(c *gin.Context) {
instanceUuidV4, _ := uuid.NewV4()
c.JSON(200, gin.H{
"message": "go-vol",
"time": time.Now(),
"count": count,
"uuid_call": instanceUuidV4.String(),
"uuid_instance": callUuidV4.String(),
"client_ip": c.ClientIP(),
"version": 1,
"version_msg": "version 1",
"node_name": nodeName,
"pod_name": podName,
"pod_namepage": podNamespace,
"pod_ip": podIP,
"service_account": serviceAccountName,
"file": filePath,
"files": listFiles(path),
"status_message": statusMessage,
})
count++
})
r.Run() // listen and serve on 0.0.0.0:8080
}
func listFiles(path string) []string {
// get a listing of files in the path
//
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
fileNames := make([]string, 0)
for _, f := range files {
fileNames = append(fileNames, f.Name())
}
return fileNames
}