-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
216 lines (169 loc) · 5.46 KB
/
resource.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
package kubedump
import (
"fmt"
"os"
"path"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/yaml"
)
// todo: these builders don't need to be pointer receivers or returns
// Resource is a collection of methods that can be used to describe a resource being handled by the kubedump controller.
type Resource interface {
fmt.Stringer
GetName() string
GetNamespace() string
GetLabels() map[string]string
GetOwnershipReferences() []apimetav1.OwnerReference
GetKind() string
GetUID() types.UID
}
func NewResourceFromFile(path string) (Resource, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not read resource file: %w", err)
}
m := map[string]interface{}{}
if err := yaml.Unmarshal(data, &m); err != nil {
return nil, fmt.Errorf("could not unmarshal data to suntructured: %w", err)
}
u := &unstructured.Unstructured{Object: m}
return NewResourceBuilder().
FromUnstructured(u).
Build(), nil
}
type resource struct {
name string
namespace string
labels map[string]string
ownerReferences []apimetav1.OwnerReference
kind string
id types.UID
}
func (resource *resource) String() string {
return fmt.Sprintf("%s/%s", resource.GetKind(), resource.GetName())
}
func (resource *resource) GetName() string {
return resource.name
}
func (resource *resource) GetNamespace() string {
return resource.namespace
}
func (resource *resource) GetLabels() map[string]string {
return resource.labels
}
func (resource *resource) GetOwnershipReferences() []apimetav1.OwnerReference {
return resource.ownerReferences
}
func (resource *resource) GetKind() string {
return resource.kind
}
func (resource *resource) GetUID() types.UID {
return resource.id
}
type ResourceBuilder struct {
resource resource
}
func NewResourceBuilder() *ResourceBuilder {
return &ResourceBuilder{}
}
func (builder *ResourceBuilder) FromUnstructured(u *unstructured.Unstructured) *ResourceBuilder {
builder.resource.name = u.GetName()
builder.resource.namespace = u.GetNamespace()
builder.resource.labels = u.GetLabels()
builder.resource.ownerReferences = u.GetOwnerReferences()
builder.resource.kind = u.GetKind()
builder.resource.id = u.GetUID()
return builder
}
func (builder *ResourceBuilder) FromObject(obj apimetav1.ObjectMeta) *ResourceBuilder {
builder.resource.name = obj.Name
builder.resource.namespace = obj.Namespace
builder.resource.labels = obj.Labels
builder.resource.ownerReferences = obj.OwnerReferences
builder.resource.id = obj.UID
return builder
}
func (builder *ResourceBuilder) FromType(t apimetav1.TypeMeta) *ResourceBuilder {
builder.resource.kind = t.Kind
return builder
}
func (builder *ResourceBuilder) WithName(name string) *ResourceBuilder {
builder.resource.name = name
return builder
}
func (builder *ResourceBuilder) WithNamespace(namespace string) *ResourceBuilder {
builder.resource.namespace = namespace
return builder
}
func (builder *ResourceBuilder) WithLabels(labels map[string]string) *ResourceBuilder {
builder.resource.labels = labels
return builder
}
func (builder *ResourceBuilder) WithOwnershipReferences(ownerReferences []apimetav1.OwnerReference) *ResourceBuilder {
builder.resource.ownerReferences = ownerReferences
return builder
}
func (builder *ResourceBuilder) WithKind(kind string) *ResourceBuilder {
builder.resource.kind = kind
return builder
}
func (builder *ResourceBuilder) WithId(id types.UID) *ResourceBuilder {
builder.resource.id = id
return builder
}
func (builder *ResourceBuilder) Build() Resource {
return &builder.resource
}
// ResourcePathBuilder can be used to build the directory paths to a resource. You must build the paths to any resource files or subdirectories yourself.
type ResourcePathBuilder struct {
BasePath string
Namespace string
ParentName string
ParentKind string
Name string
Kind string
}
func (builder ResourcePathBuilder) WithBase(basePath string) ResourcePathBuilder {
builder.BasePath = basePath
return builder
}
func (builder ResourcePathBuilder) WithNamespace(namespace string) ResourcePathBuilder {
builder.Namespace = namespace
return builder
}
func (builder ResourcePathBuilder) WithName(name string) ResourcePathBuilder {
builder.Name = name
return builder
}
func (builder ResourcePathBuilder) WithKind(kind string) ResourcePathBuilder {
builder.Kind = kind
return builder
}
func (builder ResourcePathBuilder) WithParentName(name string) ResourcePathBuilder {
builder.ParentName = name
return builder
}
func (builder ResourcePathBuilder) WithParentKind(kind string) ResourcePathBuilder {
builder.ParentKind = kind
return builder
}
func (builder ResourcePathBuilder) WithResource(resource Resource) ResourcePathBuilder {
builder.Namespace = resource.GetNamespace()
builder.Name = resource.GetName()
builder.Kind = resource.GetKind()
return builder
}
func (builder ResourcePathBuilder) BuildNamespace() string {
return path.Join(builder.BasePath, builder.Namespace)
}
func (builder ResourcePathBuilder) BuildKind() string {
return path.Join(builder.BasePath, builder.Namespace, builder.Kind)
}
func (builder ResourcePathBuilder) Build() string {
return path.Join(builder.BasePath, builder.Namespace, builder.Kind, builder.Name)
}
func (builder ResourcePathBuilder) BuildWithParent() string {
return path.Join(builder.BasePath, builder.Namespace, builder.ParentKind, builder.ParentName, builder.Kind, builder.Name)
}