-
Notifications
You must be signed in to change notification settings - Fork 476
/
host.go
251 lines (213 loc) · 5.96 KB
/
host.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package kubernetes
import (
"fmt"
"reflect"
"strings"
core_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"github.com/kiali/kiali/config"
)
// Host represents the FQDN format for Istio hostnames
type Host struct {
Service string
Namespace string
Cluster string
// CompleteInput is true when Service, Namespace and Cluster fields are present.
// It is true for simple service names and FQDN services.
// It is false for service.namespace format and service entries.
CompleteInput bool
}
// ParseHost takes as an input a hostname (simple or full FQDN), namespace and clusterName and returns a parsed Host struct
func ParseHost(hostName, namespace, cluster string) Host {
if cluster == "" {
cluster = config.Get().ExternalServices.Istio.IstioIdentityDomain
}
domainParts := strings.Split(hostName, ".")
host := Host{
Service: domainParts[0],
}
if len(domainParts) > 1 {
if len(domainParts) > 2 {
parsedClusterName := strings.Join(domainParts[2:], ".")
if parsedClusterName == cluster {
// FQDN input
host.Cluster = cluster
host.CompleteInput = true
}
}
if host.CompleteInput {
host.Namespace = domainParts[1]
} else {
// ServiceEntry or broken hostname
host.Service = hostName
}
} else {
// Simple format
host.Namespace = namespace
host.Cluster = cluster
host.CompleteInput = true
}
return host
}
// GetHost parses hostName and returns a Host struct. It considers Namespaces in the cluster to be more accurate
// when deciding if the hostName is a ServiceEntry or a service.namespace host definition.
func GetHost(hostName, namespace, cluster string, clusterNamespaces []string) Host {
hParts := strings.Split(hostName, ".")
// It might be a service entry or a 2-format host specification
if len(hParts) == 2 {
// It is subject of validation when object is within the namespace
// Otherwise is considered as a service entry
if hParts[1] == namespace || includes(clusterNamespaces, hParts[1]) {
return Host{
Service: hParts[0],
Namespace: hParts[1],
Cluster: cluster,
CompleteInput: true,
}
}
}
return ParseHost(hostName, namespace, cluster)
}
func includes(nss []string, namespace string) bool {
for _, ns := range nss {
if ns == namespace {
return true
}
}
return false
}
// String outputs a full FQDN version of the Host
func (h Host) String() string {
var hostname string
if h.CompleteInput {
if h.Cluster != "" {
hostname = fmt.Sprintf("%s.%s.%s", h.Service, h.Namespace, h.Cluster)
} else {
hostname = fmt.Sprintf("%s.%s", h.Service, h.Namespace)
}
} else {
hostname = h.Service
}
return hostname
}
func ParseTwoPartHost(host Host) (string, string) {
localSvc, localNs := host.Service, host.Namespace
if !host.CompleteInput {
svcParts := strings.Split(host.Service, ".")
if len(svcParts) > 1 {
localSvc = svcParts[0]
localNs = svcParts[1]
}
}
return localSvc, localNs
}
func HasMatchingWorkloads(service string, workloadList []labels.Set) bool {
appLabel := config.Get().IstioLabels.AppLabelName
// Check Workloads
for _, wl := range workloadList {
if service == wl.Get(appLabel) {
return true
}
}
return false
}
func HasMatchingServices(service string, services []core_v1.Service) bool {
for _, s := range services {
if service == s.Name {
return true
}
}
return false
}
func HasMatchingServiceEntries(service string, serviceEntries map[string][]string) bool {
for k := range serviceEntries {
hostKey := k
if i := strings.Index(k, "*"); i > -1 {
hostKey = k[i+1:]
}
if strings.HasSuffix(service, hostKey) {
return true
}
}
if _, found := serviceEntries[service]; found {
return true
}
return false
}
func HasMatchingVirtualServices(host Host, virtualServices []IstioObject) bool {
for _, vs := range virtualServices {
rawHosts, found := vs.GetSpec()["hosts"]
if !found {
continue
}
slice := reflect.ValueOf(rawHosts)
if slice.Kind() != reflect.Slice {
continue
}
for hostIdx := 0; hostIdx < slice.Len(); hostIdx++ {
vHost, ok := slice.Index(hostIdx).Interface().(string)
if !ok {
continue
}
// vHost is wildcard, then any host fit in
if vHost == "*" {
return true
}
// vHost is simple name (namespaces should match)
if vHost == host.Service && vs.GetObjectMeta().Namespace == host.Namespace {
return true
}
// vHost twoparts name
if vHost == fmt.Sprintf("%s.%s", host.Service, host.Namespace) {
return true
}
// vHost FQDN (no-wildcarded)
if vHost == fmt.Sprintf("%s.%s.%s", host.Service, host.Namespace, config.Get().ExternalServices.Istio.IstioIdentityDomain) {
return true
}
// vHost if wildcard FQDN
if vHost == fmt.Sprintf("*.%s.%s", host.Namespace, config.Get().ExternalServices.Istio.IstioIdentityDomain) {
return true
}
// Non-internal service name
hostS := ParseHost(vHost, vs.GetObjectMeta().Namespace, vs.GetObjectMeta().ClusterName)
if hostS.Service == host.Service && hostS.CompleteInput == host.CompleteInput && !hostS.CompleteInput {
return true
}
// Non-internal wildcard service name
if HostWithinWildcardHost(host.Service, vHost) {
return true
}
}
}
return false
}
func HostWithinWildcardHost(subdomain, wildcardDomain string) bool {
if !strings.HasPrefix(wildcardDomain, "*") {
return false
}
return len(wildcardDomain) > 2 && strings.HasSuffix(subdomain, wildcardDomain[2:])
}
func ParseGatewayAsHost(gateway, currentNamespace, currentCluster string) Host {
host := Host{
Service: gateway,
Namespace: currentNamespace,
Cluster: currentCluster,
CompleteInput: true,
}
if strings.Contains(gateway, ".") {
parts := strings.Split(gateway, ".")
host.Service = parts[0]
if len(parts) > 1 {
host.Namespace = parts[1]
if len(parts) > 2 {
host.Cluster = strings.Join(parts[2:], ".")
}
}
} else if strings.Contains(gateway, "/") {
parts := strings.Split(gateway, "/")
host.Namespace = parts[0]
host.Service = parts[1]
}
return host
}