Skip to content

Commit

Permalink
fix: failed to update old version agent group config when creating ne…
Browse files Browse the repository at this point in the history
…w version
  • Loading branch information
ZhengYa-0110 authored and SongZhen0704 committed Dec 12, 2024
1 parent 1893620 commit 3e91c8f
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 31 deletions.
2 changes: 1 addition & 1 deletion server/agent_config/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func (p *MigrationToolData) fmtDictValKeyMap() {
// "enabled_features": []string{"static_config.ebpf.on-cpu-profile.regex", "static_config.ebpf.off-cpu-profile.regex"}, // 无法降级
},

"inputs.cdpf.af_packet.bond_interfaces": {
"inputs.cbpf.af_packet.bond_interfaces": {
"slave_interfaces": "tap-interfaces",
},
"inputs.resources.kubernetes.api_resources": {
Expand Down
101 changes: 84 additions & 17 deletions server/agent_config/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,34 @@ static_config:
value: HTTP
HTTP2:
- field-name: HTTP2-1
`),
wantErr: false,
},
{
name: "case02",
args: args{
bytes: []byte(`inputs:
cbpf:
af_packet:
bond_interfaces:
- slave_interfaces:
- eth0
- eth1
`),
},
want: []byte(`static_config:
tap-interface-bond-groups:
- tap-interfaces:
- eth0
- eth1
`),
wantErr: false,
},
}
for _, tt := range tests {
if tt.name != "case02" {
continue
}
t.Run(tt.name, func(t *testing.T) {
toolData, err := NewMigrationToolData(nil)
if err != nil {
Expand Down Expand Up @@ -387,32 +410,31 @@ func TestGenerateUpgradeHigherToLower(t *testing.T) {
name: "case02",
args: args{},
want: map[string][]string{
"inputs.proc.process_matcher": {"static_config.os-proc-regex"},
"inputs.proc.tag_extraction.process_matcher.rewrite_name": {"static_config.os-proc-regex.rewrite-name"},
"inputs.proc.process_matcher": {"static_config.os-proc-regex"},
"inputs.cbpf.af_packet.bond_interfaces": {"static_config.tap-interface-bond-groups"},
},
wantErr: false,
},
}
for _, tt := range tests {
if tt.name != "case02" {
continue
}
t.Run(tt.name, func(t *testing.T) {
fmt, err := NewMigrationToolData(nil)
fmtt, err := NewMigrationToolData(nil)
if err != nil {
t.Fatalf("Failed to create parser: %v", err)
}
fmt.Format()
if (err != nil) != tt.wantErr {
t.Errorf("getHigherToLowers() error = \"%v\", wantErr \"%v\"", err, tt.wantErr)
return
}
if len(fmt.higherVerToLowerVerKeys) != len(tt.want) {
t.Errorf("getHigherToLowers() = \"%v\", want \"%v\"", fmt.higherVerToLowerVerKeys, tt.want)
}
for k, v := range tt.want {
if len(v) != len(fmt.higherVerToLowerVerKeys[k]) {
if len(v) != len(fmtt.higherVerToLowerVerKeys[k]) {
t.Errorf("field %s getHigherToLowers() = \"%v\", want \"%v\"", k, v, tt.want[k])
}
for i := 0; i < len(v); i++ {
if v[i] != fmt.higherVerToLowerVerKeys[k][i] {
if v[i] != fmtt.higherVerToLowerVerKeys[k][i] {
t.Errorf("field %s %d getHigherToLowers() = \"%v\", want \"%v\"", k, i, v[i], tt.want[k][i])
}
}
Expand Down Expand Up @@ -569,7 +591,7 @@ func TestLowerToHigher(t *testing.T) {
name: "case02",
args: args{
lowerToHigher: map[string]string{
"static_config.tap-interface-bond-groups": "inputs.resources.kubernetes.api_resources",
"static_config.tap-interface-bond-groups": "inputs.cbpf.af_packet.bond_interfaces",
},
dictValLowerKeyToHigher: map[string]map[string]interface{}{
"static_config.tap-interface-bond-groups": {
Expand All @@ -593,9 +615,9 @@ func TestLowerToHigher(t *testing.T) {
},
want: map[string]interface{}{
"inputs": map[string]interface{}{
"resources": map[string]interface{}{
"kubernetes": map[string]interface{}{
"api_resources": []map[string]interface{}{
"cbpf": map[string]interface{}{
"af_packet": map[string]interface{}{
"bond_interfaces": []map[string]interface{}{
{
"slave_interfaces": []string{"eth0", "eth1"},
},
Expand All @@ -610,9 +632,9 @@ func TestLowerToHigher(t *testing.T) {
},
}
for _, tt := range tests {
if tt.name != "case02" {
continue
}
// if tt.name != "case02" {
// continue
// }
t.Run(tt.name, func(t *testing.T) {
migrator := &Upgrader{
MigrationToolData: MigrationToolData{
Expand Down Expand Up @@ -800,8 +822,54 @@ func TestHigherToLower(t *testing.T) {
},
},
},
{
name: "case04",
args: args{
higherToLower: map[string][]string{
"inputs.cbpf.af_packet.bond_interfaces": {"static_config.tap-interface-bond-groups"},
},
dictValHigherKeyToLower: map[string]map[string]interface{}{
"inputs.cbpf.af_packet.bond_interfaces": {
"slave_interfaces": "tap-interfaces",
},
},
ancestor: "",
data: map[string]interface{}{
"inputs": map[string]interface{}{
"cbpf": map[string]interface{}{
"af_packet": map[string]interface{}{
"bond_interfaces": []map[string]interface{}{
{
"slave_interfaces": []string{"eth0", "eth1"},
},
{
"slave_interfaces": []string{"eth2", "eth3"},
},
},
},
},
},
},
result: map[string]interface{}{},
},
want: map[string]interface{}{
"static_config": map[string]interface{}{
"tap-interface-bond-groups": []map[string]interface{}{
{
"tap-interfaces": []string{"eth0", "eth1"},
},
{
"tap-interfaces": []string{"eth2", "eth3"},
},
},
},
},
},
}
for _, tt := range tests {
if tt.name != "case04" {
continue
}
t.Run(tt.name, func(t *testing.T) {
migrator := &Downgrader{
MigrationToolData: MigrationToolData{
Expand Down Expand Up @@ -976,7 +1044,6 @@ func TestConvDictDataValue(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
dictConv := &dictDataConv{}
got := dictConv.convDictDataValue(tt.args.data, tt.args.convMap, tt.args.longKey)
fmt.Printf("got: %v\n", got)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("convDictDataValue() = %v, want %v", got, tt.want)
}
Expand Down
19 changes: 11 additions & 8 deletions server/agent_config/template_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,11 @@ func (f *DataFomatter) LoadMapData(data map[string]interface{}) error {
}

func (f *DataFomatter) mapToYAML() ([]byte, error) {
var buf strings.Builder
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
err := enc.Encode(f.mapData)
str, err := f.dictToString(f.mapData)
if err != nil {
return nil, err
return nil, fmt.Errorf("convert dict to string error: %v", err)
}
return []byte(buf.String()), nil
return []byte(str), nil
}

func (f *DataFomatter) LoadYAMLData(yamlData []byte) error {
Expand Down Expand Up @@ -697,8 +694,14 @@ func (f *DataFomatter) formatJson(data interface{}) ([]byte, error) {
}

func (f *DataFomatter) dictToString(data interface{}) (string, error) {
bytes, err := yaml.Marshal(data)
return string(bytes), err
var buf strings.Builder
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
err := enc.Encode(data)
if err != nil {
return "", err
}
return buf.String(), nil
}

func (f *DataFomatter) isKeyComment(key string) bool {
Expand Down
20 changes: 20 additions & 0 deletions server/agent_config/template_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,26 @@ func TestConvertJSONToYAMLAndValidate(t *testing.T) {
resources:
kubernetes:
kubernetes_namespace: "111"
`),
wantErr: false,
},
{
name: "case08",
args: args{
jsonData: map[string]interface{}{
"inputs": map[string]interface{}{
"cbpf": map[string]interface{}{
"af_packet": map[string]interface{}{
"bond_interfaces": "- slave_interfaces: [eth0, eth1]\r\n- slave_interfaces: [eth2, eth3]",
},
},
},
},
},
want: []byte(`inputs:
cbpf:
af_packet:
bond_interfaces: "- slave_interfaces:\n - eth0\n - eth1\n- slave_interfaces:\n - eth2\n - eth3\n"
`),
wantErr: false,
},
Expand Down
2 changes: 2 additions & 0 deletions server/controller/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,3 +705,5 @@ const (
const TRISOLARIS_NODE_TYPE_MASTER = "master"

const CLICK_HOUSE_VERSION = "24"

const TAP_TYPE_VALUE_CLOUD_NETWORK = 3
14 changes: 9 additions & 5 deletions server/controller/http/service/agent_group_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ func (a *AgentGroupConfig) GetAgentGroupConfigTemplateJson() ([]byte, error) {
return nil, err
}
var tapTypes []model.TapType
if err := dbInfo.Select("id", "name").Find(&tapTypes).Error; err != nil {
if err := dbInfo.Where("value != ?", common.TAP_TYPE_VALUE_CLOUD_NETWORK).Select("value", "name").Find(&tapTypes).Error; err != nil {
return nil, err
}
tapTypInfos := make([]map[string]interface{}, len(tapTypes))
for i, tapType := range tapTypes {
tapTypInfos[i] = map[string]interface{}{
strconv.Itoa(tapType.ID): map[string]interface{}{
strconv.Itoa(tapType.Value): map[string]interface{}{
"ch": tapType.Name,
"en": tapType.Name,
},
Expand Down Expand Up @@ -241,7 +241,11 @@ func (a *AgentGroupConfig) CreateAgentGroupConfig(groupLcuuid string, data inter
if err != nil {
return nil, err
}
log.Infof("create agent group config, group lcuuid: %s, data: %#v, data type: %d", groupLcuuid, data, dataType, dbInfo.LogPrefixORGID)
if dataType == DataTypeJSON {
log.Infof("create agent group config, group lcuuid: %s, data: %#v, data type: %d", groupLcuuid, data, dataType, dbInfo.LogPrefixORGID)
} else {
log.Infof("create agent group config, group lcuuid: %s, data: %s, data type: %d", groupLcuuid, data.(string), dataType, dbInfo.LogPrefixORGID)
}
var agentGroup model.VTapGroup
if err := dbInfo.Where("lcuuid = ?", groupLcuuid).First(&agentGroup).Error; err != nil {
return nil, err
Expand All @@ -263,8 +267,6 @@ func (a *AgentGroupConfig) CreateAgentGroupConfig(groupLcuuid string, data inter
if err := dbInfo.Create(newConfig).Error; err != nil {
return nil, err
}

return a.GetAgentGroupConfig(groupLcuuid, dataType)
}
return nil, err
}
Expand Down Expand Up @@ -306,6 +308,8 @@ func (a *AgentGroupConfig) compatibleWithOldVersion(dbInfo *mysql.DB, groupLcuui
if err := dbInfo.Create(vtapGroupConfig).Error; err != nil {
log.Errorf("failed to create agent group lcuuid %s old version yaml: %v", groupLcuuid, err)
}
} else {
log.Errorf("failed to get agent group config (lcuuid %s): %v", groupLcuuid, err)
}
return
}
Expand Down

0 comments on commit 3e91c8f

Please sign in to comment.