Skip to content

Commit

Permalink
session: api for graceful recovery in 'tilt ci'
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Santos <nick.santos@docker.com>
  • Loading branch information
nicks committed Dec 20, 2022
1 parent 90a370a commit 3236355
Show file tree
Hide file tree
Showing 13 changed files with 1,110 additions and 658 deletions.
12 changes: 6 additions & 6 deletions internal/cli/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion internal/engine/buildcontrol/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions internal/engine/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/tiltfile/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,14 @@ def update_settings(
Accepts a list of image names, or '*' to suppress warnings for all images.
"""

def ci_settings(
k8s_grace_period: str='') -> None:
"""Configures 'tilt ci' mode.
Args:
k8s_grace_period: Grace period given for Kubernetes resources to recover after they start failing.
"""

def watch_settings(ignore: Union[str, List[str]]) -> None:
"""Configures global watches.
Expand Down
62 changes: 62 additions & 0 deletions internal/tiltfile/cisettings/ci_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cisettings

import (
"time"

"go.starlark.net/starlark"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/tilt-dev/tilt/internal/tiltfile/starkit"
"github.com/tilt-dev/tilt/internal/tiltfile/value"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
)

// Implements functions for dealing with ci settings.
type Plugin struct{}

func NewPlugin() Plugin {
return Plugin{}
}

func (e Plugin) NewState() interface{} {
return &v1alpha1.SessionCISpec{}
}

func (e Plugin) OnStart(env *starkit.Environment) error {
return env.AddBuiltin("ci_settings", e.ciSettings)
}

func (e *Plugin) ciSettings(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var k8sGracePeriod value.Duration = -1
if err := starkit.UnpackArgs(thread, fn.Name(), args, kwargs,
"k8s_grace_period?", &k8sGracePeriod); err != nil {
return nil, err
}

err := starkit.SetState(thread, func(settings *v1alpha1.SessionCISpec) *v1alpha1.SessionCISpec {
if k8sGracePeriod != -1 {
settings = settings.DeepCopy()
settings.K8sGracePeriod = &metav1.Duration{time.Duration(k8sGracePeriod)}
}
return settings
})

return starlark.None, err
}

var _ starkit.StatefulPlugin = Plugin{}

func MustState(model starkit.Model) *v1alpha1.SessionCISpec {
state, err := GetState(model)
if err != nil {
panic(err)
}
return state
}

func GetState(m starkit.Model) (*v1alpha1.SessionCISpec, error) {
state := &v1alpha1.SessionCISpec{}
err := m.Load(&state)
return state, err
}
58 changes: 58 additions & 0 deletions internal/tiltfile/cisettings/ci_settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cisettings

import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/tilt-dev/tilt/internal/tiltfile/starkit"
)

func TestK8sGracePeriod(t *testing.T) {
f := newFixture(t)
f.File("Tiltfile", `
ci_settings(k8s_grace_period='3m')
`)

result, err := f.ExecFile("Tiltfile")
require.NoError(t, err)

ci, err := GetState(result)
require.NoError(t, err)
require.Equal(t, 3*time.Minute, ci.K8sGracePeriod.Duration)
}

func TestK8sGracePeriodOverride(t *testing.T) {
f := newFixture(t)
f.File("Tiltfile", `
ci_settings(k8s_grace_period='3m')
ci_settings(k8s_grace_period='5s')
`)

result, err := f.ExecFile("Tiltfile")
require.NoError(t, err)

ci, err := GetState(result)
require.NoError(t, err)
require.Equal(t, 5*time.Second, ci.K8sGracePeriod.Duration)
}

func TestK8sGracePeriodOverrideEmpty(t *testing.T) {
f := newFixture(t)
f.File("Tiltfile", `
ci_settings(k8s_grace_period='3m')
ci_settings()
`)

result, err := f.ExecFile("Tiltfile")
require.NoError(t, err)

ci, err := GetState(result)
require.NoError(t, err)
require.Equal(t, 3*time.Minute, ci.K8sGracePeriod.Duration)
}

func newFixture(t testing.TB) *starkit.Fixture {
return starkit.NewFixture(t, NewPlugin())
}
8 changes: 8 additions & 0 deletions internal/tiltfile/tiltfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/tilt-dev/tilt/internal/ospath"
"github.com/tilt-dev/tilt/internal/sliceutils"
tiltfileanalytics "github.com/tilt-dev/tilt/internal/tiltfile/analytics"
"github.com/tilt-dev/tilt/internal/tiltfile/cisettings"
"github.com/tilt-dev/tilt/internal/tiltfile/config"
"github.com/tilt-dev/tilt/internal/tiltfile/dockerprune"
"github.com/tilt-dev/tilt/internal/tiltfile/hasher"
Expand Down Expand Up @@ -59,6 +60,7 @@ type TiltfileLoadResult struct {
DefaultRegistry *corev1alpha1.RegistryHosting
ObjectSet apiset.ObjectSet
Hashes hasher.Hashes
CISettings *corev1alpha1.SessionCISpec

// For diagnostic purposes only
BuiltinCalls []starkit.BuiltinCall `json:"-"`
Expand Down Expand Up @@ -217,6 +219,12 @@ func (tfl tiltfileLoader) Load(ctx context.Context, tf *corev1alpha1.Tiltfile, p
us, _ := updatesettings.GetState(result)
tlr.UpdateSettings = us

ci, err := cisettings.GetState(result)
if err != nil {
panic(err)
}
tlr.CISettings = ci

configSettings, _ := config.GetState(result)
if tlr.Error == nil {
tlr.EnabledManifests, tlr.Error = configSettings.EnabledResources(tf, manifests)
Expand Down
2 changes: 2 additions & 0 deletions internal/tiltfile/tiltfile_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/tilt-dev/tilt/internal/ospath"
"github.com/tilt-dev/tilt/internal/sliceutils"
"github.com/tilt-dev/tilt/internal/tiltfile/analytics"
"github.com/tilt-dev/tilt/internal/tiltfile/cisettings"
"github.com/tilt-dev/tilt/internal/tiltfile/config"
"github.com/tilt-dev/tilt/internal/tiltfile/dockerprune"
"github.com/tilt-dev/tilt/internal/tiltfile/encoding"
Expand Down Expand Up @@ -222,6 +223,7 @@ func (s *tiltfileState) loadManifests(tf *v1alpha1.Tiltfile) ([]model.Manifest,
telemetry.NewPlugin(),
metrics.NewPlugin(),
updatesettings.NewPlugin(),
cisettings.NewPlugin(),
secretsettings.NewPlugin(),
encoding.NewPlugin(),
shlex.NewPlugin(),
Expand Down
Loading

0 comments on commit 3236355

Please sign in to comment.