/
knative.go
88 lines (69 loc) · 1.7 KB
/
knative.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
package handlers
import (
"log"
"net/http"
"os"
"golang.org/x/sys/unix"
"github.com/mchmarny/tellmeall/types"
)
const (
runtimeContractURL = "https://github.com/knative/serving/blob/master/docs/runtime-contract.md"
)
func isDirRW(path string) *types.FsAccessInfo {
info := &types.FsAccessInfo{
Path: path,
Expected: "R/W",
}
if unix.Access(path, unix.O_RDWR) == nil {
info.Actual = "R/W"
info.Comment = "(Success)"
return info
}
if unix.Access(path, unix.O_RDONLY) == nil {
info.Actual = "R/-"
info.Comment = "(Failed: no write)"
return info
}
if unix.Access(path, unix.O_WRONLY) == nil {
info.Actual = "-/W"
info.Comment = "(Failed: no read)"
return info
}
info.Actual = "-/-"
info.Comment = "(Failed: neither)"
return info
}
func knativeHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Handling Knative...")
w.Header().Set("Content-Type", "application/json")
k := &types.Knative{
Meta: getMeta(r),
InfoURI: runtimeContractURL,
EnvVars: make(map[string]interface{}),
Access: make([]*types.FsAccessGroup, 0),
}
// env vars
k.EnvVars["PORT"] = os.Getenv("PORT")
k.EnvVars["K_SERVICE"] = os.Getenv("K_SERVICE")
k.EnvVars["K_REVISION"] = os.Getenv("K_REVISION")
k.EnvVars["K_CONFIGURATION"] = os.Getenv("K_CONFIGURATION")
fsg := &types.FsAccessGroup{
Group: "FILESYSTEM",
List: []*types.FsAccessInfo{
isDirRW("/tmp"),
isDirRW("/var/log"),
isDirRW("/dev/log"),
},
}
k.Access = append(k.Access, fsg)
dnsg := &types.FsAccessGroup{
Group: "DNS",
List: []*types.FsAccessInfo{
isDirRW("/etc/hosts"),
isDirRW("/etc/hostname"),
isDirRW("/etc/resolv.conf"),
},
}
k.Access = append(k.Access, dnsg)
writeJSON(w, k)
}