Skip to content

Commit

Permalink
tiltfile: k8s_context returns the name of the k8s context Tilt locked (
Browse files Browse the repository at this point in the history
…#1653)

This is useful to allow you to set a different registry when iterating against microk8s. e.g.,

if k8s_context() == 'microk8s': #microk8s uses a local registry
  default_registry('localhost:32000')
  • Loading branch information
dbentley authored May 17, 2019
1 parent ef8909b commit 5cd4858
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
6 changes: 3 additions & 3 deletions internal/cli/wire_gen.go

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

2 changes: 1 addition & 1 deletion internal/engine/upper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2342,7 +2342,7 @@ func newTestFixture(t *testing.T) *testFixture {
fakeDcc := dockercompose.NewFakeDockerComposeClient(t, ctx)
realDcc := dockercompose.NewDockerComposeClient(docker.Env{})

tfl := tiltfile.ProvideTiltfileLoader(an, realDcc)
tfl := tiltfile.ProvideTiltfileLoader(an, realDcc, "fake-context")
cc := NewConfigsController(tfl)
dcw := NewDockerComposeEventWatcher(fakeDcc)
dclm := NewDockerComposeLogManager(fakeDcc)
Expand Down
4 changes: 4 additions & 0 deletions internal/tiltfile/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,3 +924,7 @@ func uniqueResourceNames(es []k8s.K8sEntity) ([]string, error) {

return ret, nil
}

func (s *tiltfileState) k8sContext(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
return starlark.String(s.kubeContext), nil
}
11 changes: 6 additions & 5 deletions internal/tiltfile/tiltfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ func (tfl *FakeTiltfileLoader) Load(ctx context.Context, filename string, matchi
}, tfl.Err
}

func ProvideTiltfileLoader(analytics analytics.Analytics, dcCli dockercompose.DockerComposeClient) TiltfileLoader {
return tiltfileLoader{analytics: analytics, dcCli: dcCli}
func ProvideTiltfileLoader(analytics analytics.Analytics, dcCli dockercompose.DockerComposeClient, kubeContext k8s.KubeContext) TiltfileLoader {
return tiltfileLoader{analytics: analytics, dcCli: dcCli, kubeContext: kubeContext}
}

type tiltfileLoader struct {
analytics analytics.Analytics
dcCli dockercompose.DockerComposeClient
analytics analytics.Analytics
dcCli dockercompose.DockerComposeClient
kubeContext k8s.KubeContext
}

var _ TiltfileLoader = &tiltfileLoader{}
Expand All @@ -88,7 +89,7 @@ func (tfl tiltfileLoader) Load(ctx context.Context, filename string, matching ma
return TiltfileLoadResult{ConfigFiles: []string{absFilename}}, err
}

s := newTiltfileState(ctx, tfl.dcCli, absFilename)
s := newTiltfileState(ctx, tfl.dcCli, absFilename, tfl.kubeContext)
printedWarnings := false
defer func() {
tlr.ConfigFiles = s.configFiles
Expand Down
12 changes: 8 additions & 4 deletions internal/tiltfile/tiltfile_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ type resourceSet struct {

type tiltfileState struct {
// set at creation
ctx context.Context
filename localPath
dcCli dockercompose.DockerComposeClient
ctx context.Context
filename localPath
dcCli dockercompose.DockerComposeClient
kubeContext k8s.KubeContext

// added to during execution
configFiles []string
Expand Down Expand Up @@ -83,12 +84,13 @@ const (
k8sResourceAssemblyVersionReasonExplicit
)

func newTiltfileState(ctx context.Context, dcCli dockercompose.DockerComposeClient, filename string) *tiltfileState {
func newTiltfileState(ctx context.Context, dcCli dockercompose.DockerComposeClient, filename string, kubeContext k8s.KubeContext) *tiltfileState {
lp := localPath{path: filename}
s := &tiltfileState{
ctx: ctx,
filename: localPath{path: filename},
dcCli: dcCli,
kubeContext: kubeContext,
buildIndex: newBuildIndex(),
k8sByName: make(map[string]*k8sResource),
k8sImageJSONPaths: make(map[k8sObjectSelector][]k8s.JSONPath),
Expand Down Expand Up @@ -140,6 +142,7 @@ const (
k8sKindN = "k8s_kind"
k8sImageJSONPathN = "k8s_image_json_path"
workloadToResourceFunctionN = "workload_to_resource_function"
k8sContextN = "k8s_context"

// file functions
localGitRepoN = "local_git_repo"
Expand Down Expand Up @@ -262,6 +265,7 @@ func (s *tiltfileState) predeclared() starlark.StringDict {
addBuiltin(r, k8sKindN, s.k8sKind)
addBuiltin(r, k8sImageJSONPathN, s.k8sImageJsonPath)
addBuiltin(r, workloadToResourceFunctionN, s.workloadToResourceFunctionFn)
addBuiltin(r, k8sContextN, s.k8sContext)
addBuiltin(r, localGitRepoN, s.localGitRepo)
addBuiltin(r, kustomizeN, s.kustomize)
addBuiltin(r, helmN, s.helm)
Expand Down
23 changes: 22 additions & 1 deletion internal/tiltfile/tiltfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3215,6 +3215,27 @@ k8s_yaml(yml)
)
}

func TestK8sContext(t *testing.T) {
f := newFixture(t)
defer f.TearDown()

f.setupFoo()

f.file("Tiltfile", `
if k8s_context() != 'fake-context':
fail('bad context')
k8s_yaml('foo.yaml')
docker_build('gcr.io/foo', 'foo')
`)

f.load()
f.assertNextManifest("foo",
db(image("gcr.io/foo")),
deployment("foo"))
f.assertConfigFiles("Tiltfile", ".tiltignore", "foo/Dockerfile", "foo/.dockerignore", "foo.yaml")

}

type fixture struct {
ctx context.Context
t *testing.T
Expand All @@ -3232,7 +3253,7 @@ func newFixture(t *testing.T) *fixture {
f := tempdir.NewTempDirFixture(t)
an := analytics.NewMemoryAnalytics()
dcc := dockercompose.NewDockerComposeClient(docker.Env{})
tfl := ProvideTiltfileLoader(an, dcc)
tfl := ProvideTiltfileLoader(an, dcc, "fake-context")

r := &fixture{
ctx: ctx,
Expand Down

0 comments on commit 5cd4858

Please sign in to comment.