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 489
/
object.go
179 lines (148 loc) · 4.95 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
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package describer
import (
"context"
"fmt"
"path/filepath"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"github.com/vmware-tanzu/octant/internal/api"
"github.com/vmware-tanzu/octant/internal/octant"
"github.com/vmware-tanzu/octant/internal/util/kubernetes"
"github.com/vmware-tanzu/octant/pkg/action"
"github.com/vmware-tanzu/octant/pkg/store"
"github.com/vmware-tanzu/octant/pkg/view/component"
)
// defaultObjectTabs are the default tabs for an object (that is not a custom resource).
func defaultObjectTabs() []Tab {
return []Tab{
{Name: "Summary", Factory: SummaryTab},
{Name: "Metadata", Factory: MetadataTab},
{Name: "Resource Viewer", Factory: ResourceViewerTab},
{Name: "YAML", Factory: YAMLViewerTab},
{Name: "Logs", Factory: LogsTab},
{Name: "Terminal", Factory: TerminalTab},
}
}
// ObjectConfig is configuration for Object.
type ObjectConfig struct {
Path string
BaseTitle string
ObjectType func() interface{}
StoreKey store.Key
RootPath ResourceLink
TabsGenerator TabsGenerator
TabDescriptors []Tab
}
// Object describes an object.
type Object struct {
*base
path string
baseTitle string
objectType func() interface{}
objectStoreKey store.Key
disableResourceViewer bool
tabFuncDescriptors []Tab
rootPath ResourceLink
tabsGenerator TabsGenerator
}
// NewObject creates an instance of Object.
func NewObject(c ObjectConfig) *Object {
tg := c.TabsGenerator
if tg == nil {
tg = NewObjectTabsGenerator()
}
td := c.TabDescriptors
if td == nil {
td = defaultObjectTabs()
}
o := &Object{
path: c.Path,
baseTitle: c.BaseTitle,
base: newBaseDescriber(),
objectStoreKey: c.StoreKey,
objectType: c.ObjectType,
rootPath: c.RootPath,
tabsGenerator: tg,
tabFuncDescriptors: td,
}
return o
}
// Describe describes an object. An object description is comprised of multiple tabs of content.
// By default, there will be the following tabs: summary, metadata, resource viewer, and yaml.
// If the object is a pod, there will also be a log and terminal tab. If plugins can contribute
// tabs to this object, those tabs will be included as well.
//
// This function should always return a content response even if there is an error.
func (d *Object) Describe(ctx context.Context, namespace string, options Options) (component.ContentResponse, error) {
object, err := options.LoadObject(ctx, namespace, options.Fields, d.objectStoreKey)
if err != nil {
return component.EmptyContentResponse, api.NewNotFoundError(d.path)
} else if object == nil {
cr := component.NewContentResponse(component.TitleFromString("LoadObject Error"))
c := CreateErrorTab("Error", fmt.Errorf("unable to load object %s", d.objectStoreKey))
cr.Add(c)
return *cr, nil
}
item := d.objectType()
if err := kubernetes.FromUnstructured(object, item); err != nil {
cr := component.NewContentResponse(component.TitleFromString("Converting Dynamic Object Error"))
c := CreateErrorTab("Error", fmt.Errorf("converting dynamic object to a type: %w", err))
cr.Add(c)
return *cr, nil
}
accessor := meta.NewAccessor()
objectName, _ := accessor.Name(object)
kind, _ := accessor.Kind(object)
nameLink, err := options.Link.ForObject(object, kind)
if err != nil {
return component.EmptyContentResponse, err
}
title := getBreadcrumb(d.rootPath, d.baseTitle, filepath.Dir(nameLink.Ref()), namespace)
if objectName != "" {
title = append(title, component.NewText(objectName))
}
cr := component.NewContentResponse(title)
currentObject, ok := item.(runtime.Object)
if !ok {
c := CreateErrorTab("Error", fmt.Errorf("expected item to be a runtime object. It was a %T", item))
cr.Add(c)
return *cr, nil
}
objAccessor, err := meta.Accessor(currentObject)
if err != nil {
return component.EmptyContentResponse, err
}
if objAccessor.GetDeletionTimestamp() == nil {
key, err := store.KeyFromObject(currentObject)
if err != nil {
return component.EmptyContentResponse, err
}
confirmation, err := octant.DeleteObjectConfirmationButton(currentObject)
if err != nil {
return component.EmptyContentResponse, err
}
cr.AddButton("Delete", action.CreatePayload(octant.ActionDeleteObject,
key.ToActionPayload()), confirmation)
}
config := TabsGeneratorConfig{
Object: currentObject,
TabsFactory: objectTabsFactory(ctx, currentObject, d.tabFuncDescriptors, options),
Options: options,
}
tabComponents, err := d.tabsGenerator.Generate(ctx, config)
if err != nil {
return component.EmptyContentResponse, fmt.Errorf("generate tabs: %w", err)
}
cr.Add(tabComponents...)
return *cr, nil
}
// PathFilters returns the path filters for this object.
func (d *Object) PathFilters() []PathFilter {
return []PathFilter{
*NewPathFilter(d.path, d),
}
}