forked from kyma-project/kyma
/
deployment_converter.go
81 lines (67 loc) · 2.12 KB
/
deployment_converter.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
package k8s
import (
"github.com/kyma-project/kyma/components/ui-api-layer/internal/gqlschema"
api "k8s.io/api/apps/v1beta2"
)
type deploymentConverter struct{}
func (c *deploymentConverter) ToGQL(in *api.Deployment) *gqlschema.Deployment {
if in == nil {
return nil
}
labels := make(gqlschema.JSON)
for k, v := range in.Labels {
labels[k] = v
}
return &gqlschema.Deployment{
Name: in.Name,
Environment: in.Namespace,
CreationTimestamp: in.CreationTimestamp.Time,
Labels: labels,
Status: c.toGQLStatus(*in),
Containers: c.toGQLContainers(*in),
}
}
func (c *deploymentConverter) ToGQLs(in []*api.Deployment) []gqlschema.Deployment {
var result []gqlschema.Deployment
for _, item := range in {
converted := c.ToGQL(item)
if converted != nil {
result = append(result, *converted)
}
}
return result
}
func (c *deploymentConverter) toGQLStatus(in api.Deployment) gqlschema.DeploymentStatus {
var conditions []gqlschema.DeploymentCondition
for _, condition := range in.Status.Conditions {
conditions = append(conditions, c.toGQLCondition(condition))
}
return gqlschema.DeploymentStatus{
AvailableReplicas: int(in.Status.AvailableReplicas),
ReadyReplicas: int(in.Status.ReadyReplicas),
Replicas: int(in.Status.Replicas),
UpdatedReplicas: int(in.Status.UpdatedReplicas),
Conditions: conditions,
}
}
func (c *deploymentConverter) toGQLCondition(in api.DeploymentCondition) gqlschema.DeploymentCondition {
return gqlschema.DeploymentCondition{
Reason: in.Reason,
Message: in.Message,
LastUpdateTimestamp: in.LastUpdateTime.Time,
LastTransitionTimestamp: in.LastTransitionTime.Time,
Type: string(in.Type),
Status: string(in.Status),
}
}
func (c *deploymentConverter) toGQLContainers(in api.Deployment) []gqlschema.Container {
var containers []gqlschema.Container
for _, container := range in.Spec.Template.Spec.Containers {
gqlContainer := gqlschema.Container{
Name: container.Name,
Image: container.Image,
}
containers = append(containers, gqlContainer)
}
return containers
}