Skip to content

Commit

Permalink
feat: native sidecar
Browse files Browse the repository at this point in the history
Signed-off-by: Xuhui zhang <[email protected]>
  • Loading branch information
zxh326 committed Jan 24, 2025
1 parent c23373f commit df4433b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/scripts/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -2476,9 +2476,9 @@ def test_webhook_two_volume():
if len(pods.items) != 1:
raise Exception("Pods of deployment {} are not ready within 10 min.".format(deployment.name))
pod = pods.items[0]
if len(pod.spec.containers) != 3:
if len(pod.spec.containers) != 3 and len(pod.spec.init_containers) != 2:
raise Exception(
"Pod {} should have 3 containers, only {} has been found".format(pod.name, len(pod.spec.containers)))
"Pod {} should have 3 containers/ 2 init_containers, only {}/{} has been found".format(pod.name, len(pod.spec.containers), len(pod.spec.init_containers)))
LOG.info("Test pass.")

# delete test resources
Expand Down
7 changes: 5 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,11 @@ func (mpp *MountPodPatch) merge(mp MountPodPatch) {
// TODO: migrate more config for here
type Config struct {
// arrange mount pod to node with node selector instead nodeName
EnableNodeSelector bool `json:"enableNodeSelector,omitempty"`
MountPodPatch []MountPodPatch `json:"mountPodPatch"`
EnableNodeSelector bool `json:"enableNodeSelector,omitempty"`
// in sidecar mode, use k8s native sidecar instead of container
// If the k8s version is 1.29 and later, the default is true.
EnableNativeSidecar *bool `json:"enableNativeSidecar,omitempty"`
MountPodPatch []MountPodPatch `json:"mountPodPatch"`
}

func (c *Config) Unmarshal(data []byte) error {
Expand Down
8 changes: 8 additions & 0 deletions pkg/controller/app_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ func ShouldInQueue(pod *corev1.Pod) bool {
return false
}

// ignore if fuse container is running in init container
for _, cn := range pod.Spec.InitContainers {
if strings.Contains(cn.Name, common.MountContainerName) {
log.V(1).Info("There are juicefs sidecar running in init-container, no need to handle.")
return false
}
}

// ignore if no fuse container
exist := false
for _, cn := range pod.Spec.Containers {
Expand Down
63 changes: 54 additions & 9 deletions pkg/webhook/handler/mutate/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/version"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"

Expand All @@ -38,13 +39,29 @@ import (
)

var (
sidecarLog = klog.NewKlogr().WithName("sidecar")
sidecarLog = klog.NewKlogr().WithName("sidecar")
minimumSupportedSidecarVersion = version.MustParseGeneric("1.29.0")
)

// checkSupportNativeSidecar checks if the native sidecar is supported by the server version.
// If the global config is set, it will override the server version.
func checkSupportNativeSidecar(v *version.Version) bool {
if config.GlobalConfig.EnableNativeSidecar != nil {
if *config.GlobalConfig.EnableNativeSidecar {
if v != nil && !v.AtLeast(minimumSupportedSidecarVersion) {
sidecarLog.Info("WARNING: globalConfig set to enable native sidecar, but the server may not support", "version", v)
}
}
return *config.GlobalConfig.EnableNativeSidecar
}
return v != nil && v.AtLeast(minimumSupportedSidecarVersion)
}

type SidecarMutate struct {
Client *k8sclient.K8sClient
juicefs juicefs.Interface
Serverless bool
Client *k8sclient.K8sClient
juicefs juicefs.Interface
Serverless bool
supportsNativeSidecar bool

Pair []resource.PVPair
jfsSetting *config.JfsSetting
Expand All @@ -53,11 +70,27 @@ type SidecarMutate struct {
var _ Mutate = &SidecarMutate{}

func NewSidecarMutate(client *k8sclient.K8sClient, jfs juicefs.Interface, serverless bool, pair []resource.PVPair) Mutate {
var serverVersion *version.Version
v, err := client.Discovery().ServerVersion()
if err != nil {
sidecarLog.Error(err, "get server version error")
} else {
serverVersion, err = version.ParseGeneric(v.String())
if err != nil {
sidecarLog.Error(err, "parse server version error")
}
}

supportsNativeSidecar := checkSupportNativeSidecar(serverVersion)
if supportsNativeSidecar {
sidecarLog.Info("native sidecar is supported, use init-container to mount juicefs", "version", serverVersion)
}
return &SidecarMutate{
Client: client,
juicefs: jfs,
Serverless: serverless,
Pair: pair,
Client: client,
juicefs: jfs,
Serverless: serverless,
Pair: pair,
supportsNativeSidecar: checkSupportNativeSidecar(serverVersion),
}
}

Expand Down Expand Up @@ -149,7 +182,13 @@ func (s *SidecarMutate) mutate(ctx context.Context, pod *corev1.Pod, pair resour

func (s *SidecarMutate) Deduplicate(pod, mountPod *corev1.Pod, index int) {
// deduplicate container name
for _, c := range pod.Spec.Containers {
var containers []corev1.Container
if s.supportsNativeSidecar {
containers = pod.Spec.InitContainers
} else {
containers = pod.Spec.Containers
}
for _, c := range containers {
if c.Name == mountPod.Spec.Containers[0].Name {
mountPod.Spec.Containers[0].Name = fmt.Sprintf("%s-%d", c.Name, index)
}
Expand Down Expand Up @@ -214,7 +253,13 @@ func (s *SidecarMutate) GetSettings(pv corev1.PersistentVolume) (secrets, volCtx
}

func (s *SidecarMutate) injectContainer(pod *corev1.Pod, container corev1.Container) {
if s.supportsNativeSidecar {
container.RestartPolicy = util.ToPtr(corev1.ContainerRestartPolicyAlways)
pod.Spec.InitContainers = append(pod.Spec.InitContainers, container)
return
}
pod.Spec.Containers = append([]corev1.Container{container}, pod.Spec.Containers...)

}

func (s *SidecarMutate) injectVolume(pod *corev1.Pod, build builder.SidecarInterface, volumes []corev1.Volume, mountPath string, pair resource.PVPair) {
Expand Down

0 comments on commit df4433b

Please sign in to comment.