controller-tools
controller-tools copied to clipboard
Check package version against declared GroupVersion
Problem Statement
When inferring the version of a CRD from the package name:
https://github.com/kubernetes-sigs/controller-tools/blob/a57ec68d4aca081c0ead223796f18b7c6c3a61c1/pkg/crd/parser.go#L135-L145
a developer can make a mistake where they don't use the same value for the version for the declared group version in their go file. For example:
// +kubebuilder:object:generate=true
// +groupName=some.awesome.group.com
package v1beta1
import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)
var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "some.awesome.group.com", Version: "v1alpha1"}
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
Notice the mismatch between v1beta1, the package name, and the Version in GroupVersion.
I discovered this when referencing the GroupVersion for a kind that did not, in fact, have a v1alpha1 implementation.