Skip to content
This repository was archived by the owner on Nov 18, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM quay.io/operator-framework/ansible-operator:v0.18.1
FROM quay.io/operator-framework/ansible-operator:master

COPY requirements.yml ${HOME}/requirements.yml
RUN ansible-galaxy collection install -r ${HOME}/requirements.yml \
&& chmod -R ug+rwx ${HOME}/.ansible

COPY watches.yaml ${HOME}/watches.yaml

COPY roles/ ${HOME}/roles/
COPY playbooks/ ${HOME}/playbooks/
127 changes: 91 additions & 36 deletions ansible/memcached-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,91 @@
.DEFAULT_GOAL:=help
SHELL:=/bin/bash
NAMESPACE=memcached

##@ Application

install: ## Install all resources (CR/CRD's, RBAC and Operator)
@echo ....... Creating namespace .......
- kubectl create namespace ${NAMESPACE}
@echo ....... Applying CRDs and Operator .......
- kubectl apply -f deploy/crds/cache.example.com_memcacheds_crd.yaml -n ${NAMESPACE}
@echo ....... Applying Rules and Service Account .......
- kubectl apply -f deploy/role.yaml -n ${NAMESPACE}
- kubectl apply -f deploy/role_binding.yaml -n ${NAMESPACE}
- kubectl apply -f deploy/service_account.yaml -n ${NAMESPACE}
@echo ....... Applying Operator .......
- kubectl apply -f deploy/operator.yaml -n ${NAMESPACE}
@echo ....... Creating the Database .......
- kubectl apply -f deploy/crds/cache.example.com_v1alpha1_memcached_cr.yaml -n ${NAMESPACE}

uninstall: ## Uninstall all that all performed in the $ make install
@echo ....... Uninstalling .......
@echo ....... Deleting CRDs.......
- kubectl delete -f deploy/crds/cache.example.com_memcacheds_crd.yaml -n ${NAMESPACE}
@echo ....... Deleting Rules and Service Account .......
- kubectl delete -f deploy/role.yaml -n ${NAMESPACE}
- kubectl delete -f deploy/role_binding.yaml -n ${NAMESPACE}
- kubectl delete -f deploy/service_account.yaml -n ${NAMESPACE}
@echo ....... Deleting Operator .......
- kubectl delete -f deploy/operator.yaml -n ${NAMESPACE}
@echo ....... Deleting namespace ${NAMESPACE}.......
- kubectl delete namespace ${NAMESPACE}

.PHONY: help
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
# Current Operator version
VERSION ?= 0.0.1
# Default bundle image tag
BUNDLE_IMG ?= controller-bundle:$(VERSION)
# Options for 'bundle-build'
ifneq ($(origin CHANNELS), undefined)
BUNDLE_CHANNELS := --channels=$(CHANNELS)
endif
ifneq ($(origin DEFAULT_CHANNEL), undefined)
BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
endif
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)

.PHONY: bundle

# Image URL to use all building/pushing image targets
IMG ?= controller:latest

all: docker-build

# Run against the configured Kubernetes cluster in ~/.kube/config
run: ansible-operator
$(ANSIBLE_OPERATOR)

# Install CRDs into a cluster
install: kustomize
$(KUSTOMIZE) build config/crd | kubectl apply -f -

# Uninstall CRDs from a cluster
uninstall: kustomize
$(KUSTOMIZE) build config/crd | kubectl delete -f -

# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: kustomize
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -

# Undeploy controller in the configured Kubernetes cluster in ~/.kube/config
undeploy: kustomize
$(KUSTOMIZE) build config/default | kubectl delete -f -

# Build the docker image
docker-build:
docker build . -t ${IMG}

# Push the docker image
docker-push:
docker push ${IMG}

PATH := $(PATH):$(PWD)/bin
SHELL := env PATH=$(PATH) /bin/sh
OS = $(shell uname -s | tr '[:upper:]' '[:lower:]')
ARCH = $(shell uname -m | sed 's/x86_64/amd64/')
OSOPER = $(shell uname -s | tr '[:upper:]' '[:lower:]' | sed 's/darwin/apple-darwin/' | sed 's/linux/linux-gnu/')
ARCHOPER = $(shell uname -m )

kustomize:
ifeq (, $(shell which kustomize 2>/dev/null))
@{ \
set -e ;\
mkdir -p bin ;\
curl -sSLo - https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v3.5.4/kustomize_v3.5.4_$(OS)_$(ARCH).tar.gz | tar xzf - -C bin/ ;\
}
KUSTOMIZE=./bin/kustomize
else
KUSTOMIZE=$(shell which kustomize)
endif

ansible-operator:
ifeq (, $(shell which ansible-operator 2>/dev/null))
@{ \
set -e ;\
mkdir -p bin ;\
curl -LO https://github.com/operator-framework/operator-sdk/releases/download/v0.19.0/ansible-operator-v0.19.0-$(ARCHOPER)-$(OSOPER) ;\
mv ansible-operator-v0.19.0-$(ARCHOPER)-$(OSOPER) ./bin/ansible-operator ;\
chmod +x ./bin/ansible-operator ;\
}
ANSIBLE_OPERATOR=$(realpath ./bin/ansible-operator)
else
ANSIBLE_OPERATOR=$(shell which ansible-operator)
endif

# Generate bundle manifests and metadata, then validate generated files.
bundle: kustomize
operator-sdk generate kustomize manifests -q
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
operator-sdk bundle validate ./bundle

# Build the bundle image.
bundle-build:
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
8 changes: 8 additions & 0 deletions ansible/memcached-operator/PROJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
domain: example.com
layout: ansible.sdk.operatorframework.io/v1
projectName: memcached-operator
resources:
- group: cache
kind: Memcached
version: v1alpha1
version: 3-alpha
84 changes: 42 additions & 42 deletions ansible/memcached-operator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This Memcached operator is a simple example operator for the [Operator SDK][oper

- [docker][docker_tool] version 17.03+
- [kubectl][kubectl_tool] v1.14.1+
- [operator_sdk][operator_install]
- [operator_sdk][operator_install] v1.0.0+
- Access to a Kubernetes v1.14.1+ cluster

## Getting Started
Expand All @@ -24,52 +24,58 @@ $ git clone https://github.com/operator-framework/operator-sdk-samples.git
$ cd operator-sdk-samples/ansible/memcached-operator
```

### Building the operator

Build the Memcached operator image and push it to a public registry, such as quay.io:
## Building and Pushing the Project Image

To build and push your image to your repository :
```
$ export IMAGE=quay.io/example-inc/memcached-operator:v0.0.1
$ operator-sdk build $IMAGE
$ docker push $IMAGE
$ make docker-build docker-push IMG=<some-registry>/<project-name>:tag
```

**NOTE** The `quay.io/example-inc/memcached-operator:v0.0.1` is an example. You should build and push the image for your repository.
Note: To allow the cluster pull the image the repository needs to be set as public.

### Using the image
## Applying the CRDs into the cluster:

To apply the Memcached Kind(CRD):
```
# Update the operator manifest to use the built image name (if you are performing these steps on OSX, see note below)
$ sed -i 's|REPLACE_IMAGE|quay.io/example-inc/memcached-operator:v0.0.1|g' deploy/operator.yaml
# On OSX use:
$ sed -i "" 's|REPLACE_IMAGE|quay.io/example-inc/memcached-operator:v0.0.1|g' deploy/operator.yaml
$ make install
```

### Installing

Run `make install` to install the operator. Check that the operator is running in the cluster, also check that the example Memcached service was deployed.

Following the expected result.
## Applying the CR’s into the cluster:

```shell
$ kubectl get all -n memcached
To create instances (CR’s) of the Memcached Kind (CRD) in the same namespaced of the operator:
```
$ kubectl apply -f config/samples/cache_v1alpha1_memcached.yaml -n memcached-operator-system
```

NAME READY STATUS RESTARTS AGE
pod/example-memcached-memcached-b885dcc75-2crw5 1/1 Running 0 22s
pod/example-memcached-memcached-b885dcc75-69mbg 1/1 Running 0 22s
pod/example-memcached-memcached-b885dcc75-92rd7 1/1 Running 0 22s
pod/memcached-operator-df88b85f7-9s98n 2/2 Running 0 36s
## Running it on Cluster
Deploy the project to the cluster:

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/memcached-operator-metrics ClusterIP 10.98.192.187 <none> 8383/TCP 31s
```
$ make deploy IMG=<some-registry>/<project-name>:tag
```

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/example-memcached-memcached 3/3 3 3 22s
deployment.apps/memcached-operator 1/1 1 1 36s
Following the expected result.

NAME DESIRED CURRENT READY AGE
replicaset.apps/example-memcached-memcached-b885dcc75 3 3 3 22s
replicaset.apps/memcached-operator-df88b85f7 1 1 1 36s
```shell
$ kubectl get all -n memcached-operator-system
NAME READY STATUS RESTARTS AGE
pod/memcached-operator-controller-manager-7dbcd676f9-s9nrz 2/2 Running 0 113s
pod/memcached-sample-memcached-6456bdd5fc-fdbfg 1/1 Running 0 67s
pod/memcached-sample-memcached-6456bdd5fc-p97h8 1/1 Running 0 67s
pod/memcached-sample-memcached-6456bdd5fc-q4wbb 1/1 Running 0 67s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/memcached-operator-controller-manager-metrics-service ClusterIP 10.96.153.103 <none> 8443/TCP 4h52m

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/memcached-operator-controller-manager 1/1 1 1 4h52m
deployment.apps/memcached-sample-memcached 3/3 3 3 67s

NAME DESIRED CURRENT READY AGE
replicaset.apps/memcached-operator-controller-manager-5b7f656f48 0 0 0 4h52m
replicaset.apps/memcached-operator-controller-manager-7dbcd676f9 1 1 1 113s
replicaset.apps/memcached-operator-controller-manager-7dbdb5b9ff 0 0 0 52m
replicaset.apps/memcached-sample-memcached-6456bdd5fc 3 3 3 67s
```

### Uninstalling
Expand All @@ -81,22 +87,16 @@ To uninstall all that was performed in the above step run `make uninstall`.
Use the following command to check the operator logs.

```
kubectl logs deployment.apps/memcached-operator -n memcached
$ kubectl logs deployment.apps/memcached-operator-controller-manager -n memcached-operator-system -c manager
```

**NOTE:** This project is configured with the environment variable `ANSIBLE_DEBUG_LOGS` as `True`, however, note that it is `False` by default.

**NOTE** To have further information about how to develop Ansible operators with [Operator-SDK][operator_sdk] check the [Ansible User Guide for Operator-SDK][ansible-guide]

### Testing the Operator
**NOTE** To have further information about how to develop Ansible operators with [Operator-SDK][operator_sdk] check the [Ansible docs][ansible-docs].

See [Testing Ansible Operators with Molecule][ansible-test-guide] documentation to know how to use the operator framework features to test it.

[python]: https://www.python.org/
[ansible]: https://www.ansible.com/
[kubectl_tool]: https://kubernetes.io/docs/tasks/tools/install-kubectl/
[docker_tool]: https://docs.docker.com/install/
[operator_sdk]: https://github.com/operator-framework/operator-sdk
[operator_install]: https://sdk.operatorframework.io/docs/install-operator-sdk/
[ansible-test-guide]: https://sdk.operatorframework.io/docs/ansible/testing-guide/
[ansible-guide]: https://sdk.operatorframework.io/docs/ansible/quickstart/
[ansible-docs]: https://sdk.operatorframework.io/docs/docs/building-operators/ansible/
16 changes: 0 additions & 16 deletions ansible/memcached-operator/build/test-framework/Dockerfile

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: memcacheds.cache.example.com
spec:
group: cache.example.com
names:
kind: Memcached
listKind: MemcachedList
plural: memcacheds
singular: memcached
scope: Namespaced
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: Memcached is the Schema for the memcacheds API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: Spec defines the desired state of Memcached
type: object
x-kubernetes-preserve-unknown-fields: true
status:
description: Status defines the observed state of Memcached
type: object
x-kubernetes-preserve-unknown-fields: true
type: object
served: true
storage: true
subresources:
status: {}
6 changes: 6 additions & 0 deletions ansible/memcached-operator/config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This kustomization.yaml is not intended to be run by itself,
# since it depends on service name and namespace that are out of this kustomize package.
# It should be run by config/default
resources:
- bases/cache.example.com_memcacheds.yaml
# +kubebuilder:scaffold:crdkustomizeresource
26 changes: 26 additions & 0 deletions ansible/memcached-operator/config/default/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Adds namespace to all resources.
namespace: memcached-operator-system

# Value of this field is prepended to the
# names of all resources, e.g. a deployment named
# "wordpress" becomes "alices-wordpress".
# Note that it should also match with the prefix (text before '-') of the namespace
# field above.
namePrefix: memcached-operator-

# Labels to add to all resources and selectors.
#commonLabels:
# someName: someValue

bases:
- ../crd
- ../rbac
- ../manager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
#- ../prometheus

patchesStrategicMerge:
# Protect the /metrics endpoint by putting it behind auth.
# If you want your controller-manager to expose the /metrics
# endpoint w/o any authn/z, please comment the following line.
- manager_auth_proxy_patch.yaml
Loading