Skip to content

Commit daa0864

Browse files
author
jiuyu
committed
Feature: support to skip the validation of indirectly related resources involved in webhook mutating
Signed-off-by: jiuyu <guotongyu.gty@alibaba-inc.com>
1 parent 4e199c8 commit daa0864

File tree

10 files changed

+238
-237
lines changed

10 files changed

+238
-237
lines changed

pkg/application/inject/fuse/injector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,5 @@ func (s *Injector) shouldInject(pod common.FluidObject) (should bool, err error)
258258
}
259259

260260
func (s *Injector) getServerlessPlatformFromMeta(metaObj metav1.ObjectMeta) string {
261-
return utils.GetServerlessPlatfrom(metaObj.Labels)
261+
return utils.GetServerlessPlatform(metaObj.Labels)
262262
}

pkg/common/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,7 @@ const (
212212
K8sZoneLabelKey = "topology.kubernetes.io/zone"
213213
K8sRegionLabelKey = "topology.kubernetes.io/region"
214214
)
215+
216+
const (
217+
SkipPrecheckAnnotationKey = "sidecar.fluid.io/skip-precheck"
218+
)

pkg/ddc/base/runtime_helper.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ import (
2020
"fmt"
2121
"time"
2222

23+
"github.com/fluid-cloudnative/fluid/pkg/common"
2324
"github.com/fluid-cloudnative/fluid/pkg/utils"
2425
"github.com/fluid-cloudnative/fluid/pkg/utils/kubeclient"
2526
"github.com/pkg/errors"
2627
appsv1 "k8s.io/api/apps/v1"
27-
28-
"github.com/fluid-cloudnative/fluid/pkg/common"
2928
)
3029

3130
// GetFuseContainerTemplate collects the fuse container spec from the runtime's fuse daemonSet spec. The function summarizes fuse related information into
@@ -52,7 +51,11 @@ func (info *RuntimeInfo) GetFuseContainerTemplate() (template *common.FuseInject
5251

5352
template.FuseContainer.Name = common.FuseContainerName
5453

55-
hostMountPath, mountType, subPath, err := kubeclient.GetMountInfoFromVolumeClaim(info.client, info.name, info.namespace)
54+
hostMountPath, mountType, subPath, err := info.getMountInfo()
55+
if err != nil {
56+
return template, errors.Wrapf(err, "failed to get mount info")
57+
}
58+
5659
if err != nil {
5760
return template, errors.Wrapf(err, "failed get mount info from PVC \"%s/%s\"", info.namespace, info.name)
5861
}
@@ -87,3 +90,21 @@ func (info *RuntimeInfo) getFuseDaemonset() (ds *appsv1.DaemonSet, err error) {
8790
}
8891
return kubeclient.GetDaemonset(info.client, fuseName, info.GetNamespace())
8992
}
93+
94+
func (info *RuntimeInfo) getMountInfo() (path, mountType, subpath string, err error) {
95+
pv, err := kubeclient.GetPersistentVolume(info.client, info.GetPersistentVolumeName())
96+
if err != nil {
97+
err = errors.Wrapf(err, "cannot find pvc \"%s/%s\"'s bounded PV", info.namespace, info.name)
98+
return
99+
}
100+
101+
if pv.Spec.CSI != nil && len(pv.Spec.CSI.VolumeAttributes) > 0 {
102+
path = pv.Spec.CSI.VolumeAttributes[common.VolumeAttrFluidPath]
103+
mountType = pv.Spec.CSI.VolumeAttributes[common.VolumeAttrMountType]
104+
subpath = pv.Spec.CSI.VolumeAttributes[common.VolumeAttrFluidSubPath]
105+
} else {
106+
err = fmt.Errorf("the pv %s is not created by fluid", pv.Name)
107+
}
108+
109+
return
110+
}

pkg/ddc/base/runtime_helper_test.go

Lines changed: 178 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ import (
2222
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
2323
"github.com/fluid-cloudnative/fluid/pkg/common"
2424
"github.com/fluid-cloudnative/fluid/pkg/utils/fake"
25-
"sigs.k8s.io/controller-runtime/pkg/client"
26-
2725
appsv1 "k8s.io/api/apps/v1"
2826
corev1 "k8s.io/api/core/v1"
2927
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30-
3128
"k8s.io/apimachinery/pkg/runtime"
29+
"sigs.k8s.io/controller-runtime/pkg/client"
3230
)
3331

3432
// func TestGetTemplateToInjectForFuse(t *testing.T) {
@@ -1169,3 +1167,180 @@ func TestGetFuseDaemonset(t *testing.T) {
11691167
}
11701168
}
11711169
}
1170+
1171+
func TestGetMountInfoFromVolumeClaim(t *testing.T) {
1172+
namespace := "default"
1173+
testPVCInputs := []*corev1.PersistentVolumeClaim{{
1174+
ObjectMeta: metav1.ObjectMeta{Name: "fluid-dataset",
1175+
Namespace: namespace},
1176+
Spec: corev1.PersistentVolumeClaimSpec{
1177+
VolumeName: "default-fluid-dataset",
1178+
},
1179+
}, {
1180+
ObjectMeta: metav1.ObjectMeta{Name: "nonfluidpvc",
1181+
Annotations: common.ExpectedFluidAnnotations,
1182+
Namespace: namespace},
1183+
Spec: corev1.PersistentVolumeClaimSpec{
1184+
VolumeName: "nonfluidpv",
1185+
},
1186+
}, {
1187+
ObjectMeta: metav1.ObjectMeta{Name: "nopv",
1188+
Annotations: common.ExpectedFluidAnnotations,
1189+
Namespace: namespace},
1190+
Spec: corev1.PersistentVolumeClaimSpec{
1191+
VolumeName: "nopv",
1192+
},
1193+
}, {
1194+
ObjectMeta: metav1.ObjectMeta{Name: "fluid-dataset-subpath",
1195+
Annotations: common.ExpectedFluidAnnotations,
1196+
Namespace: namespace},
1197+
Spec: corev1.PersistentVolumeClaimSpec{
1198+
VolumeName: "default-fluid-dataset-subpath",
1199+
},
1200+
}}
1201+
1202+
objs := []runtime.Object{}
1203+
1204+
for _, pvc := range testPVCInputs {
1205+
objs = append(objs, pvc.DeepCopy())
1206+
}
1207+
1208+
testPVInputs := []*corev1.PersistentVolume{{
1209+
ObjectMeta: metav1.ObjectMeta{Name: "default-fluid-dataset"},
1210+
Spec: corev1.PersistentVolumeSpec{
1211+
PersistentVolumeSource: corev1.PersistentVolumeSource{
1212+
CSI: &corev1.CSIPersistentVolumeSource{
1213+
Driver: "fuse.csi.fluid.io",
1214+
VolumeAttributes: map[string]string{
1215+
common.VolumeAttrFluidPath: "/runtime-mnt/jindo/big-data/nofounddataset/jindofs-fuse",
1216+
common.VolumeAttrMountType: common.JindoRuntime,
1217+
},
1218+
},
1219+
},
1220+
},
1221+
}, {
1222+
ObjectMeta: metav1.ObjectMeta{Name: "nonfluidpv", Annotations: common.ExpectedFluidAnnotations},
1223+
Spec: corev1.PersistentVolumeSpec{},
1224+
}, {
1225+
ObjectMeta: metav1.ObjectMeta{Name: "default-fluid-dataset-subpath"},
1226+
Spec: corev1.PersistentVolumeSpec{
1227+
PersistentVolumeSource: corev1.PersistentVolumeSource{
1228+
CSI: &corev1.CSIPersistentVolumeSource{
1229+
Driver: "fuse.csi.fluid.io",
1230+
VolumeAttributes: map[string]string{
1231+
common.VolumeAttrFluidPath: "/runtime-mnt/jindo/big-data/nofounddataset/jindofs-fuse",
1232+
common.VolumeAttrMountType: common.JindoRuntime,
1233+
common.VolumeAttrFluidSubPath: "subtest",
1234+
},
1235+
},
1236+
},
1237+
},
1238+
}}
1239+
1240+
for _, pv := range testPVInputs {
1241+
objs = append(objs, pv.DeepCopy())
1242+
}
1243+
1244+
type args struct {
1245+
name string
1246+
namespace string
1247+
}
1248+
tests := []struct {
1249+
name string
1250+
args args
1251+
wantError bool
1252+
wantPath string
1253+
wantType string
1254+
wantSubPath string
1255+
}{{
1256+
name: "volumeClaim doesn't exist",
1257+
args: args{
1258+
name: "notExist",
1259+
namespace: namespace,
1260+
},
1261+
wantError: true,
1262+
}, {
1263+
name: "non fluid pv",
1264+
args: args{
1265+
name: "nonfluidpvc",
1266+
namespace: namespace,
1267+
},
1268+
wantError: true,
1269+
}, {
1270+
name: " fluid pv",
1271+
args: args{
1272+
name: "fluid-dataset",
1273+
namespace: namespace,
1274+
},
1275+
wantError: false,
1276+
wantPath: "/runtime-mnt/jindo/big-data/nofounddataset/jindofs-fuse",
1277+
wantType: common.JindoRuntime,
1278+
}, {
1279+
name: "no pv",
1280+
args: args{
1281+
name: "nopv",
1282+
namespace: namespace,
1283+
},
1284+
wantError: true,
1285+
}, {
1286+
name: "sub pv",
1287+
args: args{
1288+
name: "fluid-dataset-subpath",
1289+
namespace: namespace,
1290+
},
1291+
wantError: false,
1292+
wantPath: "/runtime-mnt/jindo/big-data/nofounddataset/jindofs-fuse",
1293+
wantType: common.JindoRuntime,
1294+
wantSubPath: "subtest",
1295+
}}
1296+
1297+
for _, tt := range tests {
1298+
t.Run(tt.name, func(t *testing.T) {
1299+
testScheme := runtime.NewScheme()
1300+
_ = corev1.AddToScheme(testScheme)
1301+
runtimeInfo := RuntimeInfo{
1302+
name: tt.args.name,
1303+
namespace: tt.args.namespace,
1304+
runtimeType: common.JindoRuntime,
1305+
client: fake.NewFakeClientWithScheme(testScheme, objs...),
1306+
}
1307+
1308+
path, mountType, subpath, err := runtimeInfo.getMountInfo()
1309+
got := err != nil
1310+
1311+
if got != tt.wantError {
1312+
t.Errorf("testcase %v getMountInfo() for %v in %v = %v, err = %v", tt.name,
1313+
tt.args.name,
1314+
tt.args.namespace,
1315+
got,
1316+
err)
1317+
}
1318+
1319+
if path != tt.wantPath {
1320+
t.Errorf("testcase %v GetMountInfoFromVolumeClaim() for %v in %v got path %v, want path = %v", tt.name,
1321+
tt.args.name,
1322+
tt.args.namespace,
1323+
path,
1324+
tt.wantPath)
1325+
}
1326+
1327+
if mountType != tt.wantType {
1328+
t.Errorf("testcase %v GetMountInfoFromVolumeClaim() for %v in %v got mountType %v, want mountType = %v", tt.name,
1329+
tt.args.name,
1330+
tt.args.namespace,
1331+
mountType,
1332+
tt.wantType)
1333+
}
1334+
1335+
if subpath != tt.wantSubPath {
1336+
t.Errorf("testcase %v GetMountInfoFromVolumeClaim() for %v in %v got subpath %v, want subpath = %v", tt.name,
1337+
tt.args.name,
1338+
tt.args.namespace,
1339+
subpath,
1340+
tt.wantSubPath)
1341+
}
1342+
1343+
})
1344+
}
1345+
1346+
}

pkg/utils/annotations.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const (
7575
PlatformUnprivileged = "Unprivileged"
7676
)
7777

78-
func GetServerlessPlatfrom(infos map[string]string) (platform string) {
78+
func GetServerlessPlatform(infos map[string]string) (platform string) {
7979
if matchedKey(infos, ServerlessPlatformKey) {
8080
return infos[ServerlessPlatformKey]
8181
}
@@ -129,6 +129,10 @@ func serverlessPlatformMatched(infos map[string]string) (match bool) {
129129
return matchedKey(infos, ServerlessPlatformKey)
130130
}
131131

132+
func SkipPrecheckEnable(infos map[string]string) (match bool) {
133+
return enabled(infos, common.SkipPrecheckAnnotationKey)
134+
}
135+
132136
// enabled checks if the given name has a value of "true"
133137
func enabled(infos map[string]string, name string) (match bool) {
134138
return matchedValue(infos, name, common.True)

pkg/utils/kubeclient/volume_claim.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ package kubeclient
1818

1919
import (
2020
"context"
21-
"fmt"
22-
23-
"github.com/fluid-cloudnative/fluid/pkg/common"
24-
"github.com/pkg/errors"
2521
v1 "k8s.io/api/core/v1"
2622
"k8s.io/apimachinery/pkg/types"
2723
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -37,30 +33,3 @@ func GetPersistentVolumeClaim(client client.Client, name, namespace string) (pvc
3733
pvc)
3834
return
3935
}
40-
41-
// GetMountInfoFromVolumeClaim gets the mountPath and type for CSI plugin
42-
func GetMountInfoFromVolumeClaim(client client.Client, name, namespace string) (path string, mountType string, subpath string, err error) {
43-
pvc, err := GetPersistentVolumeClaim(client, name, namespace)
44-
if err != nil {
45-
err = errors.Wrapf(err, "failed to get persistent volume claim")
46-
return
47-
}
48-
49-
pv, err := GetPersistentVolume(client, pvc.Spec.VolumeName)
50-
if err != nil {
51-
err = errors.Wrapf(err, "cannot find pvc \"%s/%s\"'s bounded PV", pvc.Namespace, pvc.Name)
52-
return
53-
}
54-
55-
if pv.Spec.CSI != nil && len(pv.Spec.CSI.VolumeAttributes) > 0 {
56-
path = pv.Spec.CSI.VolumeAttributes[common.VolumeAttrFluidPath]
57-
mountType = pv.Spec.CSI.VolumeAttributes[common.VolumeAttrMountType]
58-
subpath = pv.Spec.CSI.VolumeAttributes[common.VolumeAttrFluidSubPath]
59-
} else {
60-
err = fmt.Errorf("the pvc %s in %s is not created by fluid",
61-
name,
62-
namespace)
63-
}
64-
65-
return
66-
}

0 commit comments

Comments
 (0)