Skip to content

Commit

Permalink
fix: user config template patch strategy will discard some files (#7900)
Browse files Browse the repository at this point in the history
(cherry picked from commit 5f0cd21)
  • Loading branch information
sophon-zt committed Aug 5, 2024
1 parent 85597aa commit 621d117
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
8 changes: 4 additions & 4 deletions pkg/controller/configuration/template_merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,21 @@ func (c *configPatcher) Merge(baseData map[string]string, updatedData map[string
return baseData, nil
}

r := make(map[string]string)
mergedData := copyMap(baseData)
params := core.GenerateVisualizedParamsList(configPatch, formatter, nil)
for key, patch := range splitParameters(params) {
v, ok := baseData[key]
if !ok {
r[key] = updatedData[key]
mergedData[key] = updatedData[key]
continue
}
newConfig, err := core.ApplyConfigPatch([]byte(v), patch, formatter)
if err != nil {
return nil, err
}
r[key] = newConfig
mergedData[key] = newConfig
}
return r, err
return mergedData, err
}

func (c *configReplaceMerger) Merge(baseData map[string]string, updatedData map[string]string) (map[string]string, error) {
Expand Down
13 changes: 11 additions & 2 deletions pkg/controller/configuration/template_merger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ xengine_block_cache_size=307
xengine_row_cache_size=102
max_connections=666
`

testString := "// this is a test string"

const (
baseCMName = "base-cm"
updatedCMName = "updated-cm"

testConfigSpecName = "test-config"
testClusterName = "test-cluster"
testConfigName = "my.cnf"
testConfig2Name = "test.txt"
)

var (
Expand All @@ -82,12 +86,14 @@ max_connections=666
&appsv1beta1.ConfigConstraint{})
baseCMObject = &corev1.ConfigMap{
Data: map[string]string{
testConfigName: baseConfig,
testConfigName: baseConfig,
testConfig2Name: testString,
},
}
updatedCMObject = &corev1.ConfigMap{
Data: map[string]string{
testConfigName: extendConfig,
testConfigName: extendConfig,
testConfig2Name: testString,
},
}
baseCMObject.SetName(baseCMName)
Expand All @@ -101,6 +107,7 @@ max_connections=666
TemplateRef: baseCMObject.GetName(),
Namespace: "default",
},
Keys: []string{"my.cnf"},
ConfigConstraintRef: configConstraintObj.GetName(),
}

Expand Down Expand Up @@ -141,6 +148,7 @@ max_connections=666
tmpCM := baseCMObject.DeepCopy()
mergedData, err := mergerConfigTemplate(importedTemplate, templateBuilder, configSpec, tmpCM.Data, ctx, mockClient.Client())
Expect(err).To(Succeed())
Expect(mergedData).Should(HaveLen(2))

configReaders, err := cfgcore.LoadRawConfigObject(mergedData, configConstraintObj.Spec.FileFormatConfig, configSpec.Keys)
Expect(err).Should(Succeed())
Expand Down Expand Up @@ -169,6 +177,7 @@ max_connections=666
tmpCM := baseCMObject.DeepCopy()
mergedData, err := mergerConfigTemplate(importedTemplate, templateBuilder, configSpec, tmpCM.Data, ctx, mockClient.Client())
Expect(err).Should(Succeed())
Expect(mergedData).Should(HaveLen(2))
Expect(reflect.DeepEqual(mergedData, updatedCMObject.Data)).Should(BeTrue())

configReaders, err := cfgcore.LoadRawConfigObject(mergedData, configConstraintObj.Spec.FileFormatConfig, configSpec.Keys)
Expand Down
8 changes: 8 additions & 0 deletions pkg/controller/configuration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ func inDataContext() *multicluster.ClientOption {
func inDataContextUnspecified() *multicluster.ClientOption {
return multicluster.InDataContextUnspecified()
}

func copyMap(data map[string]string) map[string]string {
r := make(map[string]string, len(data))
for k, v := range data {
r[k] = v
}
return r
}

0 comments on commit 621d117

Please sign in to comment.