-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kubefs.go
94 lines (76 loc) · 2.08 KB
/
kubefs.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
package main
import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"github.com/billziss-gh/cgofuse/fuse"
"github.com/spf13/pflag"
f "github.com/configurator/kubefs/pkg/cgofusewrapper"
kube "github.com/configurator/kubefs/pkg/kube"
)
func main() {
pflag.Usage = func() {
fmt.Printf("Usage:\n\t%s <mountpoint>\n", path.Base(os.Args[0]))
pflag.PrintDefaults()
}
readonly := pflag.Bool("readonly", false, "readonly mode - never allow any write or change to the cluster")
showJsonFiles := pflag.Bool("show-json-files", false, "show .json files in file listings")
showYamlFiles := pflag.Bool("show-yaml-files", true, "show .yaml files in file listings (defaults to true, use =false to change)")
prettyJson := pflag.Bool("pretty-json", false, "pretty-print json files")
kubeconfig := pflag.StringP("kubeconfig", "c", "", "absolute path to the kubeconfig file")
pflag.Parse()
args := pflag.Args()
if len(args) != 1 {
pflag.Usage()
os.Exit(1)
}
mountpoint := args[0]
settings := &kube.Settings{
ShowJsonFiles: *showJsonFiles,
ShowYamlFiles: *showYamlFiles,
PrettyJson: *prettyJson,
Readonly: *readonly,
}
k := &kube.Kubernetes{
Settings: settings,
}
k.LoadConfig(*kubeconfig)
fs := &f.FS{
Root: k,
Readonly: *readonly,
}
h := fuse.NewFileSystemHost(fs)
h.Mount(mountpoint, nil)
}
func cleanPathAndValidateEmptyDir(mountpoint string) (string, error) {
mountpoint, err := filepath.Abs(mountpoint)
if err != nil {
return "", err
}
stat, err := os.Stat(mountpoint)
if err != nil {
// Includes both "path does not exist" and other errors
return "", err
}
if !stat.IsDir() {
return "", fmt.Errorf("Mount point %s is not a directory", mountpoint)
}
file, err := os.OpenFile(mountpoint, os.O_RDONLY, 0)
if err != nil {
return "", err
}
defer file.Close()
// Reading one file is enough to verify if directory isn't empty
// We expect EOF!
_, err = file.Readdirnames(1)
if err != io.EOF {
if err != nil {
return "", err
} else {
return "", fmt.Errorf("Mount point %s is not an empty directory", mountpoint)
}
}
return mountpoint, nil
}