Skip to content

Commit

Permalink
Index PodGroupName (#1953)
Browse files Browse the repository at this point in the history
Co-authored-by: Gabe <[email protected]>
  • Loading branch information
k8s-infra-cherrypick-robot and gabesaba authored Apr 5, 2024
1 parent 61e7426 commit 406a088
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
38 changes: 38 additions & 0 deletions pkg/controller/jobs/pod/indexer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pod

import (
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
PodGroupNameCacheKey = "PodGroupNameCacheKey"
)

func IndexPodGroupName(o client.Object) []string {
pod, ok := o.(*corev1.Pod)
if !ok {
return nil
}

if labelValue, exists := pod.GetLabels()[GroupNameLabel]; exists {
return []string{labelValue}
}
return nil
}
16 changes: 11 additions & 5 deletions pkg/controller/jobs/pod/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,13 @@ func (p *Pod) Stop(ctx context.Context, c client.Client, _ []podset.PodSetInfo,
}

func SetupIndexes(ctx context.Context, indexer client.FieldIndexer) error {
return jobframework.SetupWorkloadOwnerIndex(ctx, indexer, gvk)
if err := indexer.IndexField(ctx, &corev1.Pod{}, PodGroupNameCacheKey, IndexPodGroupName); err != nil {
return err
}
if err := jobframework.SetupWorkloadOwnerIndex(ctx, indexer, gvk); err != nil {
return err
}
return nil
}

func CanSupportIntegration(opts ...jobframework.Option) (bool, error) {
Expand All @@ -496,8 +502,8 @@ func (p *Pod) Finalize(ctx context.Context, c client.Client) error {
if groupName == "" {
podsInGroup.Items = append(podsInGroup.Items, *p.Object().(*corev1.Pod))
} else {
if err := c.List(ctx, &podsInGroup, client.MatchingLabels{
GroupNameLabel: groupName,
if err := c.List(ctx, &podsInGroup, client.MatchingFields{
PodGroupNameCacheKey: groupName,
}, client.InNamespace(p.pod.Namespace)); err != nil {
return err
}
Expand Down Expand Up @@ -611,8 +617,8 @@ func (p *Pod) Load(ctx context.Context, c client.Client, key *types.NamespacedNa
// and update the expectations after we've retrieved active pods from the store.
p.satisfiedExcessPods = p.excessPodExpectations.Satisfied(ctrl.LoggerFrom(ctx), *key)

if err := c.List(ctx, &p.list, client.MatchingLabels{
GroupNameLabel: key.Name,
if err := c.List(ctx, &p.list, client.MatchingFields{
PodGroupNameCacheKey: key.Name,
}, client.InNamespace(key.Namespace)); err != nil {
return false, err
}
Expand Down

0 comments on commit 406a088

Please sign in to comment.