kis-flow/file/config_import.go

201 lines
5.5 KiB
Go
Raw Normal View History

2024-01-12 17:27:43 +08:00
package file
import (
"fmt"
2024-03-26 14:54:50 +08:00
"github.com/aceld/kis-flow/common"
"github.com/aceld/kis-flow/config"
"github.com/aceld/kis-flow/flow"
"github.com/aceld/kis-flow/kis"
"github.com/aceld/kis-flow/metrics"
2024-04-15 17:50:02 +08:00
"gopkg.in/yaml.v3"
2024-01-12 17:27:43 +08:00
"os"
"path"
"path/filepath"
)
type allConfig struct {
Flows map[string]*config.KisFlowConfig
Funcs map[string]*config.KisFuncConfig
Conns map[string]*config.KisConnConfig
}
2024-04-15 17:50:02 +08:00
// kisTypeFlowConfigure parses Flow configuration file in yaml format
2024-01-12 17:27:43 +08:00
func kisTypeFlowConfigure(all *allConfig, confData []byte, fileName string, kisType interface{}) error {
2024-03-01 16:29:07 +08:00
flowCfg := new(config.KisFlowConfig)
if ok := yaml.Unmarshal(confData, flowCfg); ok != nil {
2024-04-15 17:50:02 +08:00
return fmt.Errorf("%s has wrong format kisType = %s", fileName, kisType)
2024-01-12 17:27:43 +08:00
}
2024-04-15 17:50:02 +08:00
// Skip the configuration loading if the Flow status is disabled
2024-03-01 16:29:07 +08:00
if common.KisOnOff(flowCfg.Status) == common.FlowDisable {
2024-01-12 17:27:43 +08:00
return nil
}
2024-03-01 16:29:07 +08:00
if _, ok := all.Flows[flowCfg.FlowName]; ok {
2024-04-15 17:50:02 +08:00
return fmt.Errorf("%s set repeat flow_id:%s", fileName, flowCfg.FlowName)
2024-01-12 17:27:43 +08:00
}
2024-04-15 17:50:02 +08:00
// Add to the configuration set
2024-03-01 16:29:07 +08:00
all.Flows[flowCfg.FlowName] = flowCfg
2024-01-12 17:27:43 +08:00
return nil
}
2024-04-15 17:50:02 +08:00
// kisTypeFuncConfigure parses Function configuration file in yaml format
2024-01-12 17:27:43 +08:00
func kisTypeFuncConfigure(all *allConfig, confData []byte, fileName string, kisType interface{}) error {
function := new(config.KisFuncConfig)
if ok := yaml.Unmarshal(confData, function); ok != nil {
2024-04-15 17:50:02 +08:00
return fmt.Errorf("%s has wrong format kisType = %s", fileName, kisType)
2024-01-12 17:27:43 +08:00
}
if _, ok := all.Funcs[function.FName]; ok {
2024-04-15 17:50:02 +08:00
return fmt.Errorf("%s set repeat function_id:%s", fileName, function.FName)
2024-01-12 17:27:43 +08:00
}
2024-04-15 17:50:02 +08:00
// Add to the configuration set
2024-01-12 17:27:43 +08:00
all.Funcs[function.FName] = function
return nil
}
2024-04-15 17:50:02 +08:00
// kisTypeConnConfigure parses Connector configuration file in yaml format
2024-01-12 17:27:43 +08:00
func kisTypeConnConfigure(all *allConfig, confData []byte, fileName string, kisType interface{}) error {
conn := new(config.KisConnConfig)
if ok := yaml.Unmarshal(confData, conn); ok != nil {
2024-04-15 17:50:02 +08:00
return fmt.Errorf("%s has wrong format kisType = %s", fileName, kisType)
2024-01-12 17:27:43 +08:00
}
if _, ok := all.Conns[conn.CName]; ok {
2024-04-15 17:50:02 +08:00
return fmt.Errorf("%s set repeat conn_id:%s", fileName, conn.CName)
2024-01-12 17:27:43 +08:00
}
2024-04-15 17:50:02 +08:00
// Add to the configuration set
2024-01-12 17:27:43 +08:00
all.Conns[conn.CName] = conn
return nil
}
2024-04-15 17:50:02 +08:00
// kisTypeGlobalConfigure parses Global configuration file in yaml format
2024-03-04 14:53:29 +08:00
func kisTypeGlobalConfigure(confData []byte, fileName string, kisType interface{}) error {
2024-04-15 17:50:02 +08:00
// Global configuration
2024-03-04 14:53:29 +08:00
if ok := yaml.Unmarshal(confData, config.GlobalConfig); ok != nil {
2024-04-15 17:50:02 +08:00
return fmt.Errorf("%s is wrong format kisType = %s", fileName, kisType)
2024-03-04 14:53:29 +08:00
}
2024-04-15 17:50:02 +08:00
// Start Metrics service
2024-03-04 14:53:29 +08:00
metrics.RunMetrics()
return nil
}
2024-04-15 17:50:02 +08:00
// parseConfigWalkYaml recursively parses all configuration files in yaml format and stores the configuration information in allConfig
2024-01-12 17:27:43 +08:00
func parseConfigWalkYaml(loadPath string) (*allConfig, error) {
all := new(allConfig)
all.Flows = make(map[string]*config.KisFlowConfig)
all.Funcs = make(map[string]*config.KisFuncConfig)
all.Conns = make(map[string]*config.KisConnConfig)
err := filepath.Walk(loadPath, func(filePath string, info os.FileInfo, err error) error {
2024-04-15 17:50:02 +08:00
// Validate the file extension
2024-01-12 17:27:43 +08:00
if suffix := path.Ext(filePath); suffix != ".yml" && suffix != ".yaml" {
return nil
}
2024-04-15 17:50:02 +08:00
// Read file content
2024-03-01 16:32:36 +08:00
confData, err := os.ReadFile(filePath)
2024-01-12 17:27:43 +08:00
if err != nil {
return err
}
confMap := make(map[string]interface{})
2024-04-15 17:50:02 +08:00
// Validate yaml format
2024-01-12 17:27:43 +08:00
if err := yaml.Unmarshal(confData, confMap); err != nil {
return err
}
2024-04-15 17:50:02 +08:00
// Check if kisType exists
var kisType interface{}
2024-01-12 17:27:43 +08:00
2024-04-15 17:50:02 +08:00
kisType, ok := confMap["kistype"]
if !ok {
return fmt.Errorf("%s has no field [kistype]", filePath)
}
2024-01-12 17:27:43 +08:00
2024-04-15 17:50:02 +08:00
switch kisType {
case common.KisIDTypeFlow:
return kisTypeFlowConfigure(all, confData, filePath, kisType)
2024-01-12 17:27:43 +08:00
2024-04-15 17:50:02 +08:00
case common.KisIDTypeFunction:
return kisTypeFuncConfigure(all, confData, filePath, kisType)
2024-03-04 14:53:29 +08:00
2024-04-15 17:50:02 +08:00
case common.KisIDTypeConnector:
return kisTypeConnConfigure(all, confData, filePath, kisType)
case common.KisIDTypeGlobal:
return kisTypeGlobalConfigure(confData, filePath, kisType)
default:
return fmt.Errorf("%s set wrong kistype %s", filePath, kisType)
2024-01-12 17:27:43 +08:00
}
})
if err != nil {
return nil, err
}
return all, nil
}
func buildFlow(all *allConfig, fp config.KisFlowFunctionParam, newFlow kis.Flow, flowName string) error {
2024-04-15 17:50:02 +08:00
// Load the Functions that the current Flow depends on
2024-01-12 17:27:43 +08:00
if funcConfig, ok := all.Funcs[fp.FuncName]; !ok {
2024-04-15 17:50:02 +08:00
return fmt.Errorf("FlowName [%s] need FuncName [%s], But has No This FuncName Config", flowName, fp.FuncName)
2024-01-12 17:27:43 +08:00
} else {
2024-03-01 16:29:07 +08:00
// flow add connector
2024-01-12 17:27:43 +08:00
if funcConfig.Option.CName != "" {
2024-04-15 17:50:02 +08:00
// Load the Connectors that the current Function depends on
2024-01-12 17:27:43 +08:00
if connConf, ok := all.Conns[funcConfig.Option.CName]; !ok {
2024-04-15 17:50:02 +08:00
return fmt.Errorf("FuncName [%s] need ConnName [%s], But has No This ConnName Config", fp.FuncName, funcConfig.Option.CName)
2024-01-12 17:27:43 +08:00
} else {
2024-04-15 17:50:02 +08:00
// Function Config associates with Connector Config
2024-01-12 17:27:43 +08:00
_ = funcConfig.AddConnConfig(connConf)
}
}
2024-03-01 16:29:07 +08:00
// flow add function
2024-03-29 18:07:57 +08:00
if err := newFlow.AppendNewFunction(funcConfig, fp.Params); err != nil {
2024-01-12 17:27:43 +08:00
return err
}
}
return nil
}
2024-04-15 17:50:02 +08:00
// ConfigImportYaml recursively parses all configuration files in yaml format
2024-01-12 17:27:43 +08:00
func ConfigImportYaml(loadPath string) error {
all, err := parseConfigWalkYaml(loadPath)
if err != nil {
return err
}
for flowName, flowConfig := range all.Flows {
2024-04-15 17:50:02 +08:00
// Build a new Flow
2024-01-12 17:27:43 +08:00
newFlow := flow.NewKisFlow(flowConfig)
for _, fp := range flowConfig.Flows {
if err := buildFlow(all, fp, newFlow, flowName); err != nil {
return err
}
}
2024-04-15 17:50:02 +08:00
// Add the flow to FlowPool
2024-01-12 17:27:43 +08:00
kis.Pool().AddFlow(flowName, newFlow)
}
return nil
}