-
Notifications
You must be signed in to change notification settings - Fork 890
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5779 from ctripcloud/work-index
Add index for work
- Loading branch information
Showing
11 changed files
with
200 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package helper | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/klog/v2" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1" | ||
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" | ||
) | ||
|
||
// IndexWork creates index for Work. | ||
func IndexWork(ctx context.Context, mgr ctrl.Manager) error { | ||
err := mgr.GetFieldIndexer().IndexField(ctx, &workv1alpha1.Work{}, workv1alpha2.ResourceBindingPermanentIDLabel, | ||
IndexerFuncBasedOnLabel(workv1alpha2.ResourceBindingPermanentIDLabel)) | ||
if err != nil { | ||
klog.Errorf("failed to create index for work, err: %v", err) | ||
return err | ||
} | ||
err = mgr.GetFieldIndexer().IndexField(ctx, &workv1alpha1.Work{}, workv1alpha2.ClusterResourceBindingPermanentIDLabel, | ||
IndexerFuncBasedOnLabel(workv1alpha2.ClusterResourceBindingPermanentIDLabel)) | ||
if err != nil { | ||
klog.Errorf("failed to create index for work, err: %v", err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// IndexerFuncBasedOnLabel returns an IndexerFunc used to index resource with the given key as label key. | ||
func IndexerFuncBasedOnLabel(key string) client.IndexerFunc { | ||
return func(obj client.Object) []string { | ||
val, ok := obj.GetLabels()[key] | ||
if !ok { | ||
return nil | ||
} | ||
return []string{val} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package helper | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func TestIndexerFuncBasedOnLabel(t *testing.T) { | ||
type args struct { | ||
key string | ||
obj client.Object | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []string | ||
}{ | ||
{ | ||
name: "cache hit", | ||
args: args{ | ||
key: "a", | ||
obj: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
"a": "a", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: []string{"a"}, | ||
}, | ||
{ | ||
name: "cache missed", | ||
args: args{ | ||
key: "b", | ||
obj: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
"a": "a", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
fn := IndexerFuncBasedOnLabel(tt.args.key) | ||
if !assert.NotNil(t, fn) { | ||
t.FailNow() | ||
} | ||
assert.Equalf(t, tt.want, fn(tt.args.obj), "IndexerFuncBasedOnLabel(%v)", tt.args.key) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.