Skip to content

Commit 063eaf8

Browse files
committed
Add test and warning for v1beta1 default removal
1 parent d9cb41e commit 063eaf8

File tree

5 files changed

+263
-0
lines changed

5 files changed

+263
-0
lines changed

Diff for: pkg/crd/gen.go

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"go/ast"
2222
"go/types"
23+
"os"
2324

2425
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2526
apiextlegacy "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
@@ -201,6 +202,10 @@ func removeDefaultsFromSchemaProps(v *apiextlegacy.JSONSchemaProps) {
201202
return
202203
}
203204

205+
if v.Default != nil {
206+
fmt.Fprintln(os.Stderr, "Warning: default unsupported in CRD version v1beta1, v1 required. Removing defaults.")
207+
}
208+
204209
// nil-out the default field
205210
v.Default = nil
206211
for name, prop := range v.Properties {

Diff for: pkg/crd/gen_integration_test.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
"bytes"
21+
"io"
22+
"io/ioutil"
23+
"os"
24+
25+
. "github.com/onsi/ginkgo"
26+
. "github.com/onsi/gomega"
27+
"sigs.k8s.io/controller-tools/pkg/crd"
28+
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
29+
"sigs.k8s.io/controller-tools/pkg/genall"
30+
"sigs.k8s.io/controller-tools/pkg/loader"
31+
"sigs.k8s.io/controller-tools/pkg/markers"
32+
)
33+
34+
var _ = Describe("CRD Generation proper defaulting", func() {
35+
var ctx *genall.GenerationContext
36+
var out *outputRule
37+
BeforeEach(func() {
38+
By("switching into testdata to appease go modules")
39+
cwd, err := os.Getwd()
40+
Expect(err).NotTo(HaveOccurred())
41+
Expect(os.Chdir("./testdata/gen")).To(Succeed()) // go modules are directory-sensitive
42+
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
43+
44+
By("loading the roots")
45+
pkgs, err := loader.LoadRoots(".")
46+
Expect(err).NotTo(HaveOccurred())
47+
Expect(pkgs).To(HaveLen(1))
48+
49+
By("settup up the context")
50+
reg := &markers.Registry{}
51+
Expect(crdmarkers.Register(reg)).To(Succeed())
52+
out = &outputRule{
53+
buf: &bytes.Buffer{},
54+
}
55+
ctx = &genall.GenerationContext{
56+
Collector: &markers.Collector{Registry: reg},
57+
Roots: pkgs,
58+
Checker: &loader.TypeChecker{},
59+
OutputRule: out,
60+
}
61+
})
62+
63+
It("should strip v1beta1 CRDs of default fields", func() {
64+
By("calling Generate")
65+
gen := &crd.Generator{
66+
CRDVersions: []string{"v1beta1"},
67+
}
68+
Expect(gen.Generate(ctx)).NotTo(HaveOccurred())
69+
70+
By("loading the desired YAML")
71+
expectedFile, err := ioutil.ReadFile("./testdata/gen/foo_crd_v1beta1.yaml")
72+
Expect(err).NotTo(HaveOccurred())
73+
74+
By("comparing the two")
75+
Expect(out.buf.Bytes()).To(Equal(expectedFile))
76+
77+
})
78+
79+
It("should not strip v1 CRDs of default fields", func() {
80+
By("calling Generate")
81+
gen := &crd.Generator{
82+
CRDVersions: []string{"v1"},
83+
}
84+
Expect(gen.Generate(ctx)).NotTo(HaveOccurred())
85+
86+
By("loading the desired YAML")
87+
expectedFile, err := ioutil.ReadFile("./testdata/gen/foo_crd_v1.yaml")
88+
Expect(err).NotTo(HaveOccurred())
89+
90+
By("comparing the two")
91+
Expect(out.buf.Bytes()).To(Equal(expectedFile))
92+
93+
})
94+
})
95+
96+
type outputRule struct {
97+
buf *bytes.Buffer
98+
}
99+
100+
func (o *outputRule) Open(_ *loader.Package, itemPath string) (io.WriteCloser, error) {
101+
return nopCloser{o.buf}, nil
102+
}
103+
104+
type nopCloser struct {
105+
io.Writer
106+
}
107+
108+
func (n nopCloser) Close() error {
109+
return nil
110+
}

Diff for: pkg/crd/testdata/gen/foo_crd_v1.yaml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
---
3+
apiVersion: apiextensions.k8s.io/v1
4+
kind: CustomResourceDefinition
5+
metadata:
6+
annotations:
7+
controller-gen.kubebuilder.io/version: (unknown)
8+
creationTimestamp: null
9+
name: foos.bar.example.com
10+
spec:
11+
group: bar.example.com
12+
names:
13+
kind: Foo
14+
listKind: FooList
15+
plural: foos
16+
singular: foo
17+
scope: Namespaced
18+
versions:
19+
- name: foo
20+
schema:
21+
openAPIV3Schema:
22+
properties:
23+
apiVersion:
24+
description: 'APIVersion defines the versioned schema of this representation
25+
of an object. Servers should convert recognized schemas to the latest
26+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
27+
type: string
28+
kind:
29+
description: 'Kind is a string value representing the REST resource this
30+
object represents. Servers may infer this from the endpoint the client
31+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
32+
type: string
33+
metadata:
34+
type: object
35+
spec:
36+
properties:
37+
defaultedString:
38+
default: fooDefaultString
39+
description: This tests that defaulted fields are stripped for v1beta1,
40+
but not for v1
41+
type: string
42+
required:
43+
- defaultedString
44+
type: object
45+
status:
46+
type: object
47+
type: object
48+
served: true
49+
storage: true
50+
status:
51+
acceptedNames:
52+
kind: ""
53+
plural: ""
54+
conditions: []
55+
storedVersions: []

Diff for: pkg/crd/testdata/gen/foo_crd_v1beta1.yaml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
---
3+
apiVersion: apiextensions.k8s.io/v1beta1
4+
kind: CustomResourceDefinition
5+
metadata:
6+
annotations:
7+
controller-gen.kubebuilder.io/version: (unknown)
8+
creationTimestamp: null
9+
name: foos.bar.example.com
10+
spec:
11+
group: bar.example.com
12+
names:
13+
kind: Foo
14+
listKind: FooList
15+
plural: foos
16+
singular: foo
17+
scope: Namespaced
18+
validation:
19+
openAPIV3Schema:
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+
properties:
35+
defaultedString:
36+
description: This tests that defaulted fields are stripped for v1beta1,
37+
but not for v1
38+
type: string
39+
required:
40+
- defaultedString
41+
type: object
42+
status:
43+
type: object
44+
type: object
45+
version: foo
46+
versions:
47+
- name: foo
48+
served: true
49+
storage: true
50+
status:
51+
acceptedNames:
52+
kind: ""
53+
plural: ""
54+
conditions: []
55+
storedVersions: []

Diff for: pkg/crd/testdata/gen/foo_types.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
//go:generate ../../../../.run-controller-gen.sh paths=. output:dir=.
17+
// +groupName=bar.example.com
18+
package foo
19+
20+
import (
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
)
23+
24+
type FooSpec struct {
25+
// This tests that defaulted fields are stripped for v1beta1,
26+
// but not for v1
27+
// +kubebuilder:default=fooDefaultString
28+
DefaultedString string `json:"defaultedString"`
29+
}
30+
type FooStatus struct{}
31+
32+
type Foo struct {
33+
metav1.TypeMeta `json:",inline"`
34+
metav1.ObjectMeta `json:"metadata,omitempty"`
35+
36+
Spec FooSpec `json:"spec,omitempty"`
37+
Status FooStatus `json:"status,omitempty"`
38+
}

0 commit comments

Comments
 (0)