-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Why do Objects sometimes have empty TypeMeta
?
#1735
Comments
Previously discussed in #1517 (comment) but that also doesn't really cover the "why". |
I think this should be closed as a dup as we've already established we don't plan to implement a workaround here. |
I'm fine with closing this, I'm just trying to understand why this is happening (what makes it populated and what makes it empty). And if there's a way I can get these objects to be consistently one or the other. Without knowing the above can occur, one might be tempted to do something like this: UpdateFunc: func(e event.UpdateEvent) bool {
if e.ObjectNew.GetObjectKind().GroupVersionKind().Kind == "Pod" {
return true
}
return e.ObjectOld.GetGeneration() != e.ObjectNew.GetGeneration()
}, where you ignore status changes for one kind and not the other (which seems reasonable - correct me if that's wrong). Today this will sometimes work and sometimes not which makes it really tough to debug. |
Agreed it is an unfortunate footgun :-( I think the only way you're going to get a "why" is to just read all the relevant client-go code, which is a big task. |
FWIW the following gets consistent behavior:
utilruntime.Must(apps.AddToScheme(scheme))
func MyPredicate(scheme *runtime.Scheme) predicate.Predicate {
return predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
objOld, _ := addGVK(e.ObjectOld, scheme)
objNew, _ := addGVK(e.ObjectNew, scheme)
if objNew.GetObjectKind().GroupVersionKind().Kind == "Pod" {
return true
}
return e.ObjectOld.GetGeneration() != e.ObjectNew.GetGeneration()
},
DeleteFunc: func(e event.DeleteEvent) bool {
return true
},
}
} where func addGVK(obj client.Object, scheme *runtime.Scheme) (client.Object, error) {
gvks, _, err := scheme.ObjectKinds(obj)
if err != nil {
return obj, fmt.Errorf("missing apiVersion or kind and cannot assign it; %w", err)
}
for _, gvk := range gvks {
if len(gvk.Kind) == 0 {
continue
}
if len(gvk.Version) == 0 || gvk.Version == runtime.APIVersionInternal {
continue
}
obj.GetObjectKind().SetGroupVersionKind(gvk)
break
}
return obj, nil
}
func (r *MykindReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&grpv1alpha1.Mykind{}).
Owns(&core.Pod{}).
WithEventFilter(predicates.MyPredicate(r.Scheme)).
Complete(r)
} Result:
|
Actually you can do even simpler: if _, ok := e.Object.(*core.Pod); !ok {
fmt.Printf("Not Pod\n")
} else {
fmt.Printf("Pod\n")
} |
I built a controller that tracks a resource type called
Mykind
which creates/updates/deletesPods
.I created a predicate function as follows:
Looking at the output logs I noticed that some event objects don't have
TypeMeta
populated while others do:Why is that the case?
Looked through the repos and found a few related issues:
None of which seem to answer why this occurs.
The text was updated successfully, but these errors were encountered: