This repository has been archived by the owner on Mar 13, 2019. It is now read-only.
forked from elastic/beats
/
types.go
139 lines (124 loc) · 4.33 KB
/
types.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
package kubernetes
import (
"encoding/json"
"strings"
"github.com/elastic/beats/libbeat/logp"
corev1 "github.com/ericchiang/k8s/api/v1"
)
type ObjectMeta struct {
Annotations map[string]string `json:"annotations"`
CreationTimestamp string `json:"creationTimestamp"`
DeletionTimestamp string `json:"deletionTimestamp"`
GenerateName string `json:"generateName"`
Labels map[string]string `json:"labels"`
Name string `json:"name"`
Namespace string `json:"namespace"`
OwnerReferences []struct {
APIVersion string `json:"apiVersion"`
Controller bool `json:"controller"`
Kind string `json:"kind"`
Name string `json:"name"`
UID string `json:"uid"`
} `json:"ownerReferences"`
ResourceVersion string `json:"resourceVersion"`
SelfLink string `json:"selfLink"`
UID string `json:"uid"`
}
type Container struct {
Image string `json:"image"`
ImagePullPolicy string `json:"imagePullPolicy"`
Name string `json:"name"`
Ports []ContainerPort `json:"ports"`
Resources struct{} `json:"resources"`
TerminationMessagePath string `json:"terminationMessagePath"`
VolumeMounts []struct {
MountPath string `json:"mountPath"`
Name string `json:"name"`
ReadOnly bool `json:"readOnly"`
} `json:"volumeMounts"`
}
type ContainerPort struct {
Name string `json:"name"`
ContainerPort int64 `json:"containerPort"`
Protocol string `json:"protocol"`
}
type PodSpec struct {
Containers []Container `json:"containers"`
DNSPolicy string `json:"dnsPolicy"`
NodeName string `json:"nodeName"`
RestartPolicy string `json:"restartPolicy"`
SecurityContext struct{} `json:"securityContext"`
ServiceAccount string `json:"serviceAccount"`
ServiceAccountName string `json:"serviceAccountName"`
TerminationGracePeriodSeconds int64 `json:"terminationGracePeriodSeconds"`
}
type PodStatusCondition struct {
LastProbeTime interface{} `json:"lastProbeTime"`
LastTransitionTime string `json:"lastTransitionTime"`
Status string `json:"status"`
Type string `json:"type"`
}
type PodContainerStatus struct {
ContainerID string `json:"containerID"`
Image string `json:"image"`
ImageID string `json:"imageID"`
LastState struct {
Terminated struct {
ContainerID string `json:"containerID"`
ExitCode int64 `json:"exitCode"`
FinishedAt string `json:"finishedAt"`
Reason string `json:"reason"`
StartedAt string `json:"startedAt"`
} `json:"terminated"`
} `json:"lastState"`
Name string `json:"name"`
Ready bool `json:"ready"`
RestartCount int64 `json:"restartCount"`
State struct {
Running struct {
StartedAt string `json:"startedAt"`
} `json:"running"`
} `json:"state"`
}
type PodStatus struct {
Conditions []PodStatusCondition `json:"conditions"`
ContainerStatuses []PodContainerStatus `json:"containerStatuses"`
InitContainerStatuses []PodContainerStatus `json:"initContainerStatuses"`
HostIP string `json:"hostIP"`
Phase string `json:"phase"`
PodIP string `json:"podIP"`
StartTime string `json:"startTime"`
}
type Pod struct {
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata ObjectMeta `json:"metadata"`
Spec PodSpec `json:"spec"`
Status PodStatus `json:"status"`
}
// GetContainerID parses the container ID to get the actual ID string
func (s *PodContainerStatus) GetContainerID() string {
cID := s.ContainerID
if cID != "" {
parts := strings.Split(cID, "//")
if len(parts) == 2 {
return parts[1]
}
}
return ""
}
// GetPod converts Pod to our own type
func GetPod(pod *corev1.Pod) *Pod {
bytes, err := json.Marshal(pod)
if err != nil {
logp.Warn("Unable to marshal %v", pod.String())
return nil
}
po := &Pod{}
err = json.Unmarshal(bytes, po)
if err != nil {
logp.Warn("Unable to marshal %v", pod.String())
return nil
}
return po
}