-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk8s.go
136 lines (118 loc) · 2.77 KB
/
k8s.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
package main
import (
"encoding/json"
"fmt"
"strings"
)
type opType string
const (
opTypeUnknown opType = "unknown"
opTypeAdd = "add"
opTypeDel = "del"
)
type Pod struct {
Type string `json:"type"`
Object *struct {
Metadata *struct {
Name string `json:"name"`
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations"`
Dt string `json:"deletionTimestamp"`
} `json:"metadata"`
Status *struct {
Phase string `json:"phase"`
Ip string `json:"podIP"`
} `json:"status"`
} `json:"object"`
op opType
domain,
ip string
ports map[string]string
waitforDel bool
}
func (w *watcher) NewPod(raw []byte) (*Pod, error) {
this := &Pod{}
if err := json.Unmarshal(raw, this); err != nil {
return nil, err
}
// parse op
this.op = opTypeUnknown
switch {
case this.Object == nil ||
this.Object.Metadata == nil ||
this.Object.Metadata.Name == "" ||
this.Object.Status == nil:
// type unknown
// 避免空指针panic
case this.Object.Status.Phase == "Running" &&
this.Object.Metadata.Dt == "" &&
this.Object.Status.Ip != "":
this.ip = this.Object.Status.Ip
w.podPool[this.Object.Metadata.Name] = this.Object.Status.Ip
this.op = opTypeAdd
case this.Type == "DELETED" &&
this.Object.Metadata.Dt != "":
if ip, ok := w.podPool[this.Object.Metadata.Name]; ok {
this.ip = ip
this.op = opTypeDel
}
}
this.ports = map[string]string{}
portPrefix := "port_"
for k, v := range this.Object.Metadata.Labels {
switch {
case strings.HasPrefix(k, portPrefix):
p := strings.ToLower(strings.TrimPrefix(k, portPrefix))
this.ports[p] = v
case k == "domain":
this.domain = v
default:
// ignore
}
}
if this.domain == "" {
this.op = opTypeUnknown
}
return this, nil
}
func (this *Pod) String() string {
return fmt.Sprintf(
"\nop:%s\ndomain:%s\n%s\n%s",
this.op,
this.domain,
func() string {
if len(this.ports) == 0 {
return "no port to proxy"
}
ret := []string{}
for p, v := range this.ports {
ret = append(ret, fmt.Sprintf("%s->%s", p, v))
}
return "proxy port:" + strings.Join(ret, ",")
}(),
func() string {
bt, _ := json.MarshalIndent(this, "", "\t")
return string(bt)
}(),
)
}
func (this *Pod) GetAppconfigFilename() string {
return fmt.Sprintf("%s.json", this.domain)
}
func (this *Pod) GetDomainOnPort(portName string) string {
ret := this.domain
if portName != "" {
ret = portName + "." + ret
}
return ret
}
func (this *Pod) GetServiceNameOnPort(portName string) string {
ret := this.domain
if portName != "" {
ret += "." + portName
}
return strings.Replace(ret, ".", "_", -1)
}
func (this *Pod) GetPodHostOnPort(port string) string {
return fmt.Sprintf("%s:%s", this.ip, port)
}