-
Notifications
You must be signed in to change notification settings - Fork 0
/
xd.go
178 lines (158 loc) · 5.63 KB
/
xd.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
package k8s
import (
"encoding/json"
"fmt"
"time"
)
//XD contains the common, available data for all resources
type XD struct {
*Metadata `json:"metadata,omitempty"`
*Spec `json:"spec,omitempty"`
*Status `json:"status,omitempty"`
}
// Metadata contains the available metadata for all resources
type Metadata struct {
Name string `json:"name,omitempty"`
Created time.Time `json:"creationTimestamp,omitempty"`
Namespace string `json:"namespace,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Selflink string `json:"selfLink,omitempty"`
UID string `json:"uid,omitempty"`
OwnerReferences []OwnerReferences `json:"ownerReferences,omitempty"`
}
//OwnerReferences struct here.
type OwnerReferences struct {
OwnerKind string `json:"kind,omitempty"`
OwnerName string `json:"name,omitempty"`
OwnerUID string `json:"uid,omitempty"`
}
// Spec contains the available Spec data for all resources
type Spec struct {
ClusterIP string `json:"clusterIP,omitempty"`
ExternalID string `json:"externalID,omitempty"`
NodeName string `json:"nodeName,omitempty"`
Replicas int `json:"replicas,omitempty"`
Type string `json:"type,omitempty"`
Containers []Containers `json:"containers,omitempty"`
Ports []Ports `json:"ports,omitempty"`
}
// Containers struct definition:
type Containers struct {
ContainerName string `json:"name,omitempty"`
ContainerImage string `json:"image,omitempty"`
Ports []Ports `json:"ports,omitempty"`
}
// Ports struct definition:
type Ports struct {
ContainerPort int `json:"containerPort,omitempty"`
Port int `json:"port,omitempty"`
Protocol string `json:"protocol,omitempty"`
TargetPort int `json:"targetPort,omitempty"`
}
// Status contains the available Status data for all resources
type Status struct {
HostIP string `json:"hostIP,omitempty"`
Message string `json:"message,omitempty"`
Reason string `json:"reason,omitempty"`
Phase string `json:"phase,omitempty"`
PodIP string `json:"podIP,omitempty"`
AvailableReplicas int `json:"availableReplicas,omitempty"`
FullyLabeledReplicas int `json:"fullyLabeledReplicas,omitempty"`
ReadyReplicas int `json:"readyReplicas,omitempty"`
UnavailableReplicas int `json:"unavailableReplicas,omitempty"`
UpdatedReplicas int `json:"updatedReplicas,omitempty"`
Addresses []Addresses `json:"addresses,omitempty"`
ContainerStatuses []ContainerStatuses `json:"containerStatuses,omitempty"`
Conditions []Conditions `json:"conditions,omitempty"`
NodeInfo `json:"nodeInfo,omitempty"`
}
// Addresses definition:
type Addresses struct {
Address string `json:"address,omitempty"`
Type string `json:"type,omitempty"`
}
// Conditions definition:
type Conditions struct {
Status string `json:"status,omitempty"`
Message string `json:"message,omitempty"`
Reason string `json:"reason,omitempty"`
Type string `json:"type,omitempty"`
}
// ContainerStatuses definition (is []array):
type ContainerStatuses struct {
ContainerStatusName string `json:"name,omitempty"`
Ready bool `json:"ready"`
RestartCount int `json:"restartCount"`
*State `json:"state"`
}
// State definition
type State struct {
*Running `json:"running,omitempty"`
*Waiting `json:"waiting,omitempty"`
*Terminated `json:"terminated,omitempty"`
}
// Running definition
type Running struct {
Started time.Time `json:"startedAt,omitempty"`
}
// Waiting definition
type Waiting struct {
Message string `json:"message,omitempty"`
Reason string `json:"reason,omitempty"`
}
// Terminated definition
type Terminated struct {
Started time.Time `json:"startedAt,omitempty"`
Finished time.Time `json:"finishedAt,omitempty"`
Reason string `json:"reason,omitempty"`
//TerminatedReason string `json:"reason,omitempty"`
ExitCode int `json:"exitCode,omitempty"`
}
// NodeInfo struct definition:
type NodeInfo struct {
Architecture string `json:"architecture,omitempty"`
BootID string `json:"bootID,omitempty"`
ContainerRuntimeVersion string `json:"containerRuntimeVersion,omitempty"`
KernelVersion string `json:"kernelVersion,omitempty"`
KubeProxyVersion string `json:"kubeProxyVersion,omitempty"`
KubeletVersion string `json:"kubeletVersion,omitempty"`
MachineID string `json:"machineID,omitempty"`
OperatingSystem string `json:"operatingSystem,omitempty"`
OSImage string `json:"osImage,omitempty"`
SystemUUID string `json:"systemUUID,omitempty"`
}
// CreateXD takes json/output created by RawClient and creates an []XD struct if possible
func createXD(json []string) ([]XD, error) {
//var xds []XD
xds := make([]XD, 0, len(json))
var errd error
xdChan := make(chan XD, 100)
errChan := make(chan error, 100)
for _, j := range json {
go makeXD(j, xdChan, errChan)
}
for i := 0; i < len(json); i++ {
select {
case x := <-xdChan:
xds = append(xds, x)
case err := <-errChan:
errd = err
}
}
return xds, errd
}
func makeXD(data string, xdChan chan XD, errChan chan error) {
xd := XD{}
err := json.Unmarshal([]byte(data), &xd)
if err != nil {
errChan <- err
return
}
xdChan <- xd
return
}
// Raw returns the json output of XD
func (x *XD) Raw() string {
j, _ := json.Marshal(x)
return fmt.Sprintf("%s", j)
}