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

     1  package liveupdate
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"github.com/tilt-dev/tilt/internal/container"
     8  	"github.com/tilt-dev/tilt/pkg/apis"
     9  	"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
    10  	"github.com/tilt-dev/tilt/pkg/model"
    11  )
    12  
    13  // Generate the name for the LiveUpdate API object from an ImageTarget and ManifestName.
    14  func GetName(mn model.ManifestName, id model.TargetID) string {
    15  	return apis.SanitizeName(fmt.Sprintf("%s:%s", mn.String(), id.Name))
    16  }
    17  
    18  func IsEmptySpec(spec v1alpha1.LiveUpdateSpec) bool {
    19  	return len(spec.Syncs) == 0 && len(spec.Execs) == 0
    20  }
    21  
    22  // FallBackOnFiles returns a PathSet of files which, if any have changed, indicate
    23  // that we should fall back to an image build.
    24  func FallBackOnFiles(spec v1alpha1.LiveUpdateSpec) model.PathSet {
    25  	return model.NewPathSet(spec.StopPaths, spec.BasePath)
    26  }
    27  
    28  // Evaluates live-update syncs relative to the base path,
    29  // and returns a sync with resolved paths.
    30  func SyncSteps(spec v1alpha1.LiveUpdateSpec) []model.Sync {
    31  	var syncs []model.Sync
    32  	for _, sync := range spec.Syncs {
    33  		localPath := sync.LocalPath
    34  		if !filepath.IsAbs(localPath) {
    35  			localPath = filepath.Join(spec.BasePath, localPath)
    36  		}
    37  
    38  		syncs = append(syncs, model.Sync{LocalPath: localPath, ContainerPath: sync.ContainerPath})
    39  	}
    40  	return syncs
    41  }
    42  
    43  // Evaluates live-update exec relative to the base path,
    44  // and returns a run with resolved paths.
    45  func RunSteps(spec v1alpha1.LiveUpdateSpec) []model.Run {
    46  	var runs []model.Run
    47  	for _, exec := range spec.Execs {
    48  		runs = append(runs, model.Run{
    49  			Cmd: model.Cmd{
    50  				Argv:    exec.Args,
    51  				EchoOff: exec.EchoOff,
    52  			},
    53  			Triggers: model.NewPathSet(exec.TriggerPaths, spec.BasePath),
    54  		})
    55  	}
    56  	return runs
    57  }
    58  
    59  func ShouldRestart(spec v1alpha1.LiveUpdateSpec) bool {
    60  	return spec.Restart == v1alpha1.LiveUpdateRestartStrategyAlways
    61  }
    62  
    63  func KubernetesSelectorMatchesContainer(
    64  	ctr v1alpha1.Container,
    65  	selector *v1alpha1.LiveUpdateKubernetesSelector,
    66  	imageMap *v1alpha1.ImageMap,
    67  ) bool {
    68  	if selector == nil {
    69  		return false
    70  	}
    71  
    72  	// LiveUpdateKubernetesSelector must specify EITHER image OR ImageMap OR container name
    73  	if selector.Image != "" {
    74  		return container.ImageNamesEqual(selector.Image, ctr.Image)
    75  	}
    76  	if selector.ContainerName != "" {
    77  		return selector.ContainerName == ctr.Name
    78  	}
    79  	if selector.ImageMapName != "" {
    80  		return imageMap != nil && container.ImageNamesEqual(
    81  			imageMap.Status.ImageFromCluster, ctr.Image)
    82  	}
    83  
    84  	return false
    85  }