k3s/vendor/github.com/rancher/wrangler/pkg/objectset/objectset.go

198 lines
3.7 KiB
Go
Raw Normal View History

2019-01-22 20:53:35 +00:00
package objectset
import (
"fmt"
"reflect"
2019-05-09 22:03:45 +00:00
"sort"
2019-01-22 20:53:35 +00:00
2020-03-26 21:07:15 +00:00
"github.com/pkg/errors"
2019-07-12 17:13:20 +00:00
"github.com/rancher/wrangler/pkg/gvk"
2019-05-09 22:03:45 +00:00
"github.com/rancher/wrangler/pkg/merr"
2019-01-22 20:53:35 +00:00
"k8s.io/apimachinery/pkg/api/meta"
2019-05-09 22:03:45 +00:00
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2019-01-22 20:53:35 +00:00
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
2019-05-09 22:03:45 +00:00
type ObjectKey struct {
Name string
Namespace string
2019-01-22 20:53:35 +00:00
}
2019-05-09 22:03:45 +00:00
func NewObjectKey(obj v1.Object) ObjectKey {
return ObjectKey{
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
2019-01-22 20:53:35 +00:00
}
}
2019-05-09 22:03:45 +00:00
func (o ObjectKey) String() string {
if o.Namespace == "" {
return o.Name
2019-01-22 20:53:35 +00:00
}
2019-05-09 22:03:45 +00:00
return fmt.Sprintf("%s/%s", o.Namespace, o.Name)
2019-01-22 20:53:35 +00:00
}
2020-04-22 22:34:19 +00:00
type ObjectKeyByGVK map[schema.GroupVersionKind][]ObjectKey
2019-05-09 22:03:45 +00:00
type ObjectByGVK map[schema.GroupVersionKind]map[ObjectKey]runtime.Object
2019-01-22 20:53:35 +00:00
2019-07-12 17:13:20 +00:00
func (o ObjectByGVK) Add(obj runtime.Object) (schema.GroupVersionKind, error) {
2019-01-22 20:53:35 +00:00
metadata, err := meta.Accessor(obj)
if err != nil {
2019-07-12 17:13:20 +00:00
return schema.GroupVersionKind{}, err
}
gvk, err := gvk.Get(obj)
if err != nil {
return schema.GroupVersionKind{}, err
2019-01-22 20:53:35 +00:00
}
2019-07-12 17:13:20 +00:00
objs := o[gvk]
2019-01-22 20:53:35 +00:00
if objs == nil {
2019-05-09 22:03:45 +00:00
objs = map[ObjectKey]runtime.Object{}
2019-07-12 17:13:20 +00:00
o[gvk] = objs
2019-01-22 20:53:35 +00:00
}
2019-05-09 22:03:45 +00:00
objs[ObjectKey{
Namespace: metadata.GetNamespace(),
Name: metadata.GetName(),
2019-01-22 20:53:35 +00:00
}] = obj
2019-07-12 17:13:20 +00:00
return gvk, nil
2019-01-22 20:53:35 +00:00
}
type ObjectSet struct {
errs []error
objects ObjectByGVK
objectsByGK ObjectByGK
order []runtime.Object
gvkOrder []schema.GroupVersionKind
gvkSeen map[schema.GroupVersionKind]bool
2019-01-22 20:53:35 +00:00
}
2020-04-22 22:34:19 +00:00
func NewObjectSet(objs ...runtime.Object) *ObjectSet {
os := &ObjectSet{
objects: ObjectByGVK{},
objectsByGK: ObjectByGK{},
gvkSeen: map[schema.GroupVersionKind]bool{},
2019-01-22 20:53:35 +00:00
}
2020-04-22 22:34:19 +00:00
os.Add(objs...)
return os
2019-01-22 20:53:35 +00:00
}
2019-05-09 22:03:45 +00:00
func (o *ObjectSet) ObjectsByGVK() ObjectByGVK {
2020-04-22 22:34:19 +00:00
if o == nil {
return nil
}
2019-05-09 22:03:45 +00:00
return o.objects
}
func (o *ObjectSet) Contains(gk schema.GroupKind, key ObjectKey) bool {
_, ok := o.objectsByGK[gk][key]
return ok
}
2019-12-12 01:27:03 +00:00
func (o *ObjectSet) All() []runtime.Object {
return o.order
}
2019-01-22 20:53:35 +00:00
func (o *ObjectSet) Add(objs ...runtime.Object) *ObjectSet {
for _, obj := range objs {
o.add(obj)
}
return o
}
func (o *ObjectSet) add(obj runtime.Object) {
if obj == nil || reflect.ValueOf(obj).IsNil() {
return
}
2019-07-12 17:13:20 +00:00
gvk, err := o.objects.Add(obj)
2019-01-22 20:53:35 +00:00
if err != nil {
2020-03-26 21:07:15 +00:00
o.err(errors.Wrapf(err, "failed to add %T", obj))
return
}
_, err = o.objectsByGK.Add(obj)
if err != nil {
o.err(errors.Wrapf(err, "failed to add %T", obj))
return
}
o.order = append(o.order, obj)
2019-07-12 17:13:20 +00:00
if !o.gvkSeen[gvk] {
o.gvkSeen[gvk] = true
o.gvkOrder = append(o.gvkOrder, gvk)
}
2019-01-22 20:53:35 +00:00
}
func (o *ObjectSet) err(err error) error {
o.errs = append(o.errs, err)
return o.Err()
}
func (o *ObjectSet) AddErr(err error) {
o.errs = append(o.errs, err)
}
func (o *ObjectSet) Err() error {
2019-05-09 22:03:45 +00:00
return merr.NewErrors(o.errs...)
}
func (o *ObjectSet) Len() int {
return len(o.objects)
}
2020-04-22 22:34:19 +00:00
func (o *ObjectSet) GVKs() []schema.GroupVersionKind {
return o.GVKOrder()
}
2019-05-09 22:03:45 +00:00
func (o *ObjectSet) GVKOrder(known ...schema.GroupVersionKind) []schema.GroupVersionKind {
var rest []schema.GroupVersionKind
for _, gvk := range known {
2019-07-12 17:13:20 +00:00
if o.gvkSeen[gvk] {
2019-05-09 22:03:45 +00:00
continue
}
rest = append(rest, gvk)
}
sort.Slice(rest, func(i, j int) bool {
return rest[i].String() < rest[j].String()
})
2019-07-12 17:13:20 +00:00
return append(o.gvkOrder, rest...)
2019-01-22 20:53:35 +00:00
}
type ObjectByGK map[schema.GroupKind]map[ObjectKey]runtime.Object
func (o ObjectByGK) Add(obj runtime.Object) (schema.GroupKind, error) {
metadata, err := meta.Accessor(obj)
if err != nil {
return schema.GroupKind{}, err
}
gvk, err := gvk.Get(obj)
if err != nil {
return schema.GroupKind{}, err
}
gk := gvk.GroupKind()
objs := o[gk]
if objs == nil {
objs = map[ObjectKey]runtime.Object{}
o[gk] = objs
}
objs[ObjectKey{
Namespace: metadata.GetNamespace(),
Name: metadata.GetName(),
}] = obj
return gk, nil
}