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

     1  package build
     2  
     3  import (
     4  	"context"
     5  	"os/exec"
     6  	"strings"
     7  
     8  	"github.com/distribution/reference"
     9  
    10  	"github.com/tilt-dev/clusterid"
    11  	"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
    12  	"github.com/tilt-dev/tilt/pkg/logger"
    13  )
    14  
    15  type KINDLoader interface {
    16  	LoadToKIND(ctx context.Context, cluster *v1alpha1.Cluster, ref reference.NamedTagged) error
    17  }
    18  
    19  type cmdKINDLoader struct {
    20  }
    21  
    22  func (kl *cmdKINDLoader) LoadToKIND(ctx context.Context, cluster *v1alpha1.Cluster, ref reference.NamedTagged) error {
    23  	// In Kind5, --name specifies the name of the cluster in the kubeconfig.
    24  	// In Kind6, the -name parameter is prefixed with 'kind-' before being written to/read from the kubeconfig
    25  	k8sConn := k8sConnStatus(cluster)
    26  	kindName := k8sConn.Cluster
    27  	if k8sConn.Product == string(clusterid.ProductKIND) {
    28  		kindName = strings.TrimPrefix(kindName, "kind-")
    29  	}
    30  
    31  	cmd := exec.CommandContext(ctx, "kind", "load", "docker-image", ref.String(), "--name", kindName)
    32  	w := logger.NewMutexWriter(logger.Get(ctx).Writer(logger.InfoLvl))
    33  	cmd.Stdout = w
    34  	cmd.Stderr = w
    35  
    36  	return cmd.Run()
    37  }
    38  
    39  func NewKINDLoader() KINDLoader {
    40  	return &cmdKINDLoader{}
    41  }