Skip to content

Commit

Permalink
fix: invalid value when migrating agent group config
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhengYa-0110 committed Dec 12, 2024
1 parent 2f7a32e commit dfd8b63
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 16 deletions.
91 changes: 81 additions & 10 deletions server/agent_config/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ func Upgrade(lowerVersionDBData *AgentGroupConfigModel, domainData *DomainData)
if err != nil {
return []byte{}, err
}
upgrader := &Upgrader{
MigrationToolData: toolData,
}
upgrader := newUpgrader(toolData)
return upgrader.Upgrade(lowerVersionYAMLBytes)
}

Expand All @@ -65,6 +63,15 @@ type Upgrader struct {
dictDataConv

MigrationToolData

spacialLowerVersionKeyToValue map[string]interface{}
}

func newUpgrader(toolData MigrationToolData) *Upgrader {
return &Upgrader{
MigrationToolData: toolData,
spacialLowerVersionKeyToValue: make(map[string]interface{}),
}
}

func (m *Upgrader) Upgrade(bytes []byte) ([]byte, error) {
Expand All @@ -75,9 +82,57 @@ func (m *Upgrader) Upgrade(bytes []byte) ([]byte, error) {
}
result := make(map[string]interface{})
m.lowerToHigher(lowerVerData, "", result)
m.appendSpecialLowerVersionKeyToValue(result)
return mapToYaml(result)
}

func (m *Upgrader) appendSpecialLowerVersionKeyToValue(result map[string]interface{}) {
hasProcessMatcher := false
if inputs, ok := result["inputs"]; ok {
if proc, ok := inputs.(map[string]interface{})["proc"]; ok {
if _, ok := proc.(map[string]interface{})["process_matcher"]; ok {
hasProcessMatcher = true
if lowerVal, ok := m.spacialLowerVersionKeyToValue["static_config.ebpf.uprobe-process-name-regexs.golang-symbol"]; ok {
switch value := proc.(map[string]interface{})["process_matcher"].(type) {
case []interface{}:
value = append(value, interface{}(map[string]interface{}{"match_regex": lowerVal}))
proc.(map[string]interface{})["process_matcher"] = value
case []map[string]interface{}:
value = append(value, map[string]interface{}{"match_regex": lowerVal})
proc.(map[string]interface{})["process_matcher"] = value
default:
}
}
if lowerVal, ok := m.spacialLowerVersionKeyToValue["os-proc-sync-tagged-only"]; ok {
switch value := proc.(map[string]interface{})["process_matcher"].(type) {
case []interface{}:
for i := range value {
value[i].(map[string]interface{})["only_with_tag"] = lowerVal
}
case []map[string]interface{}:
for i := range value {
value[i]["only_with_tag"] = lowerVal
}
default:
}
}
}
}
}
if !hasProcessMatcher {
value := make(map[string]interface{})
if lowerVal1, ok := m.spacialLowerVersionKeyToValue["static_config.ebpf.uprobe-process-name-regexs.golang-symbol"]; ok {
value["match_regex"] = lowerVal1
if lowerVal2, ok := m.spacialLowerVersionKeyToValue["os-proc-sync-tagged-only"]; ok {
value["only_with_tag"] = lowerVal2
}
}
if len(value) > 0 {
m.setNestedValue(result, "inputs.proc.process_matcher", []interface{}{value})
}
}
}

func (m *Upgrader) lowerToHigher(lowerVerData interface{}, ancestor string, higherVerData map[string]interface{}) {
switch data := lowerVerData.(type) {
case map[string]interface{}:
Expand All @@ -87,11 +142,15 @@ func (m *Upgrader) lowerToHigher(lowerVerData interface{}, ancestor string, high
lowers := m.higherVerToLowerVerKeys[higher]
if len(lowers) > 1 {
for _, lower := range lowers {
log.Warnf("%s has been upgraded to %s, please configure it manually", lower, higher) // TODO return?
log.Warnf("%s has been upgraded to %s, please configure it manually", lower, higher)
}
} else {
m.setNestedValue(higherVerData, higher, m.fmtLowerVersionValue(newAncestor, value))
}
} else if newAncestor == "os-proc-sync-tagged-only" {
// 升级 static_config.os-proc-sync-tagged-only 时,需要:
// 1. 将 inputs.proc.process_matcher 里所有的 only_with_tag 设置为 static_config.os-proc-sync-tagged-only
m.setSpecialLowerVersionKeyToValue(newAncestor, value)
}
m.lowerToHigher(value, newAncestor, higherVerData)
}
Expand Down Expand Up @@ -198,17 +257,29 @@ func (m *Upgrader) fmtLowerVersionValue(longKey string, value interface{}) inter
default:
return value
}
} else if longKey == "static_config.ebpf.uprobe-process-name-regexs.golang-symbol" {
// 升级 static_config.ebpf.uprobe-process-name-regexs.golang-symbol 时,需要:
// 1. 将 inputs.proc.symbol_table.golang_specific.enabled 设置为 true
// 2. 新增一条 inputs.proc.process_matcher
switch value := value.(type) {
case string:
if value == "" {
return false
}
m.setSpecialLowerVersionKeyToValue(longKey, value)
return true
default:
return false
}
}
// TODO
// 升级 static_config.ebpf.uprobe-process-name-regexs.golang-symbol 时,需要:
// 1. 将 inputs.proc.symbol_table.golang_specific.enabled 设置为 true
// 2. 新增一条 inputs.proc.process_matcher
// 升级 static_config.os-proc-sync-tagged-only 时,需要:
// 1. 将 inputs.proc.process_matcher 里所有的 only_with_tag 设置为 static_config.os-proc-sync-tagged-only

return m.convDictData(longKey, value)
}

func (m *Upgrader) setSpecialLowerVersionKeyToValue(key string, value interface{}) {
m.spacialLowerVersionKeyToValue[key] = value
}

func (m *Upgrader) convDictData(longKey string, value interface{}) interface{} {
convMap, ok := m.dictValLowerKeyToHigher[longKey]
if !ok {
Expand Down
112 changes: 106 additions & 6 deletions server/agent_config/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"os"
"reflect"
"strings"

"testing"
)
Expand Down Expand Up @@ -124,17 +125,116 @@ static_config:
forward-capacity: 300000`),
},
},
{
name: "case03_1",
args: args{
bytes: []byte(`os-proc-sync-tagged-only: true
static_config:
ebpf:
uprobe-process-name-regexs:
golang-symbol: deepflow-.*`),
},
want: []byte(`inputs:
proc:
process_matcher:
- match_regex: deepflow-.*
only_with_tag: true
symbol_table:
golang_specific:
enabled: true
`),
},
{
name: "case03_2",
args: args{
bytes: []byte(`os-proc-sync-tagged-only: true`),
},
want: []byte(`{}
`),
},
{
name: "case03_3",
args: args{
bytes: []byte(`os-proc-sync-tagged-only: true
static_config:
os-proc-regex:
- match-regex: test-.*
ebpf:
uprobe-process-name-regexs:
golang-symbol: deepflow-.*`),
},
want: []byte(`inputs:
proc:
process_matcher:
- match_regex: test-.*
only_with_tag: true
- match_regex: deepflow-.*
only_with_tag: true
symbol_table:
golang_specific:
enabled: true
`),
},
{
name: "case03_4",
args: args{
bytes: []byte(`os-proc-sync-tagged-only: false
static_config:
os-proc-regex:
- match-regex: test-.*`),
},
want: []byte(`inputs:
proc:
process_matcher:
- match_regex: test-.*
only_with_tag: false
`),
},
{
name: "case03_5",
args: args{
bytes: []byte(`static_config:
os-proc-regex:
- match-regex: test-.*`),
},
want: []byte(`inputs:
proc:
process_matcher:
- match_regex: test-.*
`),
},
{
name: "case03_6",
args: args{
bytes: []byte(`static_config:
ebpf:
uprobe-process-name-regexs:
golang-symbol: deepflow-.*`),
},
want: []byte(`inputs:
proc:
process_matcher:
- match_regex: deepflow-.*
symbol_table:
golang_specific:
enabled: true
`),
},
}
for _, tt := range tests {
if !strings.HasPrefix(tt.name, "case03") {
continue
}
t.Run(tt.name, func(t *testing.T) {
toolData, err := NewMigrationToolData(nil)
if err != nil {
t.Fatalf("Failed to create toolData: %v", err)
return
}
migrator := &Upgrader{
MigrationToolData: toolData,
}
migrator := newUpgrader(toolData)
got, err := migrator.Upgrade(tt.args.bytes)
if (err != nil) != tt.wantErr {
t.Errorf("Upgrade() error = \"%v\", wantErr \"%v\"", err, tt.wantErr)
Expand All @@ -147,9 +247,9 @@ static_config:
if err := os.WriteFile(fmt.Sprintf("test_tmp/upgrade_%s_want.yaml", tt.name), tt.want, os.ModePerm); err != nil {
t.Fatalf("Failed to write to file: %v", err)
}
// if string(got) != string(tt.want) {
// t.Errorf("Upgrade() = \"%v\", want \"%v\"", string(got), string(tt.want))
// }
if string(got) != string(tt.want) {
t.Errorf("Upgrade() = \"%v\", want \"%v\"", string(got), string(tt.want))
}
})
}
}
Expand Down

0 comments on commit dfd8b63

Please sign in to comment.