Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controller-gen is not consistently picking up kubebuilder:default marker #967

Closed
ahmetb opened this issue May 21, 2024 · 2 comments
Closed
Labels
kind/bug Categorizes issue or PR as related to a bug. lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.

Comments

@ahmetb
Copy link
Member

ahmetb commented May 21, 2024

On controller-gen v0.13.0, I am observing that some types (I can't quite put my finger on it) don't have kubebuilder:default marker set to default the object to empty object ({}) honored.

I have a fairly simple repro below that sets //+kubebuilder:default:={} on the root level object's status field.

type Car struct { ...
// +groupName=example.com
// +kubebuilder:object:generate=true
// +kubebuilder:validation:Required
package v1alpha1

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"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: "example.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
)

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:scope=Cluster

type Car struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`
	Spec              CarSpec `json:"spec"`

	// +kubebuilder:default:={}
	Status CarStatus `json:"status,omitempty"`
}

type CarSpec struct{}

// +kubebuilder:validation:Enum=automatic;manual
type GearType string

type CarStatus struct {
	// +kubebuilder:default:=automatic
	Gear GearType `json:"name"`
}

//+kubebuilder:object:root=true

type CarList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`
	Items           []Car `json:"items"`
}

func init() {
	SchemeBuilder.Register(&Car{}, &CarList{})
}

The generated CRD does not have a status field that has a default: {} field:

          status:
            default: {}
generated CRD
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  annotations:
    controller-gen.kubebuilder.io/version: v0.13.0
  name: cars.example.com
spec:
  group: example.com
  names:
    kind: Car
    listKind: CarList
    plural: cars
    singular: car
  scope: Cluster
  versions:
  - name: v1alpha1
    schema:
      openAPIV3Schema:
        properties:
          apiVersion:
            description: 'APIVersion defines the versioned schema of this representation
              of an object. Servers should convert recognized schemas to the latest
              internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
            type: string
          kind:
            description: 'Kind is a string value representing the REST resource this
              object represents. Servers may infer this from the endpoint the client
              submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
            type: string
          metadata:
            type: object
          spec:
            type: object
          status:
            properties:
              name:
                default: automatic
                enum:
                - automatic
                - manual
                type: string
            required:
            - name
            type: object
        required:
        - spec
        type: object
    served: true
    storage: true
    subresources:
      status: {}

It's deterministic behavior, but I can't tell why the exact some marker on exact same field works on some types and not the others. 😭 Another type I have actually gets spec.validation.openAPIV3Schema.properties[status].default field populated.

/kind bug
/lifecycle frozen

@k8s-ci-robot k8s-ci-robot added lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. kind/bug Categorizes issue or PR as related to a bug. labels May 21, 2024
@ahmetb
Copy link
Member Author

ahmetb commented May 22, 2024

PEBKAC. It turns out two repos I was testing this were picking up controller-gen v0.13.0 vs v0.14.0 and the older one didn't have the commit: 2fac91e.
/close

@k8s-ci-robot
Copy link
Contributor

@ahmetb: Closing this issue.

In response to this:

PEBKAC. It turns out two repos I was testing this were picking up controller-gen v0.13.0 vs v0.14.0 and the older one didn't have the commit: 2fac91e.
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.
Projects
None yet
Development

No branches or pull requests

2 participants