Skip to content

Commit

Permalink
Merge pull request #541 from dvaldivia/add-deprecated-marker
Browse files Browse the repository at this point in the history
✨ Add deprecated marker for versions
  • Loading branch information
k8s-ci-robot committed Apr 30, 2021
2 parents 996cc39 + bf2f926 commit ecaffbc
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 5 deletions.
29 changes: 29 additions & 0 deletions pkg/crd/markers/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ var CRDMarkers = []*definitionWithHelp{

must(markers.MakeDefinition("kubebuilder:unservedversion", markers.DescribesType, UnservedVersion{})).
WithHelp(UnservedVersion{}.Help()),

must(markers.MakeDefinition("kubebuilder:deprecatedversion", markers.DescribesType, DeprecatedVersion{})).
WithHelp(DeprecatedVersion{}.Help()),
}

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

// NB(directxman12): singular was historically distinct, so we keep it here for backwards compat

// +controllertools:marker:generateHelp:category=CRD

// DeprecatedVersion marks this version as deprecated.
type DeprecatedVersion struct {
// Warning message to be shown on the deprecated version
Warning *string `marker:",optional"`
}

func (s DeprecatedVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
if version == "" {
// single-version, do nothing
return nil
}
// multi-version
for i := range crd.Versions {
ver := &crd.Versions[i]
if ver.Name != version {
continue
}
ver.Deprecated = true
ver.DeprecationWarning = s.Warning
break
}
return nil
}
16 changes: 16 additions & 0 deletions pkg/crd/markers/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions pkg/crd/parser_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()

By("loading the roots")
pkgs, err := loader.LoadRoots(".")
pkgs, err := loader.LoadRoots("./", "./unserved", "./deprecated")
Expect(err).NotTo(HaveOccurred())
Expect(pkgs).To(HaveLen(1))
Expect(pkgs).To(HaveLen(3))
cronJobPkg := pkgs[0]

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

By("requesting that the package be parsed")
parser.NeedPackage(cronJobPkg)
By("requesting that the packages be parsed")
for _, pkg := range pkgs {
parser.NeedPackage(pkg)
}

By("requesting that the CRD be generated")
groupKind := schema.GroupKind{Kind: "CronJob", Group: "testdata.kubebuilder.io"}
Expand Down
3 changes: 2 additions & 1 deletion pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.
// TODO(directxman12): test this across both versions (right now we're just
// trusting k/k conversion, which is probably fine though)

//go:generate ../../../.run-controller-gen.sh crd paths=. output:dir=.
//go:generate ../../../.run-controller-gen.sh crd paths=./;./deprecated;./unserved output:dir=.

// +groupName=testdata.kubebuilder.io
// +versionName=v1
Expand Down Expand Up @@ -288,6 +288,7 @@ type CronJobStatus struct {
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:singular=mycronjob
// +kubebuilder:storageversion

// CronJob is the Schema for the cronjobs API
type CronJob struct {
Expand Down
44 changes: 44 additions & 0 deletions pkg/crd/testdata/deprecated/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// +groupName=testdata.kubebuilder.io
// +versionName=v1beta2
package deprecated

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:singular=mycronjob
// +kubebuilder:deprecatedversion:warning="use v1 instead, this version causes your data center to catch fire when used on Tuesdays"

// CronJob is the Schema for the cronjobs API
type CronJob struct {
/*
*/
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
}

// +kubebuilder:object:root=true

// CronJobList contains a list of CronJob
type CronJobList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []CronJob `json:"items"`
}
38 changes: 38 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4097,6 +4097,44 @@ spec:
storage: true
subresources:
status: {}
- name: v1beta1
schema:
openAPIV3Schema:
description: CronJob is the Schema for the cronjobs API
properties:
apiVersion:
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'
type: string
kind:
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'
type: string
metadata:
type: object
type: object
served: false
storage: false
subresources:
status: {}
- deprecated: true
deprecationWarning: use v1 instead, this version causes your data center to catch fire when used on Tuesdays
name: v1beta2
schema:
openAPIV3Schema:
description: CronJob is the Schema for the cronjobs API
properties:
apiVersion:
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'
type: string
kind:
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'
type: string
metadata:
type: object
type: object
served: true
storage: false
subresources:
status: {}
status:
acceptedNames:
kind: ""
Expand Down
44 changes: 44 additions & 0 deletions pkg/crd/testdata/unserved/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// +groupName=testdata.kubebuilder.io
// +versionName=v1beta1
package unserved

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:singular=mycronjob
// +kubebuilder:unservedversion

// CronJob is the Schema for the cronjobs API
type CronJob struct {
/*
*/
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
}

// +kubebuilder:object:root=true

// CronJobList contains a list of CronJob
type CronJobList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []CronJob `json:"items"`
}

0 comments on commit ecaffbc

Please sign in to comment.