This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
pod.go
248 lines (228 loc) · 8.14 KB
/
pod.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
/*
* Copyright (c) 2018 VMware Inc. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*
* 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 models
import (
"fmt"
"time"
log "github.com/Sirupsen/logrus"
"github.com/dgraph-io/dgo/protos/api"
"github.com/vmware/purser/pkg/controller/dgraph"
api_v1 "k8s.io/api/core/v1"
)
// Dgraph Model Constants
const (
IsPod = "isPod"
)
// Pod schema in dgraph
type Pod struct {
dgraph.ID
IsPod bool `json:"isPod,omitempty"`
Name string `json:"name,omitempty"`
StartTime string `json:"startTime,omitempty"`
EndTime string `json:"endTime,omitempty"`
Containers []*Container `json:"containers,omitempty"`
Pods []*Pod `json:"pod,omitempty"`
Count float64 `json:"pod|count,omitempty"`
Node *Node `json:"node,omitempty"`
Namespace *Namespace `json:"namespace,omitempty"`
Deployment *Deployment `json:"deployment,omitempty"`
Replicaset *Replicaset `json:"replicaset,omitempty"`
Statefulset *Statefulset `json:"statefulset,omitempty"`
Daemonset *Daemonset `json:"daemonset,omitempty"`
Job *Job `json:"job,omitempty"`
Pvcs []*PersistentVolumeClaim `json:"pvc,omitempty"`
CPURequest float64 `json:"cpuRequest,omitempty"`
CPULimit float64 `json:"cpuLimit,omitempty"`
MemoryRequest float64 `json:"memoryRequest,omitempty"`
MemoryLimit float64 `json:"memoryLimit,omitempty"`
StorageRequest float64 `json:"storageRequest,omitempty"`
Type string `json:"type,omitempty"`
Cid []Service `json:"cid,omitempty"`
}
// Metrics ...
type Metrics struct {
CPURequest float64
CPULimit float64
MemoryRequest float64
MemoryLimit float64
}
// newPod creates a new node for the pod in the Dgraph
func newPod(k8sPod api_v1.Pod) (*api.Assigned, error) {
pod := Pod{
Name: "pod-" + k8sPod.Name,
IsPod: true,
Type: "pod",
ID: dgraph.ID{Xid: k8sPod.Namespace + ":" + k8sPod.Name},
StartTime: k8sPod.GetCreationTimestamp().Time.Format(time.RFC3339),
}
nodeUID, err := createOrGetNodeByID(k8sPod.Spec.NodeName)
if err == nil {
pod.Node = &Node{ID: dgraph.ID{UID: nodeUID, Xid: k8sPod.Spec.NodeName}}
}
namespaceUID := CreateOrGetNamespaceByID(k8sPod.Namespace)
if namespaceUID != "" {
pod.Namespace = &Namespace{ID: dgraph.ID{UID: namespaceUID, Xid: k8sPod.Namespace}}
}
pod.Pvcs, pod.StorageRequest = getPodVolumes(k8sPod)
setPodOwners(&pod, k8sPod)
return dgraph.MutateNode(pod, dgraph.CREATE)
}
// StorePod updates the pod details and create it a new node if not exists.
// It also populates Containers of a pod.
func StorePod(k8sPod api_v1.Pod) error {
xid := k8sPod.Namespace + ":" + k8sPod.Name
uid := dgraph.GetUID(xid, IsPod)
var pod Pod
if uid == "" {
assigned, err := newPod(k8sPod)
if err != nil {
return err
}
log.Infof("Pod with xid: (%s) persisted in dgraph", xid)
uid = assigned.Uids["blank-0"]
}
podDeletedTimestamp := k8sPod.GetDeletionTimestamp()
if !podDeletedTimestamp.IsZero() {
pod = Pod{
ID: dgraph.ID{Xid: xid, UID: uid},
EndTime: podDeletedTimestamp.Time.Format(time.RFC3339),
}
deleteContainersInTerminatedPod(pod.Containers, podDeletedTimestamp.Time)
} else {
namespaceUID := CreateOrGetNamespaceByID(k8sPod.Namespace)
containers, metrics := StoreAndRetrieveContainersAndMetrics(k8sPod, uid, namespaceUID)
pod = Pod{
ID: dgraph.ID{Xid: xid, UID: uid},
Containers: containers,
CPURequest: metrics.CPURequest,
CPULimit: metrics.CPULimit,
MemoryRequest: metrics.MemoryRequest,
MemoryLimit: metrics.MemoryLimit,
}
}
_, err := dgraph.MutateNode(pod, dgraph.UPDATE)
return err
}
// StorePodsInteraction store the pod interactions in Dgraph
func StorePodsInteraction(sourcePodXID string, destinationPodsXIDs []string, counts []float64) error {
uid := dgraph.GetUID(sourcePodXID, IsPod)
if uid == "" {
log.Println("Source Pod " + sourcePodXID + " is not persisted yet.")
return fmt.Errorf("source pod: %s is not persisted yet", sourcePodXID)
}
pods := retrievePodsWithCountAsEdgeWeightFromPodsXIDs(destinationPodsXIDs, counts)
source := Pod{
ID: dgraph.ID{UID: uid, Xid: sourcePodXID},
Pods: pods,
}
_, err := dgraph.MutateNode(source, dgraph.UPDATE)
return err
}
func retrievePodsFromPodsXIDs(podsXIDs []string) []*Pod {
pods := []*Pod{}
for _, podXID := range podsXIDs {
podUID := dgraph.GetUID(podXID, IsPod)
if podUID == "" {
log.Debugf("Pod uid is empty for pod xid: %s", podXID)
continue
}
pod := &Pod{
ID: dgraph.ID{UID: podUID, Xid: podXID},
}
pods = append(pods, pod)
}
return pods
}
func retrievePodsWithCountAsEdgeWeightFromPodsXIDs(podsXIDs []string, counts []float64) []*Pod {
pods := []*Pod{}
for index, podXID := range podsXIDs {
podUID := dgraph.GetUID(podXID, IsPod)
if podUID == "" {
log.Printf("Destination pod: %s is not persisted yet", podXID)
continue
}
pod := &Pod{
ID: dgraph.ID{UID: podUID, Xid: podXID},
Count: counts[index],
}
pods = append(pods, pod)
}
return pods
}
// nolint: gocyclo
func setPodOwners(pod *Pod, k8sPod api_v1.Pod) {
owners := k8sPod.GetObjectMeta().GetOwnerReferences()
if owners == nil {
return
}
for _, owner := range owners {
if owner.Kind == "Deployment" {
deploymentXID := k8sPod.Namespace + ":" + owner.Name
deploymentUID := CreateOrGetDeploymentByID(deploymentXID)
if deploymentUID != "" {
pod.Deployment = &Deployment{ID: dgraph.ID{UID: deploymentUID, Xid: deploymentXID}}
}
} else if owner.Kind == "ReplicaSet" {
replicasetXID := k8sPod.Namespace + ":" + owner.Name
replicasetUID := CreateOrGetReplicasetByID(replicasetXID)
if replicasetUID != "" {
pod.Replicaset = &Replicaset{ID: dgraph.ID{UID: replicasetUID, Xid: replicasetXID}}
}
} else if owner.Kind == "StatefulSet" {
statefulsetXID := k8sPod.Namespace + ":" + owner.Name
statefulsetUID := CreateOrGetStatefulsetByID(statefulsetXID)
if statefulsetUID != "" {
pod.Statefulset = &Statefulset{ID: dgraph.ID{UID: statefulsetUID, Xid: statefulsetXID}}
}
} else if owner.Kind == "Job" {
jobXID := k8sPod.Namespace + ":" + owner.Name
jobUID := CreateOrGetJobByID(jobXID)
if jobUID != "" {
pod.Job = &Job{ID: dgraph.ID{UID: jobUID, Xid: jobXID}}
}
} else if owner.Kind == "DaemonSet" {
daemonsetXID := k8sPod.Namespace + ":" + owner.Name
daemonsetUID := CreateOrGetDaemonsetByID(daemonsetXID)
if daemonsetUID != "" {
pod.Daemonset = &Daemonset{ID: dgraph.ID{UID: daemonsetUID, Xid: daemonsetXID}}
}
} else {
log.Error("Unknown owner type " + owner.Kind + " for pod.")
}
}
}
func getPodVolumes(k8sPod api_v1.Pod) ([]*PersistentVolumeClaim, float64) {
podVolumes := []*PersistentVolumeClaim{}
storage := 0.0
for j := 0; j < len(k8sPod.Spec.Volumes); j++ {
vol := k8sPod.Spec.Volumes[j]
if vol.PersistentVolumeClaim != nil {
pvcXID := k8sPod.Namespace + ":" + vol.PersistentVolumeClaim.ClaimName
pvcUID := CreateOrGetPersistentVolumeClaimByID(pvcXID)
if pvcUID != "" {
podVolumes = append(podVolumes, &PersistentVolumeClaim{ID: dgraph.ID{UID: pvcUID, Xid: pvcXID}})
pvc, err := getPVCFromUID(pvcUID)
if err == nil {
storage += pvc.StorageCapacity
} else {
log.Errorf("error while getting pvc from uid: (%v), error: (%v)", pvcUID, err)
}
}
}
}
return podVolumes, storage
}