-
Notifications
You must be signed in to change notification settings - Fork 0
/
workload.go
266 lines (244 loc) · 8.4 KB
/
workload.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package contextgraph
import (
"fmt"
"net/url"
"strings"
"time"
"istio.io/istio/mixer/pkg/adapter"
)
const (
membershipTypeName = "google.cloud.contextgraph.Membership"
grpcComm = "google.cloud.contextgraph.Communication.Grpc"
httpComm = "google.cloud.contextgraph.Communication.Http"
httpsComm = "google.cloud.contextgraph.Communication.Https"
tcpComm = "google.cloud.contextgraph.Communication.Tcp"
)
var (
protocolMap = map[string]string{
"http": httpComm,
"https": httpsComm,
"tcp": tcpComm,
"grpc": grpcComm,
}
)
type workloadInstance struct {
// N.B. The projects can potentially be different for each workload.
meshUID string
istioProject string
clusterProject, clusterLocation, clusterName string
uid string
owner string
workloadName, workloadNamespace string
}
type service struct {
meshUID string
namespace string
name string
istioProject string
}
// Reify turns wi into a set of Context API entities and edges.
func (wi workloadInstance) Reify(logger adapter.Logger) ([]entity, []edge) {
gcpContainer := fmt.Sprintf("//cloudresourcemanager.googleapis.com/projects/%s", wi.istioProject)
// N.B. Project names can contain ":" which needs to /not/ be escaped.
istioContainer := fmt.Sprintf("//istio.io/projects/%s", wi.istioProject)
meshUID := url.QueryEscape(wi.meshUID)
clusterProject := wi.clusterProject
// TODO: Figure out if locations should be URL-escaped or not ("aws:us-east-1" is a valid region).
clusterLocation := url.QueryEscape(wi.clusterLocation)
clusterName := url.QueryEscape(wi.clusterName)
uid := url.QueryEscape(wi.uid)
ownerUID := url.QueryEscape(wi.owner)
workloadName := url.QueryEscape(wi.workloadName)
workloadNamespace := url.QueryEscape(wi.workloadNamespace)
wiFullName := fmt.Sprintf(
"%s/meshes/%s/clusterProjects/%s/locations/%s/clusters/%s/workloadInstances/%s",
istioContainer, meshUID, clusterProject, clusterLocation, clusterName, uid,
)
wiEntity := entity{
gcpContainer,
"io.istio.WorkloadInstance",
wiFullName,
clusterLocation,
[4]string{meshUID, clusterProject, clusterName, uid},
}
ownerFullName := fmt.Sprintf(
"%s/meshes/%s/clusterProjects/%s/locations/%s/clusters/%s/owners/%s",
istioContainer, meshUID, clusterProject, clusterLocation, clusterName, ownerUID,
)
owner := entity{
gcpContainer,
"io.istio.Owner",
ownerFullName,
clusterLocation,
[4]string{meshUID, clusterProject, clusterName, ownerUID},
}
workloadFullName := fmt.Sprintf(
"%s/meshes/%s/workloads/%s/%s",
istioContainer, meshUID, workloadNamespace, workloadName,
)
workload := entity{
gcpContainer,
"io.istio.Workload",
workloadFullName,
"global",
[4]string{meshUID, workloadNamespace, workloadName, ""},
}
clusterContainer := clusterContainer(wi.clusterProject, wi.clusterLocation, wi.clusterName)
var ownerK8sFullName string
t := strings.Split(wi.owner, "/")
if len(t) >= 3 && t[0] == "kubernetes:" {
var name, namespace, typeName string
t = t[2:]
switch {
case len(t) >= 6 && t[0] == "apis" && t[1] == "v1":
// pods, RC
namespace = t[3]
name = t[5]
typeName = t[4]
case len(t) >= 7 && t[0] == "apis" && (t[1] == "extensions" || t[1] == "apps" || t[1] == "batch"):
// cronjobs, jobs, daemonsets, deployments, replicasets, statefulsets
namespace = t[4]
name = t[6]
typeName = t[1] + "/" + t[5]
}
if name != "" {
ownerK8sFullName = fmt.Sprintf("%s/k8s/namespaces/%s/%s/%s",
clusterContainer, namespace, typeName, name)
} else {
logger.Warningf("Couldn't parse owner into k8s obj: %s", wi.owner)
}
} else {
if wi.owner != "unknown" {
logger.Warningf("Unknown owner type: %s", wi.owner)
}
}
// TODO: Non-k8s owners.
// "kubernetes://istio-pilot-65d79b966c-xnbx8.istio-system"
var wiK8sFullName string
t = strings.Split(wi.uid, "/")
if len(t) == 3 && t[0] == "kubernetes:" {
nameNs := strings.Split(t[2], ".")
if len(nameNs) == 2 {
namespace, name := nameNs[1], nameNs[0]
wiK8sFullName = fmt.Sprintf("%s/k8s/namespaces/%s/pods/%s", clusterContainer, namespace, name)
} else {
logger.Warningf("Unknown workload instance type: %s", wi.uid)
}
} else {
if wi.uid != "Unknown" {
logger.Warningf("Unknown workload instance type: %s", wi.uid)
}
}
// TODO: Non-k8s workload instances
edges := []edge{
{workloadFullName, ownerFullName, membershipTypeName},
{ownerFullName, wiFullName, membershipTypeName},
}
if ownerK8sFullName != "" {
edges = append(edges, edge{ownerFullName, ownerK8sFullName, membershipTypeName})
}
if wiK8sFullName != "" {
edges = append(edges, edge{wiFullName, wiK8sFullName, membershipTypeName})
}
return []entity{wiEntity, owner, workload}, edges
}
func (s service) Reify() entity {
return entity{
containerFullName: fmt.Sprintf("//cloudresourcemanager.googleapis.com/projects/%s",
s.istioProject),
typeName: "io.istio.Service",
fullName: fmt.Sprintf("//istio.io/projects/%s/meshes/%s/services/%s/%s",
s.istioProject,
url.QueryEscape(s.meshUID),
url.QueryEscape(s.namespace),
url.QueryEscape(s.name)),
location: "global",
shortNames: [4]string{
url.QueryEscape(s.meshUID),
url.QueryEscape(s.namespace),
url.QueryEscape(s.name),
"",
},
}
}
type trafficAssertion struct {
source, destination workloadInstance
contextProtocol, apiProtocol string
destinationService service
timestamp time.Time
}
func (t trafficAssertion) Reify(logger adapter.Logger) ([]entity, []edge) {
commType, ok := protocolMap[t.contextProtocol]
if !ok {
if commType, ok = protocolMap[t.apiProtocol]; !ok {
logger.Warningf("Unknown type of protocol: %s", t.apiProtocol)
}
}
serviceEntity := t.destinationService.Reify()
entities, edges := t.source.Reify(logger)
var sourceFullNames []string
for _, entity := range entities {
sourceFullNames = append(sourceFullNames, entity.fullName)
if len(commType) > 0 {
edges = append(edges, edge{entity.fullName, serviceEntity.fullName, commType})
}
}
destEntities, destEdges := t.destination.Reify(logger)
entities = append(entities, destEntities...)
edges = append(edges, destEdges...)
var k8sSvc string
for _, entity := range destEntities {
if len(commType) > 0 {
for _, s := range sourceFullNames {
edges = append(edges, edge{s, entity.fullName, commType})
}
edges = append(edges, edge{serviceEntity.fullName, entity.fullName, commType})
if k8sSvc == "" {
k8sSvc = k8sSvcFullname(entity.shortNames[1], entity.location, entity.shortNames[2], t.destinationService.namespace, t.destinationService.name)
}
}
}
entities = append(entities, serviceEntity)
if len(k8sSvc) > 0 {
edges = append(edges, edge{serviceEntity.fullName, k8sSvc, membershipTypeName})
}
return entities, edges
}
// example: //container.googleapis.com/projects/<project>/locations/us-central1-a/clusters/<cluster>/k8s/namespaces/default/services/<service>
func k8sSvcFullname(project, location, cluster, namespace, name string) string {
return fmt.Sprintf("%s/k8s/namespaces/%s/services/%s", clusterContainer(project, location, cluster), namespace, name)
}
func clusterContainer(project, location, cluster string) string {
// TODO: Figure out what the container is for non-GCE clusters.
locType := "locations"
if strings.Count(location, "-") == 2 {
locType = "zones"
}
return fmt.Sprintf("//container.googleapis.com/projects/%s/%s/%s/clusters/%s", project, locType, location, cluster)
}
type entity struct {
containerFullName string
typeName string
fullName string
location string
// N.B. map keys can only have arrays, not slices.
// 4 is enough for all our entity types.
shortNames [4]string
}
type edge struct {
sourceFullName, destinationFullName string
typeName string
}