-
Notifications
You must be signed in to change notification settings - Fork 38
/
relationship.go
312 lines (284 loc) · 11.9 KB
/
relationship.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
Copyright The Karpor 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 topology
import (
"context"
"fmt"
"os"
"reflect"
"github.com/pkg/errors"
"github.com/KusionStack/karpor/pkg/core/entity"
"github.com/KusionStack/karpor/pkg/infra/search/storage"
"github.com/KusionStack/karpor/pkg/infra/search/storage/elasticsearch"
"github.com/KusionStack/karpor/pkg/util/ctxutil"
topologyutil "github.com/KusionStack/karpor/pkg/util/topology"
"github.com/dominikbraun/graph"
yaml "gopkg.in/yaml.v3"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/klog/v2"
)
// GetHash method returns the hash of the resource graph node.
func (rgn ResourceGraphNode) GetHash() string {
return rgn.Group + "/" + rgn.Version + "." + rgn.Kind + ":" + rgn.Namespace + "." + rgn.Name
}
// GetHash method returns the hash of the relationship graph node.
func (rgn RelationshipGraphNode) GetHash() string {
return rgn.Group + "." + rgn.Version + "." + rgn.Kind
}
// GetHash method returns the hash of the relationship.
func (r Relationship) GetHash() string {
return r.Group + "." + r.Version + "." + r.Kind
}
// ConvertToMap method converts the relationship graph node to a map[string]string.
func (rgn RelationshipGraphNode) ConvertToMap() map[string]string {
m := make(map[string]string, 0)
for _, p := range rgn.Parent {
parentHash := p.GetHash()
if _, ok := m[parentHash]; !ok {
m[parentHash] = ParentTypeKey
}
}
for _, c := range rgn.Children {
childHash := c.GetHash()
if _, ok := m[childHash]; !ok {
m[childHash] = ChildTypeKey
}
}
return m
}
// FindNodeByGVK locates the Node by GVK on a RelationshipGraph. Used to locate parent and child nodes when building the relationship graph
func (rg *RelationshipGraph) FindNodeByGVK(group, version, kind string) (*RelationshipGraphNode, error) {
for _, item := range rg.RelationshipNodes {
if item.Group == group && item.Version == version && item.Kind == kind {
return item, nil
}
}
// If not found, return a new one so it can be inserted properly
r := RelationshipGraphNode{
Group: group,
Version: version,
Kind: kind,
Parent: make([]*Relationship, 0),
Children: make([]*Relationship, 0),
}
nodeNotFoundErr := fmt.Errorf("node not found by GVK: Group: %s, Version: %s, Kind: %s", group, version, kind)
return &r, nodeNotFoundErr
}
// FindNodeOnGraph locates the Node on a built relationship graph. Used to locate parent and child nodes when traversing the relationship graph
func FindNodeOnGraph(g graph.Graph[string, RelationshipGraphNode], group, version, kind string) (*RelationshipGraphNode, error) {
klog.Infof("Locating node on relationship graph: Group: %s, Version: %s, Kind: %s\n", group, version, kind)
vertexHash := group + "." + version + "." + kind
vertex, err := g.Vertex(vertexHash)
if err != nil {
return nil, err
}
return &vertex, nil
}
// BuildBuiltinRelationshipGraph returns the relationship graph built from the YAML describing resource relationships
func BuildBuiltinRelationshipGraph(ctx context.Context, client *dynamic.DynamicClient) (graph.Graph[string, RelationshipGraphNode], *RelationshipGraph, error) {
log := ctxutil.GetLogger(ctx)
// TODO: Obtaining topological relationship from CR in the future.
// Get the file path from the environment variable, fallback to default if
// not set.
filePath := os.Getenv("KARPOR_RELATIONSHIP_FILE")
if filePath == "" {
filePath = "relationship.yaml" // Default file path
}
yamlFile, err := os.ReadFile(filePath)
if err != nil {
log.Error(err, "yamlFile.Get err")
}
r := RelationshipGraph{}
err = yaml.Unmarshal(yamlFile, &r)
if err != nil {
log.Error(err, "Unmarshal error")
}
// Process relationships between parent and child
// TODO: Think about whether two-way relationship need to be enforced and explicitly declared.
// Right now they are automatically derived
for _, ri := range r.RelationshipNodes {
for _, c := range ri.Children {
c.ParentNode = ri
c.ChildNode, err = r.FindNodeByGVK(c.Group, c.Version, c.Kind)
if err != nil {
r.RelationshipNodes = append(r.RelationshipNodes, c.ChildNode)
}
// Append the same parent-child relationship to child's parent node if it does not exist already
c.ChildNode.Parent, err = InsertIfNotExist(c.ChildNode.Parent, *c, ParentTypeKey)
if err != nil {
return nil, nil, err
}
}
for _, p := range ri.Parent {
p.ChildNode = ri
p.ParentNode, err = r.FindNodeByGVK(p.Group, p.Version, p.Kind)
if err != nil {
r.RelationshipNodes = append(r.RelationshipNodes, p.ParentNode)
}
// Append the same parent-child relationship to parent's child node if it does not exist already
p.ParentNode.Children, err = InsertIfNotExist(p.ParentNode.Children, *p, ChildTypeKey)
if err != nil {
return nil, nil, err
}
}
}
// Initialize the relationship graph based on GVK
relationshipGraphNodeHash := func(rgn RelationshipGraphNode) string {
return rgn.Group + "." + rgn.Version + "." + rgn.Kind
}
g := graph.New(relationshipGraphNodeHash, graph.Directed(), graph.PreventCycles())
// Add Vertices
for _, node := range r.RelationshipNodes {
log.Info("Adding Vertex", "nodeHash", node.GetHash())
_ = g.AddVertex(*node)
}
// Add Edges, requires all vertices to be present
for _, node := range r.RelationshipNodes {
for _, childRelation := range node.Children {
log.Info("Adding or updating Edge with type", "from", node.GetHash(), "to", childRelation.ChildNode.GetHash(), "type", childRelation.Type)
if err := g.AddEdge(node.GetHash(), childRelation.ChildNode.GetHash(), graph.EdgeAttribute("type", childRelation.Type)); err != nil && !errors.Is(err, graph.ErrEdgeAlreadyExists) {
panic(err)
}
}
// Prevent duplicate edge
for _, parentRelation := range node.Parent {
log.Info("Adding or updating Edge with type", "from", parentRelation.ParentNode.GetHash(), "to", node.GetHash(), "type", parentRelation.Type)
if err := g.AddEdge(parentRelation.ParentNode.GetHash(), node.GetHash(), graph.EdgeAttribute("type", parentRelation.Type)); err != nil && !errors.Is(err, graph.ErrEdgeAlreadyExists) {
panic(err)
}
}
}
log.Info("Built-in graph completed.")
return g, &r, nil
}
// BuildRelationshipGraph builds the complete relationship graph including the built-in one and customer-specified one
func BuildRelationshipGraph(ctx context.Context, client *dynamic.DynamicClient) (graph.Graph[string, RelationshipGraphNode], *RelationshipGraph, error) {
res, rg, _ := BuildBuiltinRelationshipGraph(ctx, client)
// TODO: Also include customized relationship graph
return res, rg, nil
}
// InsertIfNotExist inserts relation into relationList only if it does not exist already
// This is used to auto-generate two-way relationships when they are not declared explicitly
func InsertIfNotExist(relationList []*Relationship, relation Relationship, relationshipDirection string) ([]*Relationship, error) {
relationToInsert := &relation
if relationshipDirection == ParentTypeKey {
// Append parent-child relationship to child's parent also
relationToInsert.Group = relation.ParentNode.Group
relationToInsert.Version = relation.ParentNode.Version
relationToInsert.Kind = relation.ParentNode.Kind
relationToInsert.ParentNode = relation.ParentNode
relationToInsert.ChildNode = relation.ChildNode
relationToInsert.AutoGenerated = true
} else if relationshipDirection == ChildTypeKey {
// Append parent-child relationship to parent's children also
relationToInsert.Group = relation.ChildNode.Group
relationToInsert.Version = relation.ChildNode.Version
relationToInsert.Kind = relation.ChildNode.Kind
relationToInsert.ParentNode = relation.ParentNode
relationToInsert.ChildNode = relation.ChildNode
relationToInsert.AutoGenerated = true
}
// Append only if the relationship does not exist already
for _, r := range relationList {
if RelationshipEquals(r, &relation) {
klog.Infof("Relationship between %s and %s already exists. Skipping...", r.Kind, relation.Kind)
return relationList, nil
}
}
relationList = append(relationList, relationToInsert)
return relationList, nil
}
// RelationshipEquals returns true if two relationships are equal
func RelationshipEquals(r, relation *Relationship) bool {
return r.Group == relation.Group && r.Version == relation.Version && r.Kind == relation.Kind && r.Type == relation.Type && reflect.DeepEqual(r.JSONPath, relation.JSONPath)
}
// CountRelationshipGraph returns the same RelationshipGraph with the count for each resource
func (rg *RelationshipGraph) CountRelationshipGraph(ctx context.Context, dynamicClient *dynamic.DynamicClient, discoveryClient *discovery.DiscoveryClient, countNamespace string) (*RelationshipGraph, error) {
log := ctxutil.GetLogger(ctx)
for _, node := range rg.RelationshipNodes {
var resList *unstructured.UnstructuredList
var resCount int
resGVR, err := topologyutil.GetGVRFromGVK(schema.GroupVersion{Group: node.Group, Version: node.Version}.String(), node.Kind)
if err != nil {
return rg, err
}
if countNamespace == "" {
resList, err = dynamicClient.Resource(resGVR).List(ctx, metav1.ListOptions{})
} else if countNamespace != "" && GVRNamespaced(resGVR, *discoveryClient) {
resList, err = dynamicClient.Resource(resGVR).Namespace(countNamespace).List(ctx, metav1.ListOptions{})
} else {
continue
}
if k8serrors.IsNotFound(err) {
resCount = 0
} else if err != nil {
return rg, err
} else {
resCount = len(resList.Items)
}
log.Info("Counted resources for Vertex", "node", node.GetHash(), "count", resCount)
node.ResourceCount = resCount
}
return rg, nil
}
// GVRNamespaced returns true if a given GVR is namespaced based on the result of discovery client
func GVRNamespaced(gvr schema.GroupVersionResource, discoveryClient discovery.DiscoveryClient) bool {
apiResourceList, err := discoveryClient.ServerResourcesForGroupVersion(gvr.GroupVersion().String())
if err != nil {
return false
}
// Iterate over the APIResources to find the one that matches the Resource and determine if it is namespaced
for _, apiResource := range apiResourceList.APIResources {
if apiResource.Name == gvr.Resource {
if apiResource.Namespaced {
return true
} else {
return false
}
}
}
return false
}
// CountRelationshipGraphByCustomResourceGroup returns the same RelationshipGraph with the count for each custom resource group
func (rg *RelationshipGraph) CountRelationshipGraphByCustomResourceGroup(ctx context.Context, cl storage.SearchStorage, resourceGroup *entity.ResourceGroup, name string) (*RelationshipGraph, error) {
log := ctxutil.GetLogger(ctx)
if len(resourceGroup.Kind) != 0 {
return &RelationshipGraph{
RelationshipNodes: []*RelationshipGraphNode{},
}, nil
}
if len(resourceGroup.APIVersion) != 0 {
return nil, errors.New("apiVersion should be empty")
}
for _, node := range rg.RelationshipNodes {
kvs, err := elasticsearch.ConvertResourceGroup2Map(resourceGroup)
if err != nil {
return nil, errors.Wrap(err, "failed to convert resource group to map")
}
kvs["apiVersion"] = schema.GroupVersion{Group: node.Group, Version: node.Version}.String()
kvs["kind"] = node.Kind
kvs["cluster"] = name
sr, err := cl.SearchByTerms(ctx, kvs, nil)
if err != nil {
return rg, err
}
log.Info("Counted resources for Vertex", "node", node.GetHash(), "count", sr.Total)
node.ResourceCount = sr.Total
}
return rg, nil
}