Skip to content

Commit

Permalink
add ownerUID index for controllerrevisions
Browse files Browse the repository at this point in the history
Signed-off-by: shiyan2016 <[email protected]>
  • Loading branch information
shiyan2016 committed Sep 9, 2024
1 parent 4918768 commit 09f5315
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
5 changes: 5 additions & 0 deletions pkg/util/fieldindex/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func RegisterFieldIndexes(c cache.Cache) error {
if err = c.IndexField(context.TODO(), &v1.PersistentVolumeClaim{}, IndexNameForOwnerRefUID, ownerIndexFunc); err != nil {
return
}
// controller revision ownerReference
if err = c.IndexField(context.TODO(), &appsv1.ControllerRevision{}, IndexNameForOwnerRefUID, ownerIndexFunc); err != nil {

Check failure on line 69 in pkg/util/fieldindex/register.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: appsv1) (typecheck)

Check failure on line 69 in pkg/util/fieldindex/register.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: appsv1) (typecheck)

Check failure on line 69 in pkg/util/fieldindex/register.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: appsv1) (typecheck)

Check failure on line 69 in pkg/util/fieldindex/register.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: appsv1 (typecheck)

Check failure on line 69 in pkg/util/fieldindex/register.go

View workflow job for this annotation

GitHub Actions / unit-tests

undefined: appsv1
return
}

// ImagePullJob ownerReference
if err = c.IndexField(context.TODO(), &appsv1alpha1.ImagePullJob{}, IndexNameForOwnerRefUID, ownerIndexFunc); err != nil {
return
Expand Down
15 changes: 8 additions & 7 deletions pkg/util/history/controller_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ import (
apps "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"k8s.io/kubernetes/pkg/controller/history"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/openkruise/kruise/pkg/util/fieldindex"
)

// NewHistory returns an an instance of Interface that uses client to communicate with the API Server and lister to list ControllerRevisions.
Expand All @@ -44,16 +47,14 @@ type realHistory struct {
func (rh *realHistory) ListControllerRevisions(parent metav1.Object, selector labels.Selector) ([]*apps.ControllerRevision, error) {
// List all revisions in the namespace that match the selector
revisions := apps.ControllerRevisionList{}
err := rh.List(context.TODO(), &revisions, &client.ListOptions{Namespace: parent.GetNamespace(), LabelSelector: selector})
if err != nil {
return nil, err
opts := &client.ListOptions{
Namespace: parent.GetNamespace(),
FieldSelector: fields.SelectorFromSet(fields.Set{fieldindex.IndexNameForOwnerRefUID: string(parent.GetUID())}),
}
err := rh.List(context.TODO(), &revisions, opts)
var owned []*apps.ControllerRevision
for i := range revisions.Items {
ref := metav1.GetControllerOf(&revisions.Items[i])
if ref == nil || ref.UID == parent.GetUID() {
owned = append(owned, &revisions.Items[i])
}
owned = append(owned, &revisions.Items[i])
}
return owned, err
}
Expand Down

0 comments on commit 09f5315

Please sign in to comment.