-
Notifications
You must be signed in to change notification settings - Fork 407
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 e49d50f
Showing
16 changed files
with
1,454 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
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 sharecache | ||
|
||
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,135 @@ | ||
/* | ||
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 sharecache | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/fields" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"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/sharecache/storage" | ||
) | ||
|
||
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(), | ||
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() | ||
|
||
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.ModifyWatchObject() | ||
}() | ||
|
||
assert.Nil(t, err) | ||
event := <-receive.ResultChan() | ||
assert.Equal(t, watch.Modified, event.Type) | ||
} |
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,83 @@ | ||
/* | ||
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 sharecache | ||
|
||
import ( | ||
v1 "k8s.io/api/core/v1" | ||
discovery "k8s.io/api/discovery/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apiserver/pkg/storage" | ||
|
||
ystorage "github.com/openyurtio/openyurt/pkg/yurthub/sharecache/storage" | ||
) | ||
|
||
var serviceGVR = &schema.GroupVersionResource{ | ||
Group: "", | ||
Version: "v1", | ||
Resource: "services", | ||
} | ||
|
||
var endpointSliceGVR = &schema.GroupVersionResource{ | ||
Group: "discovery.k8s.io", | ||
Version: "v1", | ||
Resource: "endpointslices", | ||
} | ||
|
||
type FakeCacheManager struct { | ||
cacheMap map[string]Interface | ||
resourceConfigMap map[string]*ResourceCacheConfig | ||
} | ||
|
||
func NewFakeCacheManager() *FakeCacheManager { | ||
return &FakeCacheManager{ | ||
map[string]Interface{ | ||
serviceGVR.String(): ystorage.NewFakeServiceStorage(), | ||
endpointSliceGVR.String(): ystorage.NewFakeEndpointSliceStorage(), | ||
}, | ||
map[string]*ResourceCacheConfig{ | ||
serviceGVR.String(): { | ||
KeyFunc: NamespaceKeyFunc, | ||
NewListFunc: func() runtime.Object { | ||
return &v1.ServiceList{} | ||
}, | ||
NewFunc: func() runtime.Object { | ||
return &v1.Service{} | ||
}, | ||
GetAttrsFunc: storage.DefaultNamespaceScopedAttr, | ||
}, | ||
endpointSliceGVR.String(): { | ||
KeyFunc: NamespaceKeyFunc, | ||
NewListFunc: func() runtime.Object { | ||
return &discovery.EndpointSliceList{} | ||
}, | ||
NewFunc: func() runtime.Object { | ||
return &discovery.EndpointSlice{} | ||
}, | ||
GetAttrsFunc: storage.DefaultNamespaceScopedAttr, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (fcm *FakeCacheManager) ResourceCacheConfig(gvr *schema.GroupVersionResource) (*ResourceCacheConfig, error) { | ||
return fcm.resourceConfigMap[gvr.String()], nil | ||
} | ||
|
||
func (fcm *FakeCacheManager) ResourceCache(gvr *schema.GroupVersionResource) (Interface, func(), error) { | ||
return fcm.cacheMap[gvr.String()], nil, nil | ||
} |
Oops, something went wrong.