openapi.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 testing
  14. import (
  15. "k8s.io/apimachinery/pkg/runtime/schema"
  16. "k8s.io/kube-openapi/pkg/util/proto"
  17. "k8s.io/kube-openapi/pkg/util/proto/testing"
  18. "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
  19. )
  20. // FakeResources is a wrapper to directly load the openapi schema from a
  21. // file, and get the schema for given GVK. This is only for test since
  22. // it's assuming that the file is there and everything will go fine.
  23. type FakeResources struct {
  24. fake testing.Fake
  25. }
  26. var _ openapi.Resources = &FakeResources{}
  27. // NewFakeResources creates a new FakeResources.
  28. func NewFakeResources(path string) *FakeResources {
  29. return &FakeResources{
  30. fake: testing.Fake{Path: path},
  31. }
  32. }
  33. // LookupResource will read the schema, parse it and return the
  34. // resources. It doesn't return errors and will panic instead.
  35. func (f *FakeResources) LookupResource(gvk schema.GroupVersionKind) proto.Schema {
  36. s, err := f.fake.OpenAPISchema()
  37. if err != nil {
  38. panic(err)
  39. }
  40. resources, err := openapi.NewOpenAPIData(s)
  41. if err != nil {
  42. panic(err)
  43. }
  44. return resources.LookupResource(gvk)
  45. }
  46. // EmptyResources implement a Resources that just doesn't have any resources.
  47. type EmptyResources struct{}
  48. var _ openapi.Resources = EmptyResources{}
  49. // LookupResource will always return nil. It doesn't have any resources.
  50. func (f EmptyResources) LookupResource(gvk schema.GroupVersionKind) proto.Schema {
  51. return nil
  52. }
  53. // CreateOpenAPISchemaFunc returns a function useful for the TestFactory.
  54. func CreateOpenAPISchemaFunc(path string) func() (openapi.Resources, error) {
  55. return func() (openapi.Resources, error) {
  56. return NewFakeResources(path), nil
  57. }
  58. }