Skip to content

Commit

Permalink
cli: add a --context flag for setting kube context. Fixes #3296
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks committed Aug 28, 2020
1 parent 65df886 commit d1feeda
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 54 deletions.
1 change: 1 addition & 0 deletions internal/cli/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ While Tilt is running, you can view the UI at %s:%d
addStartServerFlags(cmd)
addDevServerFlags(cmd)
addTiltfileFlag(cmd, &c.fileName)
addKubeContextFlag(cmd)

cmd.Flags().BoolVar(&logActionsFlag, "logactions", false, "log all actions and state changes")
cmd.Flags().Lookup("logactions").Hidden = true
Expand Down
1 change: 1 addition & 0 deletions internal/cli/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (c *doctorCmd) register() *cobra.Command {
Use: "doctor",
Short: "Print diagnostic information about the Tilt environment, for filing bug reports",
}
addKubeContextFlag(cmd)
return cmd
}

Expand Down
1 change: 1 addition & 0 deletions internal/cli/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ In that case, see https://tilt.dev/user_config.html and/or comments in your Tilt
}

addTiltfileFlag(cmd, &c.fileName)
addKubeContextFlag(cmd)
cmd.Flags().BoolVar(&c.deleteNamespaces, "delete-namespaces", false, "delete namespaces defined in the Tiltfile (by default, don't)")

return cmd
Expand Down
11 changes: 11 additions & 0 deletions internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"github.com/spf13/cobra"

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

Expand All @@ -13,6 +14,10 @@ func addTiltfileFlag(cmd *cobra.Command, s *string) {
cmd.Flags().StringVarP(s, "file", "f", tiltfile.FileName, "Path to Tiltfile")
}

func addKubeContextFlag(cmd *cobra.Command) {
cmd.Flags().StringVar(&kubeContextOverride, "context", "", "Kubernetes context override. Equivalent to kubectl --context")
}

// For commands that talk to the web server.
func addConnectServerFlags(cmd *cobra.Command) {
cmd.Flags().IntVar(&webPort, "port", DefaultWebPort, "Port for the Tilt HTTP server. Only necessary if you started Tilt with --port.")
Expand All @@ -29,3 +34,9 @@ func addDevServerFlags(cmd *cobra.Command) {
cmd.Flags().IntVar(&webDevPort, "webdev-port", DefaultWebDevPort, "Port for the Tilt Dev Webpack server. Only applies when using --web-mode=local")
cmd.Flags().Var(&webModeFlag, "web-mode", "Values: local, prod. Controls whether to use prod assets or a local dev server. (If flag not specified: if Tilt was built from source, it will use a local asset server; otherwise, prod assets.)")
}

var kubeContextOverride string

func ProvideKubeContextOverride() k8s.KubeContextOverride {
return k8s.KubeContextOverride(kubeContextOverride)
}
1 change: 1 addition & 0 deletions internal/cli/tiltfile_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Run with -v | --verbose to print Tiltfile execution logs on stderr, regardless o
}

addTiltfileFlag(cmd, &c.fileName)
addKubeContextFlag(cmd)
cmd.Flags().BoolVarP(&c.builtinTimings, "builtin-timings", "b", false, "If true, print timing data for Tiltfile builtin calls instead of Tiltfile result JSON")
cmd.Flags().DurationVar(&c.durThreshold, "dur-threshold", 0, "Only compatible with Builtin Timings mode. Should be a Go duration string. If passed, only print information about builtin calls lasting this duration and longer.")

Expand Down
1 change: 1 addition & 0 deletions internal/cli/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ local resources--i.e. those using serve_cmd--are terminated when you exit Tilt.
addStartServerFlags(cmd)
addDevServerFlags(cmd)
addTiltfileFlag(cmd, &c.fileName)
addKubeContextFlag(cmd)
cmd.Flags().Lookup("logactions").Hidden = true
cmd.Flags().StringVar(&c.outputSnapshotOnExit, "output-snapshot-on-exit", "", "If specified, Tilt will dump a snapshot of its state to the specified path when it exits")

Expand Down
3 changes: 2 additions & 1 deletion internal/cli/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ var K8sWireSet = wire.NewSet(
k8s.ProvideContainerRuntime,
k8s.ProvideServerVersion,
k8s.ProvideK8sClient,
k8s.ProvideOwnerFetcher)
k8s.ProvideOwnerFetcher,
ProvideKubeContextOverride)

var BaseWireSet = wire.NewSet(
K8sWireSet,
Expand Down
90 changes: 52 additions & 38 deletions internal/cli/wire_gen.go

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

1 change: 1 addition & 0 deletions internal/engine/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func provideDockerComposeBuildAndDeployer(

// EnvNone ensures that we get an exploding k8s client.
wire.Value(k8s.Env(k8s.EnvNone)),
wire.Value(k8s.KubeContextOverride("")),
k8s.ProvideClientConfig,
k8s.ProvideConfigNamespace,
k8s.ProvideKubeContext,
Expand Down
12 changes: 6 additions & 6 deletions internal/engine/wire_gen.go

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

10 changes: 6 additions & 4 deletions internal/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type PodID string
type NodeID string
type ServiceName string
type KubeContext string
type KubeContextOverride string

// NOTE(nick): This isn't right. DefaultNamespace is a function of your kubectl context.
const DefaultNamespace = Namespace("default")
Expand Down Expand Up @@ -474,14 +475,15 @@ func ProvideClientset(cfg RESTConfigOrError) ClientsetOrError {
return ClientsetOrError{Clientset: clientset, Error: err}
}

func ProvideClientConfig() clientcmd.ClientConfig {
func ProvideClientConfig(contextOverride KubeContextOverride) clientcmd.ClientConfig {
rules := clientcmd.NewDefaultClientConfigLoadingRules()
rules.DefaultClientConfig = &clientcmd.DefaultClientConfig

overrides := &clientcmd.ConfigOverrides{}
overrides := &clientcmd.ConfigOverrides{
CurrentContext: string(contextOverride),
}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
rules,
overrides)
rules, overrides)
}

// The namespace in the kubeconfig.
Expand Down

0 comments on commit d1feeda

Please sign in to comment.