Skip to content

imjoseangel/restapi-operator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 

Repository files navigation

Ansible Kubernetes Operator

How to run on Mac

Install with brew

brew install operator-sdk

Confirm version

operator-sdk version

operator-sdk version: "v1.0.0", commit: "d7d5e0cd6cf5468bb66e0849f08fda5bf557f4fa", kubernetes version: "v1.18.2", go version: "go1.14.7 darwin/amd64", GOOS: "darwin", GOARCH: "amd64"

Create and Initialize the Project

mkdir ~/memcached-operator  && cd ~/memcached-operator/
operator-sdk init --plugins=ansible --domain example.com

Create the API

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

Modify the Manager

Update the file roles/memcached/tasks/main.yml:

---
# tasks file for Memcached
- name: start memcached
  community.kubernetes.k8s:
    definition:
      kind: Deployment
      apiVersion: apps/v1
      metadata:
        name: '{{ ansible_operator_meta.name }}-memcached'
        namespace: '{{ ansible_operator_meta.namespace }}'
      spec:
        replicas: "{{ size }}"
        selector:
          matchLabels:
            app: memcached
        template:
          metadata:
            labels:
              app: memcached
          spec:
            containers:
            - name: memcached
              command:
              - memcached
              - -m=64
              - -o
              - modern
              - -v
              image: "docker.io/memcached:1.4.36-alpine"
              ports:
                - containerPort: 11211

Add the defaults to roles/memcached/defaults/main.yml

---
# defaults file for Memcached
size: 1

And update the file config/samples/cache_v1alpha1_memcached.yaml

---
apiVersion: cache.example.com/v1alpha1
kind: Memcached
metadata:
  name: memcached-sample
spec:
  size: 3

Define your image and container registry:

export IMG=docker.io/imjoseangel/memcached-operator:v1

And run:

make docker-build docker-push IMG=$IMG

Note: Be sure that there are no extra spaces in your PATH environment or the command will fail.

Run the Operator

Apply the Memcached Kind (CRD)

make