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
/
container.go
151 lines (138 loc) · 5.32 KB
/
container.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
/*
* 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"
"github.com/vmware/purser/pkg/controller/utils"
api_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
// Dgraph Model Constants
const (
IsContainer = "isContainer"
)
// Container schema in dgraph
type Container struct {
dgraph.ID
IsContainer bool `json:"isContainer,omitempty"`
Name string `json:"name,omitempty"`
StartTime string `json:"startTime,omitempty"`
EndTime string `json:"endTime,omitempty"`
Pod Pod `json:"pod,omitempty"`
Procs []*Proc `json:"procs,omitempty"`
Namespace *Namespace `json:"namespace,omitempty"`
CPURequest float64 `json:"cpuRequest,omitempty"`
CPULimit float64 `json:"cpuLimit,omitempty"`
MemoryRequest float64 `json:"memoryRequest,omitempty"`
MemoryLimit float64 `json:"memoryLimit,omitempty"`
Type string `json:"type,omitempty"`
}
func newContainer(container api_v1.Container, podUID, namespaceUID string, pod api_v1.Pod) (*api.Assigned, error) {
containerXid := pod.Namespace + ":" + pod.Name + ":" + container.Name
requests := container.Resources.Requests
limits := container.Resources.Limits
c := &Container{
ID: dgraph.ID{Xid: containerXid},
Name: "container-" + container.Name,
IsContainer: true,
Type: "container",
StartTime: pod.GetCreationTimestamp().Time.Format(time.RFC3339),
Pod: Pod{ID: dgraph.ID{UID: podUID, Xid: pod.Namespace + ":" + pod.Name}},
CPURequest: utils.ConvertToFloat64CPU(requests.Cpu()),
CPULimit: utils.ConvertToFloat64CPU(limits.Cpu()),
MemoryRequest: utils.ConvertToFloat64GB(requests.Memory()),
MemoryLimit: utils.ConvertToFloat64GB(limits.Memory()),
}
if namespaceUID != "" {
c.Namespace = &Namespace{ID: dgraph.ID{UID: namespaceUID, Xid: pod.Namespace}}
}
return dgraph.MutateNode(c, dgraph.CREATE)
}
// StoreAndRetrieveContainersAndMetrics fetchs the list of containers in given pod
// Create a new container in dgraph if container is not in it.
func StoreAndRetrieveContainersAndMetrics(pod api_v1.Pod, podUID, namespaceUID string) ([]*Container, Metrics) {
containers := []*Container{}
cpuRequest := &resource.Quantity{}
memoryRequest := &resource.Quantity{}
cpuLimit := &resource.Quantity{}
memoryLimit := &resource.Quantity{}
for _, c := range pod.Spec.Containers {
container, err := storeContainerIfNotExist(c, pod, podUID, namespaceUID)
if err == nil {
containers = append(containers, container)
requests := c.Resources.Requests
limits := c.Resources.Limits
utils.AddResourceAToResourceB(requests.Cpu(), cpuRequest)
utils.AddResourceAToResourceB(requests.Memory(), memoryRequest)
utils.AddResourceAToResourceB(limits.Cpu(), cpuLimit)
utils.AddResourceAToResourceB(limits.Memory(), memoryLimit)
}
}
return containers, Metrics{
CPURequest: utils.ConvertToFloat64CPU(cpuRequest),
CPULimit: utils.ConvertToFloat64CPU(cpuLimit),
MemoryRequest: utils.ConvertToFloat64GB(memoryRequest),
MemoryLimit: utils.ConvertToFloat64GB(memoryLimit),
}
}
// StoreContainerProcessEdge ...
func StoreContainerProcessEdge(containerXID string, procsXIDs []string) error {
containerUID := dgraph.GetUID(containerXID, IsContainer)
if containerUID == "" {
return fmt.Errorf("Container: %s not persisted in dgraph", containerXID)
}
procs := retrieveProcessesFromProcessesXIDs(procsXIDs)
container := Container{
ID: dgraph.ID{UID: containerUID, Xid: containerXID},
Procs: procs,
}
_, err := dgraph.MutateNode(container, dgraph.UPDATE)
return err
}
func storeContainerIfNotExist(c api_v1.Container, pod api_v1.Pod, podUID, namespaceUID string) (*Container, error) {
podXid := pod.Namespace + ":" + pod.Name
containerXid := podXid + ":" + c.Name
containerUID := dgraph.GetUID(containerXid, IsContainer)
var container *Container
if containerUID == "" {
assigned, err := newContainer(c, podUID, namespaceUID, pod)
if err != nil {
log.Errorf("Unable to create container: %s", containerXid)
return container, err
}
log.Infof("Container with xid: (%s) persisted in dgraph", containerXid)
containerUID = assigned.Uids["blank-0"]
}
container = &Container{
ID: dgraph.ID{UID: containerUID, Xid: containerXid},
}
return container, nil
}
func deleteContainersInTerminatedPod(containers []*Container, endTime time.Time) {
for _, container := range containers {
container.EndTime = endTime.Format(time.RFC3339)
}
_, err := dgraph.MutateNode(containers, dgraph.UPDATE)
if err != nil {
log.Error(err)
}
deleteProcessesInTerminatedContainers(containers)
}