kubectl debug: Initialize pod client and builder in complete
This PR initializes podclient and builder in complete function instead run function. Kubernetes-commit: d66b339868ea08ef4d3bda09fdf64abacaa3f41e
This commit is contained in:
parent
2c98b95ac3
commit
c0da8f17bf
|
@ -40,6 +40,7 @@ import (
|
||||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||||
"k8s.io/cli-runtime/pkg/printers"
|
"k8s.io/cli-runtime/pkg/printers"
|
||||||
"k8s.io/cli-runtime/pkg/resource"
|
"k8s.io/cli-runtime/pkg/resource"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||||
"k8s.io/client-go/tools/cache"
|
"k8s.io/client-go/tools/cache"
|
||||||
watchtools "k8s.io/client-go/tools/watch"
|
watchtools "k8s.io/client-go/tools/watch"
|
||||||
|
@ -133,6 +134,7 @@ type DebugOptions struct {
|
||||||
|
|
||||||
podClient corev1client.CoreV1Interface
|
podClient corev1client.CoreV1Interface
|
||||||
|
|
||||||
|
Builder *resource.Builder
|
||||||
genericclioptions.IOStreams
|
genericclioptions.IOStreams
|
||||||
WarningPrinter *printers.WarningPrinter
|
WarningPrinter *printers.WarningPrinter
|
||||||
|
|
||||||
|
@ -243,6 +245,20 @@ func (o *DebugOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
|
||||||
o.Applier = applier
|
o.Applier = applier
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clientConfig, err := f.ToRESTConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := kubernetes.NewForConfig(clientConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
o.podClient = client.CoreV1()
|
||||||
|
|
||||||
|
o.Builder = f.NewBuilder()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,13 +344,7 @@ func (o *DebugOptions) Validate() error {
|
||||||
func (o *DebugOptions) Run(f cmdutil.Factory, cmd *cobra.Command) error {
|
func (o *DebugOptions) Run(f cmdutil.Factory, cmd *cobra.Command) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
clientset, err := f.KubernetesClientSet()
|
r := o.Builder.
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("internal error getting clientset: %v", err)
|
|
||||||
}
|
|
||||||
o.podClient = clientset.CoreV1()
|
|
||||||
|
|
||||||
r := f.NewBuilder().
|
|
||||||
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
|
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
|
||||||
FilenameParam(o.explicitNamespace, &o.FilenameOptions).
|
FilenameParam(o.explicitNamespace, &o.FilenameOptions).
|
||||||
NamespaceParam(o.Namespace).DefaultNamespace().ResourceNames("pods", o.TargetNames...).
|
NamespaceParam(o.Namespace).DefaultNamespace().ResourceNames("pods", o.TargetNames...).
|
||||||
|
@ -343,7 +353,7 @@ func (o *DebugOptions) Run(f cmdutil.Factory, cmd *cobra.Command) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = r.Visit(func(info *resource.Info, err error) error {
|
err := r.Visit(func(info *resource.Info, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO(verb): configurable early return
|
// TODO(verb): configurable early return
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -1665,7 +1665,7 @@ func TestCompleteAndValidate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if diff := cmp.Diff(tc.wantOpts, opts, cmpFilter, cmpopts.IgnoreFields(DebugOptions{},
|
if diff := cmp.Diff(tc.wantOpts, opts, cmpFilter, cmpopts.IgnoreFields(DebugOptions{},
|
||||||
"attachChanged", "shareProcessedChanged", "podClient", "WarningPrinter", "Applier", "explicitNamespace")); diff != "" {
|
"attachChanged", "shareProcessedChanged", "podClient", "WarningPrinter", "Applier", "explicitNamespace", "Builder")); diff != "" {
|
||||||
t.Error("CompleteAndValidate unexpected diff in generated object: (-want +got):\n", diff)
|
t.Error("CompleteAndValidate unexpected diff in generated object: (-want +got):\n", diff)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue