When I create a Component's custom resource, the component is well created and the MetaData well defined (Kind, ApiVersion)
oc get cp/fruit-client -o yaml
apiVersion: component.k8s.io/v1alpha1
kind: Component
metadata:
name: fruit-client
namespace: my-spring-app
spec:
deployment: innerloop
exposeService: true
runtime: springboot
version: 1.5.16
but when the reconcile's function of the controller fetch the Component, then the Kind, ApiVersion fields are empty
What is the root cause of this issue ?

okd : 3.11
operator-sdk : v0.1.1+git
Code of the API : https://github.com/snowdrop/component-operator/tree/operator-0.1.1/pkg/apis
Code of the controller : https://github.com/snowdrop/component-operator/blob/operator-0.1.1/pkg/controller/component/handler.go#L94
I have debugged the code and what I can say is that if the Component has already been cached, then we will get it with the fields set but if the obj has not been yet cached (= when it is created the first time), then the fields are empty ....
which operator sdk cli version did you use to generate the project?
generate
As mentioned into my ticket, I used v0.1.1 to regenerate the API of my project.
BTW, I confirm that when the first time the object is retrieved [1] from the cache of the reader, then the fields are empty.
// Get checks the indexer for the object and writes a copy of it if found
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out runtime.Object) error {
storeKey := objectKeyToStoreKey(key)
// Lookup the object from the indexer cache
obj, exists, err := c.indexer.GetByKey(storeKey) // TypeMeta/Kind and ApiVersion = ""

Question :
deepCopy process ? controller-runtime project ?cc @hasbro17 You might know more about this.
2. Why is it is important to you that these fields are set?
This is needed by some OpenShift's resources when they are created (DeploymentConfig, Route, ImageStream,....)
Error DeploymentConfig.apps.openshift.io "fruit-client" is invalid: metadata.ownerReferences.kind: Invalid value: "": kind must not be empty is generated here - https://goo.gl/SuzM9p

- if this is something that you can not live without then you will need to submit a bug to controller runtime.
Ticket created : https://github.com/kubernetes-sigs/controller-runtime/issues/202
@cmoulliard pushed a PR to controller-runtime kubernetes-sigs/controller-runtime#203
Well done @shawn-hurley :-) I will retest it.
I had the same issue. When I create my CR, which should create a deployment, the controller is complaining about that deployment has missing field in its ownerReferences:
"[metadata.ownerReferences.apiVersion: Invalid value: \"\": version must not be empty, metadata.ownerReferences.kind: Invalid value: \"\": kind must not be empty]"
I added some debug code to my controller and found that after I called this function:
err := r.client.Get(context.TODO(), request.NamespacedName, my-cr-object), my-cr-object's API_Version and Kind will always be "", while other fields are correct.
I looked at the actual my-cr-object in my k8s cluster and all fields are correct there. So I suspect that there is some bug in this part: r.client.Get(context.TODO(), request.NamespacedName, my-cr-object)
Waiting on the next release for the controller-runtime that includes the fix for this:
https://github.com/kubernetes-sigs/controller-runtime/pull/212
The PR of Shawn has been merged top of the master branch of controller-runtime but v0.1.8 is not yet out.
I created a ticket to have more info about release's cadence and milestones of controller-runtime. https://github.com/kubernetes-sigs/controller-runtime/issues/231
Issues go stale after 90d of inactivity.
Mark the issue as fresh by commenting /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.
Exclude this issue from closing by commenting /lifecycle frozen.
If this issue is safe to close now please do so with /close.
/lifecycle stale
@cmoulliard do you know of the progress of this upstream?
Stale issues rot after 30d of inactivity.
Mark the issue as fresh by commenting /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.
Exclude this issue from closing by commenting /lifecycle frozen.
If this issue is safe to close now please do so with /close.
/lifecycle rotten
/remove-lifecycle stale
This was resolved in an older release of controller-runtime, so this should be resolved in the SDK as well
/close
@AlexNPavel: Closing this issue.
In response to this:
This was resolved in an older release of controller-runtime, so this should be resolved in the SDK as well
/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/test-infra repository.
@AlexNPavel are you sure this has been resolved? I'm hitting the same issue on operator-sdk v0.10.0 / controller-runtime v0.1.12
Sorry, but this issue has not yet been resolved. A workaround is to check the Kind is empty, and if so to do a DeepCopy and set it explicitly:
if cr.Kind == "" {
cr = cr.DeepCopy()
cr.APIVersion, cr.Kind = CustomResourceGVK.ToAPIVersionAndKind()
}
HI @tliron,
Could you please, let us know if you check it with the latest version of SDK 0.15.1? Note that it was solved in the latest versions according to @AlexNPavel.
Thanks for the attention, @camilamacedo86 ! Upon closer look I don't think the issue is in the Operator SDK, but rather in k8s.io/client-go v0.17.1. Do you have insight into what is going on? How was this underlying issue solved in the Operator SDK?
Hi @tliron,
The SDK uses the controller-runtime as dep. The controller-runtime is who is responsible to provide the client which will be used in the controllers to CRUD the resources in the cluster. In this way see:
@AlexNPavel answers;
This was resolved in an older release of controller-runtime, so this should be resolved in the SDK as well
In this way, in the latest versions of SDK, it is using the latest versions of controller-runtime which has this fixed.
@tliron I am wondering where you are putting
if cr.Kind == "" {
cr = cr.DeepCopy()
cr.APIVersion, cr.Kind = CustomResourceGVK.ToAPIVersionAndKind()
}
For us we are stuck with Operator 0.17.1 which suppose to fix the issue as it's using sigs.k8s.io/controller-runtime 0.5.8 but still kind and APIVersion are empty.
@shinji62 I put that code immediately after I do a "get" from the indexer.
For me I don't get the ToAPIVersionAndKind function.. not sure how to get that.
Sorry, it is pseudo-code. You need to get you APIVersion-Group-Kind combination from somewhere. You can even provide them as strings directly in case you know exactly which resource you are retrieving.