diff --git a/server/agent_config/migrator.go b/server/agent_config/migrator.go index 8f911a8848d..859bc8cb9d6 100644 --- a/server/agent_config/migrator.go +++ b/server/agent_config/migrator.go @@ -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": { diff --git a/server/agent_config/migrator_test.go b/server/agent_config/migrator_test.go index 76d3cae0ded..9c525ce9033 100644 --- a/server/agent_config/migrator_test.go +++ b/server/agent_config/migrator_test.go @@ -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 { @@ -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]) } } @@ -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": { @@ -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"}, }, @@ -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{ @@ -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{ @@ -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) } diff --git a/server/agent_config/template_json.go b/server/agent_config/template_json.go index 203e6b68940..9588f7b90b3 100644 --- a/server/agent_config/template_json.go +++ b/server/agent_config/template_json.go @@ -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 { @@ -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 { diff --git a/server/agent_config/template_json_test.go b/server/agent_config/template_json_test.go index 6cc62691ae2..74538cf2158 100644 --- a/server/agent_config/template_json_test.go +++ b/server/agent_config/template_json_test.go @@ -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, }, diff --git a/server/controller/common/const.go b/server/controller/common/const.go index 464fe930ed4..8ad55b17f1e 100644 --- a/server/controller/common/const.go +++ b/server/controller/common/const.go @@ -705,3 +705,5 @@ const ( const TRISOLARIS_NODE_TYPE_MASTER = "master" const CLICK_HOUSE_VERSION = "24" + +const TAP_TYPE_VALUE_CLOUD_NETWORK = 3 diff --git a/server/controller/http/service/agent_group_config.go b/server/controller/http/service/agent_group_config.go index e06388676f1..dc27cb43b17 100644 --- a/server/controller/http/service/agent_group_config.go +++ b/server/controller/http/service/agent_group_config.go @@ -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, }, @@ -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 @@ -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 } @@ -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 }