/
k8s.go
161 lines (139 loc) · 5.14 KB
/
k8s.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
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.,
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 common
import (
"encoding/json"
"strings"
"time"
"github.com/Tencent/bk-bcs/bcs-common/common/blog"
k8scorev1 "k8s.io/api/core/v1"
k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// K8SContainerPort simplified for k8s container port
type K8SContainerPort struct {
Name string `json:"name"`
HostPort int32 `json:"hostPort"`
ContainerPort int32 `json:"containerPort"`
Protocol string `json:"protocol"`
HostIP string `json:"hostIP"`
}
// K8SContainer simplified data for k8s container
type K8SContainer struct {
Name string `json:"name"`
Image string `json:"image"`
Ports []K8SContainerPort `json:"ports,omitempty"`
Resources k8scorev1.ResourceRequirements `json:"resources,omitempty"`
}
// K8SPodSpec simplified data of k8s pod spec
type K8SPodSpec struct {
Containers []K8SContainer `json:"containers,omitempty"`
DNSPolicy k8scorev1.DNSPolicy `json:"dnsPolicy,omitempty"`
NodeSelector map[string]string `json:"nodeSelector"`
NodeName string `json:"nodeName"`
HostNetwork bool `json:"hostNetwork"`
Hostname string `json:"hostname"`
}
// K8SContainerStatus simplified data of container status
type K8SContainerStatus struct {
Name string `json:"name"`
Ready bool `json:"ready"`
RestartCount int32 `json:"restartCount"`
ContainerID string `json:"containerID"`
}
// K8SPodStatus simplified data of k8s pod status
type K8SPodStatus struct {
Phase string `json:"phase"`
Reason string `json:"reason"`
HostIP string `json:"hostIP"`
PodIP string `json:"podIP"`
StartTime *k8smetav1.Time `json:"startTime"`
ContainerStatuses []K8SContainerStatus `json:"containerStatuses"`
}
// K8SPod k8s pod in storage
type K8SPod struct {
ID string `json:"id"`
k8smetav1.ObjectMeta `json:"metadata"`
Spec K8SPodSpec `json:"spec"`
Status K8SPodStatus `json:"status"`
}
// GetCreationTimestamp get creation timestamp
func (kp *K8SPod) GetCreationTimestamp() time.Time {
return kp.ObjectMeta.CreationTimestamp.Time
}
// SetCreationTimestamp set creation timestamp
func (kp *K8SPod) SetCreationTimestamp(t time.Time) {
kp.ObjectMeta.CreationTimestamp.Time = t
}
func convertK8STime(k8sTime string) string {
output := strings.Replace(k8sTime, "T", " ", -1)
output = output[0:19]
return output
}
// ConvertK8SPod convert k8s pod to bk cmdb pod
func ConvertK8SPod(cluster string, kPod *K8SPod) (*Pod, error) {
newPod := &Pod{}
newPod.PodName = kPod.GetName()
newPod.PodNamespace = kPod.GetNamespace()
newPod.PodCluster = cluster
newPod.PodClusterType = ClusterTypeK8S
newPod.PodUUID = string(kPod.GetUID())
if len(kPod.OwnerReferences) != 0 {
newPod.WorkloadName = kPod.OwnerReferences[0].Name
newPod.WorkloadType = kPod.OwnerReferences[0].Kind
}
labelBytes, err := json.Marshal(kPod.Labels)
if err != nil {
blog.Errorf("k8d pod encoding labels failed, err %s", err.Error())
return nil, err
}
newPod.PodLabels = string(labelBytes)
if kPod.Annotations != nil {
annotationBytes, err := json.Marshal(kPod.Annotations)
if err != nil {
blog.Errorf("k8d pod encoding annotation failed, err %s", err.Error())
return nil, err
}
newPod.PodAnnotations = string(annotationBytes)
} else {
newPod.PodAnnotations = "{}"
}
newPod.PodIP = kPod.Status.PodIP
newPod.PodStatus = string(kPod.Status.Phase)
if kPod.Spec.HostNetwork {
newPod.PodNetworkMode = K8SNetworkModeHost
} else {
newPod.PodNetworkMode = K8SNetworkModeCni
}
newPod.PodNetworkType = K8SNetworkTypeCni
var containerData ContainerData
for _, c := range kPod.Spec.Containers {
cStr, err := json.Marshal(&c)
if err != nil {
blog.Errorf("k8s pod encoding container failed, err %s", err.Error())
return nil, err
}
containerData.Data = append(containerData.Data, string(cStr))
}
for _, c := range kPod.Status.ContainerStatuses {
cStr, err := json.Marshal(&c)
if err != nil {
blog.Errorf("k8s pod encoding container status failed, err %s", err.Error())
return nil, err
}
containerData.Status = append(containerData.Status, string(cStr))
}
newPod.PodCreateTime = convertK8STime(kPod.CreationTimestamp.String())
if kPod.Status.StartTime != nil {
newPod.PodStartTime = convertK8STime(kPod.Status.StartTime.String())
}
return newPod, nil
}