This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 488
/
object.go
312 lines (245 loc) · 9.26 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
309
310
311
312
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"context"
"fmt"
"strings"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/vmware-tanzu/octant/pkg/action"
"github.com/vmware-tanzu/octant/pkg/view/component"
"github.com/vmware-tanzu/octant/pkg/view/flexlayout"
)
//go:generate mockgen -destination=./fake/mock_object_interface.go -package=fake github.com/vmware-tanzu/octant/internal/printer ObjectInterface
// ObjectInterface is an interface for printing an object.
type ObjectInterface interface {
// AddButton adds a button.
AddButton(name string, payload action.Payload, buttonOptions ...component.ButtonOption)
// EnableEvents enables showing events.
EnableEvents()
// EnableJobTemplate adds a job template.
EnableJobTemplate(templateSpec batchv1beta1.JobTemplateSpec)
// EnablePodTemplate adds a pod template.
EnablePodTemplate(templateSpec corev1.PodTemplateSpec)
// RegisterConfig registers a config summary component.
RegisterConfig(summary *component.Summary)
// RegisterItems registers items to be shown
RegisterItems(items ...ItemDescriptor)
// RegisterSummary registers a summary summary component.
RegisterSummary(summary *component.Summary)
}
func defaultPodTemplateGen(ctx context.Context, object runtime.Object, template corev1.PodTemplateSpec, fl *flexlayout.FlexLayout, options Options) error {
podTemplate := NewPodTemplate(object, template)
if err := podTemplate.AddToFlexLayout(ctx, fl, options); err != nil {
return fmt.Errorf("add pod template to layout: %w", err)
}
return nil
}
func defaultJobTemplateGen(ctx context.Context, object runtime.Object, template batchv1beta1.JobTemplateSpec, fl *flexlayout.FlexLayout, options Options) error {
podTemplate := NewJobTemplate(ctx, object, template)
if err := podTemplate.AddToFlexLayout(fl, options); err != nil {
return fmt.Errorf("add job template to layout: %w", err)
}
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 fmt.Errorf("add events to layout: %w", err)
}
return nil
}
type conditionsGenFn func(ctx context.Context, object runtime.Object, fl *flexlayout.FlexLayout) error
type conditionsFilterFn func(*component.Table)
type mapGenFn func(runtime.Object) (map[string]interface{}, error)
func conditionsGenFactory(sortKey string, columns [][]string, mapFn mapGenFn) conditionsGenFn {
return func(ctx context.Context, object runtime.Object, fl *flexlayout.FlexLayout) error {
if err := createConditionsForObject(ctx, fl, object, sortKey, columns, mapFn); err != nil {
return fmt.Errorf("add conditions to layout: %w", err)
}
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 {
Component component.Component
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
isConditionsEnabled bool
itemsLists [][]ItemDescriptor
isPodTemplateEnabled bool
podTemplateOptions podTemplateOptions
isJobTemplateEnabled bool
jobTemplateOptions jobTemplateOptions
object runtime.Object
flexLayout *flexlayout.FlexLayout
PodTemplateGen func(context.Context, runtime.Object, corev1.PodTemplateSpec, *flexlayout.FlexLayout, Options) error
JobTemplateGen func(context.Context, runtime.Object, batchv1beta1.JobTemplateSpec, *flexlayout.FlexLayout, Options) error
EventsGen func(ctx context.Context, object runtime.Object, fl *flexlayout.FlexLayout, options Options) error
ConditionsGen func(ctx context.Context, object runtime.Object, fl *flexlayout.FlexLayout) error
}
// NewObject creates an instance of Object.
func NewObject(object runtime.Object, options ...ObjectOpts) *Object {
o := &Object{
object: object,
flexLayout: flexlayout.New(),
PodTemplateGen: defaultPodTemplateGen,
JobTemplateGen: defaultJobTemplateGen,
EventsGen: defaultEventsGen,
ConditionsGen: conditionsGenFactory("", nil, nil),
}
for _, option := range options {
option(o)
}
o.isConditionsEnabled = true
return o
}
var _ ObjectInterface = &Object{}
// 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
}
// EnableConditions enables the conditions view for the object.
func (o *Object) DisableConditions() {
o.isConditionsEnabled = false
}
// 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 fmt.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 fmt.Errorf("add component to %q layout: %w", title, err)
}
return 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, fmt.Errorf("object is nil")
}
summarySection := o.flexLayout.AddSection()
pluginPrinter := options.DashConfig.PluginManager()
if pluginPrinter == nil {
return nil, fmt.Errorf("plugin printer is nil")
}
pr, err := pluginPrinter.Print(ctx, o.object)
if err != nil {
return nil, fmt.Errorf("plugin manager: %w", err)
}
if err := o.summaryComponent("Configuration", o.config, summarySection, pr.Config...); err != nil {
return nil, fmt.Errorf("generate configuration component: %w", err)
}
if err := o.summaryComponent("Status", o.summary, summarySection, pr.Status...); err != nil {
return nil, fmt.Errorf("generate summary component: %w", err)
}
for _, items := range o.itemsLists {
section := o.flexLayout.AddSection()
for _, item := range items {
var c component.Component
if item.Component != nil {
c = item.Component
} else {
vc, err := item.Func()
if err != nil {
return nil, fmt.Errorf("failed to create item view: %w", err)
}
if vc == nil {
// don't print nil objects
continue
}
c = vc
}
if err := section.Add(c, item.Width); err != nil {
return nil, fmt.Errorf("unable to add item to layout section in object printer: %w", err)
}
}
}
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, fmt.Errorf("unable to add plugin item to layout section in object printer: %w", err)
}
}
}
if o.isPodTemplateEnabled {
if err := o.PodTemplateGen(ctx, o.object, o.podTemplateOptions.template, o.flexLayout, options); err != nil {
return nil, fmt.Errorf("generate pod template: %w", err)
}
}
if o.isJobTemplateEnabled {
if err := o.JobTemplateGen(ctx, o.object, o.jobTemplateOptions.template, o.flexLayout, options); err != nil {
return nil, fmt.Errorf("generate job template: %w", err)
}
}
if o.isEventsEnabled {
if err := o.EventsGen(ctx, o.object, o.flexLayout, options); err != nil {
return nil, fmt.Errorf("add events to layout: %w", err)
}
}
if o.isConditionsEnabled {
if err := o.ConditionsGen(ctx, o.object, o.flexLayout); err != nil {
// Only error if the it is an error other than no status found.
if !strings.Contains(err.Error(), "no status found") {
return nil, fmt.Errorf("add conditions to layout: %w", err)
}
}
}
return o.flexLayout.ToComponent("Summary"), nil
}
func (o *Object) AddButton(name string, payload action.Payload, buttonOptions ...component.ButtonOption) {
o.flexLayout.AddButton(name, payload, buttonOptions...)
}