mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
refactor factory to support fake openapiv3
This commit is contained in:
parent
54ec651ab5
commit
81dd9e3d25
@ -24,7 +24,7 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||||
"k8s.io/client-go/discovery"
|
openapiclient "k8s.io/client-go/openapi"
|
||||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||||
"k8s.io/kubectl/pkg/explain"
|
"k8s.io/kubectl/pkg/explain"
|
||||||
explainv2 "k8s.io/kubectl/pkg/explain/v2"
|
explainv2 "k8s.io/kubectl/pkg/explain/v2"
|
||||||
@ -74,7 +74,7 @@ type ExplainOptions struct {
|
|||||||
OutputFormat string
|
OutputFormat string
|
||||||
|
|
||||||
// Client capable of fetching openapi documents from the user's cluster
|
// Client capable of fetching openapi documents from the user's cluster
|
||||||
DiscoveryClient discovery.DiscoveryInterface
|
OpenAPIV3Client openapiclient.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewExplainOptions(parent string, streams genericclioptions.IOStreams) *ExplainOptions {
|
func NewExplainOptions(parent string, streams genericclioptions.IOStreams) *ExplainOptions {
|
||||||
@ -125,13 +125,12 @@ func (o *ExplainOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only openapi v3 needs the discovery client.
|
// Only openapi v3 needs the openapiv3client
|
||||||
if o.EnableOpenAPIV3 {
|
if o.EnableOpenAPIV3 {
|
||||||
discoveryClient, err := f.ToDiscoveryClient()
|
o.OpenAPIV3Client, err = f.OpenAPIV3Client()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
o.DiscoveryClient = discoveryClient
|
|
||||||
}
|
}
|
||||||
|
|
||||||
o.args = args
|
o.args = args
|
||||||
@ -176,7 +175,7 @@ func (o *ExplainOptions) Run() error {
|
|||||||
return explainv2.PrintModelDescription(
|
return explainv2.PrintModelDescription(
|
||||||
fieldsPath,
|
fieldsPath,
|
||||||
o.Out,
|
o.Out,
|
||||||
o.DiscoveryClient.OpenAPIV3(),
|
o.OpenAPIV3Client,
|
||||||
fullySpecifiedGVR,
|
fullySpecifiedGVR,
|
||||||
recursive,
|
recursive,
|
||||||
o.OutputFormat,
|
o.OutputFormat,
|
||||||
|
@ -38,6 +38,8 @@ import (
|
|||||||
"k8s.io/client-go/dynamic"
|
"k8s.io/client-go/dynamic"
|
||||||
fakedynamic "k8s.io/client-go/dynamic/fake"
|
fakedynamic "k8s.io/client-go/dynamic/fake"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
|
openapiclient "k8s.io/client-go/openapi"
|
||||||
|
"k8s.io/client-go/openapi/openapitest"
|
||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/rest/fake"
|
"k8s.io/client-go/rest/fake"
|
||||||
"k8s.io/client-go/restmapper"
|
"k8s.io/client-go/restmapper"
|
||||||
@ -418,6 +420,7 @@ type TestFactory struct {
|
|||||||
|
|
||||||
UnstructuredClientForMappingFunc resource.FakeClientFunc
|
UnstructuredClientForMappingFunc resource.FakeClientFunc
|
||||||
OpenAPISchemaFunc func() (openapi.Resources, error)
|
OpenAPISchemaFunc func() (openapi.Resources, error)
|
||||||
|
OpenAPIV3ClientFunc func() (openapiclient.Client, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTestFactory returns an initialized TestFactory instance
|
// NewTestFactory returns an initialized TestFactory instance
|
||||||
@ -533,6 +536,13 @@ func (f *TestFactory) OpenAPISchema() (openapi.Resources, error) {
|
|||||||
return openapitesting.EmptyResources{}, nil
|
return openapitesting.EmptyResources{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *TestFactory) OpenAPIV3Client() (openapiclient.Client, error) {
|
||||||
|
if f.OpenAPIV3ClientFunc != nil {
|
||||||
|
return f.OpenAPIV3ClientFunc()
|
||||||
|
}
|
||||||
|
return openapitest.NewFakeClient(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// NewBuilder returns an initialized resource.Builder instance
|
// NewBuilder returns an initialized resource.Builder instance
|
||||||
func (f *TestFactory) NewBuilder() *resource.Builder {
|
func (f *TestFactory) NewBuilder() *resource.Builder {
|
||||||
return resource.NewFakeBuilder(
|
return resource.NewFakeBuilder(
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"k8s.io/cli-runtime/pkg/resource"
|
"k8s.io/cli-runtime/pkg/resource"
|
||||||
"k8s.io/client-go/dynamic"
|
"k8s.io/client-go/dynamic"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
|
openapiclient "k8s.io/client-go/openapi"
|
||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
"k8s.io/kubectl/pkg/util/openapi"
|
"k8s.io/kubectl/pkg/util/openapi"
|
||||||
"k8s.io/kubectl/pkg/validation"
|
"k8s.io/kubectl/pkg/validation"
|
||||||
@ -63,4 +64,7 @@ type Factory interface {
|
|||||||
Validator(validationDirective string) (validation.Schema, error)
|
Validator(validationDirective string) (validation.Schema, error)
|
||||||
// OpenAPISchema returns the parsed openapi schema definition
|
// OpenAPISchema returns the parsed openapi schema definition
|
||||||
OpenAPISchema() (openapi.Resources, error)
|
OpenAPISchema() (openapi.Resources, error)
|
||||||
|
// OpenAPIV3Schema returns a client for fetching parsed schemas for
|
||||||
|
// any group version
|
||||||
|
OpenAPIV3Client() (openapiclient.Client, error)
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ import (
|
|||||||
"k8s.io/client-go/discovery"
|
"k8s.io/client-go/discovery"
|
||||||
"k8s.io/client-go/dynamic"
|
"k8s.io/client-go/dynamic"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
|
openapiclient "k8s.io/client-go/openapi"
|
||||||
"k8s.io/client-go/openapi/cached"
|
"k8s.io/client-go/openapi/cached"
|
||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
@ -211,3 +212,12 @@ func (f *factoryImpl) openAPIGetter() discovery.OpenAPISchemaInterface {
|
|||||||
|
|
||||||
return f.oapi
|
return f.oapi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *factoryImpl) OpenAPIV3Client() (openapiclient.Client, error) {
|
||||||
|
discovery, err := f.clientGetter.ToDiscoveryClient()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return discovery.OpenAPIV3(), nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user