/
object.go
308 lines (240 loc) · 8.85 KB
/
object.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
/*
* Copyright (c) 2019 Kubenext, Inc. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"context"
"fmt"
"github.com/pkg/errors"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"github.com/kubenext/kubeon/internal/octant"
"github.com/kubenext/kubeon/pkg/action"
"github.com/kubenext/kubeon/pkg/store"
"github.com/kubenext/kubeon/pkg/view/component"
"github.com/kubenext/kubeon/pkg/view/flexlayout"
)
//go:generate mockgen -destination=./fake/mock_object_interface.go -package=fake github.com/kubenext/kubeon/internal/printer ObjectInterface
// ObjectInterface is an interface for printing an object.
type ObjectInterface interface {
AddButton(name string, payload action.Payload, buttonOptions ...component.ButtonOption)
}
func defaultMetadataGen(object runtime.Object, fl *flexlayout.FlexLayout, options Options) error {
metadata, err := NewMetadata(object, options.Link)
if err != nil {
return errors.Wrap(err, "create metadata generator")
}
if err := metadata.AddToFlexLayout(fl); err != nil {
return errors.Wrap(err, "add metadata to layout")
}
return nil
}
func defaultPodTemplateGen(object runtime.Object, template corev1.PodTemplateSpec, fl *flexlayout.FlexLayout, options Options) error {
podTemplate := NewPodTemplate(object, template)
if err := podTemplate.AddToFlexLayout(fl, options); err != nil {
return errors.Wrap(err, "add pod template to layout")
}
return nil
}
func defaultJobTemplateGen(object runtime.Object, template batchv1beta1.JobTemplateSpec, fl *flexlayout.FlexLayout, options Options) error {
podTemplate := NewJobTemplate(object, template)
if err := podTemplate.AddToFlexLayout(fl, options); err != nil {
return errors.Wrap(err, "add job template to layout")
}
return nil
}
func defaultEventsGen(ctx context.Context, object runtime.Object, fl *flexlayout.FlexLayout, options Options) error {
if err := createEventsForObject(ctx, fl, object, options); err != nil {
return errors.Wrap(err, "add events to layout")
}
return nil
}
// ObjectPrinterFunc is a func that create a view.
type ObjectPrinterFunc func() (component.Component, error)
// ObjectPrinterLayoutFunc is a func that render a view in a flex layout.
type ObjectPrinterLayoutFunc func(*flexlayout.FlexLayout) error
// ItemDescriptor describes a func to print a view and its width.
type ItemDescriptor struct {
Func ObjectPrinterFunc
Width int
}
type podTemplateOptions struct {
template corev1.PodTemplateSpec
}
type jobTemplateOptions struct {
template batchv1beta1.JobTemplateSpec
}
// ObjectOpts are options for configuration Object.
type ObjectOpts func(o *Object)
// Object prints an object.
type Object struct {
config *component.Summary
summary *component.Summary
isEventsEnabled bool
itemsLists [][]ItemDescriptor
isPodTemplateEnabled bool
podTemplateOptions podTemplateOptions
isJobTemplateEnabled bool
jobTemplateOptions jobTemplateOptions
object runtime.Object
flexLayout *flexlayout.FlexLayout
MetadataGen func(runtime.Object, *flexlayout.FlexLayout, Options) error
PodTemplateGen func(runtime.Object, corev1.PodTemplateSpec, *flexlayout.FlexLayout, Options) error
JobTemplateGen func(runtime.Object, batchv1beta1.JobTemplateSpec, *flexlayout.FlexLayout, Options) error
EventsGen func(ctx context.Context, object runtime.Object, fl *flexlayout.FlexLayout, options Options) error
}
// NewObject creates an instance of Object.
func NewObject(object runtime.Object, options ...ObjectOpts) *Object {
o := &Object{
object: object,
flexLayout: flexlayout.New(),
MetadataGen: defaultMetadataGen,
PodTemplateGen: defaultPodTemplateGen,
JobTemplateGen: defaultJobTemplateGen,
EventsGen: defaultEventsGen,
}
for _, option := range options {
option(o)
}
return o
}
// RegisterConfig registers the config view for an object.
func (o *Object) RegisterConfig(summary *component.Summary) {
o.config = summary
}
// RegisterSummary registers a summary view for an object.
func (o *Object) RegisterSummary(summary *component.Summary) {
o.summary = summary
}
// EnablePodTemplate enables the pod template view for the object.
func (o *Object) EnablePodTemplate(templateSpec corev1.PodTemplateSpec) {
o.isPodTemplateEnabled = true
o.podTemplateOptions.template = templateSpec
}
// EnableJobTemplate enables the job template view for the object.
func (o *Object) EnableJobTemplate(templateSpec batchv1beta1.JobTemplateSpec) {
o.isJobTemplateEnabled = true
o.jobTemplateOptions.template = templateSpec
}
// EnableEvents enables the event view for the object.
func (o *Object) EnableEvents() {
o.isEventsEnabled = true
}
// RegisterItems registers one or more items to be printed in a section.
// Each call to RegisterItems will create a new section.
func (o *Object) RegisterItems(items ...ItemDescriptor) {
o.itemsLists = append(o.itemsLists, items)
}
func (o *Object) summaryComponent(title string, summary *component.Summary, section *flexlayout.Section, additional ...component.SummarySection) error {
if section == nil {
return errors.Errorf("section is nil")
}
if summary == nil {
summary = component.NewSummary(title)
} else {
summary.SetTitleText(title)
}
summary.Add(additional...)
if len(summary.Sections()) < 1 {
return nil
}
if err := section.Add(summary, component.WidthHalf); err != nil {
return errors.Wrapf(err, "add component to %q layout", title)
}
return nil
}
func deleteObjectConfirmation(object runtime.Object) (component.ButtonOption, error) {
if object == nil {
return nil, errors.New("object is nil")
}
_, kind := object.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
accessor, err := meta.Accessor(object)
if err != nil {
return nil, err
}
confirmationTitle := fmt.Sprintf("Delete %s", kind)
confirmationBody := fmt.Sprintf("Are you sure you want to delete *%s* **%s**? This action is permanent and cannot be recovered.", kind, accessor.GetName())
return component.WithButtonConfirmation(confirmationTitle, confirmationBody), nil
}
// ToComponent converts Object to a view.
func (o *Object) ToComponent(ctx context.Context, options Options) (component.Component, error) {
if o.object == nil {
return nil, errors.New("object is nil")
}
accessor, err := meta.Accessor(o.object)
if err != nil {
return nil, err
}
if accessor.GetDeletionTimestamp() == nil {
key, err := store.KeyFromObject(o.object)
if err != nil {
return nil, err
}
confirmation, err := deleteObjectConfirmation(o.object)
if err != nil {
return nil, errors.Wrap(err, "create delete confirmation")
}
o.AddButton("Delete", action.CreatePayload(octant.ActionDeleteObject,
key.ToActionPayload()), confirmation)
}
summarySection := o.flexLayout.AddSection()
pluginPrinter := options.DashConfig.PluginManager()
if pluginPrinter == nil {
return nil, errors.New("plugin printer is nil")
}
pr, err := pluginPrinter.Print(ctx, o.object)
if err != nil {
return nil, errors.Wrap(err, "plugin manager")
}
if err := o.summaryComponent("Configuration", o.config, summarySection, pr.Config...); err != nil {
return nil, errors.Wrap(err, "generate configuration component")
}
if err := o.summaryComponent("Status", o.summary, summarySection, pr.Status...); err != nil {
return nil, errors.Wrap(err, "generate summary component")
}
if err := o.MetadataGen(o.object, o.flexLayout, options); err != nil {
return nil, errors.Wrap(err, "generate metadata")
}
for _, items := range o.itemsLists {
section := o.flexLayout.AddSection()
for _, item := range items {
vc, err := item.Func()
if err != nil {
return nil, errors.Wrap(err, "failed to create item view")
}
if err := section.Add(vc, item.Width); err != nil {
return nil, errors.Wrap(err, "unable to add item to layout section in object printer")
}
}
}
if len(pr.Items) > 0 {
section := o.flexLayout.AddSection()
for _, item := range pr.Items {
if err := section.Add(item.View, item.Width); err != nil {
return nil, errors.Wrap(err, "unable to add plugin item to layout section in object printer")
}
}
}
if o.isPodTemplateEnabled {
if err := o.PodTemplateGen(o.object, o.podTemplateOptions.template, o.flexLayout, options); err != nil {
return nil, errors.Wrap(err, "generate pod template")
}
}
if o.isJobTemplateEnabled {
if err := o.JobTemplateGen(o.object, o.jobTemplateOptions.template, o.flexLayout, options); err != nil {
return nil, errors.Wrap(err, "generate job template")
}
}
if o.isEventsEnabled {
if err := o.EventsGen(ctx, o.object, o.flexLayout, options); err != nil {
return nil, errors.Wrap(err, "add events to layout")
}
}
return o.flexLayout.ToComponent("Summary"), nil
}
func (o *Object) AddButton(name string, payload action.Payload, buttonOptions ...component.ButtonOption) {
o.flexLayout.AddButton(name, payload, buttonOptions...)
}