/
pod_template.go
184 lines (147 loc) · 5.12 KB
/
pod_template.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
/*
* Copyright (c) 2019 Kubenext, Inc. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/kubenext/kubeon/pkg/view/component"
"github.com/kubenext/kubeon/pkg/view/flexlayout"
)
type podTemplateLayoutOptions struct {
parent runtime.Object
containers []corev1.Container
podTemplateSpec corev1.PodTemplateSpec
isInit bool
printOptions Options
}
type podTemplateFunc func(fl *flexlayout.FlexLayout, options podTemplateLayoutOptions) error
type PodTemplate struct {
parent runtime.Object
podTemplateSpec corev1.PodTemplateSpec
podTemplateHeaderFunc podTemplateFunc
podTemplateInitContainersFunc podTemplateFunc
podTemplateContainersFunc podTemplateFunc
podTemplatePodConfigurationFunc podTemplateFunc
}
func NewPodTemplate(parent runtime.Object, podTemplateSpec corev1.PodTemplateSpec) *PodTemplate {
return &PodTemplate{
parent: parent,
podTemplateSpec: podTemplateSpec,
podTemplateHeaderFunc: podTemplateHeader,
podTemplateInitContainersFunc: podTemplateContainers,
podTemplateContainersFunc: podTemplateContainers,
podTemplatePodConfigurationFunc: podTemplatePodConfiguration,
}
}
func (pt *PodTemplate) AddToFlexLayout(fl *flexlayout.FlexLayout, options Options) error {
if fl == nil {
return errors.New("flex layout is nil")
}
baseOptions := podTemplateLayoutOptions{
parent: pt.parent,
podTemplateSpec: pt.podTemplateSpec,
printOptions: options,
}
if err := pt.podTemplateHeaderFunc(fl, baseOptions); err != nil {
return errors.Wrap(err, "pod template header")
}
initContainerOptions := baseOptions
initContainerOptions.containers = pt.podTemplateSpec.Spec.InitContainers
initContainerOptions.isInit = true
if err := pt.podTemplateInitContainersFunc(fl, initContainerOptions); err != nil {
return errors.Wrap(err, "pod template init containers")
}
containerOptions := baseOptions
containerOptions.containers = pt.podTemplateSpec.Spec.Containers
containerOptions.isInit = false
if err := pt.podTemplateContainersFunc(fl, containerOptions); err != nil {
return errors.Wrap(err, "pod template containers")
}
if err := pt.podTemplatePodConfigurationFunc(fl, baseOptions); err != nil {
return errors.Wrap(err, "pod template pod configuration")
}
return nil
}
func podTemplateHeader(fl *flexlayout.FlexLayout, options podTemplateLayoutOptions) error {
headerSection := fl.AddSection()
podTemplateHeader := NewPodTemplateHeader(options.podTemplateSpec.ObjectMeta.Labels)
headerLabels := podTemplateHeader.Create()
if err := headerSection.Add(headerLabels, component.WidthFull); err != nil {
return errors.Wrap(err, "add pod template header")
}
return nil
}
func podTemplateContainers(fl *flexlayout.FlexLayout, options podTemplateLayoutOptions) error {
if len(options.containers) < 1 {
return nil
}
portForwarder := options.printOptions.DashConfig.PortForwarder()
containerSection := fl.AddSection()
width := component.WidthHalf
for index, container := range options.containers {
containerConfig := NewContainerConfiguration(options.parent, &container, portForwarder, options.isInit, options.printOptions)
summary, err := containerConfig.Create()
if err != nil {
return err
}
if len(options.containers)%2 != 0 && len(options.containers) == index+1 {
width = component.WidthFull
}
if err := containerSection.Add(summary, width); err != nil {
return errors.Wrap(err, "add container")
}
}
return nil
}
func podTemplatePodConfiguration(fl *flexlayout.FlexLayout, options podTemplateLayoutOptions) error {
podSection := fl.AddSection()
volumeTable, err := printVolumes(options.podTemplateSpec.Spec.Volumes)
if err != nil {
return errors.Wrap(err, "print volumes")
}
if !volumeTable.IsEmpty() {
if err := podSection.Add(volumeTable, component.WidthHalf); err != nil {
return err
}
}
tolerationList, err := printTolerations(options.podTemplateSpec.Spec)
if err != nil {
return errors.Wrap(err, "print tolerations")
}
if !tolerationList.IsEmpty() {
if err := podSection.Add(tolerationList, component.WidthHalf); err != nil {
return err
}
}
affinityList, err := printAffinity(options.podTemplateSpec.Spec)
if err != nil {
return errors.Wrap(err, "print affinities")
}
if !affinityList.IsEmpty() {
if err := podSection.Add(affinityList, component.WidthHalf); err != nil {
return err
}
}
return nil
}
// PodTemplateHeader creates a pod template header. It consists of a
// selectors component with title `Pod Template` and the associated
// match selectors.
type PodTemplateHeader struct {
labels map[string]string
}
// NewPodTemplateHeader creates an instance of PodTemplateHeader.
func NewPodTemplateHeader(labels map[string]string) *PodTemplateHeader {
return &PodTemplateHeader{
labels: labels,
}
}
// Create creates a labels component.
func (pth *PodTemplateHeader) Create() *component.Labels {
view := component.NewLabels(pth.labels)
view.Metadata.SetTitleText("Pod Template")
return view
}