clientbacked_dryrun.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright 2017 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 apiclient
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "github.com/pkg/errors"
  18. apierrors "k8s.io/apimachinery/pkg/api/errors"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/client-go/dynamic"
  22. clientset "k8s.io/client-go/kubernetes"
  23. clientsetscheme "k8s.io/client-go/kubernetes/scheme"
  24. "k8s.io/client-go/rest"
  25. core "k8s.io/client-go/testing"
  26. "k8s.io/client-go/tools/clientcmd"
  27. )
  28. // ClientBackedDryRunGetter implements the DryRunGetter interface for use in NewDryRunClient() and proxies all GET and LIST requests to the backing API server reachable via rest.Config
  29. type ClientBackedDryRunGetter struct {
  30. client clientset.Interface
  31. dynamicClient dynamic.Interface
  32. }
  33. // InitDryRunGetter should implement the DryRunGetter interface
  34. var _ DryRunGetter = &ClientBackedDryRunGetter{}
  35. // NewClientBackedDryRunGetter creates a new ClientBackedDryRunGetter instance based on the rest.Config object
  36. func NewClientBackedDryRunGetter(config *rest.Config) (*ClientBackedDryRunGetter, error) {
  37. client, err := clientset.NewForConfig(config)
  38. if err != nil {
  39. return nil, err
  40. }
  41. dynamicClient, err := dynamic.NewForConfig(config)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return &ClientBackedDryRunGetter{
  46. client: client,
  47. dynamicClient: dynamicClient,
  48. }, nil
  49. }
  50. // NewClientBackedDryRunGetterFromKubeconfig creates a new ClientBackedDryRunGetter instance from the given KubeConfig file
  51. func NewClientBackedDryRunGetterFromKubeconfig(file string) (*ClientBackedDryRunGetter, error) {
  52. config, err := clientcmd.LoadFromFile(file)
  53. if err != nil {
  54. return nil, errors.Wrap(err, "failed to load kubeconfig")
  55. }
  56. clientConfig, err := clientcmd.NewDefaultClientConfig(*config, &clientcmd.ConfigOverrides{}).ClientConfig()
  57. if err != nil {
  58. return nil, errors.Wrap(err, "failed to create API client configuration from kubeconfig")
  59. }
  60. return NewClientBackedDryRunGetter(clientConfig)
  61. }
  62. // HandleGetAction handles GET actions to the dryrun clientset this interface supports
  63. func (clg *ClientBackedDryRunGetter) HandleGetAction(action core.GetAction) (bool, runtime.Object, error) {
  64. unstructuredObj, err := clg.dynamicClient.Resource(action.GetResource()).Namespace(action.GetNamespace()).Get(action.GetName(), metav1.GetOptions{})
  65. // Inform the user that the requested object wasn't found.
  66. printIfNotExists(err)
  67. if err != nil {
  68. return true, nil, err
  69. }
  70. newObj, err := decodeUnstructuredIntoAPIObject(action, unstructuredObj)
  71. if err != nil {
  72. fmt.Printf("error after decode: %v %v\n", unstructuredObj, err)
  73. return true, nil, err
  74. }
  75. return true, newObj, err
  76. }
  77. // HandleListAction handles LIST actions to the dryrun clientset this interface supports
  78. func (clg *ClientBackedDryRunGetter) HandleListAction(action core.ListAction) (bool, runtime.Object, error) {
  79. listOpts := metav1.ListOptions{
  80. LabelSelector: action.GetListRestrictions().Labels.String(),
  81. FieldSelector: action.GetListRestrictions().Fields.String(),
  82. }
  83. unstructuredList, err := clg.dynamicClient.Resource(action.GetResource()).Namespace(action.GetNamespace()).List(listOpts)
  84. if err != nil {
  85. return true, nil, err
  86. }
  87. newObj, err := decodeUnstructuredIntoAPIObject(action, unstructuredList)
  88. if err != nil {
  89. fmt.Printf("error after decode: %v %v\n", unstructuredList, err)
  90. return true, nil, err
  91. }
  92. return true, newObj, err
  93. }
  94. // Client gets the backing clientset.Interface
  95. func (clg *ClientBackedDryRunGetter) Client() clientset.Interface {
  96. return clg.client
  97. }
  98. // decodeUnversionedIntoAPIObject converts the *unversioned.Unversioned object returned from the dynamic client
  99. // to bytes; and then decodes it back _to an external api version (k8s.io/api)_ using the normal API machinery
  100. func decodeUnstructuredIntoAPIObject(action core.Action, unstructuredObj runtime.Unstructured) (runtime.Object, error) {
  101. objBytes, err := json.Marshal(unstructuredObj)
  102. if err != nil {
  103. return nil, err
  104. }
  105. newObj, err := runtime.Decode(clientsetscheme.Codecs.UniversalDecoder(action.GetResource().GroupVersion()), objBytes)
  106. if err != nil {
  107. return nil, err
  108. }
  109. return newObj, nil
  110. }
  111. func printIfNotExists(err error) {
  112. if apierrors.IsNotFound(err) {
  113. fmt.Println("[dryrun] The GET request didn't yield any result, the API Server returned a NotFound error.")
  114. }
  115. }