-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
session: api for graceful recovery in 'tilt ci'
Signed-off-by: Nick Santos <nick.santos@docker.com>
- Loading branch information
Showing
13 changed files
with
1,110 additions
and
658 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.