-
Notifications
You must be signed in to change notification settings - Fork 0
/
crd_section.go
executable file
·107 lines (83 loc) · 2.1 KB
/
crd_section.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
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package describer
import (
"context"
"sort"
"sync"
"github.com/kubenext/lissio/internal/log"
"github.com/kubenext/lissio/pkg/view/component"
)
type CRDSection struct {
describers map[string]Describer
path string
title string
mu sync.Mutex
}
var _ Describer = (*CRDSection)(nil)
func NewCRDSection(p, title string) *CRDSection {
return &CRDSection{
describers: make(map[string]Describer),
path: p,
title: title,
}
}
func (csd *CRDSection) Add(name string, describer Describer) {
csd.mu.Lock()
defer csd.mu.Unlock()
csd.describers[name] = describer
}
func (csd *CRDSection) Remove(name string) {
csd.mu.Lock()
defer csd.mu.Unlock()
delete(csd.describers, name)
}
func (csd *CRDSection) Describe(ctx context.Context, namespace string, options Options) (component.ContentResponse, error) {
csd.mu.Lock()
defer csd.mu.Unlock()
var names []string
for name := range csd.describers {
names = append(names, name)
}
sort.Strings(names)
list := component.NewList("Custom Resources", nil)
for _, name := range names {
resp, err := csd.describers[name].Describe(ctx, namespace, options)
if err != nil {
return component.EmptyContentResponse, err
}
for i := range resp.Components {
if nestedList, ok := resp.Components[i].(*component.List); ok {
for i := range nestedList.Config.Items {
item := nestedList.Config.Items[i]
if !item.IsEmpty() {
list.Add(item)
}
}
}
}
}
cr := component.ContentResponse{
Components: []component.Component{list},
Title: component.TitleFromString(csd.title),
}
return cr, nil
}
func (csd *CRDSection) PathFilters() []PathFilter {
return []PathFilter{
*NewPathFilter(csd.path, csd),
}
}
func (csd *CRDSection) Reset(ctx context.Context) error {
csd.mu.Lock()
defer csd.mu.Unlock()
logger := log.From(ctx)
for name := range csd.describers {
logger.With("describer-name", name, "crd-section-path", csd.path).
Debugf("removing crd from section")
delete(csd.describers, name)
}
return nil
}