github.com/cilium/controller-tools@v0.3.1-0.20230329170030-f2b7ff866fde/pkg/crd/crd_spec_test.go (about) 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 crd_test 18 19 import ( 20 . "github.com/onsi/ginkgo" 21 . "github.com/onsi/gomega" 22 apiextlegacy "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" 23 24 "sigs.k8s.io/controller-tools/pkg/crd" 25 ) 26 27 var _ = Describe("CRD Generation", func() { 28 Describe("Utilities", func() { 29 Describe("MergeIdenticalVersionInfo", func() { 30 It("should replace per-version schemata with a top-level schema if only one version", func() { 31 spec := &apiextlegacy.CustomResourceDefinition{ 32 Spec: apiextlegacy.CustomResourceDefinitionSpec{ 33 Versions: []apiextlegacy.CustomResourceDefinitionVersion{ 34 { 35 Name: "v1", 36 Storage: true, 37 Schema: &apiextlegacy.CustomResourceValidation{ 38 OpenAPIV3Schema: &apiextlegacy.JSONSchemaProps{ 39 Required: []string{"foo"}, 40 Type: "object", 41 Properties: map[string]apiextlegacy.JSONSchemaProps{"foo": apiextlegacy.JSONSchemaProps{Type: "string"}}, 42 }, 43 }, 44 }, 45 }, 46 }, 47 } 48 crd.MergeIdenticalVersionInfo(spec) 49 Expect(spec.Spec.Validation).To(Equal(&apiextlegacy.CustomResourceValidation{ 50 OpenAPIV3Schema: &apiextlegacy.JSONSchemaProps{ 51 Required: []string{"foo"}, 52 Type: "object", 53 Properties: map[string]apiextlegacy.JSONSchemaProps{"foo": apiextlegacy.JSONSchemaProps{Type: "string"}}, 54 }, 55 })) 56 Expect(spec.Spec.Versions).To(Equal([]apiextlegacy.CustomResourceDefinitionVersion{ 57 {Name: "v1", Storage: true}, 58 })) 59 }) 60 It("should replace per-version schemata with a top-level schema if all are identical", func() { 61 spec := &apiextlegacy.CustomResourceDefinition{ 62 Spec: apiextlegacy.CustomResourceDefinitionSpec{ 63 Versions: []apiextlegacy.CustomResourceDefinitionVersion{ 64 { 65 Name: "v1", 66 Schema: &apiextlegacy.CustomResourceValidation{ 67 OpenAPIV3Schema: &apiextlegacy.JSONSchemaProps{ 68 Required: []string{"foo"}, 69 Type: "object", 70 Properties: map[string]apiextlegacy.JSONSchemaProps{"foo": apiextlegacy.JSONSchemaProps{Type: "string"}}, 71 }, 72 }, 73 }, 74 { 75 Name: "v2", 76 Storage: true, 77 Schema: &apiextlegacy.CustomResourceValidation{ 78 OpenAPIV3Schema: &apiextlegacy.JSONSchemaProps{ 79 Required: []string{"foo"}, 80 Type: "object", 81 Properties: map[string]apiextlegacy.JSONSchemaProps{"foo": apiextlegacy.JSONSchemaProps{Type: "string"}}, 82 }, 83 }, 84 }, 85 }, 86 }, 87 } 88 crd.MergeIdenticalVersionInfo(spec) 89 Expect(spec.Spec.Validation).To(Equal(&apiextlegacy.CustomResourceValidation{ 90 OpenAPIV3Schema: &apiextlegacy.JSONSchemaProps{ 91 Required: []string{"foo"}, 92 Type: "object", 93 Properties: map[string]apiextlegacy.JSONSchemaProps{"foo": apiextlegacy.JSONSchemaProps{Type: "string"}}, 94 }, 95 })) 96 Expect(spec.Spec.Versions).To(Equal([]apiextlegacy.CustomResourceDefinitionVersion{ 97 {Name: "v1"}, {Name: "v2", Storage: true}, 98 })) 99 }) 100 101 It("shouldn't merge different schemata", func() { 102 spec := &apiextlegacy.CustomResourceDefinition{ 103 Spec: apiextlegacy.CustomResourceDefinitionSpec{ 104 Versions: []apiextlegacy.CustomResourceDefinitionVersion{ 105 { 106 Name: "v1", 107 Schema: &apiextlegacy.CustomResourceValidation{ 108 OpenAPIV3Schema: &apiextlegacy.JSONSchemaProps{ 109 Type: "object", 110 Properties: map[string]apiextlegacy.JSONSchemaProps{"foo": apiextlegacy.JSONSchemaProps{Type: "string"}}, 111 }, 112 }, 113 }, 114 { 115 Name: "v2", 116 Storage: true, 117 Schema: &apiextlegacy.CustomResourceValidation{ 118 OpenAPIV3Schema: &apiextlegacy.JSONSchemaProps{ 119 Required: []string{"foo"}, 120 Type: "object", 121 Properties: map[string]apiextlegacy.JSONSchemaProps{"foo": apiextlegacy.JSONSchemaProps{Type: "string"}}, 122 }, 123 }, 124 }, 125 }, 126 }, 127 } 128 orig := spec.DeepCopy() 129 crd.MergeIdenticalVersionInfo(spec) 130 Expect(spec).To(Equal(orig)) 131 }) 132 133 It("should replace per-version subresources with top-level subresources if only one version", func() { 134 spec := &apiextlegacy.CustomResourceDefinition{ 135 Spec: apiextlegacy.CustomResourceDefinitionSpec{ 136 Versions: []apiextlegacy.CustomResourceDefinitionVersion{ 137 { 138 Name: "v1", 139 Storage: true, 140 Subresources: &apiextlegacy.CustomResourceSubresources{ 141 Status: &apiextlegacy.CustomResourceSubresourceStatus{}, 142 }, 143 }, 144 }, 145 }, 146 } 147 148 crd.MergeIdenticalVersionInfo(spec) 149 Expect(spec.Spec.Subresources).To(Equal(&apiextlegacy.CustomResourceSubresources{ 150 Status: &apiextlegacy.CustomResourceSubresourceStatus{}, 151 })) 152 Expect(spec.Spec.Versions).To(Equal([]apiextlegacy.CustomResourceDefinitionVersion{ 153 {Name: "v1", Storage: true}, 154 })) 155 }) 156 157 It("should replace per-version subresources with top-level subresources if all are identical", func() { 158 spec := &apiextlegacy.CustomResourceDefinition{ 159 Spec: apiextlegacy.CustomResourceDefinitionSpec{ 160 Versions: []apiextlegacy.CustomResourceDefinitionVersion{ 161 { 162 Name: "v1", 163 Subresources: &apiextlegacy.CustomResourceSubresources{ 164 Status: &apiextlegacy.CustomResourceSubresourceStatus{}, 165 }, 166 }, 167 { 168 Name: "v2", 169 Storage: true, 170 Subresources: &apiextlegacy.CustomResourceSubresources{ 171 Status: &apiextlegacy.CustomResourceSubresourceStatus{}, 172 }, 173 }, 174 }, 175 }, 176 } 177 178 crd.MergeIdenticalVersionInfo(spec) 179 Expect(spec.Spec.Subresources).To(Equal(&apiextlegacy.CustomResourceSubresources{ 180 Status: &apiextlegacy.CustomResourceSubresourceStatus{}, 181 })) 182 Expect(spec.Spec.Versions).To(Equal([]apiextlegacy.CustomResourceDefinitionVersion{ 183 {Name: "v1"}, {Name: "v2", Storage: true}, 184 })) 185 }) 186 187 It("shouldn't merge different subresources", func() { 188 spec := &apiextlegacy.CustomResourceDefinition{ 189 Spec: apiextlegacy.CustomResourceDefinitionSpec{ 190 Versions: []apiextlegacy.CustomResourceDefinitionVersion{ 191 { 192 Name: "v1", 193 Subresources: &apiextlegacy.CustomResourceSubresources{ 194 Status: &apiextlegacy.CustomResourceSubresourceStatus{}, 195 }, 196 }, 197 { 198 Name: "v2", 199 Storage: true, 200 }, 201 }, 202 }, 203 } 204 orig := spec.DeepCopy() 205 crd.MergeIdenticalVersionInfo(spec) 206 Expect(spec).To(Equal(orig)) 207 }) 208 209 It("should replace per-version printer columns with top-level printer columns if only one version", func() { 210 spec := &apiextlegacy.CustomResourceDefinition{ 211 Spec: apiextlegacy.CustomResourceDefinitionSpec{ 212 Versions: []apiextlegacy.CustomResourceDefinitionVersion{ 213 { 214 Name: "v1", 215 Storage: true, 216 AdditionalPrinterColumns: []apiextlegacy.CustomResourceColumnDefinition{ 217 {Name: "Cheddar", JSONPath: ".spec.cheddar"}, 218 {Name: "Parmesan", JSONPath: ".status.parmesan"}, 219 }, 220 }, 221 }, 222 }, 223 } 224 225 crd.MergeIdenticalVersionInfo(spec) 226 Expect(spec.Spec.AdditionalPrinterColumns).To(Equal([]apiextlegacy.CustomResourceColumnDefinition{ 227 {Name: "Cheddar", JSONPath: ".spec.cheddar"}, 228 {Name: "Parmesan", JSONPath: ".status.parmesan"}, 229 })) 230 Expect(spec.Spec.Versions).To(Equal([]apiextlegacy.CustomResourceDefinitionVersion{ 231 {Name: "v1", Storage: true}, 232 })) 233 }) 234 235 It("should replace per-version printer columns with top-level printer columns if all are identical", func() { 236 spec := &apiextlegacy.CustomResourceDefinition{ 237 Spec: apiextlegacy.CustomResourceDefinitionSpec{ 238 Versions: []apiextlegacy.CustomResourceDefinitionVersion{ 239 { 240 Name: "v1", 241 AdditionalPrinterColumns: []apiextlegacy.CustomResourceColumnDefinition{ 242 {Name: "Cheddar", JSONPath: ".spec.cheddar"}, 243 {Name: "Parmesan", JSONPath: ".status.parmesan"}, 244 }, 245 }, 246 { 247 Name: "v2", 248 Storage: true, 249 AdditionalPrinterColumns: []apiextlegacy.CustomResourceColumnDefinition{ 250 {Name: "Cheddar", JSONPath: ".spec.cheddar"}, 251 {Name: "Parmesan", JSONPath: ".status.parmesan"}, 252 }, 253 }, 254 }, 255 }, 256 } 257 258 crd.MergeIdenticalVersionInfo(spec) 259 Expect(spec.Spec.AdditionalPrinterColumns).To(Equal([]apiextlegacy.CustomResourceColumnDefinition{ 260 {Name: "Cheddar", JSONPath: ".spec.cheddar"}, 261 {Name: "Parmesan", JSONPath: ".status.parmesan"}, 262 })) 263 Expect(spec.Spec.Versions).To(Equal([]apiextlegacy.CustomResourceDefinitionVersion{ 264 {Name: "v1"}, {Name: "v2", Storage: true}, 265 })) 266 }) 267 268 It("shouldn't merge different printer columns", func() { 269 spec := &apiextlegacy.CustomResourceDefinition{ 270 Spec: apiextlegacy.CustomResourceDefinitionSpec{ 271 Versions: []apiextlegacy.CustomResourceDefinitionVersion{ 272 { 273 Name: "v1", 274 AdditionalPrinterColumns: []apiextlegacy.CustomResourceColumnDefinition{ 275 {Name: "Cheddar", JSONPath: ".spec.cheddar"}, 276 {Name: "Parmesan", JSONPath: ".status.parmesan"}, 277 }, 278 }, 279 { 280 Name: "v2", 281 Storage: true, 282 }, 283 }, 284 }, 285 } 286 orig := spec.DeepCopy() 287 crd.MergeIdenticalVersionInfo(spec) 288 Expect(spec).To(Equal(orig)) 289 }) 290 }) 291 }) 292 })