-
Notifications
You must be signed in to change notification settings - Fork 48
/
types.go
416 lines (347 loc) · 12 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright © 2023 Horizoncd.
//
// 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 cd
import (
applicationV1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/gitops-engine/pkg/health"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
regionmodels "github.com/horizoncd/horizon/pkg/region/models"
)
type GetStepParams struct {
Environment string
Cluster string
RegionEntity *regionmodels.RegionEntity
}
type GetClusterStateParams struct {
Environment string
Cluster string
RegionEntity *regionmodels.RegionEntity
}
type GetResourceTreeParams struct {
Environment string
Cluster string
RegionEntity *regionmodels.RegionEntity
}
type GetClusterStateV2Params struct {
Application string
Environment string
Cluster string
RegionEntity *regionmodels.RegionEntity
}
type CreateClusterParams struct {
Environment string
Cluster string
GitRepoURL string
ValueFiles []string
RegionEntity *regionmodels.RegionEntity
Namespace string
}
type DeployClusterParams struct {
Environment string
Cluster string
Revision string
Region string
}
type GetPodEventsParams struct {
RegionEntity *regionmodels.RegionEntity
Environment string
Cluster string
Namespace string
Pod string
}
type GetPodParams struct {
RegionEntity *regionmodels.RegionEntity
Cluster string
Namespace string
Pod string
}
type DeletePodsParams struct {
RegionEntity *regionmodels.RegionEntity
Namespace string
Pods []string
}
type DeleteClusterParams struct {
Environment string
Cluster string
Region string
}
type ExecuteActionParams struct {
RegionEntity *regionmodels.RegionEntity
Namespace string
Action string
GVR schema.GroupVersionResource
ResourceName string
ClusterID uint
}
type GetContainerLogParams struct {
RegionEntity *regionmodels.RegionEntity
Namespace string
Cluster string
Pod string
Container string
Environment string
TailLines int64
}
type ExecParams struct {
Commands []string
Environment string
Cluster string
RegionEntity *regionmodels.RegionEntity
Namespace string
PodList []string
}
type ExecResp struct {
key string
Result bool
Stdout string
Stderr string
Error error
}
type OperationResult struct {
Result bool
Error error
}
type containerList struct {
name string
containers []*corev1.Container
}
func (c *containerList) Len() int { return len(c.containers) }
func (c *containerList) Less(i, j int) bool {
if c.containers[i].Name == c.name {
return true
} else if c.containers[j].Name == c.name {
return false
}
a := c.containers[i].Name
b := c.containers[j].Name
return a <= b
}
func (c *containerList) Swap(i, j int) {
c.containers[i], c.containers[j] = c.containers[j], c.containers[i]
}
type ClusterStateV2 struct {
Status string `json:"status"`
}
// ClusterState cluster state
type ClusterState struct {
// Status:
// Processing
// Healthy
// Suspended
// Degraded
// Missing
// Unknown
Status health.HealthStatusCode `json:"status,omitempty" yaml:"status,omitempty"`
// Step
Step *Step `json:"step"`
// Replicas the actual number of replicas running in k8s
Replicas int `json:"replicas,omitempty" yaml:"replicas,omitempty"`
// DesiredReplicas desired replicas
DesiredReplicas *int `json:"desiredReplicas,omitempty" yaml:"desiredReplicas,omitempty"`
// PodTemplateHash
PodTemplateHash string `json:"podTemplateHash,omitempty" yaml:"podTemplateHash,omitempty"`
// PodTemplateHashKey label key from Deployment or Rollout
PodTemplateHashKey string `json:"podTemplateHashKey,omitempty" yaml:"podTemplateHashKey,omitempty"`
// Revision the desired revision
Revision string `json:"revision,omitempty" yaml:"revision,omitempty"`
// Versions versions detail
// key is pod-template-hash, if equal to PodTemplateHash, the version is the desired version
Versions map[string]*ClusterVersion `json:"versions,omitempty" yaml:"versions,omitempty"`
// ManualPaused indicates whether the cluster is in manual pause state
ManualPaused bool `json:"manualPaused" yaml:"manualPaused"`
}
type Step struct {
Index int `json:"index"`
Total int `json:"total"`
Replicas []int `json:"replicas"`
ManualPaused bool `json:"manualPaused"`
AutoPromote bool `json:"autoPromote"`
Extra *string `json:"extra"`
}
// ClusterVersion version information
type ClusterVersion struct {
// Replicas the replicas of this revision
Replicas int `json:"replicas,omitempty" yaml:"replicas,omitempty"`
Revision string `json:"revision,omitempty" yaml:"revision,omitempty"`
// Pods the pods detail of this revision, the key is pod name
Pods map[string]*ClusterPod `json:"pods,omitempty" yaml:"pods,omitempty"`
}
// ClusterPod pod detail
type ClusterPod struct {
Metadata PodMetadata `json:"metadata,omitempty" yaml:"metadata,omitempty"`
Spec PodSpec `json:"spec,omitempty" yaml:"spec,omitempty"`
Status PodStatus `json:"status,omitempty" yaml:"status,omitempty"`
DeletionTimestamp *metav1.Time `json:"deletionTimestamp,omitempty"`
}
type PodMetadata struct {
CreationTimestamp metav1.Time `json:"creationTimestamp"`
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
}
type PodSpec struct {
NodeName string `json:"nodeName,omitempty" yaml:"nodeName,omitempty"`
InitContainers []*Container `json:"initContainers,omitempty" yaml:"initContainers,omitempty"`
Containers []*Container `json:"containers,omitempty" yaml:"containers,omitempty"`
}
type Container struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Image string `json:"image,omitempty" yaml:"image,omitempty"`
}
type PodStatus struct {
HostIP string `json:"hostIP,omitempty" yaml:"hostIP,omitempty"`
PodIP string `json:"podIP,omitempty" yaml:"podIP,omitempty"`
Phase string `json:"phase,omitempty" yaml:"phase,omitempty"`
Events []Event `json:"events,omitempty" yaml:"events,omitempty"`
ContainerStatuses []*ContainerStatus `json:"containerStatuses,omitempty" yaml:"containerStatuses,omitempty"`
LifeCycle []*LifeCycleItem `json:"lifeCycle" yaml:"lifeCycle"`
}
type ContainerStatus struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Ready bool `json:"ready" yaml:"ready"`
RestartCount int32 `json:"restartCount"`
State ContainerState `json:"state" yaml:"state"`
ImageID string `json:"imageID" yaml:"imageID"`
}
type Event struct {
Type string `json:"type" yaml:"type"`
Reason string `json:"reason,omitempty" yaml:"reason,omitempty"`
Message string `json:"message,omitempty" yaml:"message,omitempty"`
Count int32 `json:"count,omitempty" yaml:"count,omitempty"`
EventTimestamp metav1.Time `json:"eventTimestamp,omitempty" yaml:"eventTimestamp,omitempty"`
}
// ContainerDetail represents more information about a container
type ContainerDetail struct {
// Name of the container.
Name string `json:"name"`
// Image URI of the container.
Image string `json:"image"`
// List of environment variables.
Env []corev1.EnvVar `json:"env"`
// Commands of the container
Commands []string `json:"commands"`
// Command arguments
Args []string `json:"args"`
// Information about mounted volumes
VolumeMounts []VolumeMount `json:"volumeMounts"`
// Security configuration that will be applied to a container.
SecurityContext *corev1.SecurityContext `json:"securityContext"`
// Status of a pod container
Status *corev1.ContainerStatus `json:"status"`
// Probes
LivenessProbe *corev1.Probe `json:"livenessProbe"`
ReadinessProbe *corev1.Probe `json:"readinessProbe"`
StartupProbe *corev1.Probe `json:"startupProbe"`
}
type VolumeMount struct {
// Name of the variable.
Name string `json:"name"`
// Is the volume read only ?
ReadOnly bool `json:"readOnly"`
// Path within the container at which the volume should be mounted. Must not contain ':'.
MountPath string `json:"mountPath"`
// Path within the volume from which the container's volume should be mounted. Defaults to "" (volume's root).
SubPath string `json:"subPath"`
// Information about the Volume itself
Volume corev1.Volume `json:"volume"`
}
type ContainerState struct {
State string `json:"state" yaml:"state"`
Reason string `json:"reason" yaml:"reason"`
Message string `json:"message" yaml:"message"`
StartedAt *metav1.Time `json:"startedAt,omitempty"`
}
type LifeCycleItem struct {
Type string `json:"type" yaml:"type"`
Status string `json:"status" yaml:"status"`
Message string `json:"message" yaml:"status"`
CompleteTime metav1.Time `json:"completeTime" yaml:"completeTime"`
}
type ResourceNode struct {
applicationV1alpha1.ResourceNode
PodDetail *CompactPod
}
type ResourceTreeNode struct {
*applicationV1alpha1.ResourceNode
children []*ResourceTreeNode
}
// for pod compact
type CompactPodMetadata struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
CreationTimestamp metav1.Time `json:"creationTimestamp"`
DeletionTimestamp *metav1.Time `json:"deletionTimestamp"`
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations"`
}
type CompactContainer struct {
Name string `json:"name"`
Image string `json:"image"`
ReadinessProbe *corev1.Probe `json:"readinessProbe,omitempty"`
}
type CompactPodSpec struct {
InitContainers []CompactContainer `json:"initContainers"`
Containers []CompactContainer `json:"containers"`
}
type CompactPodStatus struct {
Phase corev1.PodPhase `json:"phase"`
Conditions []corev1.PodCondition `json:"conditions"`
PodIP string `json:"podIP"`
ContainerStatuses []corev1.ContainerStatus `json:"containerStatuses"`
}
type CompactPod struct {
Metadata CompactPodMetadata `json:"metadata"`
Spec CompactPodSpec `json:"spec"`
Status CompactPodStatus `json:"status"`
}
func Compact(pod corev1.Pod) CompactPod {
var (
containers []CompactContainer
initcontainers []CompactContainer
)
for _, container := range pod.Spec.Containers {
containers = append(containers, ContainerCompact(container))
}
for _, container := range pod.Spec.InitContainers {
initcontainers = append(initcontainers, ContainerCompact(container))
}
return CompactPod{
Metadata: CompactPodMetadata{
Name: pod.Name,
Namespace: pod.Namespace,
CreationTimestamp: pod.CreationTimestamp,
DeletionTimestamp: pod.DeletionTimestamp,
Labels: pod.Labels,
Annotations: pod.Annotations,
},
Spec: CompactPodSpec{
Containers: containers,
InitContainers: initcontainers,
},
Status: CompactPodStatus{
Phase: pod.Status.Phase,
Conditions: pod.Status.Conditions,
PodIP: pod.Status.PodIP,
ContainerStatuses: pod.Status.ContainerStatuses,
},
}
}
func ContainerCompact(container corev1.Container) CompactContainer {
return CompactContainer{
Name: container.Name,
Image: container.Image,
ReadinessProbe: container.ReadinessProbe,
}
}