-
Notifications
You must be signed in to change notification settings - Fork 3
/
glasses.go
149 lines (119 loc) · 3.13 KB
/
glasses.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"text/tabwriter"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
var (
k8sHostname string
versionUrl = "https://github.com/wakeful/glasses"
version = "dev"
hostFile = flag.String("host-file", "/etc/hosts", "host file location")
writeHostFile = flag.Bool("write", false, "rewrite host file?")
showVersion = flag.Bool("version", false, "show version and exit")
)
const (
sectionStart = "# generated using glasses start #"
sectionEnd = "# generated using glasses end #\n"
)
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE")
}
type Rule struct {
Domain string
Service string
}
func (r *Rule) String() string { return fmt.Sprintf("%s %s\t# %s", k8sHostname, r.Domain, r.Service) }
type HostsList []Rule
func (h HostsList) Len() int { return len(h) }
func (h HostsList) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h HostsList) Less(i, j int) bool {
return strings.ToLower(h[i].Domain) < strings.ToLower(h[j].Domain)
}
func k8sHost(config *rest.Config) string {
u, err := url.Parse(config.Host)
if err != nil {
log.Fatalln(err.Error())
}
return u.Hostname()
}
func tryWriteToHostFile(hostEntries string) error {
block := []byte(fmt.Sprintf("%s\n%s\n%s", sectionStart, hostEntries, sectionEnd))
fileContent, err := ioutil.ReadFile(*hostFile)
if err != nil {
return err
}
re := regexp.MustCompile(fmt.Sprintf("(?ms)%s(.*)%s", sectionStart, sectionEnd))
if re.Match(fileContent) {
fileContent = re.ReplaceAll(fileContent, block)
} else {
fileContent = append(fileContent, block...)
}
if err := ioutil.WriteFile(*hostFile, fileContent, 0644); err != nil {
return err
}
fmt.Println(hostEntries)
return nil
}
func main() {
flag.Parse()
if *showVersion {
fmt.Printf("Glasses\n url: %s\n version: %s", versionUrl, version)
os.Exit(2)
}
fmt.Println("# reading k8s ingress resource...")
config, err := clientcmd.BuildConfigFromFlags("", filepath.Join(homeDir(), ".kube", "config"))
if err != nil {
log.Fatalln(err.Error())
}
k8sHostname = k8sHost(config)
client, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalln(err.Error())
}
ingress, err := client.ExtensionsV1beta1().Ingresses("").List(metaV1.ListOptions{})
if err != nil {
log.Fatalln(err.Error())
}
var entries HostsList
for _, elem := range ingress.Items {
for _, rule := range elem.Spec.Rules {
entries = append(entries, Rule{
Domain: rule.Host,
Service: elem.Name,
})
}
}
sort.Sort(HostsList(entries))
var hostEntries string
for _, item := range entries {
hostEntries = hostEntries + fmt.Sprintf("%s\n", item.String())
}
wBuffer := new(bytes.Buffer)
writer := tabwriter.NewWriter(wBuffer, 0, 0, 2, ' ', 0)
fmt.Fprint(writer, hostEntries)
writer.Flush()
if !*writeHostFile {
fmt.Println(wBuffer.String())
os.Exit(0)
}
if err := tryWriteToHostFile(wBuffer.String()); err != nil {
log.Fatalln(err)
}
}