Skip to content

Commit a653df4

Browse files
authored
Merge pull request #308 from munnerz/skipversion-marker
✨ Add skipversion marker to selectively exclude CRD versions
2 parents ba0eb80 + 5fc42b1 commit a653df4

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pkg/crd/markers/crd.go

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

4343
must(markers.MakeDefinition("kubebuilder:storageversion", markers.DescribesType, StorageVersion{})).
4444
WithHelp(StorageVersion{}.Help()),
45+
46+
must(markers.MakeDefinition("kubebuilder:skipversion", markers.DescribesType, SkipVersion{})).
47+
WithHelp(SkipVersion{}.Help()),
4548
}
4649

4750
// TODO: categories and singular used to be annotations types
@@ -165,6 +168,34 @@ func (s StorageVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, ver
165168

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

171+
// SkipVersion removes the particular version of the CRD from the CRDs spec.
172+
//
173+
// This is useful if you need to skip generating and listing version entries
174+
// for 'internal' resource versions, which typically exist if using the
175+
// Kubernetes upstream conversion-gen tool.
176+
type SkipVersion struct{}
177+
178+
func (s SkipVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
179+
if version == "" {
180+
// single-version, this is an invalid state
181+
return fmt.Errorf("cannot skip a version if there is only a single version")
182+
}
183+
var versions []apiext.CustomResourceDefinitionVersion
184+
// multi-version
185+
for i := range crd.Versions {
186+
ver := crd.Versions[i]
187+
if ver.Name == version {
188+
// skip the skipped version
189+
continue
190+
}
191+
versions = append(versions, ver)
192+
}
193+
crd.Versions = versions
194+
return nil
195+
}
196+
197+
// +controllertools:marker:generateHelp:category=CRD
198+
168199
// PrintColumn adds a column to "kubectl get" output for this CRD.
169200
type PrintColumn struct {
170201
// Name specifies the name of the column.

pkg/crd/markers/zz_generated.markerhelp.go

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

0 commit comments

Comments
 (0)