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

fix: configmap mounted with subPath do not update when changed (#3870) #3883

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
15 changes: 13 additions & 2 deletions internal/configuration/config_manager/handler_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,27 @@ func GetSupportReloadConfigSpecs(configSpecs []appsv1alpha1.ComponentConfigSpec,
continue
}
reloadConfigSpecMeta = append(reloadConfigSpecMeta, ConfigSpecMeta{
ToolsImageSpec: cc.Spec.ToolsImageSpec,
ScriptConfig: cc.Spec.ScriptConfigs,
ConfigSpecInfo: ConfigSpecInfo{
ReloadOptions: cc.Spec.ReloadOptions,
ConfigSpec: configSpec,
ReloadType: FromReloadTypeConfig(reloadOptions),
DownwardAPIOptions: cc.Spec.DownwardAPIOptions,
FormatterConfig: *cc.Spec.FormatterConfig,
},
ToolsImageSpec: cc.Spec.ToolsImageSpec,
ScriptConfig: cc.Spec.ScriptConfigs,
})
}
return reloadConfigSpecMeta, nil
}

func FilterSubPathVolumeMount(metas []ConfigSpecMeta, volumes []corev1.VolumeMount) []ConfigSpecMeta {
var filtered []ConfigSpecMeta
for _, meta := range metas {
v := FindVolumeMount(volumes, meta.ConfigSpec.VolumeName)
if v == nil || v.SubPath == "" || meta.ReloadType == appsv1alpha1.TPLScriptType {
filtered = append(filtered, meta)
}
}
return filtered
}
78 changes: 78 additions & 0 deletions internal/configuration/config_manager/handler_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -372,3 +373,80 @@ var _ = Describe("Handler Util Test", func() {
})
})
})

func TestFilterSubPathVolumeMount(t *testing.T) {
createConfigMeta := func(volumeName string, reloadType appsv1alpha1.CfgReloadType) ConfigSpecMeta {
return ConfigSpecMeta{ConfigSpecInfo: ConfigSpecInfo{
ReloadType: reloadType,
ConfigSpec: appsv1alpha1.ComponentConfigSpec{
ComponentTemplateSpec: appsv1alpha1.ComponentTemplateSpec{
VolumeName: volumeName,
}}}}
}

type args struct {
metas []ConfigSpecMeta
volumes []corev1.VolumeMount
}
tests := []struct {
name string
args args
want []ConfigSpecMeta
}{{
name: "test1",
args: args{
metas: []ConfigSpecMeta{
createConfigMeta("test1", appsv1alpha1.UnixSignalType),
createConfigMeta("test2", appsv1alpha1.ShellType),
createConfigMeta("test3", appsv1alpha1.TPLScriptType),
},
volumes: []corev1.VolumeMount{
{Name: "test1", SubPath: "test1"},
{Name: "test2", SubPath: "test2"},
{Name: "test3", SubPath: "test3"},
},
},
want: []ConfigSpecMeta{
createConfigMeta("test3", appsv1alpha1.TPLScriptType),
},
}, {
name: "test2",
args: args{
metas: []ConfigSpecMeta{
createConfigMeta("test1", appsv1alpha1.UnixSignalType),
createConfigMeta("test2", appsv1alpha1.ShellType),
createConfigMeta("test3", appsv1alpha1.TPLScriptType),
},
volumes: []corev1.VolumeMount{
{Name: "test1"},
{Name: "test2"},
{Name: "test3"},
},
},
want: []ConfigSpecMeta{
createConfigMeta("test1", appsv1alpha1.UnixSignalType),
createConfigMeta("test2", appsv1alpha1.ShellType),
createConfigMeta("test3", appsv1alpha1.TPLScriptType),
},
}, {
name: "test3",
args: args{
metas: []ConfigSpecMeta{
createConfigMeta("test1", appsv1alpha1.UnixSignalType),
createConfigMeta("test2", appsv1alpha1.ShellType),
createConfigMeta("test3", appsv1alpha1.TPLScriptType),
},
volumes: []corev1.VolumeMount{},
},
want: []ConfigSpecMeta{
createConfigMeta("test1", appsv1alpha1.UnixSignalType),
createConfigMeta("test2", appsv1alpha1.ShellType),
createConfigMeta("test3", appsv1alpha1.TPLScriptType),
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, FilterSubPathVolumeMount(tt.args.metas, tt.args.volumes), "FilterSubPathVolumeMount(%v, %v)", tt.args.metas, tt.args.volumes)
})
}
}
3 changes: 2 additions & 1 deletion internal/configuration/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
cfgutil "github.com/apecloud/kubeblocks/internal/configuration/util"
)

type ParamPairs struct {
Expand All @@ -42,7 +43,7 @@ func MergeAndValidateConfigs(configConstraint appsv1alpha1.ConfigConstraintSpec,

newCfg map[string]string
configOperator ConfigOperator
updatedKeys = set.NewLinkedHashSetString()
updatedKeys = cfgutil.NewSet()
)

cmKeySet := FromCMKeysSelector(cmKey)
Expand Down
3 changes: 2 additions & 1 deletion internal/configuration/config_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
cfgutil "github.com/apecloud/kubeblocks/internal/configuration/util"
testapps "github.com/apecloud/kubeblocks/internal/testutil/apps"
testutil "github.com/apecloud/kubeblocks/internal/testutil/k8s"
"github.com/apecloud/kubeblocks/test/testdata"
Expand Down Expand Up @@ -266,7 +267,7 @@ func TestFromUpdatedConfig(t *testing.T) {
"key2": "config context2",
"key3": "config context2",
},
sets: set.NewLinkedHashSetString(),
sets: cfgutil.NewSet(),
},
want: map[string]string{},
}}
Expand Down
15 changes: 6 additions & 9 deletions internal/controller/plan/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"fmt"
"strings"

"github.com/StudioSol/set"
"github.com/spf13/viper"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -118,13 +117,8 @@ func updateResourceAnnotationsWithTemplate(obj client.Object, allTemplateAnnotat
// into PodSpec if configuration reload option is on
func buildConfigManagerWithComponent(podSpec *corev1.PodSpec, configSpecs []appsv1alpha1.ComponentConfigSpec,
ctx context.Context, cli client.Client, cluster *appsv1alpha1.Cluster, component *component.SynthesizedComponent) error {
var (
err error

buildParams *cfgcm.CfgManagerBuildParams
// volumeDirs []corev1.VolumeMount
// usingConfigSpecs []appsv1alpha1.ComponentConfigSpec
)
var err error
var buildParams *cfgcm.CfgManagerBuildParams

volumeDirs, usingConfigSpecs := getUsingVolumesByConfigSpecs(podSpec, configSpecs)
if len(volumeDirs) == 0 {
Expand All @@ -134,6 +128,9 @@ func buildConfigManagerWithComponent(podSpec *corev1.PodSpec, configSpecs []apps
if err != nil {
return err
}
// Configmap uses subPath case: https://github.com/kubernetes/kubernetes/issues/50345
// The files are being updated on the host VM, but can't be updated in the container.
configSpecMetas = cfgcm.FilterSubPathVolumeMount(configSpecMetas, volumeDirs)
if len(configSpecMetas) == 0 {
return nil
}
Expand Down Expand Up @@ -240,7 +237,7 @@ func getUsingVolumesByConfigSpecs(podSpec *corev1.PodSpec, configSpecs []appsv1a
if !cfgcore.NeedReloadVolume(configSpec) {
continue
}
sets := set.NewLinkedHashSetString()
sets := util.NewSet()
for _, container := range config2Containers[configSpec.Name] {
volume := intctrlutil.GetVolumeMountByVolume(container, configSpec.VolumeName)
if volume != nil && !sets.InArray(volume.Name) {
Expand Down
3 changes: 2 additions & 1 deletion internal/gotemplate/tpl_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

cfgcore "github.com/apecloud/kubeblocks/internal/configuration"
cfgutil "github.com/apecloud/kubeblocks/internal/configuration/util"
types2 "github.com/apecloud/kubeblocks/internal/controller/client"
)

Expand Down Expand Up @@ -194,7 +195,7 @@ func NewTplEngine(values *TplValues, funcs *BuiltInObjectsFunc, tplName string,
tplValues: values,
ctx: ctx,
cli: cli,
importModules: set.NewLinkedHashSetString(),
importModules: cfgutil.NewSet(),
importFuncs: make(map[string]functional),
}

Expand Down