快速实现基于Kustomize的Operator

        Operator最近越来越流行,它是CoreOS推出的旨在管理安装k8s应用的技术框架, 属于devops的范畴,你可以简单理解为一个Installer,熟悉k8s的同学可能马上就会问,那置Helm于何地?你说对了,Operator就是来革Helm的命的!Operator其实说白了就是一个k8s CRD 和 Controller, 一般都是用Redhat推出的工具:Operator-sdk 来实现。
        今天的并不想讲太多的Operator,想介绍一个帮助快速实现Operator的开源项目:https://github.com/vincent-pli/operator-sample-kustomize

        这个项目是大概想法是:

利用Template(yaml,Kustomize的结构)定义应用。└── install └── helloWorld ├── configMap.yaml ├── deployment.yaml ├── kustomization.yaml ├── namespace.yaml └── service.yaml引入Extension,定义安装应用过程中的行为。

模板(Template)

模板的默认路径在template目录下, 看看结构:

└── install
    └── helloWorld
        ├── configMap.yaml
        ├── deployment.yaml
        ├── kustomization.yaml
        ├── namespace.yaml
        └── service.yaml

install是CRD的名字,没啥意义,忽略。

helloWorld是Component的名字,支持多个Component组成一个应用的情况。

注意Component需要在./deploy/operator.yaml中定义:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: operator-sample-kustomize
spec:
  replicas: 1
  selector:
    matchLabels:
      name: operator-sample-kustomize
  template:
    metadata:
      labels:
        name: operator-sample-kustomize
    spec:
      serviceAccountName: operator-sample-kustomize
      containers:
        - name: operator-sample-kustomize
          # Replace this with the built image name
          image: index.docker.io/vincentpli/operator-sample:v0.0.1
          command:
          - operator-sample-kustomize
          imagePullPolicy: Always
          env:
            - name: WATCH_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: OPERATOR_NAME
              value: "operator-sample-kustomize"
            - name: TEMPLATES_PATH
              value: "/usr/local/templates"
            - name: COMPONENTS
              value: "helloworld"

多个Component使用逗号(,)隔开。

熟悉的同学应该看出来了helloWorld目录下其实是Kustomize的配置文件。

扩展(Extension)

引入了Extension的概念,是把一些Optional的操作plugin化,让这些操作可以方便的enable/disable。

先放一个CR的例子:

apiVersion: install.example.com/v1alpha1
kind: Install
metadata:
  name: example-install
spec:
  # Add fields here
  targetNamespace: operator-sample
  setowner: true
  registry: 
    override:
      the-container: monopole/hello:1

目前可用的Extension有:

  • nsinject: 注入namespace,注意上面例子中的targetNamespace,如果这个字段存在,plugin:nsinject被激活,所有在template中的k8s resource都被设置为namespace: operator-sample
  • ownerset:设置resource的owner。上面例子中的setowner, 如果这字段被设置为true,plugin: ownerset被激活,所有在template中的k8s resource的owner都被设置为该CR: example-install
  • imagereplacement:替换container中的image url。如上例子表明:template中resource里面的名为“the-container”的container中的image url被替换为“monopole/hello:1”

 

实现新的Extension

实现一个新的Extension还是比较简单的,先看项目结构:

快速实现基于Kustomize的Operator_第1张图片

./pkg/extension是extension实现的代码。

./pkg/controller/install/add_* 是该extension被纳入操作的代码

具体实现请看代码,比较简单,一看便知。

 

该项目还有很多缺点,比如无UT,无文档等等,有待后续加强。

你可能感兴趣的:(Cloud)