-
Notifications
You must be signed in to change notification settings - Fork 375
/
service_search_result.go
194 lines (178 loc) · 4.96 KB
/
service_search_result.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package nacos
import (
"net"
"strconv"
"strings"
"github.com/erda-project/erda-proto-go/msp/registercenter/pb"
)
// ServiceSearchResult .
type ServiceSearchResult struct {
ServiceName string `json:"serviceName"`
GroupName string `json:"groupName"`
ClusterMap map[string]*ServiceHosts `json:"clusterMap"`
OwnerNameList []string `json:"ownerNameList"`
IPList []string `json:"ipList"`
OwnerMap map[string]*pb.InterfaceOwner `json:"ownerMap"`
}
// ToHTTPService .
func (s *ServiceSearchResult) ToHTTPService() *pb.HTTPService {
var service pb.HTTPService
service.ServiceName = s.ServiceName
if len(s.ClusterMap) <= 0 || s.ClusterMap["DEFAULT"] == nil || len(s.ClusterMap["DEFAULT"].Hosts) <= 0 && s.ClusterMap["DEFAULT"].Hosts[0] != nil {
return &service
}
def := s.ClusterMap["DEFAULT"]
service.ServiceDomain = def.Hosts[0].getDomain()
for _, item := range def.Hosts {
service.HttpServiceDto = append(service.HttpServiceDto, item.ToHTTPServiceItem())
}
return &service
}
func (s *ServiceSearchResult) getSide() string {
idx := strings.Index(s.ServiceName, ":")
if idx < 0 {
return ""
}
return s.ServiceName[0:idx]
}
func (s *ServiceSearchResult) getInterfaceName() string {
idx := strings.Index(s.ServiceName, ":")
if idx < 0 {
return s.ServiceName
}
return s.ServiceName[idx+1:]
}
// ToInterface .
func (s *ServiceSearchResult) ToInterface() *pb.Interface {
var i pb.Interface
i.Interfacename = s.getInterfaceName()
switch s.getSide() {
case "consumers":
i.Consumerlist = append(i.Consumerlist, s.getIPs()...)
i.Consumermap = make(map[string]*pb.InterfaceOwner)
for k, v := range s.getOwnerMap() {
i.Consumermap[k] = v
}
case "providers":
i.Providerlist = append(i.Providerlist, s.getIPs()...)
i.Providermap = make(map[string]*pb.InterfaceOwner)
for k, v := range s.getOwnerMap() {
i.Providermap[k] = v
}
}
return &i
}
func (s *ServiceSearchResult) getIPs() []string {
if s.IPList != nil {
return s.IPList
}
if len(s.ClusterMap) <= 0 || s.ClusterMap["DEFAULT"] == nil || len(s.ClusterMap["DEFAULT"].Hosts) <= 0 {
s.IPList = make([]string, 0)
return nil
}
for _, item := range s.ClusterMap["DEFAULT"].Hosts {
if item == nil {
continue
}
s.IPList = append(s.IPList, item.IP)
}
return s.IPList
}
func (s *ServiceSearchResult) getOwnerMap() map[string]*pb.InterfaceOwner {
if s.OwnerMap != nil {
return s.OwnerMap
}
s.OwnerMap = make(map[string]*pb.InterfaceOwner)
if len(s.ClusterMap) <= 0 || s.ClusterMap["DEFAULT"] == nil || len(s.ClusterMap["DEFAULT"].Hosts) <= 0 {
return s.OwnerMap
}
for _, item := range s.ClusterMap["DEFAULT"].Hosts {
if item == nil {
continue
}
owner := getOwner(item.IP, item.MetaData["owner"])
if owner == nil {
continue
}
s.OwnerMap[item.IP] = owner
}
return s.OwnerMap
}
func getOwner(ip, owner string) *pb.InterfaceOwner {
ip, owner = strings.TrimSpace(ip), strings.TrimSpace(owner)
if len(ip) <= 0 || len(owner) <= 0 {
return nil
}
parts := strings.Split(owner, "_")
if len(parts) < 3 {
return nil
}
o := pb.InterfaceOwner{
Ip: ip,
Owner: owner,
ProjectId: parts[0],
Env: parts[1],
HostIp: parts[2],
}
if len(parts) >= 5 {
o.ApplicationId = parts[3]
o.Feature = parts[4]
}
if len(parts) >= 7 {
o.ServiceName = parts[6]
}
return &o
}
func (s *ServiceSearchResult) getHostByIP(ip string) *ServiceHost {
if len(ip) == 0 {
return nil
}
def := s.ClusterMap["DEFAULT"]
if def == nil {
return nil
}
for _, h := range def.Hosts {
if h != nil && h.IP == ip {
return h
}
}
return nil
}
// ServiceHosts
type ServiceHosts struct {
Hosts []*ServiceHost `json:"hosts"`
}
// ServiceHost .
type ServiceHost struct {
Valid bool `json:"valid"`
Port int64 `json:"port"`
IP string `json:"ip"`
Weight float64 `json:"weight"`
Enabled bool `json:"enabled"`
MetaData map[string]string `json:"metadata"`
}
func (s *ServiceHost) getDomain() string {
h := s.MetaData["SELF_HOST"]
if len(h) <= 0 {
return ""
}
return net.JoinHostPort(h, strconv.FormatInt(s.Port, 10))
}
func (s *ServiceHost) ToHTTPServiceItem() *pb.HTTPServiceItem {
var data pb.HTTPServiceItem
data.Address = net.JoinHostPort(s.IP, strconv.FormatInt(s.Port, 10))
data.Online = s.Enabled
return &data
}