k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/spec3/component_test.go (about) 1 /* 2 Copyright 2021 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 spec3_test 18 19 import ( 20 "encoding/json" 21 "testing" 22 23 "k8s.io/kube-openapi/pkg/util/jsontesting" 24 "k8s.io/kube-openapi/pkg/validation/spec" 25 26 "reflect" 27 28 "k8s.io/kube-openapi/pkg/spec3" 29 ) 30 31 func TestSchemasJSONSerialization(t *testing.T) { 32 cases := []struct { 33 name string 34 target spec3.Components 35 expectedOutput string 36 }{ 37 { 38 name: "scenario1: smoke test serialization of spec3.Components.Schemas", 39 target: spec3.Components{ 40 Schemas: map[string]*spec.Schema{ 41 "io.k8s.api.admissionregistration.v1beta1.MutatingWebhook": { 42 SchemaProps: spec.SchemaProps{ 43 Description: "MutatingWebhook describes an admission webhook and the resources and operations it applies to.", 44 Type: []string{"object"}, 45 Properties: map[string]spec.Schema{ 46 "name": { 47 SchemaProps: spec.SchemaProps{ 48 Description: "The name of the admission webhook. Name should be fully qualified, e.g., imagepolicy.kubernetes.io, where \"imagepolicy\" is the name of the webhook, and kubernetes.io is the name of the organization. Required.", 49 Type: []string{"string"}, 50 Format: "", 51 }, 52 }, 53 "clientConfig": { 54 SchemaProps: spec.SchemaProps{ 55 Description: "ClientConfig defines how to communicate with the hook. Required", 56 Ref: spec.MustCreateRef("k8s.io/api/admissionregistration/v1beta1.WebhookClientConfig"), 57 }, 58 }, 59 "rules": { 60 SchemaProps: spec.SchemaProps{ 61 Description: "Rules describes what operations on what resources/subresources the webhook cares about. The webhook cares about an operation if it matches _any_ Rule. However, in order to prevent ValidatingAdmissionWebhooks and MutatingAdmissionWebhooks from putting the cluster in a state which cannot be recovered from without completely disabling the plugin, ValidatingAdmissionWebhooks and MutatingAdmissionWebhooks are never called on admission requests for ValidatingWebhookConfiguration and MutatingWebhookConfiguration objects.", 62 Type: []string{"array"}, 63 Items: &spec.SchemaOrArray{ 64 Schema: &spec.Schema{ 65 SchemaProps: spec.SchemaProps{ 66 Ref: spec.MustCreateRef("k8s.io/api/admissionregistration/v1beta1.RuleWithOperations"), 67 }, 68 }, 69 }, 70 }, 71 }, 72 "failurePolicy": { 73 SchemaProps: spec.SchemaProps{ 74 Description: "FailurePolicy defines how unrecognized errors from the admission endpoint are handled - allowed values are Ignore or Fail. Defaults to Ignore.", 75 Type: []string{"string"}, 76 Format: "", 77 }, 78 }, 79 "matchPolicy": { 80 SchemaProps: spec.SchemaProps{ 81 Description: "matchPolicy defines how the \"rules\" list is used to match incoming requests. Allowed values are \"Exact\" or \"Equivalent\".\n\n- Exact: match a request only if it exactly matches a specified rule. For example, if deployments can be modified via apps/v1, apps/v1beta1, and extensions/v1beta1, but \"rules\" only included apiGroups:[\"apps\"], apiVersions:[\"v1\"], resources: [\"deployments\"], a request to apps/v1beta1 or extensions/v1beta1 would not be sent to the webhook.\n\n- Equivalent: match a request if modifies a resource listed in rules, even via another API group or version. For example, if deployments can be modified via apps/v1, apps/v1beta1, and extensions/v1beta1, and \"rules\" only included apiGroups:[\"apps\"], apiVersions:[\"v1\"], resources: [\"deployments\"], a request to apps/v1beta1 or extensions/v1beta1 would be converted to apps/v1 and sent to the webhook.\n\nDefaults to \"Exact\"", 82 Type: []string{"string"}, 83 Format: "", 84 }, 85 }, 86 "namespaceSelector": { 87 SchemaProps: spec.SchemaProps{ 88 Description: "NamespaceSelector decides whether to run the webhook on an object based on whether the namespace for that object matches the selector. If the object itself is a namespace, the matching is performed on object.metadata.labels. If the object is another cluster scoped resource, it never skips the webhook.\n\nFor example, to run the webhook on any objects whose namespace is not associated with \"runlevel\" of \"0\" or \"1\"; you will set the selector as follows: \"namespaceSelector\": {\n \"matchExpressions\": [\n {\n \"key\": \"runlevel\",\n \"operator\": \"NotIn\",\n \"values\": [\n \"0\",\n \"1\"\n ]\n }\n ]\n}\n\nIf instead you want to only run the webhook on any objects whose namespace is associated with the \"environment\" of \"prod\" or \"staging\"; you will set the selector as follows: \"namespaceSelector\": {\n \"matchExpressions\": [\n {\n \"key\": \"environment\",\n \"operator\": \"In\",\n \"values\": [\n \"prod\",\n \"staging\"\n ]\n }\n ]\n}\n\nSee https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/ for more examples of label selectors.\n\nDefault to the empty LabelSelector, which matches everything.", 89 Ref: spec.MustCreateRef("k8s.io/apimachinery/pkg/apis/meta/v1.LabelSelector"), 90 }, 91 }, 92 "objectSelector": { 93 SchemaProps: spec.SchemaProps{ 94 Description: "ObjectSelector decides whether to run the webhook based on if the object has matching labels. objectSelector is evaluated against both the oldObject and newObject that would be sent to the webhook, and is considered to match if either object matches the selector. A null object (oldObject in the case of create, or newObject in the case of delete) or an object that cannot have labels (like a DeploymentRollback or a PodProxyOptions object) is not considered to match. Use the object selector only if the webhook is opt-in, because end users may skip the admission webhook by setting the labels. Default to the empty LabelSelector, which matches everything.", 95 Ref: spec.MustCreateRef("k8s.io/apimachinery/pkg/apis/meta/v1.LabelSelector"), 96 }, 97 }, 98 "sideEffects": { 99 SchemaProps: spec.SchemaProps{ 100 Description: "SideEffects states whether this webhookk has side effects. Acceptable values are: Unknown, None, Some, NoneOnDryRun Webhooks with side effects MUST implement a reconciliation system, since a request may be rejected by a future step in the admission change and the side effects therefore need to be undone. Requests with the dryRun attribute will be auto-rejected if they match a webhook with sideEffects == Unknown or Some. Defaults to Unknown.", 101 Type: []string{"string"}, 102 Format: "", 103 }, 104 }, 105 "timeoutSeconds": { 106 SchemaProps: spec.SchemaProps{ 107 Description: "TimeoutSeconds specifies the timeout for this webhook. After the timeout passes, the webhook call will be ignored or the API call will fail based on the failure policy. The timeout value must be between 1 and 30 seconds. Default to 30 seconds.", 108 Type: []string{"integer"}, 109 Format: "int32", 110 }, 111 }, 112 "admissionReviewVersions": { 113 SchemaProps: spec.SchemaProps{ 114 Description: "AdmissionReviewVersions is an ordered list of preferred AdmissionReview versions the Webhook expects. API server will try to use first version in the list which it supports. If none of the versions specified in this list supported by API server, validation will fail for this object. If a persisted webhook configuration specifies allowed versions and does not include any versions known to the API Server, calls to the webhook will fail and be subject to the failure policy. Default to ['v1beta1'].", 115 Type: []string{"array"}, 116 Items: &spec.SchemaOrArray{ 117 Schema: &spec.Schema{ 118 SchemaProps: spec.SchemaProps{ 119 Type: []string{"string"}, 120 Format: "", 121 }, 122 }, 123 }, 124 }, 125 }, 126 "reinvocationPolicy": { 127 SchemaProps: spec.SchemaProps{ 128 Description: "reinvocationPolicy indicates whether this webhook should be called multiple times as part of a single admission evaluation. Allowed values are \"Never\" and \"IfNeeded\".\n\nNever: the webhook will not be called more than once in a single admission evaluation.\n\nIfNeeded: the webhook will be called at least one additional time as part of the admission evaluation if the object being admitted is modified by other admission plugins after the initial webhook call. Webhooks that specify this option *must* be idempotent, able to process objects they previously admitted. Note: * the number of additional invocations is not guaranteed to be exactly one. * if additional invocations result in further modifications to the object, webhooks are not guaranteed to be invoked again. * webhooks that use this option may be reordered to minimize the number of additional invocations. * to validate an object after all mutations are guaranteed complete, use a validating admission webhook instead.\n\nDefaults to \"Never\".", 129 Type: []string{"string"}, 130 Format: "", 131 }, 132 }, 133 }, 134 Required: []string{"name", "clientConfig"}, 135 }, 136 }, 137 }, 138 }, 139 expectedOutput: `{"schemas":{"io.k8s.api.admissionregistration.v1beta1.MutatingWebhook":{"description":"MutatingWebhook describes an admission webhook and the resources and operations it applies to.","type":"object","required":["name","clientConfig"],"properties":{"admissionReviewVersions":{"description":"AdmissionReviewVersions is an ordered list of preferred AdmissionReview versions the Webhook expects. API server will try to use first version in the list which it supports. If none of the versions specified in this list supported by API server, validation will fail for this object. If a persisted webhook configuration specifies allowed versions and does not include any versions known to the API Server, calls to the webhook will fail and be subject to the failure policy. Default to ['v1beta1'].","type":"array","items":{"type":"string"}},"clientConfig":{"description":"ClientConfig defines how to communicate with the hook. Required","$ref":"k8s.io/api/admissionregistration/v1beta1.WebhookClientConfig"},"failurePolicy":{"description":"FailurePolicy defines how unrecognized errors from the admission endpoint are handled - allowed values are Ignore or Fail. Defaults to Ignore.","type":"string"},"matchPolicy":{"description":"matchPolicy defines how the \"rules\" list is used to match incoming requests. Allowed values are \"Exact\" or \"Equivalent\".\n\n- Exact: match a request only if it exactly matches a specified rule. For example, if deployments can be modified via apps/v1, apps/v1beta1, and extensions/v1beta1, but \"rules\" only included apiGroups:[\"apps\"], apiVersions:[\"v1\"], resources: [\"deployments\"], a request to apps/v1beta1 or extensions/v1beta1 would not be sent to the webhook.\n\n- Equivalent: match a request if modifies a resource listed in rules, even via another API group or version. For example, if deployments can be modified via apps/v1, apps/v1beta1, and extensions/v1beta1, and \"rules\" only included apiGroups:[\"apps\"], apiVersions:[\"v1\"], resources: [\"deployments\"], a request to apps/v1beta1 or extensions/v1beta1 would be converted to apps/v1 and sent to the webhook.\n\nDefaults to \"Exact\"","type":"string"},"name":{"description":"The name of the admission webhook. Name should be fully qualified, e.g., imagepolicy.kubernetes.io, where \"imagepolicy\" is the name of the webhook, and kubernetes.io is the name of the organization. Required.","type":"string"},"namespaceSelector":{"description":"NamespaceSelector decides whether to run the webhook on an object based on whether the namespace for that object matches the selector. If the object itself is a namespace, the matching is performed on object.metadata.labels. If the object is another cluster scoped resource, it never skips the webhook.\n\nFor example, to run the webhook on any objects whose namespace is not associated with \"runlevel\" of \"0\" or \"1\"; you will set the selector as follows: \"namespaceSelector\": {\n \"matchExpressions\": [\n {\n \"key\": \"runlevel\",\n \"operator\": \"NotIn\",\n \"values\": [\n \"0\",\n \"1\"\n ]\n }\n ]\n}\n\nIf instead you want to only run the webhook on any objects whose namespace is associated with the \"environment\" of \"prod\" or \"staging\"; you will set the selector as follows: \"namespaceSelector\": {\n \"matchExpressions\": [\n {\n \"key\": \"environment\",\n \"operator\": \"In\",\n \"values\": [\n \"prod\",\n \"staging\"\n ]\n }\n ]\n}\n\nSee https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/ for more examples of label selectors.\n\nDefault to the empty LabelSelector, which matches everything.","$ref":"k8s.io/apimachinery/pkg/apis/meta/v1.LabelSelector"},"objectSelector":{"description":"ObjectSelector decides whether to run the webhook based on if the object has matching labels. objectSelector is evaluated against both the oldObject and newObject that would be sent to the webhook, and is considered to match if either object matches the selector. A null object (oldObject in the case of create, or newObject in the case of delete) or an object that cannot have labels (like a DeploymentRollback or a PodProxyOptions object) is not considered to match. Use the object selector only if the webhook is opt-in, because end users may skip the admission webhook by setting the labels. Default to the empty LabelSelector, which matches everything.","$ref":"k8s.io/apimachinery/pkg/apis/meta/v1.LabelSelector"},"reinvocationPolicy":{"description":"reinvocationPolicy indicates whether this webhook should be called multiple times as part of a single admission evaluation. Allowed values are \"Never\" and \"IfNeeded\".\n\nNever: the webhook will not be called more than once in a single admission evaluation.\n\nIfNeeded: the webhook will be called at least one additional time as part of the admission evaluation if the object being admitted is modified by other admission plugins after the initial webhook call. Webhooks that specify this option *must* be idempotent, able to process objects they previously admitted. Note: * the number of additional invocations is not guaranteed to be exactly one. * if additional invocations result in further modifications to the object, webhooks are not guaranteed to be invoked again. * webhooks that use this option may be reordered to minimize the number of additional invocations. * to validate an object after all mutations are guaranteed complete, use a validating admission webhook instead.\n\nDefaults to \"Never\".","type":"string"},"rules":{"description":"Rules describes what operations on what resources/subresources the webhook cares about. The webhook cares about an operation if it matches _any_ Rule. However, in order to prevent ValidatingAdmissionWebhooks and MutatingAdmissionWebhooks from putting the cluster in a state which cannot be recovered from without completely disabling the plugin, ValidatingAdmissionWebhooks and MutatingAdmissionWebhooks are never called on admission requests for ValidatingWebhookConfiguration and MutatingWebhookConfiguration objects.","type":"array","items":{"$ref":"k8s.io/api/admissionregistration/v1beta1.RuleWithOperations"}},"sideEffects":{"description":"SideEffects states whether this webhookk has side effects. Acceptable values are: Unknown, None, Some, NoneOnDryRun Webhooks with side effects MUST implement a reconciliation system, since a request may be rejected by a future step in the admission change and the side effects therefore need to be undone. Requests with the dryRun attribute will be auto-rejected if they match a webhook with sideEffects == Unknown or Some. Defaults to Unknown.","type":"string"},"timeoutSeconds":{"description":"TimeoutSeconds specifies the timeout for this webhook. After the timeout passes, the webhook call will be ignored or the API call will fail based on the failure policy. The timeout value must be between 1 and 30 seconds. Default to 30 seconds.","type":"integer","format":"int32"}}}}}`, 140 }, 141 142 // scenario 2 143 { 144 name: "scenario2: schema can be defined as a ref, see: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#componentsObject", 145 target: spec3.Components{ 146 Schemas: map[string]*spec.Schema{ 147 "io.k8s.api.admissionregistration.v1beta1.MutatingWebhook": { 148 SchemaProps: spec.SchemaProps{ 149 Ref: spec.MustCreateRef("k8s.io/api/admissionregistration/v1beta1.WebhookClientConfig"), 150 }, 151 }, 152 }, 153 }, 154 expectedOutput: `{"schemas":{"io.k8s.api.admissionregistration.v1beta1.MutatingWebhook":{"$ref":"k8s.io/api/admissionregistration/v1beta1.WebhookClientConfig"}}}`, 155 }, 156 } 157 for _, tc := range cases { 158 t.Run(tc.name, func(t *testing.T) { 159 rawTarget, err := json.Marshal(tc.target) 160 if err != nil { 161 t.Fatal(err) 162 } 163 if err := jsontesting.JsonCompare([]byte(tc.expectedOutput), rawTarget); err != nil { 164 t.Error(err) 165 } 166 167 var expected spec3.Components 168 json.Unmarshal(rawTarget, &expected) 169 170 if !reflect.DeepEqual(expected, tc.target) { 171 t.Fatalf("round trip error %s", tc.name) 172 } 173 }) 174 } 175 }