factory.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "k8s.io/apimachinery/pkg/api/meta"
  16. "k8s.io/cli-runtime/pkg/genericclioptions"
  17. "k8s.io/cli-runtime/pkg/resource"
  18. "k8s.io/client-go/dynamic"
  19. "k8s.io/client-go/kubernetes"
  20. restclient "k8s.io/client-go/rest"
  21. "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
  22. "k8s.io/kubernetes/pkg/kubectl/validation"
  23. )
  24. // Factory provides abstractions that allow the Kubectl command to be extended across multiple types
  25. // of resources and different API sets.
  26. // The rings are here for a reason. In order for composers to be able to provide alternative factory implementations
  27. // they need to provide low level pieces of *certain* functions so that when the factory calls back into itself
  28. // it uses the custom version of the function. Rather than try to enumerate everything that someone would want to override
  29. // we split the factory into rings, where each ring can depend on methods in an earlier ring, but cannot depend
  30. // upon peer methods in its own ring.
  31. // TODO: make the functions interfaces
  32. // TODO: pass the various interfaces on the factory directly into the command constructors (so the
  33. // commands are decoupled from the factory).
  34. type Factory interface {
  35. genericclioptions.RESTClientGetter
  36. // DynamicClient returns a dynamic client ready for use
  37. DynamicClient() (dynamic.Interface, error)
  38. // KubernetesClientSet gives you back an external clientset
  39. KubernetesClientSet() (*kubernetes.Clientset, error)
  40. // Returns a RESTClient for accessing Kubernetes resources or an error.
  41. RESTClient() (*restclient.RESTClient, error)
  42. // NewBuilder returns an object that assists in loading objects from both disk and the server
  43. // and which implements the common patterns for CLI interactions with generic resources.
  44. NewBuilder() *resource.Builder
  45. // Returns a RESTClient for working with the specified RESTMapping or an error. This is intended
  46. // for working with arbitrary resources and is not guaranteed to point to a Kubernetes APIServer.
  47. ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)
  48. // Returns a RESTClient for working with Unstructured objects.
  49. UnstructuredClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)
  50. // Returns a schema that can validate objects stored on disk.
  51. Validator(validate bool) (validation.Schema, error)
  52. // OpenAPISchema returns the schema openapi schema definition
  53. OpenAPISchema() (openapi.Resources, error)
  54. }