Skip to content

Commit 5fdf3d2

Browse files
Syspretor玖宇
andauthored
Enhancement: support to try by apireader client when errors occur in webhook (#5177)
Signed-off-by: 玖宇 <guotongyu.gty@alibaba-inc.com> Co-authored-by: 玖宇 <guotongyu.gty@alibaba-inc.com>
1 parent 7abd1cd commit 5fdf3d2

26 files changed

Lines changed: 128 additions & 82 deletions

cmd/webhook/app/webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func handle() {
164164
}
165165

166166
// register admission handlers
167-
handler.Register(mgr, mgr.GetClient(), setupLog)
167+
handler.Register(mgr, setupLog)
168168

169169
// register pod mutating handlers
170170
err = plugins.RegisterMutatingHandlers(mgr.GetClient())

pkg/application/inject/fuse/injector_runtime_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func TestInjectList(t *testing.T) {
219219
if err != nil {
220220
t.Errorf("testcase %s failed due to error %v", testcase.name, err)
221221
}
222-
runtimeInfo.SetClient(fakeClient)
222+
runtimeInfo.SetAPIReader(fakeClient)
223223
runtimeInfos[pvc] = runtimeInfo
224224
}
225225

@@ -261,7 +261,7 @@ func TestInjectUnstructured(t *testing.T) {
261261
if err != nil {
262262
t.Errorf("testcase %s failed due to error %v", name, err)
263263
}
264-
runtimeInfo.SetClient(fakeClient)
264+
runtimeInfo.SetAPIReader(fakeClient)
265265
runtimeInfos[name] = runtimeInfo
266266
pod := corev1.Pod{
267267
TypeMeta: metav1.TypeMeta{
@@ -330,7 +330,7 @@ func TestInjectObject(t *testing.T) {
330330
if err != nil {
331331
t.Errorf("testcase %s failed due to error %v", name, err)
332332
}
333-
runtimeInfo.SetClient(fakeClient)
333+
runtimeInfo.SetAPIReader(fakeClient)
334334
runtimeInfos[name] = runtimeInfo
335335

336336
deploy := appsv1.Deployment{

pkg/application/inject/fuse/injector_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ func TestInjectPod(t *testing.T) {
11581158
if err != nil {
11591159
t.Errorf("testcase %s failed due to error %v", testcase.name, err)
11601160
}
1161-
runtimeInfo.SetClient(fakeClient)
1161+
runtimeInfo.SetAPIReader(fakeClient)
11621162
runtimeInfos[pvc] = runtimeInfo
11631163
}
11641164

@@ -1448,7 +1448,7 @@ func TestSkipInjectPod(t *testing.T) {
14481448
if err != nil {
14491449
t.Errorf("testcase %s failed due to error %v", testcase.name, err)
14501450
}
1451-
runtimeInfo.SetClient(fakeClient)
1451+
runtimeInfo.SetAPIReader(fakeClient)
14521452
runtimeInfos[pvc] = runtimeInfo
14531453
}
14541454

@@ -2375,7 +2375,7 @@ func TestInjectPodWithMultiplePVC(t *testing.T) {
23752375
if err != nil {
23762376
t.Errorf("testcase %s failed due to error %v", testcase.name, err)
23772377
}
2378-
runtimeInfo.SetClient(fakeClient)
2378+
runtimeInfo.SetAPIReader(fakeClient)
23792379
runtimeInfos[pvc] = runtimeInfo
23802380
}
23812381

@@ -2839,7 +2839,7 @@ func TestInjectPodWithDatasetSubPath(t *testing.T) {
28392839
if err != nil {
28402840
t.Errorf("testcase %s failed due to error %v", testcase.name, err)
28412841
}
2842-
runtimeInfo.SetClient(fakeClient)
2842+
runtimeInfo.SetAPIReader(fakeClient)
28432843
runtimeInfos[pvc] = runtimeInfo
28442844
}
28452845

@@ -4120,7 +4120,7 @@ func TestInjectPodUnprivileged(t *testing.T) {
41204120
if err != nil {
41214121
t.Errorf("testcase %s failed due to error %v", testcase.name, err)
41224122
}
4123-
runtimeInfo.SetClient(fakeClient)
4123+
runtimeInfo.SetAPIReader(fakeClient)
41244124
runtimeInfos[pvc] = runtimeInfo
41254125
}
41264126

@@ -5227,7 +5227,7 @@ func TestInjectPodWithInitContainer(t *testing.T) {
52275227
if err != nil {
52285228
t.Errorf("testcase %s failed due to error %v", testcase.name, err)
52295229
}
5230-
runtimeInfo.SetClient(fakeClient)
5230+
runtimeInfo.SetAPIReader(fakeClient)
52315231
runtimeInfos[pvc] = runtimeInfo
52325232
}
52335233

@@ -6205,7 +6205,7 @@ func TestInjectPodWithEnabledFUSEMetrics(t *testing.T) {
62056205
if err != nil {
62066206
t.Errorf("testcase %s failed due to error %v", testcase.name, err)
62076207
}
6208-
runtimeInfo.SetClient(fakeClient)
6208+
runtimeInfo.SetAPIReader(fakeClient)
62096209
runtimeInfos[pvc] = runtimeInfo
62106210
}
62116211

pkg/common/webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ const (
3535
type AdmissionHandler interface {
3636
admission.Handler
3737

38-
Setup(client client.Client, decoder *admission.Decoder)
38+
Setup(client client.Client, reader client.Reader, decoder *admission.Decoder)
3939
}

pkg/ddc/alluxio/cache_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestQueryCacheStatus(t *testing.T) {
5050
defer patch1.Reset()
5151

5252
patch2 := ApplyFunc(utils.GetDataset,
53-
func(_ client.Client, _ string, _ string) (*datav1alpha1.Dataset, error) {
53+
func(_ client.Reader, _ string, _ string) (*datav1alpha1.Dataset, error) {
5454
d := &datav1alpha1.Dataset{
5555
Status: datav1alpha1.DatasetStatus{
5656
UfsTotal: "52.18MiB",
@@ -95,7 +95,7 @@ func TestQueryCacheStatus(t *testing.T) {
9595
defer patch1.Reset()
9696

9797
patch2 := ApplyFunc(utils.GetDataset,
98-
func(_ client.Client, _ string, _ string) (*datav1alpha1.Dataset, error) {
98+
func(_ client.Reader, _ string, _ string) (*datav1alpha1.Dataset, error) {
9999
d := &datav1alpha1.Dataset{
100100
Status: datav1alpha1.DatasetStatus{
101101
UfsTotal: "[Calculating]",
@@ -132,7 +132,7 @@ func TestQueryCacheStatus(t *testing.T) {
132132
defer patch1.Reset()
133133

134134
patch2 := ApplyFunc(utils.GetDataset,
135-
func(_ client.Client, _ string, _ string) (*datav1alpha1.Dataset, error) {
135+
func(_ client.Reader, _ string, _ string) (*datav1alpha1.Dataset, error) {
136136
d := &datav1alpha1.Dataset{
137137
Status: datav1alpha1.DatasetStatus{
138138
UfsTotal: "",

pkg/ddc/alluxio/shutdown_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func TestAlluxioEngineCleanupCache(t *testing.T) {
390390
defer patch1.Reset()
391391

392392
patch2 := ApplyFunc(utils.GetDataset,
393-
func(_ client.Client, _ string, _ string) (*datav1alpha1.Dataset, error) {
393+
func(_ client.Reader, _ string, _ string) (*datav1alpha1.Dataset, error) {
394394
d := &datav1alpha1.Dataset{
395395
Status: datav1alpha1.DatasetStatus{
396396
UfsTotal: "19.07MiB",

pkg/ddc/alluxio/status_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func TestCheckAndUpdateRuntimeStatus(t *testing.T) {
191191
defer patch1.Reset()
192192

193193
patch2 := ApplyFunc(utils.GetDataset,
194-
func(_ client.Client, _ string, _ string) (*datav1alpha1.Dataset, error) {
194+
func(_ client.Reader, _ string, _ string) (*datav1alpha1.Dataset, error) {
195195
d := &datav1alpha1.Dataset{
196196
Status: datav1alpha1.DatasetStatus{
197197
UfsTotal: "19.07MiB",

pkg/ddc/base/runtime.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ type RuntimeInfoInterface interface {
103103

104104
GetFuseContainerTemplate() (template *common.FuseInjectionTemplate, err error)
105105

106-
SetClient(client client.Client)
106+
SetAPIReader(apiReader client.Reader)
107107

108108
GetMetadataList() []datav1alpha1.Metadata
109109

@@ -140,7 +140,7 @@ type RuntimeInfo struct {
140140
// Check if the deprecated PV naming style is used
141141
deprecatedPVName bool
142142

143-
client client.Client
143+
apiReader client.Reader
144144

145145
annotations map[string]string
146146

@@ -380,8 +380,8 @@ func (info *RuntimeInfo) IsDeprecatedPVName() bool {
380380
return info.deprecatedPVName
381381
}
382382

383-
func (info *RuntimeInfo) SetClient(client client.Client) {
384-
info.client = client
383+
func (info *RuntimeInfo) SetAPIReader(apiReader client.Reader) {
384+
info.apiReader = apiReader
385385
}
386386

387387
func convertToTieredstoreInfo(tieredstore datav1alpha1.TieredStore) (TieredStoreInfo, error) {
@@ -445,8 +445,8 @@ func convertToTieredstoreInfo(tieredstore datav1alpha1.TieredStore) (TieredStore
445445
}
446446

447447
// GetRuntimeInfo gets the RuntimeInfo according to name and namespace of it
448-
func GetRuntimeInfo(client client.Client, name, namespace string) (runtimeInfo RuntimeInfoInterface, err error) {
449-
dataset, err := utils.GetDataset(client, name, namespace)
448+
func GetRuntimeInfo(reader client.Reader, name, namespace string) (runtimeInfo RuntimeInfoInterface, err error) {
449+
dataset, err := utils.GetDataset(reader, name, namespace)
450450
if err != nil {
451451
return runtimeInfo, err
452452
}
@@ -457,7 +457,7 @@ func GetRuntimeInfo(client client.Client, name, namespace string) (runtimeInfo R
457457
}
458458
switch runtimeType {
459459
case common.AlluxioRuntime:
460-
alluxioRuntime, err := utils.GetAlluxioRuntime(client, name, namespace)
460+
alluxioRuntime, err := utils.GetAlluxioRuntime(reader, name, namespace)
461461
if err != nil {
462462
return runtimeInfo, err
463463
}
@@ -473,7 +473,7 @@ func GetRuntimeInfo(client client.Client, name, namespace string) (runtimeInfo R
473473
runtimeInfo.SetFuseNodeSelector(alluxioRuntime.Spec.Fuse.NodeSelector)
474474
runtimeInfo.SetupFuseCleanPolicy(alluxioRuntime.Spec.Fuse.CleanPolicy)
475475
case common.JindoRuntime:
476-
jindoRuntime, err := utils.GetJindoRuntime(client, name, namespace)
476+
jindoRuntime, err := utils.GetJindoRuntime(reader, name, namespace)
477477
if err != nil {
478478
return runtimeInfo, err
479479
}
@@ -490,7 +490,7 @@ func GetRuntimeInfo(client client.Client, name, namespace string) (runtimeInfo R
490490
runtimeInfo.SetFuseNodeSelector(jindoRuntime.Spec.Fuse.NodeSelector)
491491
runtimeInfo.SetupFuseCleanPolicy(jindoRuntime.Spec.Fuse.CleanPolicy)
492492
case common.GooseFSRuntime:
493-
goosefsRuntime, err := utils.GetGooseFSRuntime(client, name, namespace)
493+
goosefsRuntime, err := utils.GetGooseFSRuntime(reader, name, namespace)
494494
if err != nil {
495495
return runtimeInfo, err
496496
}
@@ -506,7 +506,7 @@ func GetRuntimeInfo(client client.Client, name, namespace string) (runtimeInfo R
506506
runtimeInfo.SetFuseNodeSelector(goosefsRuntime.Spec.Fuse.NodeSelector)
507507
runtimeInfo.SetupFuseCleanPolicy(goosefsRuntime.Spec.Fuse.CleanPolicy)
508508
case common.JuiceFSRuntime:
509-
juicefsRuntime, err := utils.GetJuiceFSRuntime(client, name, namespace)
509+
juicefsRuntime, err := utils.GetJuiceFSRuntime(reader, name, namespace)
510510
if err != nil {
511511
return runtimeInfo, err
512512
}
@@ -522,7 +522,7 @@ func GetRuntimeInfo(client client.Client, name, namespace string) (runtimeInfo R
522522
runtimeInfo.SetFuseNodeSelector(juicefsRuntime.Spec.Fuse.NodeSelector)
523523
runtimeInfo.SetupFuseCleanPolicy(juicefsRuntime.Spec.Fuse.CleanPolicy)
524524
case common.ThinRuntime:
525-
thinRuntime, err := utils.GetThinRuntime(client, name, namespace)
525+
thinRuntime, err := utils.GetThinRuntime(reader, name, namespace)
526526
if err != nil {
527527
return runtimeInfo, err
528528
}
@@ -538,7 +538,7 @@ func GetRuntimeInfo(client client.Client, name, namespace string) (runtimeInfo R
538538
runtimeInfo.SetFuseNodeSelector(thinRuntime.Spec.Fuse.NodeSelector)
539539
runtimeInfo.SetupFuseCleanPolicy(thinRuntime.Spec.Fuse.CleanPolicy)
540540
case common.EFCRuntime:
541-
efcRuntime, err := utils.GetEFCRuntime(client, name, namespace)
541+
efcRuntime, err := utils.GetEFCRuntime(reader, name, namespace)
542542
if err != nil {
543543
return runtimeInfo, err
544544
}
@@ -554,7 +554,7 @@ func GetRuntimeInfo(client client.Client, name, namespace string) (runtimeInfo R
554554
runtimeInfo.SetFuseNodeSelector(efcRuntime.Spec.Fuse.NodeSelector)
555555
runtimeInfo.SetupFuseCleanPolicy(efcRuntime.Spec.Fuse.CleanPolicy)
556556
case common.VineyardRuntime:
557-
vineyardRuntime, err := utils.GetVineyardRuntime(client, name, namespace)
557+
vineyardRuntime, err := utils.GetVineyardRuntime(reader, name, namespace)
558558
if err != nil {
559559
return runtimeInfo, err
560560
}
@@ -575,7 +575,7 @@ func GetRuntimeInfo(client client.Client, name, namespace string) (runtimeInfo R
575575
}
576576

577577
if runtimeInfo != nil {
578-
runtimeInfo.SetClient(client)
578+
runtimeInfo.SetAPIReader(reader)
579579
runtimeInfo.SetOwnerDatasetUID(dataset.UID)
580580
}
581581
return runtimeInfo, err

pkg/ddc/base/runtime_helper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (info *RuntimeInfo) GetFuseContainerTemplate() (template *common.FuseInject
7272
}
7373

7474
func (info *RuntimeInfo) getFuseDaemonset() (ds *appsv1.DaemonSet, err error) {
75-
if info.client == nil {
75+
if info.apiReader == nil {
7676
err = fmt.Errorf("client is not set")
7777
return
7878
}
@@ -84,11 +84,11 @@ func (info *RuntimeInfo) getFuseDaemonset() (ds *appsv1.DaemonSet, err error) {
8484
default:
8585
fuseName = info.name + "-fuse"
8686
}
87-
return kubeclient.GetDaemonset(info.client, fuseName, info.GetNamespace())
87+
return kubeclient.GetDaemonset(info.apiReader, fuseName, info.GetNamespace())
8888
}
8989

9090
func (info *RuntimeInfo) getMountInfo() (path, mountType, subpath string, err error) {
91-
pv, err := kubeclient.GetPersistentVolume(info.client, info.GetPersistentVolumeName())
91+
pv, err := kubeclient.GetPersistentVolume(info.apiReader, info.GetPersistentVolumeName())
9292
if err != nil {
9393
err = errors.Wrapf(err, "cannot find pvc \"%s/%s\"'s bounded PV", info.namespace, info.name)
9494
return

pkg/ddc/base/runtime_helper_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ import (
497497
// if err != nil {
498498
// t.Errorf("testcase %s failed due to error %v", testcase.name, err)
499499
// }
500-
// runtimeInfo.SetClient(fakeClient)
500+
// runtimeInfo.SetAPIReader(fakeClient)
501501
// _, err = runtimeInfo.GetTemplateToInjectForFuse(testcase.pvcName, testcase.pvc.Namespace, options)
502502
// if (err == nil) == testcase.expectErr {
503503
// t.Errorf("testcase %s failed due to expecting want error: %v error %v", testcase.name, testcase.expectErr, err)
@@ -866,7 +866,7 @@ import (
866866
// if err != nil {
867867
// t.Errorf("testcase %s failed due to error %v", testcase.name, err)
868868
// }
869-
// runtimeInfo.SetClient(fakeClient)
869+
// runtimeInfo.SetAPIReader(fakeClient)
870870
// _, err = runtimeInfo.GetTemplateToInjectForFuse(testcase.pvcName, testcase.pvc.Namespace, options)
871871
// if (err == nil) == testcase.expectErr {
872872
// t.Errorf("testcase %s failed due to expecting want error: %v error %v", testcase.name, testcase.expectErr, err)
@@ -1075,7 +1075,7 @@ import (
10751075
// if err != nil {
10761076
// t.Errorf("testcase %s failed due to error %v", testcase.name, err)
10771077
// }
1078-
// runtimeInfo.SetClient(fakeClient)
1078+
// runtimeInfo.SetAPIReader(fakeClient)
10791079
// template, err := runtimeInfo.GetTemplateToInjectForFuse(testcase.pvcName, testcase.pvc.Namespace, options)
10801080
// if (err == nil) == testcase.expectErr {
10811081
// t.Errorf("testcase %s failed due to expecting want error: %v error %v", testcase.name, testcase.expectErr, err)
@@ -1158,7 +1158,7 @@ func TestGetFuseDaemonset(t *testing.T) {
11581158
}
11591159

11601160
if fakeClient != nil {
1161-
runtimeInfo.SetClient(fakeClient)
1161+
runtimeInfo.SetAPIReader(fakeClient)
11621162
}
11631163

11641164
_, err := runtimeInfo.getFuseDaemonset()
@@ -1302,7 +1302,7 @@ func TestGetMountInfoFromVolumeClaim(t *testing.T) {
13021302
name: tt.args.name,
13031303
namespace: tt.args.namespace,
13041304
runtimeType: common.JindoRuntime,
1305-
client: fake.NewFakeClientWithScheme(testScheme, objs...),
1305+
apiReader: fake.NewFakeClientWithScheme(testScheme, objs...),
13061306
}
13071307

13081308
path, mountType, subpath, err := runtimeInfo.getMountInfo()

0 commit comments

Comments
 (0)