Merge pull request #32152 from deads2k/client-02-make-clientset
Automatic merge from submit-queue add ClientSet to factory to remove non-generated client We should move to using generated clients in the `kubectl` client. We should really move to generated external clients, but this at least moves away from using manually created clients. @fabianofranz @mfojtik When I complete this work (move the other commands and eliminate the old API), this will ripple downstream.
This commit is contained in:
commit
97ec720a19
@ -26,8 +26,8 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/api"
|
"k8s.io/kubernetes/pkg/api"
|
||||||
|
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/unversioned"
|
||||||
"k8s.io/kubernetes/pkg/client/restclient"
|
"k8s.io/kubernetes/pkg/client/restclient"
|
||||||
client "k8s.io/kubernetes/pkg/client/unversioned"
|
|
||||||
"k8s.io/kubernetes/pkg/client/unversioned/remotecommand"
|
"k8s.io/kubernetes/pkg/client/unversioned/remotecommand"
|
||||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||||
remotecommandserver "k8s.io/kubernetes/pkg/kubelet/server/remotecommand"
|
remotecommandserver "k8s.io/kubernetes/pkg/kubelet/server/remotecommand"
|
||||||
@ -107,9 +107,9 @@ type AttachOptions struct {
|
|||||||
|
|
||||||
Pod *api.Pod
|
Pod *api.Pod
|
||||||
|
|
||||||
Attach RemoteAttach
|
Attach RemoteAttach
|
||||||
Client *client.Client
|
PodClient coreclient.PodsGetter
|
||||||
Config *restclient.Config
|
Config *restclient.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete verifies command line arguments and loads data from the command environment
|
// Complete verifies command line arguments and loads data from the command environment
|
||||||
@ -134,11 +134,11 @@ func (p *AttachOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, argsIn
|
|||||||
}
|
}
|
||||||
p.Config = config
|
p.Config = config
|
||||||
|
|
||||||
client, err := f.Client()
|
clientset, err := f.ClientSet()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.Client = client
|
p.PodClient = clientset.Core()
|
||||||
|
|
||||||
if p.CommandName == "" {
|
if p.CommandName == "" {
|
||||||
p.CommandName = cmd.CommandPath()
|
p.CommandName = cmd.CommandPath()
|
||||||
@ -156,7 +156,7 @@ func (p *AttachOptions) Validate() error {
|
|||||||
if p.Out == nil || p.Err == nil {
|
if p.Out == nil || p.Err == nil {
|
||||||
allErrs = append(allErrs, fmt.Errorf("both output and error output must be provided"))
|
allErrs = append(allErrs, fmt.Errorf("both output and error output must be provided"))
|
||||||
}
|
}
|
||||||
if p.Attach == nil || p.Client == nil || p.Config == nil {
|
if p.Attach == nil || p.PodClient == nil || p.Config == nil {
|
||||||
allErrs = append(allErrs, fmt.Errorf("client, client config, and attach must be provided"))
|
allErrs = append(allErrs, fmt.Errorf("client, client config, and attach must be provided"))
|
||||||
}
|
}
|
||||||
return utilerrors.NewAggregate(allErrs)
|
return utilerrors.NewAggregate(allErrs)
|
||||||
@ -165,7 +165,7 @@ func (p *AttachOptions) Validate() error {
|
|||||||
// Run executes a validated remote execution against a pod.
|
// Run executes a validated remote execution against a pod.
|
||||||
func (p *AttachOptions) Run() error {
|
func (p *AttachOptions) Run() error {
|
||||||
if p.Pod == nil {
|
if p.Pod == nil {
|
||||||
pod, err := p.Client.Pods(p.Namespace).Get(p.PodName)
|
pod, err := p.PodClient.Pods(p.Namespace).Get(p.PodName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -225,8 +225,12 @@ func (p *AttachOptions) Run() error {
|
|||||||
fmt.Fprintln(stderr, "If you don't see a command prompt, try pressing enter.")
|
fmt.Fprintln(stderr, "If you don't see a command prompt, try pressing enter.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
restClient, err := restclient.RESTClientFor(p.Config)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
// TODO: consider abstracting into a client invocation or client helper
|
// TODO: consider abstracting into a client invocation or client helper
|
||||||
req := p.Client.RESTClient.Post().
|
req := restClient.Post().
|
||||||
Resource("pods").
|
Resource("pods").
|
||||||
Name(pod.Name).
|
Name(pod.Name).
|
||||||
Namespace(pod.Namespace).
|
Namespace(pod.Namespace).
|
||||||
|
@ -178,7 +178,7 @@ func TestAttach(t *testing.T) {
|
|||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
tf.Namespace = "test"
|
tf.Namespace = "test"
|
||||||
tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &unversioned.GroupVersion{Version: test.version}}}
|
tf.ClientConfig = &restclient.Config{APIPath: "/api", ContentConfig: restclient.ContentConfig{NegotiatedSerializer: api.Codecs, GroupVersion: &unversioned.GroupVersion{Version: test.version}}}
|
||||||
bufOut := bytes.NewBuffer([]byte{})
|
bufOut := bytes.NewBuffer([]byte{})
|
||||||
bufErr := bytes.NewBuffer([]byte{})
|
bufErr := bytes.NewBuffer([]byte{})
|
||||||
bufIn := bytes.NewBuffer([]byte{})
|
bufIn := bytes.NewBuffer([]byte{})
|
||||||
@ -212,7 +212,7 @@ func TestAttach(t *testing.T) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if remoteAttach.url.Path != test.attachPath {
|
if remoteAttach.url.Path != test.attachPath {
|
||||||
t.Errorf("%s: Did not get expected path for exec request", test.name)
|
t.Errorf("%s: Did not get expected path for exec request: %q %q", test.name, test.attachPath, remoteAttach.url.Path)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if remoteAttach.method != "POST" {
|
if remoteAttach.method != "POST" {
|
||||||
@ -257,7 +257,7 @@ func TestAttachWarnings(t *testing.T) {
|
|||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
tf.Namespace = "test"
|
tf.Namespace = "test"
|
||||||
tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &unversioned.GroupVersion{Version: test.version}}}
|
tf.ClientConfig = &restclient.Config{APIPath: "/api", ContentConfig: restclient.ContentConfig{NegotiatedSerializer: api.Codecs, GroupVersion: &unversioned.GroupVersion{Version: test.version}}}
|
||||||
bufOut := bytes.NewBuffer([]byte{})
|
bufOut := bytes.NewBuffer([]byte{})
|
||||||
bufErr := bytes.NewBuffer([]byte{})
|
bufErr := bytes.NewBuffer([]byte{})
|
||||||
bufIn := bytes.NewBuffer([]byte{})
|
bufIn := bytes.NewBuffer([]byte{})
|
||||||
|
@ -33,9 +33,11 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/api/testapi"
|
"k8s.io/kubernetes/pkg/api/testapi"
|
||||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||||
"k8s.io/kubernetes/pkg/api/validation"
|
"k8s.io/kubernetes/pkg/api/validation"
|
||||||
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||||
"k8s.io/kubernetes/pkg/client/restclient"
|
"k8s.io/kubernetes/pkg/client/restclient"
|
||||||
"k8s.io/kubernetes/pkg/client/typed/discovery"
|
"k8s.io/kubernetes/pkg/client/typed/discovery"
|
||||||
client "k8s.io/kubernetes/pkg/client/unversioned"
|
client "k8s.io/kubernetes/pkg/client/unversioned"
|
||||||
|
clientset "k8s.io/kubernetes/pkg/client/unversioned/adapters/internalclientset"
|
||||||
"k8s.io/kubernetes/pkg/client/unversioned/fake"
|
"k8s.io/kubernetes/pkg/client/unversioned/fake"
|
||||||
"k8s.io/kubernetes/pkg/kubectl"
|
"k8s.io/kubernetes/pkg/kubectl"
|
||||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||||
@ -295,6 +297,13 @@ func NewAPIFactory() (*cmdutil.Factory, *testFactory, runtime.Codec, runtime.Neg
|
|||||||
c.ExtensionsClient.Client = fakeClient.Client
|
c.ExtensionsClient.Client = fakeClient.Client
|
||||||
return c, t.Err
|
return c, t.Err
|
||||||
},
|
},
|
||||||
|
ClientSet: func() (*internalclientset.Clientset, error) {
|
||||||
|
fakeClient := t.Client.(*fake.RESTClient)
|
||||||
|
c := client.NewOrDie(t.ClientConfig)
|
||||||
|
c.Client = fakeClient.Client
|
||||||
|
c.ExtensionsClient.Client = fakeClient.Client
|
||||||
|
return clientset.FromUnversionedClient(c), nil
|
||||||
|
},
|
||||||
ClientForMapping: func(*meta.RESTMapping) (resource.RESTClient, error) {
|
ClientForMapping: func(*meta.RESTMapping) (resource.RESTClient, error) {
|
||||||
return t.Client, t.Err
|
return t.Client, t.Err
|
||||||
},
|
},
|
||||||
|
@ -274,7 +274,12 @@ func Run(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer, cmd *cob
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
opts.Client = client
|
|
||||||
|
clientset, err := f.ClientSet()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
opts.PodClient = clientset.Core()
|
||||||
|
|
||||||
attachablePod, err := f.AttachablePodForObject(obj)
|
attachablePod, err := f.AttachablePodForObject(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -475,7 +480,13 @@ func handleAttachPod(f *cmdutil.Factory, c *client.Client, ns, name string, opts
|
|||||||
_, err = io.Copy(opts.Out, readCloser)
|
_, err = io.Copy(opts.Out, readCloser)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
opts.Client = c
|
|
||||||
|
clientset, err := f.ClientSet()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
opts.PodClient = clientset.Core()
|
||||||
|
|
||||||
opts.PodName = name
|
opts.PodName = name
|
||||||
opts.Namespace = ns
|
opts.Namespace = ns
|
||||||
// TODO: opts.Run sets opts.Err to nil, we need to find a better way
|
// TODO: opts.Run sets opts.Err to nil, we need to find a better way
|
||||||
|
@ -54,6 +54,7 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/apis/policy"
|
"k8s.io/kubernetes/pkg/apis/policy"
|
||||||
"k8s.io/kubernetes/pkg/apis/rbac"
|
"k8s.io/kubernetes/pkg/apis/rbac"
|
||||||
"k8s.io/kubernetes/pkg/apis/storage"
|
"k8s.io/kubernetes/pkg/apis/storage"
|
||||||
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||||
"k8s.io/kubernetes/pkg/client/restclient"
|
"k8s.io/kubernetes/pkg/client/restclient"
|
||||||
"k8s.io/kubernetes/pkg/client/typed/discovery"
|
"k8s.io/kubernetes/pkg/client/typed/discovery"
|
||||||
"k8s.io/kubernetes/pkg/client/typed/dynamic"
|
"k8s.io/kubernetes/pkg/client/typed/dynamic"
|
||||||
@ -98,6 +99,8 @@ type Factory struct {
|
|||||||
JSONEncoder func() runtime.Encoder
|
JSONEncoder func() runtime.Encoder
|
||||||
// Returns a client for accessing Kubernetes resources or an error.
|
// Returns a client for accessing Kubernetes resources or an error.
|
||||||
Client func() (*client.Client, error)
|
Client func() (*client.Client, error)
|
||||||
|
// ClientSet gives you back an internal, generated clientset
|
||||||
|
ClientSet func() (*internalclientset.Clientset, error)
|
||||||
// Returns a client.Config for accessing the Kubernetes server.
|
// Returns a client.Config for accessing the Kubernetes server.
|
||||||
ClientConfig func() (*restclient.Config, error)
|
ClientConfig func() (*restclient.Config, error)
|
||||||
// Returns a RESTClient for working with the specified RESTMapping or an error. This is intended
|
// Returns a RESTClient for working with the specified RESTMapping or an error. This is intended
|
||||||
@ -406,6 +409,15 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
|||||||
Client: func() (*client.Client, error) {
|
Client: func() (*client.Client, error) {
|
||||||
return clients.ClientForVersion(nil)
|
return clients.ClientForVersion(nil)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
ClientSet: func() (*internalclientset.Clientset, error) {
|
||||||
|
cfg, err := clients.ClientConfigForVersion(nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return internalclientset.NewForConfig(cfg)
|
||||||
|
},
|
||||||
ClientConfig: func() (*restclient.Config, error) {
|
ClientConfig: func() (*restclient.Config, error) {
|
||||||
return clients.ClientConfigForVersion(nil)
|
return clients.ClientConfigForVersion(nil)
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user