/
resource.go
53 lines (44 loc) · 1.23 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
package dao
import (
"context"
"fmt"
"github.com/kswapd/k13s/internal"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
var (
_ Accessor = (*Resource)(nil)
_ Describer = (*Resource)(nil)
_ Nuker = (*Resource)(nil)
)
// Resource represents an informer based resource.
type Resource struct {
Generic
}
// List returns a collection of resources.
func (r *Resource) List(ctx context.Context, ns string) ([]runtime.Object, error) {
strLabel, _ := ctx.Value(internal.KeyLabels).(string)
lsel := labels.Everything()
if strLabel != "" {
if sel, err := labels.Parse(strLabel); err == nil {
lsel = sel
}
}
return r.GetFactory().List(r.gvr.String(), ns, false, lsel)
}
// Get returns a resource instance if found, else an error.
func (r *Resource) Get(_ context.Context, path string) (runtime.Object, error) {
return r.GetFactory().Get(r.gvr.String(), path, true, labels.Everything())
}
// ToYAML returns a resource yaml.
func (r *Resource) ToYAML(path string, showManaged bool) (string, error) {
o, err := r.Get(context.Background(), path)
if err != nil {
return "", err
}
raw, err := ToYAML(o, showManaged)
if err != nil {
return "", fmt.Errorf("unable to marshal resource %w", err)
}
return raw, nil
}