Scaffolding a new Operator project

Next, you'll use the operator-sdk tool to create an Ansible Operator project with the init and create subcommands. A basic init command for an Operator with Ansible logic looks like this:

operator-sdk init --plugins=ansible --domain example.com

This command tells operator-sdk to initialize a new Ansible Operator project scaffolding with the domain of your choosing.

Among the files generated by operator-sdk init is a Kubebuilder PROJECT file. Subsequent operator-sdk commands run from the project root read this file and are aware that the project type is Ansible.

After initializing the Ansible Operator project, run the operator-sdk create api command to specify the group, version, and kind of the Custom Resource Definition (CRD) we'd like to create.

operator-sdk create api --group app --version v1alpha1 --kind App --generate-role

You should now have something like this file hierarchy:

$ tree

├── Dockerfile
├── Makefile
├── PROJECT
├── config
│   ├── crd
│   │   ├── bases
│   │   │   └── app.example.com_apps.yaml
│   │   └── kustomization.yaml
│   ├── default
│   │   ├── kustomization.yaml
│   │   └── manager_auth_proxy_patch.yaml
│   ├── manager
│   │   ├── kustomization.yaml
│   │   └── manager.yaml
│   ├── prometheus
│   │   ├── kustomization.yaml
│   │   └── monitor.yaml
│   ├── rbac
│   │   ├── app_editor_role.yaml
│   │   ├── app_viewer_role.yaml
│   │   ├── auth_proxy_client_clusterrole.yaml
│   │   ├── auth_proxy_role.yaml
│   │   ├── auth_proxy_role_binding.yaml
│   │   ├── auth_proxy_service.yaml
│   │   ├── kustomization.yaml
│   │   ├── leader_election_role.yaml
│   │   ├── leader_election_role_binding.yaml
│   │   ├── role.yaml
│   │   └── role_binding.yaml
│   ├── samples
│   │   ├── app_v1alpha1_app.yaml
│   │   └── kustomization.yaml
│   ├── scorecard
│   │   ├── bases
│   │   │   └── config.yaml
│   │   ├── kustomization.yaml
│   │   └── patches
│   │       ├── basic.config.yaml
│   │       └── olm.config.yaml
│   └── testing
│       ├── debug_logs_patch.yaml
│       ├── kustomization.yaml
│       ├── manager_image.yaml
│       └── pull_policy
│           ├── Always.yaml
│           ├── IfNotPresent.yaml
│           └── Never.yaml
├── molecule
│   ├── default
│   │   ├── converge.yml
│   │   ├── create.yml
│   │   ├── destroy.yml
│   │   ├── kustomize.yml
│   │   ├── molecule.yml
│   │   ├── prepare.yml
│   │   ├── tasks
│   │   │   └── app_test.yml
│   │   └── verify.yml
│   └── kind
│       ├── converge.yml
│       ├── create.yml
│       ├── destroy.yml
│       └── molecule.yml
├── playbooks
├── requirements.yml
├── roles
│   └── app
│       ├── README.md
│       ├── defaults
│       │   └── main.yml
│       ├── files
│       ├── handlers
│       │   └── main.yml
│       ├── meta
│       │   └── main.yml
│       ├── tasks
│       │   └── main.yml
│       ├── templates
│       └── vars
│           └── main.yml
└── watches.yaml

Running 'operator-sdk init' to create your Ansible Operator Project

Now it's your turn. The rest of this lesson will direct you to build a Kubernetes Operator around Ansible tasks that manage a memcached service.

Create a new directory for the project and change directory into it.

mkdir -p $HOME/memcached-operator
cd $HOME/memcached-operator

Initialize a new Ansible-based Operator SDK project for the Memcached Operator:

operator-sdk init --plugins=ansible --domain example.com

Create the Memcached API and generate an Ansible role skeleton. This sets up the controller to watch resources with APIVersion 'cache.example.com/v1alpha1' and Kind Memcached.

operator-sdk create api --group cache --version v1alpha1 --kind Memcached --generate-role

Project Scaffolding Layout

After creating a new operator project the directory contains a hierarchy of generated folders and files. The following list describes the key generated files and directories.

  • config/ - Kustomize YAML definitions required to launch our controller on a cluster. It is the target directory to hold our CustomResourceDefinitions, RBAC configuration, and WebhookConfigurations.
  • Dockerfile - The container build file used to build your Ansible Operator container image.
  • Makefile - make targets for building and deploying the custom controller.
  • molecule - Used for Ansible Based Operator Testing.
  • playbooks/ - placeholder for possible ansible playbook.
  • PROJECT - Kubebuilder metadata for scaffolding new components.
  • requirements.yaml - requirements.yaml for defining an Ansible collection.
  • roles/ - Contains an Ansible Role initialized using [Ansible Galaxy](https://docs.ansible.com/ansible/latest/reference_appendices/galaxy.html)
  • watches.yaml - Contains Group, Version, Kind, and Ansible invocation method.