|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package resource |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + openapi_v2 "github.com/googleapis/gnostic/OpenAPIv2" |
| 24 | + yaml "gopkg.in/yaml.v2" |
| 25 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 26 | + "k8s.io/client-go/discovery" |
| 27 | + "k8s.io/client-go/dynamic" |
| 28 | +) |
| 29 | + |
| 30 | +// VerifyDryRun returns nil if a resource group-version-kind supports |
| 31 | +// server-side dry-run. Otherwise, an error is returned. |
| 32 | +func VerifyDryRun(gvk schema.GroupVersionKind, dynamicClient dynamic.Interface, discoveryClient discovery.DiscoveryInterface) error { |
| 33 | + verifier := NewDryRunVerifier(dynamicClient, discoveryClient) |
| 34 | + return verifier.HasSupport(gvk) |
| 35 | +} |
| 36 | + |
| 37 | +func NewDryRunVerifier(dynamicClient dynamic.Interface, discoveryClient discovery.DiscoveryInterface) *DryRunVerifier { |
| 38 | + return &DryRunVerifier{ |
| 39 | + Finder: NewCRDFinder(CRDFromDynamic(dynamicClient)), |
| 40 | + OpenAPIGetter: discoveryClient, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func hasGVKExtension(extensions []*openapi_v2.NamedAny, gvk schema.GroupVersionKind) bool { |
| 45 | + for _, extension := range extensions { |
| 46 | + if extension.GetValue().GetYaml() == "" || |
| 47 | + extension.GetName() != "x-kubernetes-group-version-kind" { |
| 48 | + continue |
| 49 | + } |
| 50 | + var value map[string]string |
| 51 | + err := yaml.Unmarshal([]byte(extension.GetValue().GetYaml()), &value) |
| 52 | + if err != nil { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + if value["group"] == gvk.Group && value["kind"] == gvk.Kind && value["version"] == gvk.Version { |
| 57 | + return true |
| 58 | + } |
| 59 | + return false |
| 60 | + } |
| 61 | + return false |
| 62 | +} |
| 63 | + |
| 64 | +// DryRunVerifier verifies if a given group-version-kind supports DryRun |
| 65 | +// against the current server. Sending dryRun requests to apiserver that |
| 66 | +// don't support it will result in objects being unwillingly persisted. |
| 67 | +// |
| 68 | +// It reads the OpenAPI to see if the given GVK supports dryRun. If the |
| 69 | +// GVK can not be found, we assume that CRDs will have the same level of |
| 70 | +// support as "namespaces", and non-CRDs will not be supported. We |
| 71 | +// delay the check for CRDs as much as possible though, since it |
| 72 | +// requires an extra round-trip to the server. |
| 73 | +type DryRunVerifier struct { |
| 74 | + Finder CRDFinder |
| 75 | + OpenAPIGetter discovery.OpenAPISchemaInterface |
| 76 | +} |
| 77 | + |
| 78 | +// HasSupport verifies if the given gvk supports DryRun. An error is |
| 79 | +// returned if it doesn't. |
| 80 | +func (v *DryRunVerifier) HasSupport(gvk schema.GroupVersionKind) error { |
| 81 | + oapi, err := v.OpenAPIGetter.OpenAPISchema() |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("failed to download openapi: %v", err) |
| 84 | + } |
| 85 | + supports, err := SupportsDryRun(oapi, gvk) |
| 86 | + if err != nil { |
| 87 | + // We assume that we couldn't find the type, then check for namespace: |
| 88 | + supports, _ = SupportsDryRun(oapi, schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Namespace"}) |
| 89 | + // If namespace supports dryRun, then we will support dryRun for CRDs only. |
| 90 | + if supports { |
| 91 | + supports, err = v.Finder.HasCRD(gvk.GroupKind()) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("failed to check CRD: %v", err) |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + if !supports { |
| 98 | + return fmt.Errorf("%v doesn't support dry-run", gvk) |
| 99 | + } |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +// SupportsDryRun is a method that let's us look in the OpenAPI if the |
| 104 | +// specific group-version-kind supports the dryRun query parameter for |
| 105 | +// the PATCH end-point. |
| 106 | +func SupportsDryRun(doc *openapi_v2.Document, gvk schema.GroupVersionKind) (bool, error) { |
| 107 | + for _, path := range doc.GetPaths().GetPath() { |
| 108 | + // Is this describing the gvk we're looking for? |
| 109 | + if !hasGVKExtension(path.GetValue().GetPatch().GetVendorExtension(), gvk) { |
| 110 | + continue |
| 111 | + } |
| 112 | + for _, param := range path.GetValue().GetPatch().GetParameters() { |
| 113 | + if param.GetParameter().GetNonBodyParameter().GetQueryParameterSubSchema().GetName() == "dryRun" { |
| 114 | + return true, nil |
| 115 | + } |
| 116 | + } |
| 117 | + return false, nil |
| 118 | + } |
| 119 | + |
| 120 | + return false, errors.New("couldn't find GVK in openapi") |
| 121 | +} |
0 commit comments