Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanvallejo/031d20ab40488c98c81b9928c5a5e753 to your computer and use it in GitHub Desktop.
Save juanvallejo/031d20ab40488c98c81b9928c5a5e753 to your computer and use it in GitHub Desktop.
diff --git a/pkg/kubectl/cmd/clusterinfo_dump.go b/pkg/kubectl/cmd/clusterinfo_dump.go
index c4200b221a..cee4efe0a7 100644
--- a/pkg/kubectl/cmd/clusterinfo_dump.go
+++ b/pkg/kubectl/cmd/clusterinfo_dump.go
@@ -25,9 +25,11 @@ import (
"github.com/spf13/cobra"
+ corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+ corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
+ extensionsv1client "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
api "k8s.io/kubernetes/pkg/apis/core"
- "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
@@ -45,11 +47,12 @@ type ClusterInfoDumpOptions struct {
AllNamespaces bool
Namespaces []string
- Timeout time.Duration
- Clientset internalclientset.Interface
- Namespace string
- RESTClientGetter genericclioptions.RESTClientGetter
- LogsForObject polymorphichelpers.LogsForObjectFunc
+ Timeout time.Duration
+ Clientset corev1client.CoreV1Interface
+ ExtensionsClientset extensionsv1client.ExtensionsV1beta1Interface
+ Namespace string
+ RESTClientGetter genericclioptions.RESTClientGetter
+ LogsForObject polymorphichelpers.LogsForObjectFunc
genericclioptions.IOStreams
}
@@ -57,7 +60,7 @@ type ClusterInfoDumpOptions struct {
// NewCmdCreateSecret groups subcommands to create various types of secrets
func NewCmdClusterInfoDump(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
o := &ClusterInfoDumpOptions{
- PrintFlags: genericclioptions.NewPrintFlags("").WithTypeSetter(scheme.Scheme),
+ PrintFlags: genericclioptions.NewPrintFlags("").WithTypeSetter(scheme.Scheme).WithDefaultOutput("json"),
IOStreams: ioStreams,
}
@@ -72,6 +75,9 @@ func NewCmdClusterInfoDump(f cmdutil.Factory, ioStreams genericclioptions.IOStre
cmdutil.CheckErr(o.Run())
},
}
+
+ o.PrintFlags.AddFlags(cmd)
+
cmd.Flags().StringVar(&o.OutputDir, "output-directory", o.OutputDir, i18n.T("Where to output the files. If empty or '-' uses stdout, otherwise creates a directory hierarchy in that directory"))
cmd.Flags().StringSliceVar(&o.Namespaces, "namespaces", o.Namespaces, "A comma separated list of namespaces to dump.")
cmd.Flags().BoolVar(&o.AllNamespaces, "all-namespaces", o.AllNamespaces, "If true, dump all namespaces. If true, --namespaces is ignored.")
@@ -122,18 +128,28 @@ func (o *ClusterInfoDumpOptions) Complete(f cmdutil.Factory, cmd *cobra.Command)
return err
}
- jsonOutputFmt := "json"
- o.PrintFlags.OutputFormat = &jsonOutputFmt
o.PrintObj = printer.PrintObj
- o.Timeout, err = cmdutil.GetPodRunningTimeoutFlag(cmd)
+ config, err := f.ToRESTConfig()
if err != nil {
return err
}
- o.Clientset, err = f.ClientSet()
+
+ o.Clientset, err = corev1client.NewForConfig(config)
if err != nil {
return err
}
+
+ o.ExtensionsClientset, err = extensionsv1client.NewForConfig(config)
+ if err != nil {
+ return err
+ }
+
+ o.Timeout, err = cmdutil.GetPodRunningTimeoutFlag(cmd)
+ if err != nil {
+ return err
+ }
+
o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
@@ -146,7 +162,7 @@ func (o *ClusterInfoDumpOptions) Complete(f cmdutil.Factory, cmd *cobra.Command)
}
func (o *ClusterInfoDumpOptions) Run() error {
- nodes, err := o.Clientset.Core().Nodes().List(metav1.ListOptions{})
+ nodes, err := o.Clientset.Nodes().List(metav1.ListOptions{})
if err != nil {
return err
}
@@ -157,7 +173,7 @@ func (o *ClusterInfoDumpOptions) Run() error {
var namespaces []string
if o.AllNamespaces {
- namespaceList, err := o.Clientset.Core().Namespaces().List(metav1.ListOptions{})
+ namespaceList, err := o.Clientset.Namespaces().List(metav1.ListOptions{})
if err != nil {
return err
}
@@ -175,7 +191,7 @@ func (o *ClusterInfoDumpOptions) Run() error {
for _, namespace := range namespaces {
// TODO: this is repetitive in the extreme. Use reflection or
// something to make this a for loop.
- events, err := o.Clientset.Core().Events(namespace).List(metav1.ListOptions{})
+ events, err := o.Clientset.Events(namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
@@ -183,7 +199,7 @@ func (o *ClusterInfoDumpOptions) Run() error {
return err
}
- rcs, err := o.Clientset.Core().ReplicationControllers(namespace).List(metav1.ListOptions{})
+ rcs, err := o.Clientset.ReplicationControllers(namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
@@ -191,7 +207,7 @@ func (o *ClusterInfoDumpOptions) Run() error {
return err
}
- svcs, err := o.Clientset.Core().Services(namespace).List(metav1.ListOptions{})
+ svcs, err := o.Clientset.Services(namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
@@ -199,7 +215,7 @@ func (o *ClusterInfoDumpOptions) Run() error {
return err
}
- sets, err := o.Clientset.Extensions().DaemonSets(namespace).List(metav1.ListOptions{})
+ sets, err := o.ExtensionsClientset.DaemonSets(namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
@@ -207,7 +223,7 @@ func (o *ClusterInfoDumpOptions) Run() error {
return err
}
- deps, err := o.Clientset.Extensions().Deployments(namespace).List(metav1.ListOptions{})
+ deps, err := o.ExtensionsClientset.Deployments(namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
@@ -215,7 +231,7 @@ func (o *ClusterInfoDumpOptions) Run() error {
return err
}
- rps, err := o.Clientset.Extensions().ReplicaSets(namespace).List(metav1.ListOptions{})
+ rps, err := o.ExtensionsClientset.ReplicaSets(namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
@@ -223,7 +239,7 @@ func (o *ClusterInfoDumpOptions) Run() error {
return err
}
- pods, err := o.Clientset.Core().Pods(namespace).List(metav1.ListOptions{})
+ pods, err := o.Clientset.Pods(namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
@@ -232,7 +248,7 @@ func (o *ClusterInfoDumpOptions) Run() error {
return err
}
- printContainer := func(writer io.Writer, container api.Container, pod *api.Pod) {
+ printContainer := func(writer io.Writer, container corev1.Container, pod *corev1.Pod) {
writer.Write([]byte(fmt.Sprintf("==== START logs for container %s of pod %s/%s ====\n", container.Name, pod.Namespace, pod.Name)))
defer writer.Write([]byte(fmt.Sprintf("==== END logs for container %s of pod %s/%s ====\n", container.Name, pod.Namespace, pod.Name)))
@@ -264,8 +280,14 @@ func (o *ClusterInfoDumpOptions) Run() error {
}
}
}
+
+ dest := o.OutputDir
+ if len(dest) == 0 {
+ dest = "standard output"
+ }
+
if o.OutputDir != "-" {
- fmt.Fprintf(o.Out, "Cluster info dumped to %s\n", o.OutputDir)
+ fmt.Fprintf(o.Out, "Cluster info dumped to %s\n", dest)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment