github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/store/liveupdates/fake.go (about)

     1  package liveupdates
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/tilt-dev/tilt/internal/store/k8sconv"
     7  	"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
     8  )
     9  
    10  func FakeKubernetesResource(image string, containers []Container) *k8sconv.KubernetesResource {
    11  	r, err := k8sconv.NewKubernetesResource(FakeKubernetesDiscovery(image, containers), nil)
    12  	if err != nil {
    13  		panic(err)
    14  	}
    15  	return r
    16  }
    17  
    18  // Given the set of containers we want, create a fake KubernetesDiscovery
    19  // with those containers running.
    20  func FakeKubernetesDiscovery(image string, containers []Container) *v1alpha1.KubernetesDiscovery {
    21  	podMap := map[string]*v1alpha1.Pod{}
    22  	for _, c := range containers {
    23  		pod, ok := podMap[string(c.PodID)]
    24  		if !ok {
    25  			pod = &v1alpha1.Pod{
    26  				Name:      string(c.PodID),
    27  				Namespace: string(c.Namespace),
    28  				Phase:     "Running",
    29  			}
    30  			podMap[string(c.PodID)] = pod
    31  		}
    32  
    33  		pod.Containers = append(pod.Containers, v1alpha1.Container{
    34  			ID:    string(c.ContainerID),
    35  			Name:  string(c.ContainerName),
    36  			Ready: true,
    37  			Image: image,
    38  			State: v1alpha1.ContainerState{
    39  				Running: &v1alpha1.ContainerStateRunning{},
    40  			},
    41  		})
    42  	}
    43  
    44  	pods := []v1alpha1.Pod{}
    45  	for _, p := range podMap {
    46  		pods = append(pods, *p)
    47  	}
    48  	sort.Slice(pods, func(i, j int) bool {
    49  		return pods[i].Name < pods[j].Name
    50  	})
    51  
    52  	return &v1alpha1.KubernetesDiscovery{
    53  		Status: v1alpha1.KubernetesDiscoveryStatus{
    54  			Pods: pods,
    55  		},
    56  	}
    57  }