54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/derailed/k9s/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.Factory.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.Factory.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
|
|
}
|