Menu Close

5.5.6.2. 本地测试基于 Ansible 的 Operator

您可以使用 Operator 项目的顶层目录中的 make run 命令,测试本地运行的基于 Ansible 的 Operator 内部的逻辑。make run Makefile 目标在本地运行 ansible-operator 二进制文件,从 watches.yaml 文件中读取并使用 ~/.kube/config 文件与 Kubernetes 集群通信,就像 k8s 模块一样。

注意

您可以通过设置环境变量 ANSIBLE_ROLES_PATH 或者使用 ansible-roles-path 标记来自定义角色路径。如果在 ANSIBLE_ROLES_PATH 值中没有找到该角色,Operator 会在 {{current directory}}/roles 中查找它。

先决条件

流程

  1. 为自定义资源(CR)安装自定义资源定义(CRD)和正确的基于角色的访问控制(RBAC)定义:

    $ make install

    输出示例

    /usr/bin/kustomize build config/crd | kubectl apply -f -
    customresourcedefinition.apiextensions.k8s.io/memcacheds.cache.example.com created

  2. 运行 make run 命令:

    $ make run

    输出示例

    /home/user/memcached-operator/bin/ansible-operator run
    {"level":"info","ts":1612739145.2871568,"logger":"cmd","msg":"Version","Go Version":"go1.15.5","GOOS":"linux","GOARCH":"amd64","ansible-operator":"v1.8.0","commit":"1abf57985b43bf6a59dcd18147b3c574fa57d3f6"}
    ...
    {"level":"info","ts":1612739148.347306,"logger":"controller-runtime.metrics","msg":"metrics server is starting to listen","addr":":8080"}
    {"level":"info","ts":1612739148.3488882,"logger":"watches","msg":"Environment variable not set; using default value","envVar":"ANSIBLE_VERBOSITY_MEMCACHED_CACHE_EXAMPLE_COM","default":2}
    {"level":"info","ts":1612739148.3490262,"logger":"cmd","msg":"Environment variable not set; using default value","Namespace":"","envVar":"ANSIBLE_DEBUG_LOGS","ANSIBLE_DEBUG_LOGS":false}
    {"level":"info","ts":1612739148.3490646,"logger":"ansible-controller","msg":"Watching resource","Options.Group":"cache.example.com","Options.Version":"v1","Options.Kind":"Memcached"}
    {"level":"info","ts":1612739148.350217,"logger":"proxy","msg":"Starting to serve","Address":"127.0.0.1:8888"}
    {"level":"info","ts":1612739148.3506632,"logger":"controller-runtime.manager","msg":"starting metrics server","path":"/metrics"}
    {"level":"info","ts":1612739148.350784,"logger":"controller-runtime.manager.controller.memcached-controller","msg":"Starting EventSource","source":"kind source: cache.example.com/v1, Kind=Memcached"}
    {"level":"info","ts":1612739148.5511978,"logger":"controller-runtime.manager.controller.memcached-controller","msg":"Starting Controller"}
    {"level":"info","ts":1612739148.5512562,"logger":"controller-runtime.manager.controller.memcached-controller","msg":"Starting workers","worker count":8}

    现在,Operator 会监控 CR 的事件,创建 CR 将触发您的 Ansible 角色运行。

    注意

    考虑 config/samples/<gvk>.yaml CR 清单示例:

    apiVersion: <group>.example.com/v1alpha1
    kind: <kind>
    metadata:
      name: "<kind>-sample"

    因为未设置 spec 字段,所以调用 Ansible 时无额外变量。其他部分将涵盖从 CR 传递给 Ansible 的额外变量。为 Operator 设置适当的默认值是很重要的。

  3. 创建 CR 实例,并将默认变量 state 设置为 present

    $ oc apply -f config/samples/<gvk>.yaml
  4. 检查 example-config 配置映射是否已创建:

    $ oc get configmaps

    输出示例

    NAME                    STATUS    AGE
    example-config          Active    3s

  5. 修改 config/samples/<gvk>.yaml 文件,将 state 字段设置为 absent。例如:

    apiVersion: cache.example.com/v1
    kind: Memcached
    metadata:
      name: memcached-sample
    spec:
      state: absent
  6. 应用更改:

    $ oc apply -f config/samples/<gvk>.yaml
  7. 确认配置映射已被删除:

    $ oc get configmap