Skip to content

Commit ecaffbc

Browse files
authored
Merge pull request #541 from dvaldivia/add-deprecated-marker
✨ Add deprecated marker for versions
2 parents 996cc39 + bf2f926 commit ecaffbc

File tree

7 files changed

+179
-5
lines changed

7 files changed

+179
-5
lines changed

pkg/crd/markers/crd.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ var CRDMarkers = []*definitionWithHelp{
4848

4949
must(markers.MakeDefinition("kubebuilder:unservedversion", markers.DescribesType, UnservedVersion{})).
5050
WithHelp(UnservedVersion{}.Help()),
51+
52+
must(markers.MakeDefinition("kubebuilder:deprecatedversion", markers.DescribesType, DeprecatedVersion{})).
53+
WithHelp(DeprecatedVersion{}.Help()),
5154
}
5255

5356
// TODO: categories and singular used to be annotations types
@@ -316,3 +319,29 @@ func (s UnservedVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, ve
316319
}
317320

318321
// NB(directxman12): singular was historically distinct, so we keep it here for backwards compat
322+
323+
// +controllertools:marker:generateHelp:category=CRD
324+
325+
// DeprecatedVersion marks this version as deprecated.
326+
type DeprecatedVersion struct {
327+
// Warning message to be shown on the deprecated version
328+
Warning *string `marker:",optional"`
329+
}
330+
331+
func (s DeprecatedVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
332+
if version == "" {
333+
// single-version, do nothing
334+
return nil
335+
}
336+
// multi-version
337+
for i := range crd.Versions {
338+
ver := &crd.Versions[i]
339+
if ver.Name != version {
340+
continue
341+
}
342+
ver.Deprecated = true
343+
ver.DeprecationWarning = s.Warning
344+
break
345+
}
346+
return nil
347+
}

pkg/crd/markers/zz_generated.markerhelp.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/crd/parser_integration_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
6363
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
6464

6565
By("loading the roots")
66-
pkgs, err := loader.LoadRoots(".")
66+
pkgs, err := loader.LoadRoots("./", "./unserved", "./deprecated")
6767
Expect(err).NotTo(HaveOccurred())
68-
Expect(pkgs).To(HaveLen(1))
68+
Expect(pkgs).To(HaveLen(3))
6969
cronJobPkg := pkgs[0]
7070

7171
By("setting up the parser")
@@ -77,8 +77,10 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
7777
}
7878
crd.AddKnownTypes(parser)
7979

80-
By("requesting that the package be parsed")
81-
parser.NeedPackage(cronJobPkg)
80+
By("requesting that the packages be parsed")
81+
for _, pkg := range pkgs {
82+
parser.NeedPackage(pkg)
83+
}
8284

8385
By("requesting that the CRD be generated")
8486
groupKind := schema.GroupKind{Kind: "CronJob", Group: "testdata.kubebuilder.io"}

pkg/crd/testdata/cronjob_types.go

Lines changed: 2 additions & 1 deletion
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 crd paths=. output:dir=.
19+
//go:generate ../../../.run-controller-gen.sh crd paths=./;./deprecated;./unserved output:dir=.
2020

2121
// +groupName=testdata.kubebuilder.io
2222
// +versionName=v1
@@ -288,6 +288,7 @@ type CronJobStatus struct {
288288
// +kubebuilder:object:root=true
289289
// +kubebuilder:subresource:status
290290
// +kubebuilder:resource:singular=mycronjob
291+
// +kubebuilder:storageversion
291292

292293
// CronJob is the Schema for the cronjobs API
293294
type CronJob struct {

pkg/crd/testdata/deprecated/types.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
// +versionName=v1beta2
18+
package deprecated
19+
20+
import (
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
)
23+
24+
// +kubebuilder:object:root=true
25+
// +kubebuilder:subresource:status
26+
// +kubebuilder:resource:singular=mycronjob
27+
// +kubebuilder:deprecatedversion:warning="use v1 instead, this version causes your data center to catch fire when used on Tuesdays"
28+
29+
// CronJob is the Schema for the cronjobs API
30+
type CronJob struct {
31+
/*
32+
*/
33+
metav1.TypeMeta `json:",inline"`
34+
metav1.ObjectMeta `json:"metadata,omitempty"`
35+
}
36+
37+
// +kubebuilder:object:root=true
38+
39+
// CronJobList contains a list of CronJob
40+
type CronJobList struct {
41+
metav1.TypeMeta `json:",inline"`
42+
metav1.ListMeta `json:"metadata,omitempty"`
43+
Items []CronJob `json:"items"`
44+
}

pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4097,6 +4097,44 @@ spec:
40974097
storage: true
40984098
subresources:
40994099
status: {}
4100+
- name: v1beta1
4101+
schema:
4102+
openAPIV3Schema:
4103+
description: CronJob is the Schema for the cronjobs API
4104+
properties:
4105+
apiVersion:
4106+
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'
4107+
type: string
4108+
kind:
4109+
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'
4110+
type: string
4111+
metadata:
4112+
type: object
4113+
type: object
4114+
served: false
4115+
storage: false
4116+
subresources:
4117+
status: {}
4118+
- deprecated: true
4119+
deprecationWarning: use v1 instead, this version causes your data center to catch fire when used on Tuesdays
4120+
name: v1beta2
4121+
schema:
4122+
openAPIV3Schema:
4123+
description: CronJob is the Schema for the cronjobs API
4124+
properties:
4125+
apiVersion:
4126+
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'
4127+
type: string
4128+
kind:
4129+
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'
4130+
type: string
4131+
metadata:
4132+
type: object
4133+
type: object
4134+
served: true
4135+
storage: false
4136+
subresources:
4137+
status: {}
41004138
status:
41014139
acceptedNames:
41024140
kind: ""

pkg/crd/testdata/unserved/types.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
// +versionName=v1beta1
18+
package unserved
19+
20+
import (
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
)
23+
24+
// +kubebuilder:object:root=true
25+
// +kubebuilder:subresource:status
26+
// +kubebuilder:resource:singular=mycronjob
27+
// +kubebuilder:unservedversion
28+
29+
// CronJob is the Schema for the cronjobs API
30+
type CronJob struct {
31+
/*
32+
*/
33+
metav1.TypeMeta `json:",inline"`
34+
metav1.ObjectMeta `json:"metadata,omitempty"`
35+
}
36+
37+
// +kubebuilder:object:root=true
38+
39+
// CronJobList contains a list of CronJob
40+
type CronJobList struct {
41+
metav1.TypeMeta `json:",inline"`
42+
metav1.ListMeta `json:"metadata,omitempty"`
43+
Items []CronJob `json:"items"`
44+
}

0 commit comments

Comments
 (0)