Skip to content

Commit 584080e

Browse files
stttsDirectXMan12
authored andcommitted
Add crdschema generator
1 parent a653df4 commit 584080e

23 files changed

+1415
-10
lines changed

cmd/controller-gen/main.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/spf13/cobra"
2626

2727
"sigs.k8s.io/controller-tools/pkg/crd"
28+
crdschema "sigs.k8s.io/controller-tools/pkg/crdschema"
2829
"sigs.k8s.io/controller-tools/pkg/deepcopy"
2930
"sigs.k8s.io/controller-tools/pkg/genall"
3031
"sigs.k8s.io/controller-tools/pkg/genall/help"
@@ -46,10 +47,11 @@ var (
4647
// each turns into a command line option,
4748
// and has options for output forms.
4849
allGenerators = map[string]genall.Generator{
49-
"crd": crd.Generator{},
50-
"rbac": rbac.Generator{},
51-
"object": deepcopy.Generator{},
52-
"webhook": webhook.Generator{},
50+
"crd": crd.Generator{},
51+
"rbac": rbac.Generator{},
52+
"object": deepcopy.Generator{},
53+
"webhook": webhook.Generator{},
54+
"crdschema": crdschema.Generator{},
5355
}
5456

5557
// allOutputRules defines the list of all known output rules, giving
@@ -134,6 +136,9 @@ func main() {
134136
# Generate deepcopy/runtime.Object implementations for a particular file
135137
controller-gen object paths=./apis/v1beta1/some_types.go
136138
139+
# Generate OpenAPI v3 schemas for API packages and merge them into existing CRD manifests
140+
controller-gen crdschema paths=./pkg/apis/... crdschema:manifests=./manifests output:dir=./manifests
141+
137142
# Run all the generators for a given project
138143
controller-gen paths=./apis/...
139144

pkg/crd/gen.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
6262
parser.NeedPackage(root)
6363
}
6464

65-
metav1Pkg := findMetav1(ctx.Roots)
65+
metav1Pkg := FindMetav1(ctx.Roots)
6666
if metav1Pkg == nil {
6767
// no objects in the roots, since nothing imported metav1
6868
return nil
6969
}
7070

7171
// TODO: allow selecting a specific object
72-
kubeKinds := findKubeKinds(parser, metav1Pkg)
72+
kubeKinds := FindKubeKinds(parser, metav1Pkg)
7373
if len(kubeKinds) == 0 {
7474
// no objects in the roots
7575
return nil
@@ -116,9 +116,9 @@ func toTrivialVersions(crd *apiext.CustomResourceDefinition) {
116116
crd.Spec.AdditionalPrinterColumns = canonicalColumns
117117
}
118118

119-
// findMetav1 locates the actual package representing metav1 amongst
119+
// FindMetav1 locates the actual package representing metav1 amongst
120120
// the imports of the roots.
121-
func findMetav1(roots []*loader.Package) *loader.Package {
121+
func FindMetav1(roots []*loader.Package) *loader.Package {
122122
for _, root := range roots {
123123
pkg := root.Imports()["k8s.io/apimachinery/pkg/apis/meta/v1"]
124124
if pkg != nil {
@@ -128,10 +128,10 @@ func findMetav1(roots []*loader.Package) *loader.Package {
128128
return nil
129129
}
130130

131-
// findKubeKinds locates all types that contain TypeMeta and ObjectMeta
131+
// FindKubeKinds locates all types that contain TypeMeta and ObjectMeta
132132
// (and thus may be a Kubernetes object), and returns the corresponding
133133
// group-kinds.
134-
func findKubeKinds(parser *Parser, metav1Pkg *loader.Package) []schema.GroupKind {
134+
func FindKubeKinds(parser *Parser, metav1Pkg *loader.Package) []schema.GroupKind {
135135
// TODO(directxman12): technically, we should be finding metav1 per-package
136136
var kubeKinds []schema.GroupKind
137137
for typeIdent, info := range parser.Types {

pkg/crdschema/fixtures/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
update:
2+
cd ../../.. && \
3+
go run ./cmd/controller-gen crdschema:manifests=./pkg/crdschema/fixtures/manifests output:dir=./pkg/crdschema/fixtures/manifests paths=./pkg/crdschema/fixtures/pkg/apis/...
4+
cp manifests/*-example-crd.yaml expected
5+
egrep -v -- "- foo" < expected/kubebuilder-example-crd.yaml > manifests/kubebuilder-example-crd.yaml
6+
egrep -v -- "- foo" < expected/legacy-example-crd.yaml > manifests/legacy-example-crd.yaml
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: example.kubebuilder.crdschema.controller-tools.sigs.k8s.io
5+
spec:
6+
group: kubebuilder.crdschema.controller-tools.sigs.k8s.io
7+
scope: Cluster
8+
names:
9+
kind: Example
10+
singular: example
11+
plural: examples
12+
listKind: ExampleList
13+
version: v1
14+
validation:
15+
openAPIV3Schema:
16+
description: Example is a kind with schema changes.
17+
type: object
18+
required:
19+
- spec
20+
properties:
21+
apiVersion:
22+
description: 'APIVersion defines the versioned schema of this representation
23+
of an object. Servers should convert recognized schemas to the latest
24+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
25+
type: string
26+
kind:
27+
description: 'Kind is a string value representing the REST resource this
28+
object represents. Servers may infer this from the endpoint the client
29+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
30+
type: string
31+
metadata:
32+
type: object
33+
spec:
34+
type: object
35+
required:
36+
- bar
37+
- foo
38+
properties:
39+
bar:
40+
description: foo contains foo.
41+
type: string
42+
foo:
43+
description: foo contains foo.
44+
type: string
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: example.legacy.crdschema.controller-tools.sigs.k8s.io
5+
spec:
6+
group: legacy.crdschema.controller-tools.sigs.k8s.io
7+
scope: Cluster
8+
names:
9+
kind: Example
10+
singular: example
11+
plural: examples
12+
listKind: ExampleList
13+
version: v1
14+
validation:
15+
openAPIV3Schema:
16+
description: Example is a kind with schema changes.
17+
type: object
18+
required:
19+
- spec
20+
properties:
21+
apiVersion:
22+
description: 'APIVersion defines the versioned schema of this representation
23+
of an object. Servers should convert recognized schemas to the latest
24+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
25+
type: string
26+
kind:
27+
description: 'Kind is a string value representing the REST resource this
28+
object represents. Servers may infer this from the endpoint the client
29+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
30+
type: string
31+
metadata:
32+
type: object
33+
spec:
34+
type: object
35+
required:
36+
- bar
37+
- foo
38+
properties:
39+
bar:
40+
description: foo contains foo.
41+
type: string
42+
foo:
43+
description: foo contains foo.
44+
type: string
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: example.kubebuilder.crdschema.controller-tools.sigs.k8s.io
5+
spec:
6+
group: kubebuilder.crdschema.controller-tools.sigs.k8s.io
7+
scope: Cluster
8+
names:
9+
kind: Example
10+
singular: example
11+
plural: examples
12+
listKind: ExampleList
13+
version: v1
14+
validation:
15+
openAPIV3Schema:
16+
description: Example is a kind with schema changes.
17+
type: object
18+
required:
19+
- spec
20+
properties:
21+
apiVersion:
22+
description: 'APIVersion defines the versioned schema of this representation
23+
of an object. Servers should convert recognized schemas to the latest
24+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
25+
type: string
26+
kind:
27+
description: 'Kind is a string value representing the REST resource this
28+
object represents. Servers may infer this from the endpoint the client
29+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
30+
type: string
31+
metadata:
32+
type: object
33+
spec:
34+
type: object
35+
required:
36+
- bar
37+
properties:
38+
bar:
39+
description: foo contains foo.
40+
type: string
41+
foo:
42+
description: foo contains foo.
43+
type: string
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: unchanged.kubebuilder.crdschema.controller-tools.sigs.k8s.io
5+
spec:
6+
group: kubebuilder.crdschema.controller-tools.sigs.k8s.io
7+
scope: Cluster
8+
names:
9+
kind: Unchanged
10+
singular: unchanged
11+
plural: unchangeds
12+
listKind: UnchangedList
13+
version: v1
14+
validation:
15+
openAPIV3Schema:
16+
description: Unchanged is a kind without schema changes.
17+
type: object
18+
required:
19+
- spec
20+
properties:
21+
apiVersion:
22+
description: 'APIVersion defines the versioned schema of this representation
23+
of an object. Servers should convert recognized schemas to the latest
24+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
25+
type: string
26+
kind:
27+
description: 'Kind is a string value representing the REST resource this
28+
object represents. Servers may infer this from the endpoint the client
29+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
30+
type: string
31+
metadata:
32+
type: object
33+
spec:
34+
type: object
35+
required:
36+
- bar
37+
- foo
38+
properties:
39+
bar:
40+
description: foo contains foo.
41+
type: string
42+
foo:
43+
description: foo contains foo.
44+
type: string
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: example.legacy.crdschema.controller-tools.sigs.k8s.io
5+
spec:
6+
group: legacy.crdschema.controller-tools.sigs.k8s.io
7+
scope: Cluster
8+
names:
9+
kind: Example
10+
singular: example
11+
plural: examples
12+
listKind: ExampleList
13+
version: v1
14+
validation:
15+
openAPIV3Schema:
16+
description: Example is a kind with schema changes.
17+
type: object
18+
required:
19+
- spec
20+
properties:
21+
apiVersion:
22+
description: 'APIVersion defines the versioned schema of this representation
23+
of an object. Servers should convert recognized schemas to the latest
24+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
25+
type: string
26+
kind:
27+
description: 'Kind is a string value representing the REST resource this
28+
object represents. Servers may infer this from the endpoint the client
29+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
30+
type: string
31+
metadata:
32+
type: object
33+
spec:
34+
type: object
35+
required:
36+
- bar
37+
properties:
38+
bar:
39+
description: foo contains foo.
40+
type: string
41+
foo:
42+
description: foo contains foo.
43+
type: string
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: unchanged.legacy.crdschema.controller-tools.sigs.k8s.io
5+
spec:
6+
group: legacy.crdschema.controller-tools.sigs.k8s.io
7+
scope: Cluster
8+
names:
9+
kind: Unchanged
10+
singular: unchanged
11+
plural: unchangeds
12+
listKind: UnchangedList
13+
version: v1
14+
validation:
15+
openAPIV3Schema:
16+
description: Unchanged is a kind without schema changes.
17+
type: object
18+
required:
19+
- spec
20+
properties:
21+
apiVersion:
22+
description: 'APIVersion defines the versioned schema of this representation
23+
of an object. Servers should convert recognized schemas to the latest
24+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
25+
type: string
26+
kind:
27+
description: 'Kind is a string value representing the REST resource this
28+
object represents. Servers may infer this from the endpoint the client
29+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
30+
type: string
31+
metadata:
32+
type: object
33+
spec:
34+
type: object
35+
required:
36+
- bar
37+
- foo
38+
properties:
39+
bar:
40+
description: foo contains foo.
41+
type: string
42+
foo:
43+
description: foo contains foo.
44+
type: string
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: other.crdschema.controller-tools.sigs.k8s.io
5+
spec:
6+
group: crdschema.controller-tools.sigs.k8s.io
7+
scope: Cluster
8+
names:
9+
kind: Other
10+
singular: other
11+
plural: others
12+
listKind: OtherList
13+
version: v1

0 commit comments

Comments
 (0)