作者 | 樊大勇

KubeVela 是一个简单易用又高度可扩展的云原生应用管理引擎,是基于 Kubernetes 及阿里云与微软云共同发布的云原生应用开发模型 OAM 构建。

KubeVela 基于 OAM 模型构建了一套具体的实现,通过 Golang 编写,可以端到端地为用户构建云原生应用的平台,提供一个相对完整的解决方案。

KubeVela 项目自 2020 年 7 月份在社区里面发起,受到包括阿里、微软、Crossplane 等公司工程师在内的广大社区志愿者的欢迎,并一起投入到项目开发工作中。他们把在 OAM 实践里面的各种经验与教训,都总结沉淀到 KubeVela 项目中。

本文主要目的是探索 KubeVela 如何将一个 appfile 文件转换为 K8s 中特定的资源对象。

该过程总的来说分为两个阶段:

  1. appfile 转为 K8s 中的 application
  2. application 转换为对应的 K8s 资源对象
# vela.yaml
name: test
services:nginx:type: webserviceimage: nginxenv:- name: NAMEvalue: kubevela# svc traitsvc:type: NodePortports:- port: 80nodePort: 32017

利用 vela up 命令可以完成部署。

vela up 命令

建议:在看 vela 命令行工具代码之前,先去简单了解一下 cobra 框架。

// references/cli/up.go
// NewUpCommand will create command for applying an AppFile
func NewUpCommand(c types.Args, ioStream cmdutil.IOStreams) *cobra.Command {cmd := &cobra.Command{Use:                   "up",DisableFlagsInUseLine: true,Short:                 "Apply an appfile",Long:                  "Apply an appfile",Annotations: map[string]string{types.TagCommandType: types.TypeStart,},PersistentPreRunE: func(cmd *cobra.Command, args []string) error {return c.SetConfig()},RunE: func(cmd *cobra.Command, args []string) error {velaEnv, err := GetEnv(cmd)if err != nil {return err}kubecli, err := c.GetClient()if err != nil {return err}o := &common.AppfileOptions{Kubecli: kubecli,IO:      ioStream,Env:     velaEnv,}filePath, err := cmd.Flags().GetString(appFilePath)if err != nil {return err}return o.Run(filePath, velaEnv.Namespace, c)},}cmd.SetOut(ioStream.Out)cmd.Flags().StringP(appFilePath, "f", "", "specify file path for appfile")return cmd
}

上面源码展示的是 vela up 命令的入口。

在 PresistentPreRunE 函数中,通过调用 c.SetConfig() 完成 Kuberentes 配置信息 kubeconfig 的注入。

在 RunE 函数中:

  • 首先,获取 vela 的 env 变量,velaEnv.Namespace 对应 Kubernetes 的命名空间。

  • 其次,获取 Kubernetes 的客户端,kubectl。

  • 接着,利用 Kubernetes 客户端和 vleaEnv 来构建渲染 Appfile 需要的 AppfileOptions。

  • 最后,调用 o.Run(filePath, velaEnv.Namespace, c)。

    • 该函数需要三个参数,其中 filePath 用于指定 appfile 的位置,velaEnv.Namespace 和 c 用来将渲染后的 Application 创建到指定命名空间。

      • filePath: appfile 的路径
      • velaEnv.Namespace:对应 K8s 的 namespace
      • c:K8s 客户端

如何将一个 appfile 转为 Kubernetes 中的 Application

  • 起点:appfile

  • 终点:applicatioin

  • 路径:appfile -> application (services -> component)

    • comp[workload, traits]

1. 起点:AppFile

// references/appfile/api/appfile.go
// AppFile defines the spec of KubeVela Appfile
type AppFile struct {Name       string             `json:"name"`CreateTime time.Time          `json:"createTime,omitempty"`UpdateTime time.Time          `json:"updateTime,omitempty"`Services   map[string]Service `json:"services"`Secrets    map[string]string  `json:"secrets,omitempty"`configGetter config.Storeinitialized  bool
}// NewAppFile init an empty AppFile struct
func NewAppFile() *AppFile {return &AppFile{Services:     make(map[string]Service),Secrets:      make(map[string]string),configGetter: &config.Local{},}
}
// references/appfile/api/service.go
// Service defines the service spec for AppFile, it will contain all related information including OAM component, traits, source to image, etc...
type Service map[string]interface{}

上面两段代码是 AppFile 在客户端的声明,vela 会将指定路径的 yaml 文件读取后,赋值给一个 AppFile。

// references/appfile/api/appfile.go
// LoadFromFile will read the file and load the AppFile struct
func LoadFromFile(filename string) (*AppFile, error) {b, err := ioutil.ReadFile(filepath.Clean(filename))if err != nil {return nil, err}af := NewAppFile()// Add JSON format appfile supportext := filepath.Ext(filename)switch ext {case ".yaml", ".yml":err = yaml.Unmarshal(b, af)case ".json":af, err = JSONToYaml(b, af)default:if json.Valid(b) {af, err = JSONToYaml(b, af)} else {err = yaml.Unmarshal(b, af)}}if err != nil {return nil, err}return af, nil
}

下面为读取 vela.yaml 文件后,加载到 AppFile 中的数据:

# vela.yaml
name: test
services:nginx:type: webserviceimage: nginxenv:- name: NAMEvalue: kubevela# svc traitsvc:type: NodePortports:- port: 80nodePort: 32017
Name: test
CreateTime: 0001-01-01 00:00:00 +0000 UTC
UpdateTime: 0001-01-01 00:00:00 +0000 UTC
Services: map[nginx: map[env: [map[name: NAME value: kubevela]] image: nginx svc: map[ports: [map[nodePort: 32017 port: 80]] type: NodePort] type: webservice]]
Secrets    map[]
configGetter: 0x447abd0
initialized: false

2. 终点:application

// apis/core.oam.dev/application_types.go
type Application struct {metav1.TypeMeta   `json:",inline"`metav1.ObjectMeta `json:"metadata,omitempty"`Spec   ApplicationSpec `json:"spec,omitempty"`Status AppStatus       `json:"status,omitempty"`
}// ApplicationSpec is the spec of Application
type ApplicationSpec struct {Components []ApplicationComponent `json:"components"`// TODO(wonderflow): we should have application level scopes supported here// RolloutPlan is the details on how to rollout the resources// The controller simply replace the old resources with the new one if there is no rollout plan involved// +optionalRolloutPlan *v1alpha1.RolloutPlan `json:"rolloutPlan,omitempty"`
}

上面代码,为 Application 的声明,结合 .vela/deploy.yaml(见下面代码),可以看出,要将一个 AppFile 渲染为 Application 主要就是将 AppFile 的 Services 转化为 Application 的 Components。

# .vela/deploy.yaml
apiVersion: core.oam.dev/v1alpha2
kind: Application
metadata:creationTimestamp: nullname: testnamespace: default
spec:components:- name: nginxscopes:healthscopes.core.oam.dev: test-default-healthsettings:env:- name: NAMEvalue: kubevelaimage: nginxtraits:- name: svcproperties:ports:- nodePort: 32017port: 80type: NodePorttype: webservice
status: {}

3. 路径:Services -> Components

结合以上内容可以看出,将 Appfile 转化为 Application 主要是将 Services 渲染为 Components。

// references/appfile/api/appfile.go
// BuildOAMApplication renders Appfile into Application, Scopes and other K8s Resources.
func (app *AppFile) BuildOAMApplication(env *types.EnvMeta, io cmdutil.IOStreams, tm template.Manager, silence bool) (*v1alpha2.Application, []oam.Object, error) {...servApp := new(v1alpha2.Application)servApp.SetNamespace(env.Namespace)servApp.SetName(app.Name)servApp.Spec.Components = []v1alpha2.ApplicationComponent{}for serviceName, svc := range app.GetServices() {...// 完成 Service 到 Component 的转化comp, err := svc.RenderServiceToApplicationComponent(tm, serviceName)if err != nil {return nil, nil, err}servApp.Spec.Components = append(servApp.Spec.Components, comp)}servApp.SetGroupVersionKind(v1alpha2.SchemeGroupVersion.WithKind("Application"))auxiliaryObjects = append(auxiliaryObjects, addDefaultHealthScopeToApplication(servApp))return servApp, auxiliaryObjects, nil
}

上面的代码是 vela 将 Appfile 转化为 Application 代码实现的位置。其中 comp, err := svc.RenderServiceToApplicationComponent(tm, serviceName) 完成 Service 到 Component 的转化。

// references/appfile/api/service.go
// RenderServiceToApplicationComponent render all capabilities of a service to CUE values to KubeVela Application.
func (s Service) RenderServiceToApplicationComponent(tm template.Manager, serviceName string) (v1alpha2.ApplicationComponent, error) {// sort out configs by workload/traitworkloadKeys := map[string]interface{}{}var traits []v1alpha2.ApplicationTraitwtype := s.GetType()comp := v1alpha2.ApplicationComponent{Name:         serviceName,WorkloadType: wtype,}for k, v := range s.GetApplicationConfig() {// 判断是否为 traitif tm.IsTrait(k) {trait := v1alpha2.ApplicationTrait{Name: k,}....// 如果是 triat 加入 traits 中traits = append(traits, trait)continue}workloadKeys[k] = v}// Handle workloadKeys to settingssettings := &runtime.RawExte nsion{}pt, err := json.Marshal(workloadKeys)if err != nil {return comp, err}if err := settings.UnmarshalJSON(pt); err != nil {return comp, err}comp.Settings = *settingsif len(traits) > 0 {comp.Traits = traits}return comp, nil
}

4. 总结

执行 vela up 命令,渲染 appfile 为 Application,将数据写入到 .vela/deploy.yaml 中,并在 K8s 中创建。

Application 是如何转换为对应 K8s 资源对象

  • 起点:Application
  • 中点:ApplicationConfiguration, Component
  • 终点:Deployment, Service
  • 路径:
    • application_controller
    • applicationconfiguration controller

【建议】> 了解一下内容:> - client-to

  • controller-runtime
  • operator

1. Application

# 获取集群中的 Application
$ kubectl get application
NAMESPACE   NAME   AGE
default     test   24h

2. ApplicationConfiguration 和 Component

当 application controller 获取到 Application 资源对象之后,会根据其内容创建出对应的 ApplicationConfiguration 和 Component。

# 获取 ApplicationConfiguration 和 Component
$ kubectl get ApplicationConfiguration,Component
NAME                                         AGE
applicationconfiguration.core.oam.dev/test   24hNAME                           WORKLOAD-KIND   AGE
component.core.oam.dev/nginx   Deployment      24h

ApplicationiConfiguration 中以名字的方式引入 Component:

3. application controller

基本逻辑:
  • 获取一个 Application 资源对象。

  • 将 Application 资源对象渲染为 ApplicationConfiguration 和 Component。

  • 创建 ApplicationConfiguration 和 Component 资源对象。

代码:
// pkg/controller/core.oam.dev/v1alpha2/application/application_controller.go// Reconcile process app event
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {ctx := context.Background()applog := r.Log.WithValues("application", req.NamespacedName)// 1. 获取 Applicationapp := new(v1alpha2.Application)if err := r.Get(ctx, client.ObjectKey{Name:      req.Name,Namespace: req.Namespace,}, app); err != nil {...}...// 2. 将 Application 转换为 ApplicationConfiguration 和 Componenthandler := &appHandler{r, app, applog}...appParser := appfile.NewApplicationParser(r.Client, r.dm)...appfile, err := appParser.GenerateAppFile(ctx, app.Name, app)...ac, comps, err := appParser.GenerateApplicationConfiguration(appfile, app.Namespace)...// 3. 在集群中创建 ApplicationConfiguration 和 Component // apply appConfig & component to the clusterif err := handler.apply(ctx, ac, comps); err != nil {applog.Error(err, "[Handle apply]")app.Status.SetConditions(errorCondition("Applied", err))return handler.handleErr(err)}...return ctrl.Result{}, r.UpdateStatus(ctx, app)
}

4. applicationconfiguration controller

基本逻辑:
  • 获取 ApplicationConfiguration 资源对象。

  • 循环遍历,获取每一个 Component 并将 workload 和 trait 渲染为对应的 K8s 资源对象。

  • 创建对应的 K8s 资源对象。

代码:
// pkg/controller/core.oam.dev/v1alpha2/applicationcinfiguratioin/applicationconfiguratioin.go// Reconcile an OAM ApplicationConfigurations by rendering and instantiating its
// Components and Traits.
func (r *OAMApplicationReconciler) Reconcile(req reconcile.Request) (reconcile.Result, error) {...ac := &v1alpha2.ApplicationConfiguration{}// 1. 获取 ApplicationConfigurationif err := r.client.Get(ctx, req.NamespacedName, ac); err != nil {...}return r.ACReconcile(ctx, ac, log)
}// ACReconcile contains all the reconcile logic of an AC, it can be used by other controller
func (r *OAMApplicationReconciler) ACReconcile(ctx context.Context, ac *v1alpha2.ApplicationConfiguration,log logging.Logger) (result reconcile.Result, returnErr error) {...// 2. 渲染// 此处 workloads 包含所有Component对应的的 workload 和 tratis 的 k8s 资源对象workloads, depStatus, err := r.components.Render(ctx, ac)...applyOpts := []apply.ApplyOption{apply.MustBeControllableBy(ac.GetUID()), applyOnceOnly(ac, r.applyOnceOnlyMode, log)}// 3. 创建 workload 和 traits 对应的 k8s 资源对象if err := r.workloads.Apply(ctx, ac.Status.Workloads, workloads, applyOpts...); err != nil {...}...// the defer function will do the final status updatereturn reconcile.Result{RequeueAfter: waitTime}, nil
}

5. 总结

当 vela up 将一个 AppFile 渲染为一个 Application 后,后续的流程由 application controller 和 applicationconfiguration controller 完成。

作者简介

樊大勇,华胜天成研发工程师,GitHub ID:@just-do1。

加入 OAM

  • OAM 官网:
    https://oam.dev

  • KubeVela GitHub 项目地址:
    https://github.com/oam-dev/kubevela

  • 社区交流钉群:

源码解读:KubeVela 是如何将 appfile 转换为 K8s 特定资源对象的相关推荐

  1. Bert系列(二)——源码解读之模型主体

    本篇文章主要是解读模型主体代码modeling.py.在阅读这篇文章之前希望读者们对bert的相关理论有一定的了解,尤其是transformer的结构原理,网上的资料很多,本文内容对原理部分就不做过多 ...

  2. Bert系列(三)——源码解读之Pre-train

    https://www.jianshu.com/p/22e462f01d8c pre-train是迁移学习的基础,虽然Google已经发布了各种预训练好的模型,而且因为资源消耗巨大,自己再预训练也不现 ...

  3. linux下free源码,linux命令free源码解读:Procps free.c

    linux命令free源码解读 linux命令free源码解读:Procps free.c 作者:isayme 发布时间:September 26, 2011 分类:Linux 我们讨论的是linux ...

  4. nodeJS之eventproxy源码解读

    1.源码缩影 !(function (name, definition) { var hasDefine = typeof define === 'function', //检查上下文环境是否为AMD ...

  5. PyTorch 源码解读之即时编译篇

    点击上方"AI遇见机器学习",选择"星标"公众号 重磅干货,第一时间送达 作者丨OpenMMLab 来源丨https://zhuanlan.zhihu.com/ ...

  6. Alamofire源码解读系列(九)之响应封装(Response)

    本篇主要带来Alamofire中Response的解读 前言 在每篇文章的前言部分,我都会把我认为的本篇最重要的内容提前讲一下.我更想同大家分享这些顶级框架在设计和编码层次究竟有哪些过人的地方?当然, ...

  7. Feflow 源码解读

    Feflow 源码解读 Feflow(Front-end flow)是腾讯IVWEB团队的前端工程化解决方案,致力于改善多类型项目的开发流程中的规范和非业务相关的问题,可以让开发者将绝大部分精力集中在 ...

  8. spring-session源码解读 sesion

    2019独角兽企业重金招聘Python工程师标准>>> spring-session源码解读 sesion 博客分类: java spring 摘要: session通用策略 Ses ...

  9. 前端日报-20160527-underscore 源码解读

    underscore 源码解读 API文档浏览器 JavaScript 中加号操作符细节 抛弃 jQuery,拥抱原生 JS 从 0 开始学习 GitHub 系列之「加入 GitHub」 js实现克隆 ...

最新文章

  1. 计算机科学和PYTHON编程导论_15_概率与分布
  2. C和指针之字符串简单实现 strcpy、strcat、strstr函数
  3. Mybatis为实体类定义别名typeAliases
  4. 程序员职业生涯的11个阶段程序人生
  5. Spring Boot基础学习笔记13:路径扫描整合Servlet三大组件
  6. mysql innodb 写锁_MySQL-InnoDB-锁
  7. android动画鸿阳,18. Activity淡入淡出动画
  8. Docker基础教程
  9. 在线制作banner的网站
  10. 如何自学插画?零基础要知道的技巧!
  11. [UOJ] #261 天天爱跑步
  12. 【git】git本地如何合并分支
  13. 如何阅读一本书——“功利性”阅读法
  14. 《王者荣耀》等“爆款”游戏是如何诞生的?| 马晓轶青腾大学演讲
  15. iCMS的spider_rule.admincp.php存在报错SQL注入
  16. 2021年光明区入驻科技创新产业园科技企业租金补贴申报指南
  17. 图像处理系列——图像融合之色彩变换1(IHS)
  18. qq聊天机器人 群发工具 (java版) (二)
  19. 使用java代码实现证件照换背景色
  20. 唯美现代中国风PPT模板

热门文章

  1. python实现键盘记录器
  2. 1354. 等差数列【一般 / 暴力枚举】
  3. Tomcat在自定义xml文件中配置虚拟目录
  4. Tomcat在server.xml中配置虚拟目录
  5. 计算机房装修对门的要求,防火门尺寸要求有哪些 防火门尺寸规范
  6. 本地如何预览php文件上传,如何实现js上传图片本地预览同时支持预览截图的功能...
  7. 你只会用 ! = null 判空?嘿嘿!
  8. 通俗讲解分布式锁,看完不懂算我输
  9. SpringBoot项目中,如何更规范的使用PageHelper分页?
  10. Android Studio目录结构和Gradle构建Android项目