Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass extra args when building fuse sidecar mutators #4223

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/application/inject/fuse/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (s *Injector) inject(in runtime.Object, runtimeInfos map[string]base.Runtim
return out, fmt.Errorf("can't find any supported platform-specific mutator in pod's metadata")
}

mutatorBuildOpts := mutator.MutatorBuildOpts{
mutatorBuildArgs := mutator.MutatorBuildArgs{
Client: s.client,
Log: s.log,
Specs: podSpecs,
Expand All @@ -176,9 +176,11 @@ func (s *Injector) inject(in runtime.Object, runtimeInfos map[string]base.Runtim
EnableUnprivilegedSidecar: utils.FuseSidecarUnprivileged(podSpecs.MetaObj.Labels),
SkipSidecarPostStartInject: utils.SkipSidecarPostStartInject(podSpecs.MetaObj.Labels),
},
ExtraArgs: mutator.FindExtraArgsFromMetadata(podSpecs.MetaObj, platform),
}

mtt, err := mutator.BuildMutator(mutatorBuildOpts, platform)
s.log.V(1).Info("building mutator with mutatorBuildArgs: %v", mutatorBuildArgs)
mtt, err := mutator.BuildMutator(mutatorBuildArgs, platform)
if err != nil {
return out, err
}
Expand Down
41 changes: 33 additions & 8 deletions pkg/application/inject/fuse/mutator/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package mutator

import (
"fmt"
"strings"

"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -35,22 +37,45 @@ type Mutator interface {
GetMutatedPodSpecs() *MutatingPodSpecs
}

type MutatorBuildOpts struct {
Options common.FuseSidecarInjectOption
Client client.Client
Log logr.Logger
Specs *MutatingPodSpecs
type MutatorBuildArgs struct {
Client client.Client
Log logr.Logger
Specs *MutatingPodSpecs
Options common.FuseSidecarInjectOption
ExtraArgs map[string]string
}

var mutatorBuildFn map[string]func(MutatorBuildOpts) Mutator = map[string]func(MutatorBuildOpts) Mutator{
func (args MutatorBuildArgs) String() string {
return fmt.Sprintf("{options: %v, extraArgs: %v}", args.Options, args.ExtraArgs)
}

var mutatorBuildFn map[string]func(MutatorBuildArgs) Mutator = map[string]func(MutatorBuildArgs) Mutator{
utils.PlatformDefault: NewDefaultMutator,
utils.PlatformUnprivileged: NewUnprivilegedMutator,
}

func BuildMutator(opts MutatorBuildOpts, platform string) (Mutator, error) {
func BuildMutator(args MutatorBuildArgs, platform string) (Mutator, error) {
if fn, ok := mutatorBuildFn[platform]; ok {
return fn(opts), nil
return fn(args), nil
}

return nil, fmt.Errorf("fuse sidecar mutator cannot be found for platform %s", platform)
}

// FindExtraArgsFromMetadata tries to get extra build args for a given mutator from a metaObj.
// For any platform-specific mutator, its extra args should be key-values and defined in the format of "{platform}.fluid.io/{key}={value}" in metaObj.annotaions.
func FindExtraArgsFromMetadata(metaObj metav1.ObjectMeta, platform string) (extraArgs map[string]string) {
extraArgs = make(map[string]string)
if len(metaObj.Annotations) == 0 || len(platform) == 0 {
return
}

platformPrefix := fmt.Sprintf("%s.%s", platform, common.LabelAnnotationPrefix)
for key, value := range metaObj.Annotations {
if strings.HasPrefix(key, platformPrefix) {
extraArgs[strings.TrimPrefix(key, platformPrefix)] = value
}
}

return
}
10 changes: 5 additions & 5 deletions pkg/application/inject/fuse/mutator/mutator_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ type DefaultMutator struct {
Specs *MutatingPodSpecs
}

func NewDefaultMutator(opts MutatorBuildOpts) Mutator {
func NewDefaultMutator(args MutatorBuildArgs) Mutator {
return &DefaultMutator{
options: opts.Options,
client: opts.Client,
log: opts.Log,
Specs: opts.Specs,
options: args.Options,
client: args.Client,
log: args.Log,
Specs: args.Specs,
}
}

Expand Down
77 changes: 77 additions & 0 deletions pkg/application/inject/fuse/mutator/mutator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2023 The Fluid Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mutator

import (
"reflect"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestFindExtraArgsFromMetadata(t *testing.T) {
type args struct {
metaObj metav1.ObjectMeta
platform string
}
tests := []struct {
name string
args args
wantExtraArgs map[string]string
}{
{
name: "empty_annotations",
args: args{
metaObj: metav1.ObjectMeta{
Annotations: nil,
},
platform: "myplatform",
},
wantExtraArgs: make(map[string]string),
},
{
name: "without_extra_args",
args: args{
metaObj: metav1.ObjectMeta{
Annotations: map[string]string{"foo": "bar"},
},
platform: "myplatform",
},
wantExtraArgs: make(map[string]string),
},
{
name: "with_extra_args",
args: args{
metaObj: metav1.ObjectMeta{
Annotations: map[string]string{"foo": "bar", "myplatform.fluid.io/key1": "value1", "myplatform.fluid.io/key2": "value2"},
},
platform: "myplatform",
},
wantExtraArgs: map[string]string{
"key1": "value1",
"key2": "value2",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotExtraArgs := FindExtraArgsFromMetadata(tt.args.metaObj, tt.args.platform); !reflect.DeepEqual(gotExtraArgs, tt.wantExtraArgs) {
t.Errorf("FindExtraArgsFromMetadata() = %v, want %v", gotExtraArgs, tt.wantExtraArgs)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type UnprivilegedMutator struct {

var _ Mutator = &UnprivilegedMutator{}

func NewUnprivilegedMutator(opts MutatorBuildOpts) Mutator {
func NewUnprivilegedMutator(opts MutatorBuildArgs) Mutator {
return &UnprivilegedMutator{
DefaultMutator: DefaultMutator{
options: opts.Options,
Expand Down
5 changes: 3 additions & 2 deletions pkg/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ type FuseSidecarInjectOption struct {
}

func (f FuseSidecarInjectOption) String() string {
return fmt.Sprintf("EnableCacheDir=%v;EnableUnprivilegedSidecar=%v",
return fmt.Sprintf("EnableCacheDir=%v;EnableUnprivilegedSidecar=%v;SkipSidecarPostStartInject=%v",
f.EnableCacheDir,
f.EnableUnprivilegedSidecar)
f.EnableUnprivilegedSidecar,
f.SkipSidecarPostStartInject)
}

// The Application which is using Fluid,
Expand Down
Loading