Skip to content

Commit a5ed99b

Browse files
authored
Merge pull request #325 from kmodules/ignore-internal-types
✨ Use +kubebuilder:skip to skip package
2 parents 1e802f3 + 4d47472 commit a5ed99b

File tree

6 files changed

+190
-8
lines changed

6 files changed

+190
-8
lines changed

pkg/crd/markers/package.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@ func init() {
3333

3434
must(markers.MakeDefinition("kubebuilder:validation:Required", markers.DescribesPackage, struct{}{})).
3535
WithHelp(markers.SimpleHelp("CRD validation", "specifies that all fields in this package are required by default.")),
36+
37+
must(markers.MakeDefinition("kubebuilder:skip", markers.DescribesPackage, struct{}{})).
38+
WithHelp(markers.SimpleHelp("CRD", "don't consider this package as an API version.")),
3639
)
3740
}

pkg/crd/parser.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,20 @@ func (p *Parser) indexTypes(pkg *loader.Package) {
110110
pkgMarkers, err := markers.PackageMarkers(p.Collector, pkg)
111111
if err != nil {
112112
pkg.AddError(err)
113-
} else if nameVal := pkgMarkers.Get("groupName"); nameVal != nil {
114-
versionVal := pkg.Name // a reasonable guess
115-
if versionMarker := pkgMarkers.Get("versionName"); versionMarker != nil {
116-
versionVal = versionMarker.(string)
113+
} else {
114+
if skipPkg := pkgMarkers.Get("kubebuilder:skip"); skipPkg != nil {
115+
return
117116
}
118-
119-
p.GroupVersions[pkg] = schema.GroupVersion{
120-
Version: versionVal,
121-
Group: nameVal.(string),
117+
if nameVal := pkgMarkers.Get("groupName"); nameVal != nil {
118+
versionVal := pkg.Name // a reasonable guess
119+
if versionMarker := pkgMarkers.Get("versionName"); versionMarker != nil {
120+
versionVal = versionMarker.(string)
121+
}
122+
123+
p.GroupVersions[pkg] = schema.GroupVersion{
124+
Version: versionVal,
125+
Group: nameVal.(string),
126+
}
122127
}
123128
}
124129

pkg/crd/parser_integration_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,40 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
9898
By("comparing the two")
9999
Expect(parser.CustomResourceDefinitions[groupKind]).To(Equal(crd), "type not as expected, check pkg/crd/testdata/README.md for more details.\n\nDiff:\n\n%s", cmp.Diff(parser.CustomResourceDefinitions[groupKind], crd))
100100
})
101+
102+
It("should skip api internal package", func() {
103+
By("switching into testdata to appease go modules")
104+
cwd, err := os.Getwd()
105+
Expect(err).NotTo(HaveOccurred())
106+
Expect(os.Chdir("./testdata/internal_version")).To(Succeed()) // go modules are directory-sensitive
107+
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
108+
109+
By("loading the roots")
110+
pkgs, err := loader.LoadRoots(".")
111+
Expect(err).NotTo(HaveOccurred())
112+
Expect(pkgs).To(HaveLen(1))
113+
cronJobPkg := pkgs[0]
114+
115+
By("setting up the parser")
116+
reg := &markers.Registry{}
117+
Expect(crdmarkers.Register(reg)).To(Succeed())
118+
parser := &crd.Parser{
119+
Collector: &markers.Collector{Registry: reg},
120+
Checker: &loader.TypeChecker{},
121+
}
122+
crd.AddKnownTypes(parser)
123+
124+
By("requesting that the package be parsed")
125+
parser.NeedPackage(cronJobPkg)
126+
127+
By("checking that there is no GroupVersion")
128+
Expect(parser.GroupVersions).To(BeEmpty())
129+
130+
By("checking that there are no Types")
131+
Expect(parser.Types).To(BeEmpty())
132+
133+
By("checking that no errors occurred along the way (expect for type errors)")
134+
Expect(packageErrors(cronJobPkg, packages.TypeError)).NotTo(HaveOccurred())
135+
136+
})
101137
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module testdata.kubebuilder.io/internal_version
2+
3+
go 1.12
4+
5+
require (
6+
k8s.io/api v0.0.0-20190615205754-1d1b8b084b30
7+
k8s.io/apimachinery v0.0.0-20190612125636-6a5db36e93ad
8+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
3+
github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
4+
github.com/evanphx/json-patch v0.0.0-20190203023257-5858425f7550/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
5+
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
6+
github.com/gogo/protobuf v0.0.0-20171007142547-342cbe0a0415 h1:WSBJMqJbLxsn+bTCPyPYZfqHdJmc8MK4wrBjMft6BAM=
7+
github.com/gogo/protobuf v0.0.0-20171007142547-342cbe0a0415/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
8+
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
9+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
10+
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
11+
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf h1:+RRA9JqSOZFfKrOeqr2z77+8R2RKyh8PG66dcu1V0ck=
12+
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
13+
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
14+
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
15+
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
16+
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
17+
github.com/json-iterator/go v0.0.0-20180701071628-ab8a2e0c74be/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
18+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
19+
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
20+
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
21+
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
22+
github.com/onsi/gomega v0.0.0-20190113212917-5533ce8a0da3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
23+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
24+
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
25+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
26+
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
27+
golang.org/x/net v0.0.0-20190206173232-65e2d4e15006 h1:bfLnR+k0tq5Lqt6dflRLcZiz6UaXCMt3vhYJ1l4FQ80=
28+
golang.org/x/net v0.0.0-20190206173232-65e2d4e15006/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
29+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
30+
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
31+
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
32+
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
33+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
34+
golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db h1:6/JqlYfC1CCaLnGceQTI+sDGhC9UBSPAsBqI0Gun6kU=
35+
golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
36+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
37+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
38+
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
39+
gopkg.in/inf.v0 v0.9.0 h1:3zYtXIO92bvsdS3ggAdA8Gb4Azj0YU+TVY1uGYNFA8o=
40+
gopkg.in/inf.v0 v0.9.0/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
41+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
42+
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
43+
k8s.io/api v0.0.0-20190615205754-1d1b8b084b30 h1:/0Fr/sqn9cXa/R8CSlgq8Fy0Wp1wR2cNTSlHvrN78K4=
44+
k8s.io/api v0.0.0-20190615205754-1d1b8b084b30/go.mod h1:SR4nMi8IQTDnEi4768MsMCoZ9DyfRls7wy+TbRrFicA=
45+
k8s.io/apimachinery v0.0.0-20190612125636-6a5db36e93ad h1:x1lITOfDEbnzt8D1cZJsPbdnx/hnv28FxY2GKkxmxgU=
46+
k8s.io/apimachinery v0.0.0-20190612125636-6a5db36e93ad/go.mod h1:I4A+glKBHiTgiEjQiCCQfCAIcIMFGt291SmsvcrFzJA=
47+
k8s.io/klog v0.3.1 h1:RVgyDHY/kFKtLqh67NvEWIgkMneNoIrdkN0CxDSQc68=
48+
k8s.io/klog v0.3.1/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
49+
k8s.io/kube-openapi v0.0.0-20190228160746-b3a7cee44a30/go.mod h1:BXM9ceUBTj2QnfH2MK1odQs778ajze1RxcmP6S8RVVc=
50+
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 controller-gen crd paths=. output:dir=.
17+
18+
// +kubebuilder:skip
19+
package internal_version
20+
21+
import (
22+
corev1 "k8s.io/api/core/v1"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
)
25+
26+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
27+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
28+
29+
// CronJobSpec defines the desired state of CronJob
30+
type CronJobSpec struct {
31+
// The schedule in Cron format, see https://en.wikipedia.org/wiki/Cron.
32+
Schedule string
33+
}
34+
35+
// CronJobStatus defines the observed state of CronJob
36+
type CronJobStatus struct {
37+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
38+
// Important: Run "make" to regenerate code after modifying this file
39+
40+
// A list of pointers to currently running jobs.
41+
// +optional
42+
Active []corev1.ObjectReference
43+
44+
// Information when was the last time the job was successfully scheduled.
45+
// +optional
46+
LastScheduleTime *metav1.Time
47+
48+
// Information about the last time the job was successfully scheduled,
49+
// with microsecond precision.
50+
// +optional
51+
LastScheduleMicroTime *metav1.MicroTime
52+
}
53+
54+
// +kubebuilder:object:root=true
55+
// +kubebuilder:subresource:status
56+
// +kubebuilder:resource
57+
58+
// CronJob is the Schema for the cronjobs API
59+
type CronJob struct {
60+
/*
61+
*/
62+
metav1.TypeMeta
63+
metav1.ObjectMeta
64+
65+
Spec CronJobSpec
66+
Status CronJobStatus
67+
}
68+
69+
// +kubebuilder:object:root=true
70+
71+
// CronJobList contains a list of CronJob
72+
type CronJobList struct {
73+
metav1.TypeMeta
74+
metav1.ListMeta
75+
Items []CronJob
76+
}
77+
78+
func init() {
79+
SchemeBuilder.Register(&CronJob{}, &CronJobList{})
80+
}

0 commit comments

Comments
 (0)