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:
Arda Güçlü 2023-02-13 13:03:50 +03:00 committed by Kubernetes Publisher
parent 2c98b95ac3
commit c0da8f17bf
2 changed files with 19 additions and 9 deletions

View File

@ -40,6 +40,7 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/kubernetes"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
watchtools "k8s.io/client-go/tools/watch"
@ -133,6 +134,7 @@ type DebugOptions struct {
podClient corev1client.CoreV1Interface
Builder *resource.Builder
genericclioptions.IOStreams
WarningPrinter *printers.WarningPrinter
@ -243,6 +245,20 @@ func (o *DebugOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
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
}
@ -328,13 +344,7 @@ func (o *DebugOptions) Validate() error {
func (o *DebugOptions) Run(f cmdutil.Factory, cmd *cobra.Command) error {
ctx := context.Background()
clientset, err := f.KubernetesClientSet()
if err != nil {
return fmt.Errorf("internal error getting clientset: %v", err)
}
o.podClient = clientset.CoreV1()
r := f.NewBuilder().
r := o.Builder.
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
FilenameParam(o.explicitNamespace, &o.FilenameOptions).
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
}
err = r.Visit(func(info *resource.Info, err error) error {
err := r.Visit(func(info *resource.Info, err error) error {
if err != nil {
// TODO(verb): configurable early return
return err

View File

@ -1665,7 +1665,7 @@ func TestCompleteAndValidate(t *testing.T) {
}
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)
}
})