Skip to content

Commit

Permalink
fix: add agent group get api
Browse files Browse the repository at this point in the history
  • Loading branch information
roryye committed Sep 5, 2024
1 parent 365eefe commit 0b43cfd
Show file tree
Hide file tree
Showing 15 changed files with 686 additions and 13 deletions.
3 changes: 3 additions & 0 deletions server/agent_config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import _ "embed"
//go:embed example.yaml
var YamlAgentGroupConfig []byte

//go:embed template.yaml
var YamlAgentGroupConfigTemplate []byte

type AgentGroupConfig struct {
VTapGroupID *string `json:"VTAP_GROUP_ID" yaml:"vtap_group_id,omitempty"`
VTapGroupLcuuid *string `json:"VTAP_GROUP_LCUUID" yaml:"vtap_group_lcuuid,omitempty"`
Expand Down
14 changes: 14 additions & 0 deletions server/agent_config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@

package agent_config

import "time"

type AgentGroupConfigYaml struct {
ID int `gorm:"primaryKey;column:id;type:int;not null" json:"ID"`
Lcuuid string `gorm:"column:lcuuid;type:char(64);default:not null" json:"LCUUID"`
Yaml string `gorm:"column:yaml;type:text;default:not null" json:"YAML"`
CreatedAt time.Time `gorm:"column:created_at;type:timestamp;default:not null" json:"CREATED_AT"`
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp;default:not null" json:"UPDATED_AT"`
}

func (AgentGroupConfigYaml) TableName() string {
return "agent_group_configuration"
}

type AgentGroupConfigModel struct {
ID int `gorm:"primaryKey;column:id;type:int;not null" json:"ID"`
MaxCollectPps *int `gorm:"column:max_collect_pps;type:int;default:null" json:"MAX_COLLECT_PPS"`
Expand Down
4 changes: 2 additions & 2 deletions server/agent_config/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3027,7 +3027,7 @@ inputs:
# description:
# en: |-
# Used when deepflow-agent has only one k8s namespace query permission.
# en: |-
# ch: |-
# TODO
# upgrade_from: static_config.kubernetes-namespace
# TODO: 英文释义待明确。
Expand Down Expand Up @@ -4339,7 +4339,7 @@ processors:
# Used to extract the SpanID field in HTTP and RPC headers, supports filling
# in multiple values separated by commas. This feature can be turned off by
# setting it to empty.
# ch: |-
# ch: |-
# 配置该参数后,deepflow-agent 会尝试从 HTTP 和 RPC header 中匹配特征字段,并将匹配到
# 的结果填充到应用调用日志的`span_id`字段中,作为调用链追踪的特征值。参数支持填写多个不同的
# 特征字段,中间用`,`分隔。
Expand Down
277 changes: 277 additions & 0 deletions server/agent_config/template_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
/*
* Copyright (c) 2024 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package agent_config

import (
"encoding/json"
"fmt"
"strings"

"gopkg.in/yaml.v3"
)

func ParseYAMLToJson(yamlData []byte) ([]byte, error) {
formatedTemplate, err := formatTemplateYAML(yamlData)
if err != nil {
return nil, err
}

var node yaml.Node
err = yaml.Unmarshal(formatedTemplate, &node)
if err != nil {
return nil, err
}

jsonData, err := yamlNodeToMap(&node)
if err != nil {
return nil, err
}

jsonBytes, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
return nil, err
}
return jsonBytes, nil
}

func formatTemplateYAML(yamlData []byte) ([]byte, error) {
indentedLines, err := IndentTemplate(yamlData)
if err != nil {
return nil, err
}
return UncommentTemplate(indentedLines)
}

func IndentTemplate(yamlData []byte) ([]string, error) {
yamlStr := string(YamlAgentGroupConfigTemplate)
lines := strings.Split(yamlStr, "\n")

var indentedLines []string
var tempLines []string
for i := 0; i < len(lines); i++ {
if strings.Contains(lines[i], "---") {
j := i + 1
for ; j < len(lines); j++ {
if strings.Contains(lines[j], "---") {
break
}
}
i = j + 1
continue
}

if strings.Contains(lines[i], "#") {
if !strings.HasPrefix(strings.TrimSpace(lines[i]), "#") {
tempLines = append(tempLines, lines[i])
} else {
// add indent to config
tempLines = append(tempLines, " "+lines[i])
}
continue
}

configNames := strings.Split(lines[i], ":")
if len(configNames) == 0 {
return nil, fmt.Errorf("line(index: %d, value: %s) split by \":\" failed", i, lines[i])
}
if len(tempLines) > 0 {
indentedLines = append(indentedLines, configNames[0]+"_comment:")
indentedLines = append(indentedLines, tempLines...)
}
indentedLines = append(indentedLines, lines[i])
tempLines = []string{}
}

return indentedLines, nil
}

// UncommentTemplate removes all lines containing "TODO" and uncomments all lines
// by removing the "#" prefix. It is used to convert the agent configuration
// template file to json format.
func UncommentTemplate(indentedLines []string) ([]byte, error) {
var uncommentedLines []string
for i := 0; i < len(indentedLines); i++ {
line := indentedLines[i]
if strings.Contains(line, "TODO") {
continue
}
if !strings.HasPrefix(strings.TrimSpace(line), "#") && strings.Contains(line, "#") {
uncommentedLines = append(uncommentedLines, line)
continue
}

line = strings.ReplaceAll(line, "# ", "")
line = strings.ReplaceAll(line, "#", "")
uncommentedLines = append(uncommentedLines, line)
}
return []byte(strings.Join(uncommentedLines, "\n")), nil
}

type OrderedMap struct {
keys []string
values map[string]interface{}
}

func (om *OrderedMap) MarshalJSON() ([]byte, error) {
var pairs []string
for _, key := range om.keys {
value, _ := json.Marshal(om.values[key])
pairs = append(pairs, fmt.Sprintf("%q:%s", key, string(value)))
}
return []byte("{" + strings.Join(pairs, ",") + "}"), nil
}

// yamlNodeToMap recursively converts a YAML node into a map, including comments as metadata.
func yamlNodeToMap(node *yaml.Node) (interface{}, error) {
switch node.Kind {
case yaml.DocumentNode:
return yamlNodeToMap(node.Content[0])
case yaml.MappingNode:
// Convert ordered JSON to YAML structure
om := &OrderedMap{
keys: make([]string, 0, len(node.Content)/2),
values: make(map[string]interface{}),
}
for i := 0; i < len(node.Content); i += 2 {
keyNode := node.Content[i]
valueNode := node.Content[i+1]

key := keyNode.Value
value, err := yamlNodeToMap(valueNode)
if err != nil {
return nil, err
}

om.keys = append(om.keys, key)
om.values[key] = value
}
return om, nil
case yaml.SequenceNode:
var seq []interface{}
for _, contentNode := range node.Content {
value, err := yamlNodeToMap(contentNode)
if err != nil {
return nil, err
}
seq = append(seq, value)
}
return seq, nil
case yaml.ScalarNode:
return node.Value, nil
default:
return nil, fmt.Errorf("unsupported YAML node kind: %v", node.Kind)
}
}

func ParseJsonToYAMLAndValidate(jsonData map[string]interface{}) ([]byte, error) {
return nil, nil
// b, err := yaml.Marshal(jsonData)
// if err != nil {
// return nil, err
// }
// yamlData, err := k8syaml.JSONToYAML(b)
// if err != nil {
// return nil, err
// }
// var node yaml.Node
// err = yaml.Unmarshal(yamlData, &node)
// if err != nil {
// return nil, fmt.Errorf("unmarshal data to yaml node error: %v", err)
// }

// formatedTemplate, err := formatTemplateYAML(YamlAgentGroupConfigTemplate)
// if err != nil {
// return nil, err
// }
// var nodeTemplate yaml.Node
// err = yaml.Unmarshal(formatedTemplate, &nodeTemplate)
// if err != nil {
// return nil, fmt.Errorf("unmarshal template data to yaml node error: %v", err)
// }

// // 检查 node 中的每一项是否包含在 nodeTemplate 中
// err = validateNodeAgainstTemplate(&node, &nodeTemplate)
// if err != nil {
// return nil, fmt.Errorf("验证配置失败: %v", err)
// }

// return yamlData, nil
}

func validateNodeAgainstTemplate(node, nodeTemplate *yaml.Node) error {
if node.Kind != nodeTemplate.Kind {
return fmt.Errorf("node kind mismatch: expected %v, got %v", nodeTemplate.Kind, node.Kind)
}

switch node.Kind {
case yaml.DocumentNode:
if len(node.Content) != len(nodeTemplate.Content) {
return fmt.Errorf("document node content length mismatch")
}
for i := range node.Content {
if err := validateNodeAgainstTemplate(node.Content[i], nodeTemplate.Content[i]); err != nil {
return err
}
}
case yaml.MappingNode:
nodeMap := make(map[string]*yaml.Node)
for i := 0; i < len(node.Content); i += 2 {
nodeMap[node.Content[i].Value] = node.Content[i+1]
}

templateMap := make(map[string]*yaml.Node)
for i := 0; i < len(nodeTemplate.Content); i += 2 {
templateMap[nodeTemplate.Content[i].Value] = nodeTemplate.Content[i+1]
}

for key, value := range nodeMap {
if templateValue, exists := templateMap[key]; exists {
if err := validateNodeAgainstTemplate(value, templateValue); err != nil {
return fmt.Errorf("invalid value for key '%s': %v", key, err)
}
} else {
return fmt.Errorf("unexpected key in configuration: %s", key)
}
}
case yaml.SequenceNode:
if len(node.Content) > 0 && len(nodeTemplate.Content) > 0 {
for _, item := range node.Content {
if err := validateNodeAgainstTemplate(item, nodeTemplate.Content[0]); err != nil {
return fmt.Errorf("invalid sequence item: %v", err)
}
}
} else if len(node.Content) > 0 && len(nodeTemplate.Content) == 0 {
return fmt.Errorf("unexpected sequence in configuration")
}
case yaml.ScalarNode:
if node.Tag != nodeTemplate.Tag {
return fmt.Errorf("scalar type mismatch: expected %s, got %s", nodeTemplate.Tag, node.Tag)
}
case yaml.AliasNode:
if nodeTemplate.Kind != yaml.AliasNode {
return fmt.Errorf("unexpected alias node")
}
// For alias nodes, we should validate the actual content they refer to
if err := validateNodeAgainstTemplate(node.Alias, nodeTemplate.Alias); err != nil {
return fmt.Errorf("invalid alias node: %v", err)
}
default:
return fmt.Errorf("unsupported node kind: %v", node.Kind)
}

return nil
}
Loading

0 comments on commit 0b43cfd

Please sign in to comment.