Skip to content

Commit

Permalink
apis: remove model.LiveUpdate in favor of v1alpha1.LiveUpdateSpec (#4984
Browse files Browse the repository at this point in the history
)

* apis: remove model.LiveUpdate in favor of v1alpha1.LiveUpdateSpec

* response to comments
  • Loading branch information
nicks committed Sep 28, 2021
1 parent 5cd59be commit 314b4ba
Show file tree
Hide file tree
Showing 33 changed files with 848 additions and 970 deletions.
52 changes: 52 additions & 0 deletions internal/controllers/apis/liveupdate/liveupdate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package liveupdate

import (
"path/filepath"

"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/model"
)

func IsEmptySpec(spec v1alpha1.LiveUpdateSpec) bool {
return len(spec.Syncs) == 0 && len(spec.Execs) == 0
}

// FallBackOnFiles returns a PathSet of files which, if any have changed, indicate
// that we should fall back to an image build.
func FallBackOnFiles(spec v1alpha1.LiveUpdateSpec) model.PathSet {
return model.NewPathSet(spec.StopPaths, spec.BasePath)
}

// Evaluates live-update syncs relative to the base path,
// and returns a sync with resolved paths.
func SyncSteps(spec v1alpha1.LiveUpdateSpec) []model.Sync {
var syncs []model.Sync
for _, sync := range spec.Syncs {
localPath := sync.LocalPath
if !filepath.IsAbs(localPath) {
localPath = filepath.Join(spec.BasePath, localPath)
}

syncs = append(syncs, model.Sync{LocalPath: localPath, ContainerPath: sync.ContainerPath})
}
return syncs
}

// Evaluates live-update exec relative to the base path,
// and returns a run with resolved paths.
func RunSteps(spec v1alpha1.LiveUpdateSpec) []model.Run {
var runs []model.Run
for _, exec := range spec.Execs {
runs = append(runs, model.Run{
Cmd: model.Cmd{
Argv: exec.Args,
},
Triggers: model.NewPathSet(exec.TriggerPaths, spec.BasePath),
})
}
return runs
}

func ShouldRestart(spec v1alpha1.LiveUpdateSpec) bool {
return spec.Restart == v1alpha1.LiveUpdateRestartStrategyAlways
}
3 changes: 2 additions & 1 deletion internal/engine/analytics/analytics_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/tilt-dev/tilt/internal/analytics"
"github.com/tilt-dev/tilt/internal/controllers/apis/liveupdate"
"github.com/tilt-dev/tilt/internal/k8s"
"github.com/tilt-dev/tilt/internal/store"
)
Expand Down Expand Up @@ -90,7 +91,7 @@ func (ar *AnalyticsReporter) report(ctx context.Context) {
}
var seenLU, multiImgLU, multiContainerLU bool
for _, it := range m.ImageTargets {
if !it.LiveUpdateInfo().Empty() {
if !liveupdate.IsEmptySpec(it.LiveUpdateSpec) {
if !seenLU {
seenLU = true
liveUpdateCount++
Expand Down
11 changes: 6 additions & 5 deletions internal/engine/analytics/analytics_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import (
"github.com/tilt-dev/wmclient/pkg/analytics"

"github.com/tilt-dev/tilt/internal/store"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/model"
)

var (
lu = model.LiveUpdate{Steps: []model.LiveUpdateStep{model.LiveUpdateSyncStep{}}} // non-empty LiveUpdate
lu = v1alpha1.LiveUpdateSpec{Syncs: []v1alpha1.LiveUpdateSync{{ContainerPath: "/"}}} // non-empty LiveUpdate

imgTargDB = model.ImageTarget{BuildDetails: model.DockerBuild{}}
imgTargDBWithLU = model.ImageTarget{BuildDetails: model.DockerBuild{LiveUpdate: lu}}
imgTargDBWithLU = model.ImageTarget{LiveUpdateSpec: lu, BuildDetails: model.DockerBuild{}}

kTarg = model.K8sTarget{}
dTarg = model.DockerComposeTarget{}
Expand All @@ -35,9 +36,9 @@ var (
r3 = "gcr.io/some-project-162817/three"
r4 = "gcr.io/some-project-162817/four"

iTargWithRef1 = iTargetForRef(r1).WithBuildDetails(model.DockerBuild{LiveUpdate: lu})
iTargWithRef2 = iTargetForRef(r2).WithBuildDetails(model.DockerBuild{LiveUpdate: lu})
iTargWithRef3 = iTargetForRef(r3).WithBuildDetails(model.DockerBuild{LiveUpdate: lu})
iTargWithRef1 = iTargetForRef(r1).WithLiveUpdateSpec(lu).WithBuildDetails(model.DockerBuild{})
iTargWithRef2 = iTargetForRef(r2).WithLiveUpdateSpec(lu).WithBuildDetails(model.DockerBuild{})
iTargWithRef3 = iTargetForRef(r3).WithLiveUpdateSpec(lu).WithBuildDetails(model.DockerBuild{})
iTargWithRef4NoLU = iTargetForRef(r4)
)

Expand Down
70 changes: 36 additions & 34 deletions internal/engine/build_and_deployer_live_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package engine

import (
"fmt"
"path/filepath"
"strings"
"testing"

Expand All @@ -20,6 +21,7 @@ import (

"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/internal/k8s"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/model"
)

Expand Down Expand Up @@ -297,11 +299,11 @@ func TestLiveUpdateDiffImgMultipleContainersOnlySomeSyncsMatch(t *testing.T) {

sanchoPath := f.JoinPath("sancho")
sanchoSyncs := SanchoSyncSteps(f)
sanchoSyncs[0].Source = sanchoPath
sanchoSyncs[0].LocalPath = sanchoPath

sidecarPath := f.JoinPath("sidecar")
sidecarSyncs := SyncStepsForApp("sidecar", f)
sidecarSyncs[0].Source = sidecarPath
sidecarSyncs[0].LocalPath = sidecarPath

sanchoLU := assembleLiveUpdate(sanchoSyncs, SanchoRunSteps, false, nil, f)
sidecarLU := assembleLiveUpdate(sidecarSyncs, RunStepsForApp("sidecar"),
Expand Down Expand Up @@ -344,7 +346,7 @@ func TestLiveUpdateDiffImgMultipleContainersSameContextOnlyOneLiveUpdate(t *test

buildContext := f.Path()
sanchoSyncs := SanchoSyncSteps(f)
sanchoSyncs[0].Source = buildContext
sanchoSyncs[0].LocalPath = buildContext

sanchoLU := assembleLiveUpdate(sanchoSyncs, SanchoRunSteps, false, nil, f)
sanchoTarg := model.MustNewImageTarget(SanchoRef).WithBuildDetails(model.DockerBuild{
Expand Down Expand Up @@ -379,9 +381,9 @@ func TestLiveUpdateDiffImgMultipleContainersFallBackIfFilesDoesntMatchAnySyncs(t
defer f.TearDown()

sanchoSyncs := SanchoSyncSteps(f)
sanchoSyncs[0].Source = f.JoinPath("sancho")
sanchoSyncs[0].LocalPath = f.JoinPath("sancho")
sidecarSyncs := SyncStepsForApp("sidecar", f)
sidecarSyncs[0].Source = f.JoinPath("sidecar")
sidecarSyncs[0].LocalPath = f.JoinPath("sidecar")

sanchoLU := assembleLiveUpdate(sanchoSyncs, SanchoRunSteps, false, nil, f)
sidecarLU := assembleLiveUpdate(sidecarSyncs, RunStepsForApp("sidecar"),
Expand Down Expand Up @@ -665,10 +667,10 @@ func TestLiveUpdateRunTriggerLocalContainer(t *testing.T) {
f := newBDFixture(t, k8s.EnvDockerDesktop, container.RuntimeDocker)
defer f.TearDown()

runs := []model.LiveUpdateRunStep{
model.LiveUpdateRunStep{Command: model.ToUnixCmd("echo hello")},
model.LiveUpdateRunStep{Command: model.ToUnixCmd("echo a"), Triggers: f.NewPathSet("a.txt")}, // matches changed file
model.LiveUpdateRunStep{Command: model.ToUnixCmd("echo b"), Triggers: f.NewPathSet("b.txt")}, // does NOT match changed file
runs := []v1alpha1.LiveUpdateExec{
{Args: model.ToUnixCmd("echo hello").Argv},
{Args: model.ToUnixCmd("echo a").Argv, TriggerPaths: []string{"a.txt"}}, // matches changed file
{Args: model.ToUnixCmd("echo b").Argv, TriggerPaths: []string{"b.txt"}}, // does NOT match changed file
}
lu := assembleLiveUpdate(SanchoSyncSteps(f), runs, true, nil, f)
tCase := testCase{
Expand All @@ -691,10 +693,10 @@ func TestLiveUpdateRunTriggerExec(t *testing.T) {
f := newBDFixture(t, k8s.EnvGKE, container.RuntimeDocker)
defer f.TearDown()

runs := []model.LiveUpdateRunStep{
model.LiveUpdateRunStep{Command: model.ToUnixCmd("echo hello")},
model.LiveUpdateRunStep{Command: model.ToUnixCmd("echo a"), Triggers: f.NewPathSet("a.txt")}, // matches changed file
model.LiveUpdateRunStep{Command: model.ToUnixCmd("echo b"), Triggers: f.NewPathSet("b.txt")}, // does NOT match changed file
runs := []v1alpha1.LiveUpdateExec{
{Args: model.ToUnixCmd("echo hello").Argv},
{Args: model.ToUnixCmd("echo a").Argv, TriggerPaths: []string{"a.txt"}}, // matches changed file
{Args: model.ToUnixCmd("echo b").Argv, TriggerPaths: []string{"b.txt"}}, // does NOT match changed file
}
lu := assembleLiveUpdate(SanchoSyncSteps(f), runs, false, nil, f)
tCase := testCase{
Expand Down Expand Up @@ -828,9 +830,9 @@ func TestLiveUpdateLocalContainerChangedFileNotMatchingSyncFallsBack(t *testing.
f := newBDFixture(t, k8s.EnvDockerDesktop, container.RuntimeDocker)
defer f.TearDown()

steps := []model.LiveUpdateSyncStep{model.LiveUpdateSyncStep{
Source: f.JoinPath("specific/directory"),
Dest: "/go/src/github.com/tilt-dev/sancho",
steps := []v1alpha1.LiveUpdateSync{{
LocalPath: filepath.Join("specific", "directory"),
ContainerPath: "/go/src/github.com/tilt-dev/sancho",
}}

lu := assembleLiveUpdate(steps, SanchoRunSteps, true, []string{"a.txt"}, f)
Expand Down Expand Up @@ -859,9 +861,9 @@ func TestLiveUpdateExecChangedFileNotMatchingSyncFallsBack(t *testing.T) {
f := newBDFixture(t, k8s.EnvGKE, container.RuntimeDocker)
defer f.TearDown()

steps := []model.LiveUpdateSyncStep{model.LiveUpdateSyncStep{
Source: f.JoinPath("specific/directory"),
Dest: "/go/src/github.com/tilt-dev/sancho",
steps := []v1alpha1.LiveUpdateSync{{
LocalPath: filepath.Join("specific", "directory"),
ContainerPath: "/go/src/github.com/tilt-dev/sancho",
}}

lu := assembleLiveUpdate(steps, SanchoRunSteps, false, []string{"a.txt"}, f)
Expand Down Expand Up @@ -890,9 +892,9 @@ func TestLiveUpdateManyFilesNotMatching(t *testing.T) {
f := newBDFixture(t, k8s.EnvGKE, container.RuntimeDocker)
defer f.TearDown()

steps := []model.LiveUpdateSyncStep{model.LiveUpdateSyncStep{
Source: f.JoinPath("specific/directory"),
Dest: "/go/src/github.com/tilt-dev/sancho",
steps := []v1alpha1.LiveUpdateSync{{
LocalPath: filepath.Join("specific", "directory"),
ContainerPath: "/go/src/github.com/tilt-dev/sancho",
}}

changedFiles := []string{}
Expand Down Expand Up @@ -933,9 +935,9 @@ func TestLiveUpdateSomeFilesMatchSyncSomeDontFallsBack(t *testing.T) {
f := newBDFixture(t, k8s.EnvDockerDesktop, container.RuntimeDocker)
defer f.TearDown()

steps := []model.LiveUpdateSyncStep{model.LiveUpdateSyncStep{
Source: f.JoinPath("specific/directory"),
Dest: "/go/src/github.com/tilt-dev/sancho",
steps := []v1alpha1.LiveUpdateSync{{
LocalPath: filepath.Join("specific", "directory"),
ContainerPath: "/go/src/github.com/tilt-dev/sancho",
}}

lu := assembleLiveUpdate(steps, SanchoRunSteps, true, []string{"a.txt"}, f)
Expand Down Expand Up @@ -965,9 +967,9 @@ func TestLiveUpdateInFirstImageOfImageDependency(t *testing.T) {
f := newBDFixture(t, k8s.EnvDockerDesktop, container.RuntimeDocker)
defer f.TearDown()

steps := []model.LiveUpdateSyncStep{model.LiveUpdateSyncStep{
Source: f.JoinPath("sancho-base"),
Dest: "/go/src/github.com/tilt-dev/sancho-base",
steps := []v1alpha1.LiveUpdateSync{{
LocalPath: "sancho-base",
ContainerPath: "/go/src/github.com/tilt-dev/sancho-base",
}}

lu := assembleLiveUpdate(steps, SanchoRunSteps, true, nil, f)
Expand All @@ -986,9 +988,9 @@ func TestLiveUpdateInFirstImageOfImageDependencyWithoutSync(t *testing.T) {
f := newBDFixture(t, k8s.EnvDockerDesktop, container.RuntimeDocker)
defer f.TearDown()

steps := []model.LiveUpdateSyncStep{model.LiveUpdateSyncStep{
Source: f.JoinPath("sancho"),
Dest: "/go/src/github.com/tilt-dev/sancho",
steps := []v1alpha1.LiveUpdateSync{{
LocalPath: "sancho",
ContainerPath: "/go/src/github.com/tilt-dev/sancho",
}}

lu := assembleLiveUpdate(steps, SanchoRunSteps, true, nil, f)
Expand All @@ -1006,9 +1008,9 @@ func TestLiveUpdateInSecondImageOfImageDependency(t *testing.T) {
f := newBDFixture(t, k8s.EnvDockerDesktop, container.RuntimeDocker)
defer f.TearDown()

steps := []model.LiveUpdateSyncStep{model.LiveUpdateSyncStep{
Source: f.JoinPath("sancho"),
Dest: "/go/src/github.com/tilt-dev/sancho",
steps := []v1alpha1.LiveUpdateSync{{
LocalPath: "sancho",
ContainerPath: "/go/src/github.com/tilt-dev/sancho",
}}

lu := assembleLiveUpdate(steps, SanchoRunSteps, true, nil, f)
Expand Down
5 changes: 4 additions & 1 deletion internal/engine/build_and_deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ func TestLiveUpdateFallbackMessagingRedirect(t *testing.T) {
f := newBDFixture(t, k8s.EnvDockerDesktop, container.RuntimeDocker)
defer f.TearDown()

lu := assembleLiveUpdate([]model.LiveUpdateSyncStep{model.LiveUpdateSyncStep{Source: f.Path(), Dest: "/blah"}},
syncs := []v1alpha1.LiveUpdateSync{
{LocalPath: ".", ContainerPath: "/blah"},
}
lu := assembleLiveUpdate(syncs,
nil, false, []string{f.JoinPath("fall_back.txt")}, f)
manifest := manifestbuilder.New(f, "foobar").
WithImageTarget(NewSanchoDockerBuildImageTarget(f)).
Expand Down
11 changes: 6 additions & 5 deletions internal/engine/buildcontrol/build_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
v1 "k8s.io/api/core/v1"

"github.com/tilt-dev/tilt/internal/build"
"github.com/tilt-dev/tilt/internal/controllers/apis/liveupdate"
"github.com/tilt-dev/tilt/internal/store"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/model"
Expand Down Expand Up @@ -387,14 +388,14 @@ func IsLiveUpdateTargetWaitingOnDeploy(state store.EngineState, mt *store.Manife
}

iTarget := mt.Manifest.ImageTargetWithID(id)
luInfo := iTarget.LiveUpdateInfo()
_, pathsMatchingNoSync, err := build.FilesToPathMappings(files, luInfo.SyncSteps())
luSpec := iTarget.LiveUpdateSpec
_, pathsMatchingNoSync, err := build.FilesToPathMappings(files, liveupdate.SyncSteps(luSpec))
if err != nil || len(pathsMatchingNoSync) > 0 {
return false
}

// If any changed files match a FallBackOn file, fall back to next BuildAndDeployer
anyMatch, _, err := luInfo.FallBackOnFiles().AnyMatch(files)
anyMatch, _, err := liveupdate.FallBackOnFiles(luSpec).AnyMatch(files)
if err != nil || anyMatch {
return false
}
Expand All @@ -404,9 +405,9 @@ func IsLiveUpdateTargetWaitingOnDeploy(state store.EngineState, mt *store.Manife
// We only care about targets where there are 0 running containers for the current build.
// This is the mechanism that live update uses to determine if the container to live-update
// is still pending.
if mt.Manifest.IsK8s() {
if mt.Manifest.IsK8s() && iTarget.LiveUpdateSpec.Selector.Kubernetes != nil {
cInfos, err := store.RunningContainersForTargetForOnePod(
iTarget.ID().Name.String(), iTarget.LiveUpdateSpec, mt.State.K8sRuntimeState())
iTarget.LiveUpdateSpec.Selector.Kubernetes, mt.State.K8sRuntimeState())
if err != nil {
return false
}
Expand Down
14 changes: 7 additions & 7 deletions internal/engine/buildcontrol/build_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package buildcontrol

import (
"fmt"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -231,17 +232,16 @@ func TestHoldForDeploy(t *testing.T) {
f.WriteFile(objFile, "hello")
f.WriteFile(fallbackFile, "hello")

lu, err := model.NewLiveUpdate([]model.LiveUpdateStep{
model.LiveUpdateFallBackOnStep{Files: []string{f.JoinPath("src", "package.json")}},
model.LiveUpdateSyncStep{Source: f.JoinPath("src"), Dest: "/src"},
}, f.Path())
require.NoError(t, err)

luSpec := v1alpha1.LiveUpdateSpec{
BasePath: f.Path(),
StopPaths: []string{filepath.Join("src", "package.json")},
Syncs: []v1alpha1.LiveUpdateSync{{LocalPath: "src", ContainerPath: "/src"}},
}
sanchoImage := model.MustNewImageTarget(container.MustParseSelector("sancho")).
WithLiveUpdateSpec(luSpec).
WithBuildDetails(model.DockerBuild{BuildPath: f.Path()})
sancho := f.upsertManifest(manifestbuilder.New(f, "sancho").
WithImageTargets(sanchoImage).
WithLiveUpdate(lu).
WithK8sYAML(testyaml.SanchoYAML).
Build())

Expand Down
5 changes: 3 additions & 2 deletions internal/engine/buildcontrol/extractors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/pkg/errors"

"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/internal/controllers/apis/liveupdate"
"github.com/tilt-dev/tilt/internal/sliceutils"
"github.com/tilt-dev/tilt/internal/store"
"github.com/tilt-dev/tilt/pkg/model"
Expand Down Expand Up @@ -68,8 +69,8 @@ func extractImageTargetsForLiveUpdates(specs []model.TargetSpec, stateSet store.
continue
}

luInfo := iTarget.LiveUpdateInfo()
if luInfo.Empty() {
luSpec := iTarget.LiveUpdateSpec
if liveupdate.IsEmptySpec(luSpec) {
return nil, SilentRedirectToNextBuilderf("LiveUpdate requires that LiveUpdate details be specified")
}

Expand Down

0 comments on commit 314b4ba

Please sign in to comment.