「连载」边缘计算(十三)02-01:边缘部分源码(源码分析篇)

(接上篇)

配置模块初始化

配置模块初始化具体如下所示。

|-----------------------------------------------------------------|
| err := archaius.Init() ... CONFIG = archaius.GetConfigFactory() |

(3) 获取内存配置源

获取内存配置源具体如下所示。

|------------------------------------------------------------------------------|
| ms := memoryconfigsource.NewMemoryConfigurationSource() CONFIG.AddSource(ms) |

(4)获取命令行配置源

获取命令行配置源具体如下所示。

|-----------------------------------------------------------------------------------------|
| cmdSource := commandlinesource.NewCommandlineConfigSource() CONFIG.AddSource(cmdSource) |

(5)获取环境变量配置源

获取环境变量配置源具体如下所示。

|--------------------------------------------------------------------------------------|
| envSource := envconfigsource.NewEnvConfigurationSource() CONFIG.AddSource(envSource) |

(6)根据配置文件路径获取配置源

根据配置文件路径获取配置源具体如下所示。

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| confLocation := getConfigDirectory() + "/conf" _, err = os.Stat(confLocation) if !os.IsExist(err) { os.Mkdir(confLocation, os.ModePerm) } err = filepath.Walk(confLocation, func(location string, f os.FileInfo, err error) error { if f == nil { return err } if f.IsDir() { return nil } ext := strings.ToLower(path.Ext(location)) if ext == ".yml" || ext == ".yaml" { archaius.AddFile(location) } return nil }) ... }) |

目前,KubeEdge所用的读取配置文件的方式是根据配置文件路径获取配置源。下面深入剖析该配置文件读取方式,首先确定配置所在路径。getConfigDirectory()函数定义具体如下所示。

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/beehive/pkg/common/config/config.go |
| //constants to define config paths const ( ParameterConfigPath = "config-path" EnvironmentalConfigPath = "GOARCHAIUS_CONFIG_PATH" ) ... // get the configuration file path func getConfigDirectory() string { if config, err := CONFIG.GetValue(ParameterConfigPath).ToString(); err == nil { return config } if config, err := CONFIG.GetValue(EnvironmentalConfigPath).ToString(); err == nil { return config } return util.GetCurrentDirectory() } |

getConfigDirectory()函数中获取配置文件所在目录的方式有如下3种。

1)根据ParameterConfigPath获取,具体如下所示。

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| // get the configuration file path func getConfigDirectory() string { if config, err := CONFIG.GetValue(ParameterConfigPath).ToString(); err == nil { return config } |

2)根据EnvironmentalConfigPath获取,具体如下所示。

|-----------------------------------------------------------------------------------------------------|
| if config, err := CONFIG.GetValue(EnvironmentalConfigPath).ToString(); err == nil { return config } |

3)根据可执行文件运行所在的当前目录下获取,具体如下所示。

|-----------------------------------|
| return util.GetCurrentDirectory() |

接着分析怎么读入配置文件,具体读入方法如下所示。

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/beehive/pkg/common/config/config.go |
| confLocation := getConfigDirectory() + "/conf" _, err = os.Stat(confLocation) if !os.IsExist(err) { os.Mkdir(confLocation, os.ModePerm) } err = filepath.Walk(confLocation, func(location string, f os.FileInfo, err error) error { if f == nil { return err } if f.IsDir() { return nil } ext := strings.ToLower(path.Ext(location)) if ext == ".yml" || ext == ".yaml" { archaius.AddFile(location) } return nil }) ... }) |

在获取了配置文件所在的目录confLocation后,接着进行如下操作。

1)判断目录是否存在,如果不存在创建该目录,具体如下。

|-------------------------------------------------------------------------------------------------------------------------------------------|
| confLocation := getConfigDirectory() + "/conf" _, err = os.Stat(confLocation) if !os.IsExist(err) { os.Mkdir(confLocation, os.ModePerm) } |

2)遍历配置文件目录下符合条件的文件,加入配置信息源,具体如下。

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| err = filepath.Walk(confLocation, func(location string, f os.FileInfo, err error) error { if f == nil { return err } if f.IsDir() { return nil } ext := strings.ToLower(path.Ext(location)) if ext == ".yml" || ext == ".yaml" { archaius.AddFile(location) } return nil }) ... }) |

到此 ,config.CONFIG就获取了KubeEdge所需配置文件内容。

通过上面的分析,我们知道KubeEdge是通过KubeEdge/beehive/pkg/common/config来读取所需的配置文件的,而KubeEdge/beehive/pkg/common/config封装了archaius(https://GitHub.com/Netflix/archaius)。

「 未完待续...... 」

点击下方标题可阅读技术文章

「连载」边缘计算(一)01-16:边缘计算系统逻辑架构(原理篇)
「连载」边缘计算(二)01-17:边缘计算系统逻辑架构(原理篇)
「连载」边缘计算(三)01-18:边缘部分原理解析(原理篇)
「连载」边缘计算(四)01-19:边缘部分原理解析(原理篇)
「连载」边缘计算(五)01-22:边缘部分原理解析(原理篇)
「连载」边缘计算(六)01-23:边缘部分原理解析(原理篇)
「连载」边缘计算(七)01-24:边缘部分原理解析(原理篇)
「连载」边缘计算(八)01-25:边缘部分源码(源码分析篇)
「连载」边缘计算(九)01-26:边缘部分源码(源码分析篇)

「连载」边缘计算(十)01-29:边缘部分源码(源码分析篇)
「连载」边缘计算(十一)01-30:边缘部分源码(源码分析篇)
「连载」边缘计算(十二)01-31:边缘部分源码(源码分析篇)

相关推荐
努力的小雨13 小时前
手里捏着纸,谁来暂停视频?我把折纸老师塞进了 AI 眼镜
后端
狗头大军之江苏分军13 小时前
《潮水漫过十七岁》倒计时
前端·javascript·后端
Wellijohn14 小时前
dict、tuple、dataclass 还是 slots?我把 6 种数据载体的 8 项开销全测了一遍
后端·python·性能优化
HarmonSir15 小时前
OpenManus 的架构解析:另一个通用 Agent 的最小内核与扩展边界
后端·架构
hsfxuebao15 小时前
Sandbox(沙箱)到底是什么?
后端
一条Go15 小时前
proto 契约:校验注解、错误码与「文案即接口」
后端·github
狗头大军之江苏分军15 小时前
《潮水漫过十七岁》好久没写了
前端·javascript·后端
苍何16 小时前
用 OiiOii Motion + Opus 5.5 做动画视频,效果猛的离谱
后端