Skip to content

Commit 92e95c1

Browse files
authored
Merge pull request #403 from rajathagasthya/crd-serve
✨ Add kubebuilder:unservedversion CRD marker
2 parents 1c6d384 + 5fc00ff commit 92e95c1

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

pkg/crd/markers/crd.go

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

4646
must(markers.MakeDefinition("kubebuilder:skipversion", markers.DescribesType, SkipVersion{})).
4747
WithHelp(SkipVersion{}.Help()),
48+
49+
must(markers.MakeDefinition("kubebuilder:unservedversion", markers.DescribesType, UnservedVersion{})).
50+
WithHelp(UnservedVersion{}.Help()),
4851
}
4952

5053
// TODO: categories and singular used to be annotations types
@@ -293,4 +296,23 @@ func (s Resource) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version s
293296
return nil
294297
}
295298

299+
// +controllertools:marker:generateHelp:category=CRD
300+
301+
// UnservedVersion does not serve this version.
302+
//
303+
// This is useful if you need to drop support for a version in favor of a newer version.
304+
type UnservedVersion struct{}
305+
306+
func (s UnservedVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
307+
for i := range crd.Versions {
308+
ver := &crd.Versions[i]
309+
if ver.Name != version {
310+
continue
311+
}
312+
ver.Served = false
313+
break
314+
}
315+
return nil
316+
}
317+
296318
// NB(directxman12): singular was historically distinct, so we keep it here for backwards compat

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.

pkg/crd/spec.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@ func (p *Parser) NeedCRDFor(groupKind schema.GroupKind, maxDescLen *int) {
151151
packages[0].AddError(fmt.Errorf("CRD for %s has no storage version", groupKind))
152152
}
153153

154+
served := false
155+
for _, ver := range crd.Spec.Versions {
156+
if ver.Served {
157+
served = true
158+
break
159+
}
160+
}
161+
if !served {
162+
// just add the error to the first relevant package for this CRD,
163+
// since there's no specific error location
164+
packages[0].AddError(fmt.Errorf("CRD for %s with version(s) %v does not serve any version", groupKind, crd.Spec.Versions))
165+
}
166+
154167
// NB(directxman12): CRD's status doesn't have omitempty markers, which means things
155168
// get serialized as null, which causes the validator to freak out. Manually set
156169
// these to empty till we get a better solution.

0 commit comments

Comments
 (0)