dryrun.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 openapi
  14. import (
  15. "errors"
  16. openapi_v2 "github.com/googleapis/gnostic/OpenAPIv2"
  17. yaml "gopkg.in/yaml.v2"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. )
  20. func hasGVKExtension(extensions []*openapi_v2.NamedAny, gvk schema.GroupVersionKind) bool {
  21. for _, extension := range extensions {
  22. if extension.GetValue().GetYaml() == "" ||
  23. extension.GetName() != "x-kubernetes-group-version-kind" {
  24. continue
  25. }
  26. var value map[string]string
  27. err := yaml.Unmarshal([]byte(extension.GetValue().GetYaml()), &value)
  28. if err != nil {
  29. continue
  30. }
  31. if value["group"] == gvk.Group && value["kind"] == gvk.Kind && value["version"] == gvk.Version {
  32. return true
  33. }
  34. return false
  35. }
  36. return false
  37. }
  38. // SupportsDryRun is a method that let's us look in the OpenAPI if the
  39. // specific group-version-kind supports the dryRun query parameter for
  40. // the PATCH end-point.
  41. func SupportsDryRun(doc *openapi_v2.Document, gvk schema.GroupVersionKind) (bool, error) {
  42. for _, path := range doc.GetPaths().GetPath() {
  43. // Is this describing the gvk we're looking for?
  44. if !hasGVKExtension(path.GetValue().GetPatch().GetVendorExtension(), gvk) {
  45. continue
  46. }
  47. for _, param := range path.GetValue().GetPatch().GetParameters() {
  48. if param.GetParameter().GetNonBodyParameter().GetQueryParameterSubSchema().GetName() == "dryRun" {
  49. return true, nil
  50. }
  51. }
  52. return false, nil
  53. }
  54. return false, errors.New("couldn't find GVK in openapi")
  55. }