github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/controllers/apiset/apiset.go (about)

     1  package apiset
     2  
     3  import (
     4  	"k8s.io/apimachinery/pkg/runtime"
     5  	"k8s.io/apimachinery/pkg/runtime/schema"
     6  	ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
     7  )
     8  
     9  // A set of API Objects of different types.
    10  type ObjectSet map[string]TypedObjectSet
    11  
    12  func (s ObjectSet) GetSetForType(o Object) TypedObjectSet {
    13  	return s[o.GetGroupVersionResource().String()]
    14  }
    15  
    16  func (s ObjectSet) Add(o Object) {
    17  	s.GetOrCreateTypedSet(o)[o.GetName()] = o
    18  }
    19  
    20  // `o` is only used to indicate the type - it does not get added to the set
    21  func (s ObjectSet) AddSetForType(o Object, set TypedObjectSet) {
    22  	gvk := o.GetGroupVersionResource()
    23  	dst := s[gvk.String()]
    24  	if dst == nil {
    25  		s[gvk.String()] = set
    26  		return
    27  	}
    28  
    29  	for k, v := range set {
    30  		dst[k] = v
    31  	}
    32  }
    33  
    34  func (s ObjectSet) GetOrCreateTypedSet(o Object) TypedObjectSet {
    35  	gvk := o.GetGroupVersionResource()
    36  	set := s[gvk.String()]
    37  	if set == nil {
    38  		set = TypedObjectSet{}
    39  		s[gvk.String()] = set
    40  	}
    41  	return set
    42  }
    43  
    44  // A set of API Objects of the same type.
    45  type TypedObjectSet map[string]Object
    46  
    47  // An API object with the methods we need to do bulk creation.
    48  type Object interface {
    49  	ctrlclient.Object
    50  	GetSpec() interface{}
    51  	GetGroupVersionResource() schema.GroupVersionResource
    52  	NewList() runtime.Object
    53  }