Operator-sdk : Naming Convention in Spec and Status structs

12/20/2020

I am developing an operator and wondering is there a link or document where the naming best practices are described for Spec and Status fields? For example:

Where I can locate all the info related to comments for each field or parameter to set field optional or set the default value a shown following:

// MyAppStatus defines the observed state of MyAppSPec
type MyAppStatus struct {
        // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
        // Important: Run "make" to regenerate code after modifying this file
        AppCount              int32                       `json:"appCount,omitempty"`
        Apps                 map[string]string           `json:"apps,omitempty"`
}

How do I set the default value for AppCount? Also, is AppCount is correct in naming? Can I have naming indirection i.e can I set it to the following:

AppCount              int32                       `json:"count,omitempty"`

This will help me exposing different names in the YAML file but inside the code, I will refer to AppCount. Please let me know if there a document for the comments beast practices on each field and fields flag such as omit empty or optional?

From the perspective of the comments, I have seen many operators refer to kUbeBuilder as shown below:

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

Where I can find the details of the above and when to use them?

-- drifter
kubernetes
operator-sdk

1 Answer

12/21/2020

You can find the docs on controller-gen's annotations at https://book.kubebuilder.io/reference/markers.html (read the subsections). omitempty means the field is optional. The JSON name and struct name should generally be the same (except for case) unless you have a very good reason not to.

-- coderanger
Source: StackOverflow