Skip to content

Commit

Permalink
Feature: support to skip the validation of indirectly related resourc…
Browse files Browse the repository at this point in the history
…es involved in webhook mutating (fluid-cloudnative#4473)

Signed-off-by: jiuyu <[email protected]>
Co-authored-by: jiuyu <[email protected]>
  • Loading branch information
Syspretor and jiuyu authored Jan 16, 2025
1 parent 665c872 commit 4353d62
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 238 deletions.
2 changes: 1 addition & 1 deletion pkg/application/inject/fuse/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,5 @@ func (s *Injector) shouldInject(pod common.FluidObject) (should bool, err error)
}

func (s *Injector) getServerlessPlatformFromMeta(metaObj metav1.ObjectMeta) string {
return utils.GetServerlessPlatfrom(metaObj.Labels)
return utils.GetServerlessPlatform(metaObj.Labels)
}
4 changes: 4 additions & 0 deletions pkg/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,7 @@ const (
K8sZoneLabelKey = "topology.kubernetes.io/zone"
K8sRegionLabelKey = "topology.kubernetes.io/region"
)

const (
SkipPrecheckAnnotationKey = "sidecar.fluid.io/skip-precheck"
)
25 changes: 21 additions & 4 deletions pkg/ddc/base/runtime_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ import (
"fmt"
"time"

"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/fluid-cloudnative/fluid/pkg/utils/kubeclient"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"

"github.com/fluid-cloudnative/fluid/pkg/common"
)

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

template.FuseContainer.Name = common.FuseContainerName

hostMountPath, mountType, subPath, err := kubeclient.GetMountInfoFromVolumeClaim(info.client, info.name, info.namespace)
hostMountPath, mountType, subPath, err := info.getMountInfo()
if err != nil {
return template, errors.Wrapf(err, "failed get mount info from PVC \"%s/%s\"", info.namespace, info.name)
return template, errors.Wrapf(err, "failed to get mount info by runtimeInfo %s/%s", info.namespace, info.name)
}

fuseVolMount, err := kubeclient.GetFuseMountInContainer(mountType, ds.Spec.Template.Spec.Containers[0])
Expand Down Expand Up @@ -87,3 +86,21 @@ func (info *RuntimeInfo) getFuseDaemonset() (ds *appsv1.DaemonSet, err error) {
}
return kubeclient.GetDaemonset(info.client, fuseName, info.GetNamespace())
}

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

if pv.Spec.CSI != nil && len(pv.Spec.CSI.VolumeAttributes) > 0 {
path = pv.Spec.CSI.VolumeAttributes[common.VolumeAttrFluidPath]
mountType = pv.Spec.CSI.VolumeAttributes[common.VolumeAttrMountType]
subpath = pv.Spec.CSI.VolumeAttributes[common.VolumeAttrFluidSubPath]
} else {
err = fmt.Errorf("the pv %s is not created by fluid", pv.Name)
}

return
}
181 changes: 178 additions & 3 deletions pkg/ddc/base/runtime_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/utils/fake"
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// func TestGetTemplateToInjectForFuse(t *testing.T) {
Expand Down Expand Up @@ -1169,3 +1167,180 @@ func TestGetFuseDaemonset(t *testing.T) {
}
}
}

func TestGetMountInfoFromVolumeClaim(t *testing.T) {
namespace := "default"
testPVCInputs := []*corev1.PersistentVolumeClaim{{
ObjectMeta: metav1.ObjectMeta{Name: "fluid-dataset",
Namespace: namespace},
Spec: corev1.PersistentVolumeClaimSpec{
VolumeName: "default-fluid-dataset",
},
}, {
ObjectMeta: metav1.ObjectMeta{Name: "nonfluidpvc",
Annotations: common.ExpectedFluidAnnotations,
Namespace: namespace},
Spec: corev1.PersistentVolumeClaimSpec{
VolumeName: "nonfluidpv",
},
}, {
ObjectMeta: metav1.ObjectMeta{Name: "nopv",
Annotations: common.ExpectedFluidAnnotations,
Namespace: namespace},
Spec: corev1.PersistentVolumeClaimSpec{
VolumeName: "nopv",
},
}, {
ObjectMeta: metav1.ObjectMeta{Name: "fluid-dataset-subpath",
Annotations: common.ExpectedFluidAnnotations,
Namespace: namespace},
Spec: corev1.PersistentVolumeClaimSpec{
VolumeName: "default-fluid-dataset-subpath",
},
}}

objs := []runtime.Object{}

for _, pvc := range testPVCInputs {
objs = append(objs, pvc.DeepCopy())
}

testPVInputs := []*corev1.PersistentVolume{{
ObjectMeta: metav1.ObjectMeta{Name: "default-fluid-dataset"},
Spec: corev1.PersistentVolumeSpec{
PersistentVolumeSource: corev1.PersistentVolumeSource{
CSI: &corev1.CSIPersistentVolumeSource{
Driver: "fuse.csi.fluid.io",
VolumeAttributes: map[string]string{
common.VolumeAttrFluidPath: "/runtime-mnt/jindo/big-data/nofounddataset/jindofs-fuse",
common.VolumeAttrMountType: common.JindoRuntime,
},
},
},
},
}, {
ObjectMeta: metav1.ObjectMeta{Name: "nonfluidpv", Annotations: common.ExpectedFluidAnnotations},
Spec: corev1.PersistentVolumeSpec{},
}, {
ObjectMeta: metav1.ObjectMeta{Name: "default-fluid-dataset-subpath"},
Spec: corev1.PersistentVolumeSpec{
PersistentVolumeSource: corev1.PersistentVolumeSource{
CSI: &corev1.CSIPersistentVolumeSource{
Driver: "fuse.csi.fluid.io",
VolumeAttributes: map[string]string{
common.VolumeAttrFluidPath: "/runtime-mnt/jindo/big-data/nofounddataset/jindofs-fuse",
common.VolumeAttrMountType: common.JindoRuntime,
common.VolumeAttrFluidSubPath: "subtest",
},
},
},
},
}}

for _, pv := range testPVInputs {
objs = append(objs, pv.DeepCopy())
}

type args struct {
name string
namespace string
}
tests := []struct {
name string
args args
wantError bool
wantPath string
wantType string
wantSubPath string
}{{
name: "volumeClaim doesn't exist",
args: args{
name: "notExist",
namespace: namespace,
},
wantError: true,
}, {
name: "non fluid pv",
args: args{
name: "nonfluidpvc",
namespace: namespace,
},
wantError: true,
}, {
name: " fluid pv",
args: args{
name: "fluid-dataset",
namespace: namespace,
},
wantError: false,
wantPath: "/runtime-mnt/jindo/big-data/nofounddataset/jindofs-fuse",
wantType: common.JindoRuntime,
}, {
name: "no pv",
args: args{
name: "nopv",
namespace: namespace,
},
wantError: true,
}, {
name: "sub pv",
args: args{
name: "fluid-dataset-subpath",
namespace: namespace,
},
wantError: false,
wantPath: "/runtime-mnt/jindo/big-data/nofounddataset/jindofs-fuse",
wantType: common.JindoRuntime,
wantSubPath: "subtest",
}}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testScheme := runtime.NewScheme()
_ = corev1.AddToScheme(testScheme)
runtimeInfo := RuntimeInfo{
name: tt.args.name,
namespace: tt.args.namespace,
runtimeType: common.JindoRuntime,
client: fake.NewFakeClientWithScheme(testScheme, objs...),
}

path, mountType, subpath, err := runtimeInfo.getMountInfo()
got := err != nil

if got != tt.wantError {
t.Errorf("testcase %v getMountInfo() for %v in %v = %v, err = %v", tt.name,
tt.args.name,
tt.args.namespace,
got,
err)
}

if path != tt.wantPath {
t.Errorf("testcase %v GetMountInfoFromVolumeClaim() for %v in %v got path %v, want path = %v", tt.name,
tt.args.name,
tt.args.namespace,
path,
tt.wantPath)
}

if mountType != tt.wantType {
t.Errorf("testcase %v GetMountInfoFromVolumeClaim() for %v in %v got mountType %v, want mountType = %v", tt.name,
tt.args.name,
tt.args.namespace,
mountType,
tt.wantType)
}

if subpath != tt.wantSubPath {
t.Errorf("testcase %v GetMountInfoFromVolumeClaim() for %v in %v got subpath %v, want subpath = %v", tt.name,
tt.args.name,
tt.args.namespace,
subpath,
tt.wantSubPath)
}

})
}

}
6 changes: 5 additions & 1 deletion pkg/utils/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const (
PlatformUnprivileged = "Unprivileged"
)

func GetServerlessPlatfrom(infos map[string]string) (platform string) {
func GetServerlessPlatform(infos map[string]string) (platform string) {
if matchedKey(infos, ServerlessPlatformKey) {
return infos[ServerlessPlatformKey]
}
Expand Down Expand Up @@ -129,6 +129,10 @@ func serverlessPlatformMatched(infos map[string]string) (match bool) {
return matchedKey(infos, ServerlessPlatformKey)
}

func SkipPrecheckEnable(infos map[string]string) (match bool) {
return enabled(infos, common.SkipPrecheckAnnotationKey)
}

// enabled checks if the given name has a value of "true"
func enabled(infos map[string]string, name string) (match bool) {
return matchedValue(infos, name, common.True)
Expand Down
31 changes: 0 additions & 31 deletions pkg/utils/kubeclient/volume_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ package kubeclient

import (
"context"
"fmt"

"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -37,30 +33,3 @@ func GetPersistentVolumeClaim(client client.Client, name, namespace string) (pvc
pvc)
return
}

// GetMountInfoFromVolumeClaim gets the mountPath and type for CSI plugin
func GetMountInfoFromVolumeClaim(client client.Client, name, namespace string) (path string, mountType string, subpath string, err error) {
pvc, err := GetPersistentVolumeClaim(client, name, namespace)
if err != nil {
err = errors.Wrapf(err, "failed to get persistent volume claim")
return
}

pv, err := GetPersistentVolume(client, pvc.Spec.VolumeName)
if err != nil {
err = errors.Wrapf(err, "cannot find pvc \"%s/%s\"'s bounded PV", pvc.Namespace, pvc.Name)
return
}

if pv.Spec.CSI != nil && len(pv.Spec.CSI.VolumeAttributes) > 0 {
path = pv.Spec.CSI.VolumeAttributes[common.VolumeAttrFluidPath]
mountType = pv.Spec.CSI.VolumeAttributes[common.VolumeAttrMountType]
subpath = pv.Spec.CSI.VolumeAttributes[common.VolumeAttrFluidSubPath]
} else {
err = fmt.Errorf("the pvc %s in %s is not created by fluid",
name,
namespace)
}

return
}
Loading

0 comments on commit 4353d62

Please sign in to comment.