Skip to content

Commit 5fc00ff

Browse files
Add unservedversion marker
Used to set `served=false` for an apiversion.
1 parent d6efdcd commit 5fc00ff

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
@@ -290,4 +293,23 @@ func (s Resource) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version s
290293
return nil
291294
}
292295

296+
// +controllertools:marker:generateHelp:category=CRD
297+
298+
// UnservedVersion does not serve this version.
299+
//
300+
// This is useful if you need to drop support for a version in favor of a newer version.
301+
type UnservedVersion struct{}
302+
303+
func (s UnservedVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
304+
for i := range crd.Versions {
305+
ver := &crd.Versions[i]
306+
if ver.Name != version {
307+
continue
308+
}
309+
ver.Served = false
310+
break
311+
}
312+
return nil
313+
}
314+
293315
// 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)