Skip to content

Commit

Permalink
delete initContainer and put init cmd in mount container
Browse files Browse the repository at this point in the history
Signed-off-by: zwwhdls <[email protected]>
  • Loading branch information
zwwhdls committed Oct 12, 2023
1 parent a7510b6 commit e7b8f16
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 128 deletions.
1 change: 0 additions & 1 deletion pkg/juicefs/mount/builder/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (

const (
JfsDirName = "jfs-dir"
JfsRootDirName = "jfs-root-dir"
UpdateDBDirName = "updatedb"
UpdateDBCfgFile = "/etc/updatedb.conf"
)
Expand Down
10 changes: 8 additions & 2 deletions pkg/juicefs/mount/builder/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package builder
import (
"crypto/sha256"
"fmt"
"strings"

batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -48,7 +49,10 @@ func (r *JobBuilder) NewJobForCreateVolume() *batchv1.Job {
jobName := GenJobNameByVolumeId(r.jfsSetting.VolumeId) + "-createvol"
job := r.newJob(jobName)
jobCmd := r.getCreateVolumeCmd()
job.Spec.Template.Spec.Containers[0].Command = []string{"sh", "-c", jobCmd}
initCmd := r.genInitCommand()
cmd := strings.Join([]string{initCmd, jobCmd}, "\n")
job.Spec.Template.Spec.Containers[0].Command = []string{"sh", "-c", cmd}

klog.Infof("create volume job cmd: %s", jobCmd)
return job
}
Expand All @@ -57,7 +61,9 @@ func (r *JobBuilder) NewJobForDeleteVolume() *batchv1.Job {
jobName := GenJobNameByVolumeId(r.jfsSetting.VolumeId) + "-delvol"
job := r.newJob(jobName)
jobCmd := r.getDeleteVolumeCmd()
job.Spec.Template.Spec.Containers[0].Command = []string{"sh", "-c", jobCmd}
initCmd := r.genInitCommand()
cmd := strings.Join([]string{initCmd, jobCmd}, "\n")
job.Spec.Template.Spec.Containers[0].Command = []string{"sh", "-c", cmd}
klog.Infof("delete volume job cmd: %s", jobCmd)
return job
}
Expand Down
69 changes: 4 additions & 65 deletions pkg/juicefs/mount/builder/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package builder
import (
"fmt"
"path/filepath"
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -43,8 +44,10 @@ func NewPodBuilder(setting *config.JfsSetting, capacity int64) *PodBuilder {
func (r *PodBuilder) NewMountPod(podName string) *corev1.Pod {
pod := r.genCommonJuicePod(r.genCommonContainer)

cmd := r.genMountCommand()
pod.Name = podName
mountCmd := r.genMountCommand()
initCmd := r.genInitCommand()
cmd := strings.Join([]string{initCmd, mountCmd}, "\n")
pod.Spec.Containers[0].Command = []string{"sh", "-c", cmd}
pod.Spec.Containers[0].Env = []corev1.EnvVar{{
Name: "JFS_FOREGROUND",
Expand All @@ -56,14 +59,6 @@ func (r *PodBuilder) NewMountPod(podName string) *corev1.Pod {
pod.Spec.Volumes = append(pod.Spec.Volumes, volumes...)
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, volumeMounts...)

// generate initContainer
if r.jfsSetting.FormatCmd != "" {
initVolumeMounts := r.genInitVolumes()
initContainer := r.genInitContainer()
initContainer.VolumeMounts = append(initContainer.VolumeMounts, initVolumeMounts...)
pod.Spec.InitContainers = []corev1.Container{initContainer}
}

// add cache-dir hostpath & PVC volume
cacheVolumes, cacheVolumeMounts := r.genCacheDirVolumes()
pod.Spec.Volumes = append(pod.Spec.Volumes, cacheVolumes...)
Expand Down Expand Up @@ -169,35 +164,9 @@ func (r *PodBuilder) genHostPathVolumes() (volumes []corev1.Volume, volumeMounts
return
}

// genInitContainer: generate init container
func (r *PodBuilder) genInitContainer() corev1.Container {
rootUser := int64(0)
isPrivileged := true
secretName := r.jfsSetting.SecretName
container := corev1.Container{
Name: "jfs-format",
Image: r.jfsSetting.Attr.Image,
SecurityContext: &corev1.SecurityContext{
Privileged: &isPrivileged,
RunAsUser: &rootUser,
},
}

initCmd := r.genInitCommand()
container.Command = []string{"sh", "-c", initCmd}

container.EnvFrom = append(container.EnvFrom, corev1.EnvFromSource{
SecretRef: &corev1.SecretEnvSource{LocalObjectReference: corev1.LocalObjectReference{
Name: secretName,
}},
})
return container
}

// genPodVolumes: generate volumes for mount pod
// 1. jfs dir: mount point used to propagate the mount point in the mount container to host
// 2. update db dir: mount updatedb.conf from host to mount pod
// 3. jfs config dir: mount jfs config dir as emptyDir to deliver config file to mount container
func (r *PodBuilder) genPodVolumes() ([]corev1.Volume, []corev1.VolumeMount) {
dir := corev1.HostPathDirectoryOrCreate
file := corev1.HostPathFileOrCreate
Expand Down Expand Up @@ -233,39 +202,9 @@ func (r *PodBuilder) genPodVolumes() ([]corev1.Volume, []corev1.VolumeMount) {
})
}

if r.jfsSetting.FormatCmd != "" {
// initContainer will generate xx.conf to share with mount container
volumes = append(volumes, corev1.Volume{
Name: JfsRootDirName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
})
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: JfsRootDirName,
MountPath: "/root/.juicefs",
})
}

return volumes, volumeMounts
}

// genInitVolumes: generate volumes for initContainer in mount pod
// jfs config dir: mount jfs config dir as emptyDir to deliver config file to initContainer
func (r *PodBuilder) genInitVolumes() []corev1.VolumeMount {
volumeMounts := []corev1.VolumeMount{}

if r.jfsSetting.FormatCmd != "" {
// initContainer will generate xx.conf to share with mount container
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: JfsRootDirName,
MountPath: "/root/.juicefs",
})
}

return volumeMounts
}

// genCleanCachePod: generate pod to clean cache in host
func (r *PodBuilder) genCleanCachePod() *corev1.Pod {
volumeMountPrefix := "/var/jfsCache"
Expand Down
11 changes: 1 addition & 10 deletions pkg/juicefs/mount/builder/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,23 +174,14 @@ func Test_getCacheDirVolumes(t *testing.T) {
volumeMounts := []corev1.VolumeMount{{
Name: JfsDirName,
MountPath: config.PodMountBase,
}, {
Name: JfsRootDirName,
MountPath: "/root/.juicefs",
}}

volumes := []corev1.Volume{{
Name: JfsDirName,
VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{
Path: config.MountPointPath,
Type: &dir,
}}}, {
Name: JfsRootDirName,
VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{
Path: config.JFSConfigPath,
Type: &dir,
}},
}}
}}}}

s, _ := config.ParseSetting(map[string]string{"name": "test"}, nil, optionWithoutCacheDir, true)
r.jfsSetting = s
Expand Down
19 changes: 2 additions & 17 deletions pkg/webhook/handler/mutate/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ func (s *SidecarMutate) mutate(ctx context.Context, pod *corev1.Pod, pair util.P
s.injectAnnotation(out, mountPod.Annotations)
// inject container
s.injectContainer(out, mountPod.Spec.Containers[0])
if len(mountPod.Spec.InitContainers) > 0 {
// inject initContainer
s.injectInitContainer(out, mountPod.Spec.InitContainers[0])
}

return
}
Expand All @@ -146,16 +142,9 @@ func (s *SidecarMutate) Deduplicate(pod, mountPod *corev1.Pod, index int) {
return
}

// deduplicate initContainer name
for _, c := range pod.Spec.InitContainers {
if c.Name == mountPod.Spec.InitContainers[0].Name {
mountPod.Spec.InitContainers[0].Name = fmt.Sprintf("%s-%d", c.Name, index)
}
}

// deduplicate volume name
for i, mv := range mountPod.Spec.Volumes {
if mv.Name == builder.UpdateDBDirName || mv.Name == builder.JfsDirName || mv.Name == builder.JfsRootDirName {
if mv.Name == builder.UpdateDBDirName || mv.Name == builder.JfsDirName {
continue
}
mountIndex := 0
Expand Down Expand Up @@ -215,18 +204,14 @@ func (s *SidecarMutate) injectContainer(pod *corev1.Pod, container corev1.Contai
pod.Spec.Containers = append([]corev1.Container{container}, pod.Spec.Containers...)
}

func (s *SidecarMutate) injectInitContainer(pod *corev1.Pod, container corev1.Container) {
pod.Spec.InitContainers = append([]corev1.Container{container}, pod.Spec.InitContainers...)
}

func (s *SidecarMutate) injectVolume(pod *corev1.Pod, build builder.SidecarInterface, volumes []corev1.Volume, mountPath string, pair util.PVPair) {
mountedVolume := []corev1.Volume{}
podVolumes := make(map[string]bool)
for _, volume := range pod.Spec.Volumes {
podVolumes[volume.Name] = true
}
for _, v := range volumes {
if v.Name == builder.UpdateDBDirName || v.Name == builder.JfsDirName || v.Name == builder.JfsRootDirName {
if v.Name == builder.UpdateDBDirName || v.Name == builder.JfsDirName {
if _, ok := podVolumes[v.Name]; ok {
continue
}
Expand Down
33 changes: 0 additions & 33 deletions pkg/webhook/handler/mutate/sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,39 +190,6 @@ func TestSidecarMutate_injectVolume(t *testing.T) {
}
}

func TestSidecarMutate_injectInitContainer(t *testing.T) {
type args struct {
pod *corev1.Pod
container corev1.Container
}
tests := []struct {
name string
args args
wantInitContainerLen int
}{
{
name: "test inject init container",
args: args{
pod: &corev1.Pod{},
container: corev1.Container{
Name: "format",
Image: "juicedata/mount:latest",
},
},
wantInitContainerLen: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &SidecarMutate{}
s.injectInitContainer(tt.args.pod, tt.args.container)
if len(tt.args.pod.Spec.InitContainers) != tt.wantInitContainerLen {
t.Errorf("injectInitContainer() = %v, want %v", tt.args.pod.Spec.InitContainers, tt.wantInitContainerLen)
}
})
}
}

func TestSidecarMutate_injectContainer(t *testing.T) {
type args struct {
pod *corev1.Pod
Expand Down

0 comments on commit e7b8f16

Please sign in to comment.