Skip to content

Commit

Permalink
refactor: refactor command dev (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
wencaiwulue authored Jul 26, 2024
1 parent 9283c2f commit 49adeac
Show file tree
Hide file tree
Showing 16 changed files with 1,000 additions and 2,368 deletions.
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ Run the Kubernetes pod in the local Docker container, and cooperate with the ser
the specified header to the local, or all the traffic to the local.

```shell
~ kubevpn dev deployment/authors --headers a=1 -it --rm --entrypoint sh
~ kubevpn dev deployment/authors --headers a=1 --entrypoint sh
connectting to cluster
start to connect
got cidr from cache
Expand Down Expand Up @@ -500,13 +500,13 @@ docker logs $(docker ps --format '{{.Names}}' | grep nginx_default_kubevpn)
If you just want to start up a docker image, you can use a simple way like this:
```shell
kubevpn dev deployment/authors --no-proxy -it --rm
kubevpn dev deployment/authors --no-proxy
```
Example:
```shell
➜ ~ kubevpn dev deployment/authors --no-proxy -it --rm
➜ ~ kubevpn dev deployment/authors --no-proxy
connectting to cluster
start to connect
got cidr from cache
Expand Down Expand Up @@ -567,14 +567,7 @@ e008f553422a: Pull complete
33f0298d1d4f: Pull complete
Digest: sha256:115b975a97edd0b41ce7a0bc1d8428e6b8569c91a72fe31ea0bada63c685742e
Status: Downloaded newer image for naison/kubevpn:v2.0.0
root@d0b3dab8912a:/app# kubevpn dev deployment/authors --headers user=naison -it --entrypoint sh

----------------------------------------------------------------------------------
Warn: Use sudo to execute command kubevpn can not use user env KUBECONFIG.
Because of sudo user env and user env are different.
Current env KUBECONFIG value:
----------------------------------------------------------------------------------

root@d0b3dab8912a:/app# kubevpn dev deployment/authors --headers user=naison --entrypoint sh
hostname is d0b3dab8912a
connectting to cluster
start to connect
Expand Down
15 changes: 4 additions & 11 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ leave workload deployments/productpage successfully
Docker。

```shell
~ kubevpn dev deployment/authors --headers a=1 -it --rm --entrypoint sh
~ kubevpn dev deployment/authors --headers a=1 --entrypoint sh
connectting to cluster
start to connect
got cidr from cache
Expand Down Expand Up @@ -406,13 +406,13 @@ fc04e42799a5 nginx:latest "/docker-entrypoint.…" 37 sec
如果你只是想在本地启动镜像,可以用一种简单的方式:
```shell
kubevpn dev deployment/authors --no-proxy -it --rm
kubevpn dev deployment/authors --no-proxy
```
例如:
```shell
➜ ~ kubevpn dev deployment/authors --no-proxy -it --rm
➜ ~ kubevpn dev deployment/authors --no-proxy
connectting to cluster
start to connect
got cidr from cache
Expand Down Expand Up @@ -471,14 +471,7 @@ e008f553422a: Pull complete
33f0298d1d4f: Pull complete
Digest: sha256:115b975a97edd0b41ce7a0bc1d8428e6b8569c91a72fe31ea0bada63c685742e
Status: Downloaded newer image for naison/kubevpn:v2.0.0
root@d0b3dab8912a:/app# kubevpn dev deployment/authors --headers user=naison -it --entrypoint sh

----------------------------------------------------------------------------------
Warn: Use sudo to execute command kubevpn can not use user env KUBECONFIG.
Because of sudo user env and user env are different.
Current env KUBECONFIG value:
----------------------------------------------------------------------------------

root@d0b3dab8912a:/app# kubevpn dev deployment/authors --headers user=naison --entrypoint sh
hostname is d0b3dab8912a
connectting to cluster
start to connect
Expand Down
76 changes: 41 additions & 35 deletions cmd/kubevpn/cmds/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

dockercomp "github.com/docker/cli/cli/command/completion"
"github.com/containerd/containerd/platforms"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
Expand All @@ -20,15 +20,8 @@ import (
)

func CmdDev(f cmdutil.Factory) *cobra.Command {
client, cli, err := util.GetClient()
if err != nil {
log.Fatal(err)
}
var options = &dev.Options{
Factory: f,
NoProxy: false,
Cli: client,
DockerCli: cli,
ExtraRouteInfo: handler.ExtraRouteInfo{},
}
var sshConf = &util.SshConfig{}
Expand Down Expand Up @@ -67,29 +60,29 @@ func CmdDev(f cmdutil.Factory) *cobra.Command {
kubevpn dev deployment/productpage --ssh-alias <alias>
# Switch to terminal mode; send stdin to 'bash' and sends stdout/stderror from 'bash' back to the client
kubevpn dev deployment/authors -n default --kubeconfig ~/.kube/config --ssh-alias dev -i -t --entrypoint /bin/bash
kubevpn dev deployment/authors -n default --kubeconfig ~/.kube/config --ssh-alias dev --entrypoint /bin/bash
or
kubevpn dev deployment/authors -n default --kubeconfig ~/.kube/config --ssh-alias dev -it --entrypoint /bin/bash
kubevpn dev deployment/authors -n default --kubeconfig ~/.kube/config --ssh-alias dev --entrypoint /bin/bash
# Support ssh auth GSSAPI
kubevpn dev deployment/authors -n default --ssh-addr <HOST:PORT> --ssh-username <USERNAME> --gssapi-keytab /path/to/keytab -it --entrypoint /bin/bash
kubevpn dev deployment/authors -n default --ssh-addr <HOST:PORT> --ssh-username <USERNAME> --gssapi-cache /path/to/cache -it --entrypoint /bin/bash
kubevpn dev deployment/authors -n default --ssh-addr <HOST:PORT> --ssh-username <USERNAME> --gssapi-password <PASSWORD> -it --entrypoint /bin/bash
kubevpn dev deployment/authors -n default --ssh-addr <HOST:PORT> --ssh-username <USERNAME> --gssapi-keytab /path/to/keytab --entrypoint /bin/bash
kubevpn dev deployment/authors -n default --ssh-addr <HOST:PORT> --ssh-username <USERNAME> --gssapi-cache /path/to/cache --entrypoint /bin/bash
kubevpn dev deployment/authors -n default --ssh-addr <HOST:PORT> --ssh-username <USERNAME> --gssapi-password <PASSWORD> --entrypoint /bin/bash
`)),
ValidArgsFunction: completion.ResourceTypeAndNameCompletionFunc(f),
Args: cobra.MatchAll(cobra.OnlyValidArgs),
DisableFlagsInUseLine: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
fmt.Fprintf(os.Stdout, "You must specify the type of resource to proxy. %s\n\n", cmdutil.SuggestAPIResources("kubevpn"))
_, _ = fmt.Fprintf(os.Stdout, "You must specify the type of resource to proxy. %s\n\n", cmdutil.SuggestAPIResources("kubevpn"))
fullCmdName := cmd.Parent().CommandPath()
usageString := "Required resource not specified."
if len(fullCmdName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "explain") {
usageString = fmt.Sprintf("%s\nUse \"%s explain <resource>\" for a detailed description of that resource (e.g. %[2]s explain pods).", usageString, fullCmdName)
}
return cmdutil.UsageErrorf(cmd, usageString)
}
err = cmd.Flags().Parse(args[1:])
err := cmd.Flags().Parse(args[1:])
if err != nil {
return err
}
Expand All @@ -99,6 +92,15 @@ func CmdDev(f cmdutil.Factory) *cobra.Command {
return fmt.Errorf(`not support type engine: %s, support ("%s"|"%s")`, config.EngineGvisor, config.EngineMix, config.EngineRaw)
}

if p := options.RunOptions.Platform; p != "" {
if _, err = platforms.Parse(p); err != nil {
return fmt.Errorf("error parsing specified platform: %v", err)
}
}
if err = validatePullOpt(options.RunOptions.Pull); err != nil {
return err
}

err = daemon.StartupDaemon(cmd.Context())
if err != nil {
return err
Expand All @@ -109,7 +111,7 @@ func CmdDev(f cmdutil.Factory) *cobra.Command {
options.Workload = args[0]
for i, arg := range args {
if arg == "--" && i != len(args)-1 {
options.Copts.Args = args[i+1:]
options.ContainerOptions.Args = args[i+1:]
break
}
}
Expand All @@ -123,7 +125,12 @@ func CmdDev(f cmdutil.Factory) *cobra.Command {
}
}
}()
err = dev.DoDev(cmd.Context(), options, sshConf, cmd.Flags(), f, transferImage)

if err := options.InitClient(f); err != nil {
return err
}

err := options.Main(cmd.Context(), sshConf, cmd.Flags(), transferImage)
return err
},
}
Expand All @@ -141,26 +148,25 @@ func CmdDev(f cmdutil.Factory) *cobra.Command {
// diy docker options
cmd.Flags().StringVar(&options.DevImage, "dev-image", "", "Use to startup docker container, Default is pod image")
// origin docker options
dev.AddDockerFlags(options, cmd.Flags(), cli)

_ = cmd.RegisterFlagCompletionFunc(
"env",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return os.Environ(), cobra.ShellCompDirectiveNoFileComp
},
)
_ = cmd.RegisterFlagCompletionFunc(
"env-file",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveDefault
},
)
_ = cmd.RegisterFlagCompletionFunc(
"network",
dockercomp.NetworkNames(cli),
)
dev.AddDockerFlags(options, cmd.Flags())

handler.AddExtraRoute(cmd.Flags(), &options.ExtraRouteInfo)
util.AddSshFlags(cmd.Flags(), sshConf)
return cmd
}

func validatePullOpt(val string) error {
switch val {
case dev.PullImageAlways, dev.PullImageMissing, dev.PullImageNever, "":
// valid option, but nothing to do yet
return nil
default:
return fmt.Errorf(
"invalid pull option: '%s': must be one of %q, %q or %q",
val,
dev.PullImageAlways,
dev.PullImageMissing,
dev.PullImageNever,
)
}
}
Loading

0 comments on commit 49adeac

Please sign in to comment.