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

Improve kubectl plugin #3870

Merged
merged 1 commit into from
Mar 14, 2019
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
84 changes: 84 additions & 0 deletions cmd/plugin/commands/backends/backends.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package backends

import (
"fmt"
"github.com/spf13/cobra"

"k8s.io/cli-runtime/pkg/genericclioptions"

"k8s.io/ingress-nginx/cmd/plugin/kubectl"
"k8s.io/ingress-nginx/cmd/plugin/request"
"k8s.io/ingress-nginx/cmd/plugin/util"
)

// CreateCommand creates and returns this cobra subcommand
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
var pod, deployment *string
cmd := &cobra.Command{
Use: "backends",
Short: "Inspect the dynamic backend information of an ingress-nginx instance",
RunE: func(cmd *cobra.Command, args []string) error {
backend, err := cmd.Flags().GetString("backend")
if err != nil {
return err
}
onlyList, err := cmd.Flags().GetBool("list")
if err != nil {
return err
}
if onlyList && backend != "" {
return fmt.Errorf("--list and --backend cannot both be specified")
}

util.PrintError(backends(flags, *pod, *deployment, backend, onlyList))
return nil
},
}

pod = util.AddPodFlag(cmd)
deployment = util.AddDeploymentFlag(cmd)
cmd.Flags().String("backend", "", "Output only the information for the given backend")
cmd.Flags().Bool("list", false, "Output a newline-separated list of backend names")

return cmd
}

func backends(flags *genericclioptions.ConfigFlags, podName string, deployment string, backend string, onlyList bool) error {
var command []string
if onlyList {
command = []string{"/dbg", "backends", "list"}
} else if backend != "" {
command = []string{"/dbg", "backends", "get", backend}
} else {
command = []string{"/dbg", "backends", "all"}
}

pod, err := request.ChoosePod(flags, podName, deployment)
if err != nil {
return err
}

out, err := kubectl.PodExecString(flags, &pod, command)
if err != nil {
return err
}

fmt.Printf(out)
return nil
}
70 changes: 70 additions & 0 deletions cmd/plugin/commands/certs/certs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package certs

import (
"fmt"
"github.com/spf13/cobra"

"k8s.io/cli-runtime/pkg/genericclioptions"

"k8s.io/ingress-nginx/cmd/plugin/kubectl"
"k8s.io/ingress-nginx/cmd/plugin/request"
"k8s.io/ingress-nginx/cmd/plugin/util"
)

// CreateCommand creates and returns this cobra subcommand
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
var pod, deployment *string
cmd := &cobra.Command{
Use: "certs",
Short: "Output the certificate data stored in an ingress-nginx pod",
RunE: func(cmd *cobra.Command, args []string) error {
host, err := cmd.Flags().GetString("host")
if err != nil {
return err
}

util.PrintError(certs(flags, *pod, *deployment, host))
return nil
},
}

cmd.Flags().String("host", "", "Get the cert for this hostname")
cobra.MarkFlagRequired(cmd.Flags(), "host")
pod = util.AddPodFlag(cmd)
deployment = util.AddDeploymentFlag(cmd)

return cmd
}

func certs(flags *genericclioptions.ConfigFlags, podName string, deployment string, host string) error {
command := []string{"/dbg", "certs", "get", host}

pod, err := request.ChoosePod(flags, podName, deployment)
if err != nil {
return err
}

out, err := kubectl.PodExecString(flags, &pod, command)
if err != nil {
return err
}

fmt.Print(out)
return nil
}
78 changes: 78 additions & 0 deletions cmd/plugin/commands/conf/conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package conf

import (
"fmt"
"github.com/spf13/cobra"
"strings"

"k8s.io/cli-runtime/pkg/genericclioptions"

"k8s.io/ingress-nginx/cmd/plugin/kubectl"
"k8s.io/ingress-nginx/cmd/plugin/request"
"k8s.io/ingress-nginx/cmd/plugin/util"
"k8s.io/ingress-nginx/internal/nginx"
)

// CreateCommand creates and returns this cobra subcommand
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
var pod, deployment *string
cmd := &cobra.Command{
Use: "conf",
Short: "Inspect the generated nginx.conf",
RunE: func(cmd *cobra.Command, args []string) error {
host, err := cmd.Flags().GetString("host")
if err != nil {
return err
}

util.PrintError(conf(flags, host, *pod, *deployment))
return nil
},
}
cmd.Flags().String("host", "", "Print just the server block with this hostname")
pod = util.AddPodFlag(cmd)
deployment = util.AddDeploymentFlag(cmd)

return cmd
}

func conf(flags *genericclioptions.ConfigFlags, host string, podName string, deployment string) error {
pod, err := request.ChoosePod(flags, podName, deployment)
if err != nil {
return err
}

nginxConf, err := kubectl.PodExecString(flags, &pod, []string{"/dbg", "conf"})
if err != nil {
return err
}

if host != "" {
block, err := nginx.GetServerBlock(nginxConf, host)
if err != nil {
return err
}

fmt.Println(strings.TrimRight(strings.Trim(block, " \n"), " \n\t"))
} else {
fmt.Print(nginxConf)
}

return nil
}
72 changes: 72 additions & 0 deletions cmd/plugin/commands/exec/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package exec

import (
"github.com/spf13/cobra"

"k8s.io/cli-runtime/pkg/genericclioptions"

"k8s.io/ingress-nginx/cmd/plugin/kubectl"
"k8s.io/ingress-nginx/cmd/plugin/request"
"k8s.io/ingress-nginx/cmd/plugin/util"
)

// CreateCommand creates and returns this cobra subcommand
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
opts := execFlags{}
var pod, deployment *string

cmd := &cobra.Command{
Use: "exec",
Short: "Execute a command inside an ingress-nginx pod",
RunE: func(cmd *cobra.Command, args []string) error {
util.PrintError(exec(flags, *pod, *deployment, args, opts))
return nil
},
}
pod = util.AddPodFlag(cmd)
deployment = util.AddDeploymentFlag(cmd)
cmd.Flags().BoolVarP(&opts.TTY, "tty", "t", false, "Stdin is a TTY")
cmd.Flags().BoolVarP(&opts.Stdin, "stdin", "i", false, "Pass stdin to the container")

return cmd
}

type execFlags struct {
TTY bool
Stdin bool
}

func exec(flags *genericclioptions.ConfigFlags, podName string, deployment string, cmd []string, opts execFlags) error {
pod, err := request.ChoosePod(flags, podName, deployment)
if err != nil {
return err
}

args := []string{"exec"}
if opts.TTY {
args = append(args, "-t")
}
if opts.Stdin {
args = append(args, "-i")
}

args = append(args, []string{"-n", pod.Namespace, pod.Name, "--"}...)
args = append(args, cmd...)
return kubectl.Exec(flags, args)
}
60 changes: 60 additions & 0 deletions cmd/plugin/commands/general/general.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package general

import (
"fmt"
"github.com/spf13/cobra"

"k8s.io/cli-runtime/pkg/genericclioptions"

"k8s.io/ingress-nginx/cmd/plugin/kubectl"
"k8s.io/ingress-nginx/cmd/plugin/request"
"k8s.io/ingress-nginx/cmd/plugin/util"
)

// CreateCommand creates and returns this cobra subcommand
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
var pod, deployment *string
cmd := &cobra.Command{
Use: "general",
Short: "Inspect the other dynamic ingress-nginx information",
RunE: func(cmd *cobra.Command, args []string) error {
util.PrintError(general(flags, *pod, *deployment))
return nil
},
}
pod = util.AddPodFlag(cmd)
deployment = util.AddDeploymentFlag(cmd)

return cmd
}

func general(flags *genericclioptions.ConfigFlags, podName string, deployment string) error {
pod, err := request.ChoosePod(flags, podName, deployment)
if err != nil {
return err
}

out, err := kubectl.PodExecString(flags, &pod, []string{"/dbg", "general"})
if err != nil {
return err
}

fmt.Print(out)
return nil
}
Loading