-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fbfa01f
commit 8ecdab1
Showing
13 changed files
with
1,289 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
Copyright 2024 The OpenYurt 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 multiplexer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/watch" | ||
kstorage "k8s.io/apiserver/pkg/storage" | ||
"k8s.io/apiserver/pkg/storage/cacher" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
) | ||
|
||
type Interface interface { | ||
Watch(ctx context.Context, key string, opts kstorage.ListOptions) (watch.Interface, error) | ||
GetList(ctx context.Context, key string, opts kstorage.ListOptions, listObj runtime.Object) error | ||
} | ||
|
||
type ResourceCacheConfig struct { | ||
KeyFunc func(runtime.Object) (string, error) | ||
NewFunc func() runtime.Object | ||
NewListFunc func() runtime.Object | ||
GetAttrsFunc kstorage.AttrFunc | ||
NamespaceScoped bool | ||
} | ||
|
||
func NewResourceCache( | ||
s kstorage.Interface, | ||
resource *schema.GroupVersionResource, | ||
config *ResourceCacheConfig) (Interface, func(), error) { | ||
|
||
cacheConfig := cacher.Config{ | ||
Storage: s, | ||
Versioner: kstorage.APIObjectVersioner{}, | ||
GroupResource: resource.GroupResource(), | ||
KeyFunc: config.KeyFunc, | ||
NewFunc: config.NewFunc, | ||
NewListFunc: config.NewListFunc, | ||
GetAttrsFunc: config.GetAttrsFunc, | ||
Codec: scheme.Codecs.LegacyCodec(resource.GroupVersion()), | ||
} | ||
|
||
cacher, err := cacher.NewCacherFromConfig(cacheConfig) | ||
if err != nil { | ||
return nil, func() {}, fmt.Errorf("failed to new cacher from config, error: %v", err) | ||
} | ||
|
||
var once sync.Once | ||
destroyFunc := func() { | ||
once.Do(func() { | ||
cacher.Stop() | ||
}) | ||
} | ||
|
||
return cacher, destroyFunc, nil | ||
} |
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,143 @@ | ||
/* | ||
Copyright 2024 The OpenYurt 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 multiplexer | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/fields" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/apiserver/pkg/endpoints/request" | ||
"k8s.io/apiserver/pkg/registry/generic/registry" | ||
"k8s.io/apiserver/pkg/storage" | ||
|
||
ystorage "github.com/openyurtio/openyurt/pkg/yurthub/multiplexer/storage" | ||
) | ||
|
||
var serviceGVR = &schema.GroupVersionResource{ | ||
Group: "", | ||
Version: "v1", | ||
Resource: "services", | ||
} | ||
|
||
var serviceKeyFunc = func(obj runtime.Object) (string, error) { | ||
accessor, err := meta.Accessor(obj) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return registry.NamespaceKeyFunc(request.WithNamespace(request.NewContext(), accessor.GetNamespace()), "", accessor.GetName()) | ||
} | ||
|
||
var newServiceFunc = func() runtime.Object { | ||
return &v1.Service{} | ||
} | ||
|
||
var newServiceListFunc = func() runtime.Object { | ||
return &v1.ServiceList{} | ||
} | ||
|
||
func TestResourceCache_GetList(t *testing.T) { | ||
cache, _, err := NewResourceCache( | ||
ystorage.NewFakeServiceStorage([]v1.Service{*newService(metav1.NamespaceSystem, "coredns")}), | ||
serviceGVR, | ||
&ResourceCacheConfig{ | ||
serviceKeyFunc, | ||
newServiceFunc, | ||
newServiceListFunc, | ||
storage.DefaultNamespaceScopedAttr, | ||
true, | ||
}, | ||
) | ||
|
||
assert.Nil(t, err) | ||
assertCacheGetList(t, cache) | ||
} | ||
|
||
func mockListOptions() storage.ListOptions { | ||
return storage.ListOptions{ | ||
ResourceVersion: "100", | ||
Recursive: true, | ||
Predicate: storage.SelectionPredicate{ | ||
Label: labels.Everything(), | ||
Field: fields.Everything(), | ||
}, | ||
} | ||
} | ||
|
||
func assertCacheGetList(t testing.TB, cache Interface) { | ||
t.Helper() | ||
|
||
serviceList := &v1.ServiceList{} | ||
err := cache.GetList(context.Background(), "", mockListOptions(), serviceList) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, 1, len(serviceList.Items)) | ||
} | ||
|
||
func TestResourceCache_Watch(t *testing.T) { | ||
fakeStorage := ystorage.NewFakeServiceStorage([]v1.Service{*newService(metav1.NamespaceSystem, "coredns")}) | ||
|
||
cache, _, err := NewResourceCache( | ||
fakeStorage, | ||
serviceGVR, | ||
&ResourceCacheConfig{ | ||
serviceKeyFunc, | ||
newServiceFunc, | ||
newServiceListFunc, | ||
storage.DefaultNamespaceScopedAttr, | ||
true, | ||
}, | ||
) | ||
|
||
assert.Nil(t, err) | ||
assertCacheWatch(t, cache, fakeStorage) | ||
} | ||
|
||
func mockWatchOptions() storage.ListOptions { | ||
var sendInitialEvents = true | ||
|
||
return storage.ListOptions{ | ||
ResourceVersion: "100", | ||
Predicate: storage.SelectionPredicate{ | ||
Label: labels.Everything(), | ||
Field: fields.Everything(), | ||
}, | ||
Recursive: true, | ||
SendInitialEvents: &sendInitialEvents, | ||
} | ||
} | ||
|
||
func assertCacheWatch(t testing.TB, cache Interface, fs *ystorage.FakeServiceStorage) { | ||
receive, err := cache.Watch(context.TODO(), "", mockWatchOptions()) | ||
|
||
go func() { | ||
fs.AddWatchObject(newService(metav1.NamespaceSystem, "coredns2")) | ||
}() | ||
|
||
assert.Nil(t, err) | ||
event := <-receive.ResultChan() | ||
assert.Equal(t, watch.Added, event.Type) | ||
} |
Oops, something went wrong.