Skip to content

Commit ce59fd8

Browse files
ntoofuk8s-infra-cherrypick-robot
authored and
k8s-infra-cherrypick-robot
committed
Add a testcase to reproduce bug of markers
Markers might be lost in generated CRD when the package containing markers is referenced by several packages and CRD is generated by those packages. For more details, see #783.
1 parent 20b9845 commit ce59fd8

File tree

5 files changed

+236
-0
lines changed

5 files changed

+236
-0
lines changed

pkg/crd/parser_integration_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,52 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
266266
By("checking that no errors occurred along the way (expect for type errors)")
267267
Expect(packageErrors(cronJobPkg, packages.TypeError)).NotTo(HaveOccurred())
268268
})
269+
270+
It("should generate markers properly among several package versions", func() {
271+
By("switching into testdata to appease go modules")
272+
cwd, err := os.Getwd()
273+
Expect(err).NotTo(HaveOccurred())
274+
Expect(os.Chdir("./testdata/multiple_versions")).To(Succeed())
275+
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
276+
277+
By("loading the roots")
278+
pkgs, err := loader.LoadRoots("./v1beta1", "./v1beta2")
279+
Expect(err).NotTo(HaveOccurred())
280+
Expect(pkgs).To(HaveLen(2))
281+
282+
By("setting up the parser")
283+
reg := &markers.Registry{}
284+
Expect(crdmarkers.Register(reg)).To(Succeed())
285+
parser := &crd.Parser{
286+
Collector: &markers.Collector{Registry: reg},
287+
Checker: &loader.TypeChecker{},
288+
}
289+
crd.AddKnownTypes(parser)
290+
291+
By("requesting that the package be parsed")
292+
for _, pkg := range pkgs {
293+
parser.NeedPackage(pkg)
294+
}
295+
296+
By("requesting that the CRD be generated")
297+
groupKind := schema.GroupKind{Kind: "VersionedResource", Group: "testdata.kubebuilder.io"}
298+
parser.NeedCRDFor(groupKind, nil)
299+
300+
By("fixing top level ObjectMeta on the CRD")
301+
crd.FixTopLevelMetadata(parser.CustomResourceDefinitions[groupKind])
302+
303+
By("loading the desired YAML")
304+
expectedFile, err := ioutil.ReadFile("testdata.kubebuilder.io_versionedresources.yaml")
305+
Expect(err).NotTo(HaveOccurred())
306+
307+
By("parsing the desired YAML")
308+
var crd apiext.CustomResourceDefinition
309+
Expect(yaml.Unmarshal(expectedFile, &crd)).To(Succeed())
310+
// clear the annotations -- we don't care about the attribution annotation
311+
crd.Annotations = nil
312+
313+
By("comparing the two")
314+
Expect(parser.CustomResourceDefinitions[groupKind]).To(Equal(crd), "type not as expected, check pkg/crd/testdata/README.md for more details.\n\nDiff:\n\n%s", cmp.Diff(parser.CustomResourceDefinitions[groupKind], crd))
315+
})
316+
269317
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package multiple_versions
2+
3+
type InnerStruct struct {
4+
Foo string `json:"foo,omitempty"`
5+
}
6+
7+
type OuterStruct struct {
8+
// +structType=atomic
9+
Struct InnerStruct `json:"struct,omitempty"`
10+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
controller-gen.kubebuilder.io/version: (devel)
7+
creationTimestamp: null
8+
name: versionedresources.testdata.kubebuilder.io
9+
spec:
10+
group: testdata.kubebuilder.io
11+
names:
12+
kind: VersionedResource
13+
listKind: VersionedResourceList
14+
plural: versionedresources
15+
singular: versionedresource
16+
scope: Namespaced
17+
versions:
18+
- name: v1beta1
19+
schema:
20+
openAPIV3Schema:
21+
properties:
22+
apiVersion:
23+
description: 'APIVersion defines the versioned schema of this representation
24+
of an object. Servers should convert recognized schemas to the latest
25+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
26+
type: string
27+
kind:
28+
description: 'Kind is a string value representing the REST resource this
29+
object represents. Servers may infer this from the endpoint the client
30+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
31+
type: string
32+
metadata:
33+
type: object
34+
spec:
35+
properties:
36+
struct:
37+
properties:
38+
struct:
39+
properties:
40+
foo:
41+
type: string
42+
type: object
43+
x-kubernetes-map-type: atomic
44+
type: object
45+
type: object
46+
required:
47+
- spec
48+
type: object
49+
served: true
50+
storage: false
51+
subresources:
52+
status: {}
53+
- name: v1beta2
54+
schema:
55+
openAPIV3Schema:
56+
properties:
57+
apiVersion:
58+
description: 'APIVersion defines the versioned schema of this representation
59+
of an object. Servers should convert recognized schemas to the latest
60+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
61+
type: string
62+
kind:
63+
description: 'Kind is a string value representing the REST resource this
64+
object represents. Servers may infer this from the endpoint the client
65+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
66+
type: string
67+
metadata:
68+
type: object
69+
spec:
70+
properties:
71+
struct:
72+
properties:
73+
struct:
74+
properties:
75+
foo:
76+
type: string
77+
type: object
78+
x-kubernetes-map-type: atomic
79+
type: object
80+
type: object
81+
required:
82+
- spec
83+
type: object
84+
served: true
85+
storage: true
86+
subresources:
87+
status: {}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
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+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
// +groupName=testdata.kubebuilder.io
17+
package v1beta1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
multiver "testdata.kubebuilder.io/cronjob/multiple_versions"
22+
)
23+
24+
type VersionedResourceSpec struct {
25+
Struct multiver.OuterStruct `json:"struct,omitempty"`
26+
}
27+
28+
// +kubebuilder:object:root=true
29+
// +kubebuilder:subresource:status
30+
// +kubebuilder:resource:singular=versionedresource
31+
32+
type VersionedResource struct {
33+
metav1.TypeMeta `json:",inline"`
34+
metav1.ObjectMeta `json:"metadata,omitempty"`
35+
36+
Spec VersionedResourceSpec `json:"spec"`
37+
}
38+
39+
// +kubebuilder:object:root=true
40+
41+
type VersionedResourceList struct {
42+
metav1.TypeMeta `json:",inline"`
43+
metav1.ListMeta `json:"metadata,omitempty"`
44+
Items []VersionedResource `json:"items"`
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
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+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
// +groupName=testdata.kubebuilder.io
17+
package v1beta2
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
multiver "testdata.kubebuilder.io/cronjob/multiple_versions"
22+
)
23+
24+
type VersionedResourceSpec struct {
25+
Struct multiver.OuterStruct `json:"struct,omitempty"`
26+
}
27+
28+
// +kubebuilder:storageversion
29+
// +kubebuilder:object:root=true
30+
// +kubebuilder:subresource:status
31+
// +kubebuilder:resource:singular=versionedresource
32+
33+
type VersionedResource struct {
34+
metav1.TypeMeta `json:",inline"`
35+
metav1.ObjectMeta `json:"metadata,omitempty"`
36+
37+
Spec VersionedResourceSpec `json:"spec"`
38+
}
39+
40+
// +kubebuilder:object:root=true
41+
42+
type VersionedResourceList struct {
43+
metav1.TypeMeta `json:",inline"`
44+
metav1.ListMeta `json:"metadata,omitempty"`
45+
Items []VersionedResource `json:"items"`
46+
}

0 commit comments

Comments
 (0)