Kubebuilder: Field Selector spec.nodeName does not work

Created on 28 Dec 2018  路  7Comments  路  Source: kubernetes-sigs/kubebuilder

fieldSelector := "spec.nodeName=minikube"
listOptions := &client.ListOptions{}
    if fieldSelector != "" {
        listOptions.SetFieldSelector(fieldSelector)
    }
    listOptions.Namespace = namespace
    podList := &corev1.PodList{}
    err := r.List(context.TODO(), listOptions, podList)

This returns the error

Index with name field:spec.nodeName does not exist

I tried to trace through and see the following in cachedReader.

The indexName = field:spec.nodeName and indexKey = /minikube

In thread_safe_store.go

```func (c *threadSafeMap) ByIndex(indexName, indexKey string) ([]interface{}, error) {
c.lock.RLock()
defer c.lock.RUnlock()

indexFunc := c.indexers[indexName]
if indexFunc == nil {
    return nil, fmt.Errorf("Index with name %s does not exist", indexName)
}

```

I see that the index is only on the namespace. How do i fix this or get around so that the index exists for the fields ?

lifecyclrotten

Most helpful comment

Following the logic in the book https://book.kubebuilder.io/cronjob-tutorial/controller-implementation.html#setup

Add this to your SetupWithManager

if err := mgr.GetFieldIndexer().IndexField(&corev1.Pod{}, "spec.nodeName", func(rawObj runtime.Object) []string {
        pod := rawObj.(*corev1.Pod)
        return []string{pod.Spec.NodeName}
    }); err != nil {
        return err
    }

All 7 comments

The only index created by default is on namespace. While you can manually add additional indexers to the same cache, it is likely easier to use an uncached client for those specific requests.

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

@fejta-bot: Closing this issue.

In response to this:

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/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.

Following the logic in the book https://book.kubebuilder.io/cronjob-tutorial/controller-implementation.html#setup

Add this to your SetupWithManager

if err := mgr.GetFieldIndexer().IndexField(&corev1.Pod{}, "spec.nodeName", func(rawObj runtime.Object) []string {
        pod := rawObj.(*corev1.Pod)
        return []string{pod.Spec.NodeName}
    }); err != nil {
        return err
    }

error: Index with name field:involvedObject.name does not exist
support controller-time鈥榮 client get definite pod event

_ = mgr.GetFieldIndexer().IndexField(context.Background(), &corev1.Event{}, "involvedObject.name", func(rawObj runtime.Object) []string {
event := rawObj.(*corev1.Event)
return []string{event.InvolvedObject.Name}
})

fieldSelector, err := fields.ParseSelector("involvedObject.name" + "=" + "zx-demo-8658bdb788-65h8c")
if err != nil {
fmt.Println(err.Error())
}
if err := mgr.GetClient().List(context.Background(), &events, client.MatchingFieldsSelector{fieldSelector}); err != nil {
fmt.Println(err.Error())
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

quanguachong picture quanguachong  路  3Comments

bingosummer picture bingosummer  路  6Comments

Adirio picture Adirio  路  6Comments

functicons picture functicons  路  4Comments

alok87 picture alok87  路  4Comments