Skip to content

Commit

Permalink
Remove unused logic in webhook
Browse files Browse the repository at this point in the history
Signed-off-by: jiuyu <guotongyu.gty@alibaba-inc.com>
  • Loading branch information
jiuyu committed Dec 26, 2024
1 parent bb4b1ff commit 3882ce1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 168 deletions.
110 changes: 29 additions & 81 deletions pkg/application/inject/fuse/injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,19 @@ import (
"strings"
"testing"

"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/google/go-cmp/cmp"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
"github.com/fluid-cloudnative/fluid/pkg/utils/fake"
"gopkg.in/yaml.v3"
"k8s.io/utils/ptr"

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"
"k8s.io/utils/ptr"

datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/fluid-cloudnative/fluid/pkg/utils/fake"
"github.com/google/go-cmp/cmp"
)

func TestInjectPod(t *testing.T) {
Expand Down Expand Up @@ -3110,19 +3106,11 @@ func TestInjectPodUnprivileged(t *testing.T) {
// "jindo",
"bash",
"-c",
"time /check-mount.sh >> /proc/1/fd/1",
"time /check-mount.sh /jfs jindo >> /proc/1/fd/1",
},
},
},
},
Resources: corev1.ResourceRequirements{
Limits: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceName(common.DefaultFuseDeviceResourceName): resource.MustParse("1"),
},
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceName(common.DefaultFuseDeviceResourceName): resource.MustParse("1"),
},
},
Command: []string{"/entrypoint.sh"},
Image: "unprivileged-pvc-pod",
SecurityContext: &corev1.SecurityContext{
Expand All @@ -3134,7 +3122,7 @@ func TestInjectPodUnprivileged(t *testing.T) {
MountPath: "/mnt/disk1",
},
{
Name: "check-mount-unprivileged-0",
Name: "check-mount-0",
ReadOnly: true,
MountPath: "/check-mount.sh",
SubPath: "check-mount.sh",
Expand Down Expand Up @@ -3189,11 +3177,11 @@ func TestInjectPodUnprivileged(t *testing.T) {
},
},
{
Name: "check-mount-unprivileged-0",
Name: "check-mount-0",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "dataset-jindo-check-mount-unprivileged",
Name: "dataset-jindo-check-mount",
},
DefaultMode: ptr.To(mode),
},
Expand Down Expand Up @@ -3491,19 +3479,11 @@ func TestInjectPodUnprivileged(t *testing.T) {
// "jindo",
"bash",
"-c",
"time /check-mount.sh >> /proc/1/fd/1",
"time /check-mount.sh /jfs jindo >> /proc/1/fd/1",
},
},
},
},
Resources: corev1.ResourceRequirements{
Limits: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceName(common.DefaultFuseDeviceResourceName): resource.MustParse("1"),
},
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceName(common.DefaultFuseDeviceResourceName): resource.MustParse("1"),
},
},
Command: []string{"/entrypoint.sh"},
Image: "unprivileged-pvc-pod",
SecurityContext: &corev1.SecurityContext{
Expand All @@ -3515,7 +3495,7 @@ func TestInjectPodUnprivileged(t *testing.T) {
MountPath: "/mnt/disk",
},
{
Name: "check-mount-unprivileged-1",
Name: "check-mount-1",
ReadOnly: true,
MountPath: "/check-mount.sh",
SubPath: "check-mount.sh",
Expand All @@ -3536,19 +3516,11 @@ func TestInjectPodUnprivileged(t *testing.T) {
// "jindo",
"bash",
"-c",
"time /check-mount.sh >> /proc/1/fd/1",
"time /check-mount.sh /jfs jindo >> /proc/1/fd/1",
},
},
},
},
Resources: corev1.ResourceRequirements{
Limits: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceName(common.DefaultFuseDeviceResourceName): resource.MustParse("1"),
},
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceName(common.DefaultFuseDeviceResourceName): resource.MustParse("1"),
},
},
Command: []string{"/entrypoint.sh"},
Image: "unprivileged-pvc-pod",
SecurityContext: &corev1.SecurityContext{
Expand All @@ -3560,7 +3532,7 @@ func TestInjectPodUnprivileged(t *testing.T) {
MountPath: "/mnt/disk",
},
{
Name: "check-mount-unprivileged-0",
Name: "check-mount-0",
ReadOnly: true,
MountPath: "/check-mount.sh",
SubPath: "check-mount.sh",
Expand Down Expand Up @@ -3628,11 +3600,11 @@ func TestInjectPodUnprivileged(t *testing.T) {
},
},
{
Name: "check-mount-unprivileged-0",
Name: "check-mount-0",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "dataset1-jindo-check-mount-unprivileged",
Name: "dataset1-jindo-check-mount",
},
DefaultMode: ptr.To(mode),
},
Expand All @@ -3648,11 +3620,11 @@ func TestInjectPodUnprivileged(t *testing.T) {
},
},
{
Name: "check-mount-unprivileged-1",
Name: "check-mount-1",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "dataset2-jindo-check-mount-unprivileged",
Name: "dataset2-jindo-check-mount",
},
DefaultMode: ptr.To(mode),
},
Expand Down Expand Up @@ -3952,19 +3924,11 @@ func TestInjectPodUnprivileged(t *testing.T) {
// "jindo",
"bash",
"-c",
"time /check-mount.sh >> /proc/1/fd/1",
"time /check-mount.sh /jfs jindo >> /proc/1/fd/1",
},
},
},
},
Resources: corev1.ResourceRequirements{
Limits: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceName(common.DefaultFuseDeviceResourceName): resource.MustParse("1"),
},
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceName(common.DefaultFuseDeviceResourceName): resource.MustParse("1"),
},
},
Command: []string{"/entrypoint.sh"},
Image: "unprivileged-pvc-pod",
SecurityContext: &corev1.SecurityContext{
Expand All @@ -3976,7 +3940,7 @@ func TestInjectPodUnprivileged(t *testing.T) {
MountPath: "/mnt/disk",
},
{
Name: "check-mount-unprivileged-1",
Name: "check-mount-1",
ReadOnly: true,
MountPath: "/check-mount.sh",
SubPath: "check-mount.sh",
Expand All @@ -3997,19 +3961,11 @@ func TestInjectPodUnprivileged(t *testing.T) {
// "jindo",
"bash",
"-c",
"time /check-mount.sh >> /proc/1/fd/1",
"time /check-mount.sh /jfs jindo >> /proc/1/fd/1",
},
},
},
},
Resources: corev1.ResourceRequirements{
Limits: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceName(common.DefaultFuseDeviceResourceName): resource.MustParse("1"),
},
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceName(common.DefaultFuseDeviceResourceName): resource.MustParse("1"),
},
},
Command: []string{"/entrypoint.sh"},
Image: "unprivileged-pvc-pod",
SecurityContext: &corev1.SecurityContext{
Expand All @@ -4021,7 +3977,7 @@ func TestInjectPodUnprivileged(t *testing.T) {
MountPath: "/mnt/disk",
},
{
Name: "check-mount-unprivileged-0",
Name: "check-mount-0",
ReadOnly: true,
MountPath: "/check-mount.sh",
SubPath: "check-mount.sh",
Expand Down Expand Up @@ -4096,11 +4052,11 @@ func TestInjectPodUnprivileged(t *testing.T) {
},
},
{
Name: "check-mount-unprivileged-0",
Name: "check-mount-0",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "dataset-a-jindo-check-mount-unprivileged",
Name: "dataset-a-jindo-check-mount",
},
DefaultMode: ptr.To(mode),
},
Expand All @@ -4116,11 +4072,11 @@ func TestInjectPodUnprivileged(t *testing.T) {
},
},
{
Name: "check-mount-unprivileged-1",
Name: "check-mount-1",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "dataset-b-jindo-check-mount-unprivileged",
Name: "dataset-b-jindo-check-mount",
},
DefaultMode: ptr.To(mode),
},
Expand Down Expand Up @@ -6114,14 +6070,6 @@ func TestInjectPodWithEnabledFUSEMetrics(t *testing.T) {
Args: []string{
"-oroot_ns=jindo", "-okernel_cache", "-oattr_timeout=9000", "-oentry_timeout=9000", "-ometrics_port=15000",
},
Resources: corev1.ResourceRequirements{
Limits: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceName(common.DefaultFuseDeviceResourceName): resource.MustParse("1"),
},
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceName(common.DefaultFuseDeviceResourceName): resource.MustParse("1"),
},
},
Lifecycle: &corev1.Lifecycle{
PostStart: &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{
Expand Down Expand Up @@ -6151,7 +6099,7 @@ func TestInjectPodWithEnabledFUSEMetrics(t *testing.T) {
Name: "fuse-device-0",
MountPath: "/dev/fuse",
}, {
Name: "check-mount-unprivileged-0",
Name: "check-mount-0",
ReadOnly: true,
MountPath: "/check-mount.sh",
SubPath: "check-mount.sh",
Expand Down Expand Up @@ -6213,11 +6161,11 @@ func TestInjectPodWithEnabledFUSEMetrics(t *testing.T) {
},
},
}, {
Name: "check-mount-unprivileged-0",
Name: "check-mount-0",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "duplicate3-jindo-check-mount-unprivileged",
Name: "duplicate3-jindo-check-mount",
},
DefaultMode: ptr.To(mode),
},
Expand Down
29 changes: 6 additions & 23 deletions pkg/application/inject/fuse/mutator/mutator_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@ import (
"fmt"
"strings"

"github.com/fluid-cloudnative/fluid/pkg/application/inject/fuse/poststart"
"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/fluid-cloudnative/fluid/pkg/utils/kubeclient"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var fuseDeviceResourceName string
"github.com/fluid-cloudnative/fluid/pkg/application/inject/fuse/poststart"
"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/fluid-cloudnative/fluid/pkg/utils/kubeclient"
)

var (
// datavolume-, volume-localtime for JindoFS
Expand All @@ -50,10 +48,6 @@ var (
hostFuseDeviceNames = []string{"alluxio-fuse-device", "jindofs-fuse-device", "goosefs-fuse-device", "thin-fuse-device"}
)

func init() {
fuseDeviceResourceName = utils.GetStringValueFromEnv(common.EnvFuseDeviceResourceName, common.DefaultFuseDeviceResourceName)
}

// TODO: DefaultMutator will be rewritten with polymorphism withe platform-specific mutation logic
type DefaultMutator struct {
options common.FuseSidecarInjectOption
Expand Down Expand Up @@ -383,17 +377,6 @@ func (helper *defaultMutatorHelper) transformTemplateWithUnprivilegedSidecarEnab
template.FuseContainer.VolumeMounts = utils.TrimVolumeMounts(template.FuseContainer.VolumeMounts, hostFuseDeviceNames)
template.VolumesToAdd = utils.TrimVolumes(template.VolumesToAdd, hostFuseDeviceNames)

// add virtual fuse device resource
if template.FuseContainer.Resources.Limits == nil {
template.FuseContainer.Resources.Limits = map[corev1.ResourceName]resource.Quantity{}
}
template.FuseContainer.Resources.Limits[corev1.ResourceName(fuseDeviceResourceName)] = resource.MustParse("1")

if template.FuseContainer.Resources.Requests == nil {
template.FuseContainer.Resources.Requests = map[corev1.ResourceName]resource.Quantity{}
}
template.FuseContainer.Resources.Requests[corev1.ResourceName(fuseDeviceResourceName)] = resource.MustParse("1")

// invalidate privileged fuse container
if template.FuseContainer.SecurityContext != nil {
privilegedContainer := false
Expand Down
4 changes: 2 additions & 2 deletions pkg/application/inject/fuse/mutator/mutator_unprivileged.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (mutator *unprivilegedMutatorHelper) prepareFuseContainerPostStartScript()
}

// Fluid assumes pvc name is the same with runtime's name
gen := poststart.NewUnprivilegedPostStartScriptGenerator()
gen := poststart.NewDefaultPostStartScriptGenerator()
cmKey := gen.GetConfigMapKeyByOwner(types.NamespacedName{Namespace: datasetNamespace, Name: datasetName}, template.FuseMountInfo.FsType)
cm := gen.BuildConfigMap(ownerReference, cmKey)

Expand All @@ -161,7 +161,7 @@ func (mutator *unprivilegedMutatorHelper) prepareFuseContainerPostStartScript()
if template.FuseContainer.Lifecycle == nil {
template.FuseContainer.Lifecycle = &corev1.Lifecycle{}
}
template.FuseContainer.Lifecycle.PostStart = gen.GetPostStartCommand()
template.FuseContainer.Lifecycle.PostStart = gen.GetPostStartCommand(template.FuseMountInfo.ContainerMountPath, template.FuseMountInfo.FsType, template.FuseMountInfo.SubPath)
template.VolumesToAdd = append(template.VolumesToAdd, gen.GetVolume(cmKey))

return nil
Expand Down
Loading

0 comments on commit 3882ce1

Please sign in to comment.