Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add k8s_namespace() to pull cached namespace #5935

Merged
merged 3 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 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 @@ -3327,7 +3327,7 @@ func newTestFixture(t *testing.T, options ...fixtureOptions) *testFixture {
au := engineanalytics.NewAnalyticsUpdater(ta, engineanalytics.CmdTags{}, engineMode)
ar := engineanalytics.ProvideAnalyticsReporter(ta, st, kClient, env, feature.MainDefaults)
fakeDcc := dockercompose.NewFakeDockerComposeClient(t, ctx)
k8sContextPlugin := k8scontext.NewPlugin("fake-context", env)
k8sContextPlugin := k8scontext.NewPlugin("fake-context", "default", env)
versionPlugin := version.NewPlugin(model.TiltBuild{Version: "0.5.0"})
configPlugin := config.NewPlugin("up")
execer := localexec.NewFakeExecer(t)
Expand Down
21 changes: 16 additions & 5 deletions internal/tiltfile/k8scontext/k8scontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ import (
// Implements functions for dealing with the Kubernetes context.
// Exposes an API for other plugins to get and validate the allowed k8s context.
type Plugin struct {
context k8s.KubeContext
env clusterid.Product
context k8s.KubeContext
namespace k8s.Namespace
env clusterid.Product
}

func NewPlugin(context k8s.KubeContext, env clusterid.Product) Plugin {
func NewPlugin(context k8s.KubeContext, namespace k8s.Namespace, env clusterid.Product) Plugin {
return Plugin{
context: context,
env: env,
context: context,
namespace: namespace,
env: env,
}
}

Expand All @@ -41,13 +43,22 @@ func (e Plugin) OnStart(env *starkit.Environment) error {
if err != nil {
return err
}

err = env.AddBuiltin("k8s_namespace", e.k8sNamespace)
if err != nil {
return err
}
return nil
}

func (e Plugin) k8sContext(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
return starlark.String(e.context), nil
}

func (e Plugin) k8sNamespace(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
return starlark.String(e.namespace), nil
}

func (e Plugin) allowK8sContexts(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var contexts starlark.Value
if err := starkit.UnpackArgs(thread, fn.Name(), args, kwargs,
Expand Down
30 changes: 26 additions & 4 deletions internal/tiltfile/k8scontext/k8scontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,30 @@ import (
"github.com/tilt-dev/tilt/internal/tiltfile/starkit"
)

func TestK8sNamespaceDefaultNamespace(t *testing.T) {
f := NewFixture(t, "gke-blorg", "default", clusterid.ProductGKE)
f.File("Tiltfile", `
print(k8s_namespace())
`)
_, err := f.ExecFile("Tiltfile")
assert.NoError(t, err)

assert.Equal(t, "default\n", f.PrintOutput())
}

func TestK8sNamespaceNonStandardNamespace(t *testing.T) {
f := NewFixture(t, "gke-blorg", "im-a-teapot", clusterid.ProductGKE)
f.File("Tiltfile", `
print(k8s_namespace())
`)
_, err := f.ExecFile("Tiltfile")
assert.NoError(t, err)

assert.Equal(t, "im-a-teapot\n", f.PrintOutput())
}

func TestAllowK8sContext(t *testing.T) {
f := NewFixture(t, "gke-blorg", clusterid.ProductGKE)
f := NewFixture(t, "gke-blorg", "default", clusterid.ProductGKE)
f.File("Tiltfile", `
allow_k8s_contexts('gke-blorg')
`)
Expand All @@ -26,7 +48,7 @@ allow_k8s_contexts('gke-blorg')
}

func TestForbidK8sContext(t *testing.T) {
f := NewFixture(t, "gke-blorg", clusterid.ProductGKE)
f := NewFixture(t, "gke-blorg", "default", clusterid.ProductGKE)
f.File("Tiltfile", `
`)
model, err := f.ExecFile("Tiltfile")
Expand All @@ -38,6 +60,6 @@ func TestForbidK8sContext(t *testing.T) {
assert.True(t, MustState(model).IsAllowed(f.Tiltfile()))
}

func NewFixture(tb testing.TB, ctx k8s.KubeContext, env clusterid.Product) *starkit.Fixture {
return starkit.NewFixture(tb, NewPlugin(ctx, env))
func NewFixture(tb testing.TB, ctx k8s.KubeContext, ns k8s.Namespace, env clusterid.Product) *starkit.Fixture {
return starkit.NewFixture(tb, NewPlugin(ctx, ns, env))
}
12 changes: 8 additions & 4 deletions internal/tiltfile/tiltfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3600,6 +3600,8 @@ func TestK8sContext(t *testing.T) {
f.file("Tiltfile", `
if k8s_context() != 'fake-context':
fail('bad context')
if k8s_namespace() != 'fake-namespace':
fail('bad namespace')
k8s_yaml('foo.yaml')
docker_build('gcr.io/foo', 'foo')
`)
Expand Down Expand Up @@ -5750,9 +5752,10 @@ type fixture struct {
out *bytes.Buffer
t *testing.T
*tempdir.TempDirFixture
k8sContext k8s.KubeContext
k8sEnv clusterid.Product
webHost model.WebHost
k8sContext k8s.KubeContext
k8sNamespace k8s.Namespace
k8sEnv clusterid.Product
webHost model.WebHost

ta *tiltanalytics.TiltAnalytics
an *analytics.MemoryAnalytics
Expand All @@ -5765,7 +5768,7 @@ type fixture struct {
func (f *fixture) newTiltfileLoader() TiltfileLoader {
dcc := dockercompose.NewDockerComposeClient(docker.LocalEnv{})

k8sContextPlugin := k8scontext.NewPlugin(f.k8sContext, f.k8sEnv)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

k8sContextPlugin := k8scontext.NewPlugin(f.k8sContext, f.k8sNamespace, f.k8sEnv)
versionPlugin := version.NewPlugin(model.TiltBuild{Version: "0.5.0"})
configPlugin := config.NewPlugin("up")
localEnv := localexec.DefaultEnv(12345, f.webHost)
Expand Down Expand Up @@ -5797,6 +5800,7 @@ func newFixture(t *testing.T) *fixture {
an: ma,
ta: ta,
k8sContext: "fake-context",
k8sNamespace: "fake-namespace",
k8sEnv: clusterid.ProductDockerDesktop,
features: features,
}
Expand Down