Skip to content

Commit 1903475

Browse files
committed
pkg/webhook: use v1 type for both v1 and v1beta1 webhook config generation
.run-controller-gen.sh: fix readlink check *: update testdata with v1.19.2 deps, regenerate, fix tests
1 parent 3e2063f commit 1903475

20 files changed

+2520
-3362
lines changed

.run-controller-gen.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ set -o pipefail
1111
readlink=$(command -v readlink)
1212

1313
check_readlink() {
14-
if ! ${readlink} -f &>/dev/null; then
14+
local test_file="$(mktemp)"
15+
trap "rm -f $test_file" EXIT
16+
if ! ${readlink} -f "$test_file" &>/dev/null; then
1517
if [[ "${OSTYPE}" == "darwin"* ]]; then
1618
if command -v greadlink; then
1719
readlink=$(command -v greadlink)

pkg/crd/gen_integration_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io"
2222
"io/ioutil"
2323
"os"
24+
"path/filepath"
2425

2526
"github.com/google/go-cmp/cmp"
2627
. "github.com/onsi/ginkgo"
@@ -33,13 +34,18 @@ import (
3334
)
3435

3536
var _ = Describe("CRD Generation proper defaulting", func() {
36-
var ctx *genall.GenerationContext
37-
var out *outputRule
37+
var (
38+
ctx *genall.GenerationContext
39+
out *outputRule
40+
41+
genDir = filepath.Join("testdata", "gen")
42+
)
43+
3844
BeforeEach(func() {
3945
By("switching into testdata to appease go modules")
4046
cwd, err := os.Getwd()
4147
Expect(err).NotTo(HaveOccurred())
42-
Expect(os.Chdir("./testdata/gen")).To(Succeed()) // go modules are directory-sensitive
48+
Expect(os.Chdir(genDir)).To(Succeed()) // go modules are directory-sensitive
4349
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
4450

4551
By("loading the roots")
@@ -69,8 +75,9 @@ var _ = Describe("CRD Generation proper defaulting", func() {
6975
Expect(gen.Generate(ctx)).NotTo(HaveOccurred())
7076

7177
By("loading the desired YAML")
72-
expectedFile, err := ioutil.ReadFile("./testdata/gen/foo_crd_v1beta1.yaml")
78+
expectedFile, err := ioutil.ReadFile(filepath.Join(genDir, "bar.example.com_foos.v1beta1.yaml"))
7379
Expect(err).NotTo(HaveOccurred())
80+
expectedFile = fixAnnotations(expectedFile)
7481

7582
By("comparing the two")
7683
Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile)))
@@ -85,14 +92,20 @@ var _ = Describe("CRD Generation proper defaulting", func() {
8592
Expect(gen.Generate(ctx)).NotTo(HaveOccurred())
8693

8794
By("loading the desired YAML")
88-
expectedFile, err := ioutil.ReadFile("./testdata/gen/foo_crd_v1.yaml")
95+
expectedFile, err := ioutil.ReadFile(filepath.Join(genDir, "bar.example.com_foos.yaml"))
8996
Expect(err).NotTo(HaveOccurred())
97+
expectedFile = fixAnnotations(expectedFile)
9098

9199
By("comparing the two")
92100
Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile)))
93101
})
94102
})
95103

104+
// fixAnnotations fixes the attribution annotation for tests.
105+
func fixAnnotations(crdBytes []byte) []byte {
106+
return bytes.Replace(crdBytes, []byte("(devel)"), []byte("(unknown)"), 1)
107+
}
108+
96109
type outputRule struct {
97110
buf *bytes.Buffer
98111
}

pkg/crd/testdata/cronjob_types.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616
// TODO(directxman12): test this across both versions (right now we're just
1717
// trusting k/k conversion, which is probably fine though)
1818

19-
//go:generate ../../../.run-controller-gen.sh paths=. output:dir=.
19+
//go:generate ../../../.run-controller-gen.sh crd paths=. output:dir=.
2020

2121
// +groupName=testdata.kubebuilder.io
2222
// +versionName=v1
@@ -159,9 +159,10 @@ type CronJobSpec struct {
159159
// +kubebuilder:validation:Type=object
160160
// +kubebuilder:pruning:PreserveUnknownFields
161161
type Preserved struct {
162-
ConcreteField string `json:"concreteField"`
163-
Rest map[string]interface{} `json:"-"`
162+
ConcreteField string `json:"concreteField"`
163+
Rest map[string]interface{} `json:"-"`
164164
}
165+
165166
func (p *Preserved) UnmarshalJSON(data []byte) error {
166167
if err := json.Unmarshal(data, &p.Rest); err != nil {
167168
return err
@@ -303,7 +304,3 @@ type CronJobList struct {
303304
metav1.ListMeta `json:"metadata,omitempty"`
304305
Items []CronJob `json:"items"`
305306
}
306-
307-
func init() {
308-
SchemeBuilder.Register(&CronJob{}, &CronJobList{})
309-
}

pkg/crd/testdata/gen/foo_crd_v1beta1.yaml renamed to pkg/crd/testdata/gen/bar.example.com_foos.v1beta1.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1beta1
44
kind: CustomResourceDefinition
55
metadata:
66
annotations:
7-
controller-gen.kubebuilder.io/version: (unknown)
7+
controller-gen.kubebuilder.io/version: (devel)
88
creationTimestamp: null
99
name: foos.bar.example.com
1010
spec:
@@ -19,10 +19,10 @@ spec:
1919
openAPIV3Schema:
2020
properties:
2121
apiVersion:
22-
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
22+
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
2323
type: string
2424
kind:
25-
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
25+
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
2626
type: string
2727
metadata:
2828
type: object

pkg/crd/testdata/gen/foo_crd_v1.yaml renamed to pkg/crd/testdata/gen/bar.example.com_foos.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
44
kind: CustomResourceDefinition
55
metadata:
66
annotations:
7-
controller-gen.kubebuilder.io/version: (unknown)
7+
controller-gen.kubebuilder.io/version: (devel)
88
creationTimestamp: null
99
name: foos.bar.example.com
1010
spec:
@@ -21,10 +21,10 @@ spec:
2121
openAPIV3Schema:
2222
properties:
2323
apiVersion:
24-
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
24+
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
2525
type: string
2626
kind:
27-
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
27+
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
2828
type: string
2929
metadata:
3030
type: object

pkg/crd/testdata/gen/foo_types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
*/
1515

16-
//go:generate ../../../../.run-controller-gen.sh paths=. output:dir=.
16+
//go:generate ../../../../.run-controller-gen.sh crd:crdVersions=v1beta1 paths=. output:dir=.
17+
//go:generate mv bar.example.com_foos.yaml bar.example.com_foos.v1beta1.yaml
18+
//go:generate ../../../../.run-controller-gen.sh crd:crdVersions=v1 paths=. output:dir=.
19+
1720
// +groupName=bar.example.com
1821
package foo
1922

pkg/crd/testdata/go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module testdata.kubebuilder.io/cronjob
22

3-
go 1.13
3+
go 1.15
44

55
require (
6-
k8s.io/api v0.0.0-20190615205754-1d1b8b084b30
7-
k8s.io/apimachinery v0.0.0-20190612125636-6a5db36e93ad
6+
k8s.io/api v0.19.2
7+
k8s.io/apimachinery v0.19.2
88
)

0 commit comments

Comments
 (0)