Skip to content

Commit 5ad6898

Browse files
authored
Merge pull request #29156 from adellape/osdk_47_ansible
2 parents 12de474 + 80a1ffe commit 5ad6898

File tree

88 files changed

+2308
-1694
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+2308
-1694
lines changed

_topic_map.yml

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,18 +1139,44 @@ Topics:
11391139
File: osdk-about
11401140
- Name: Installing the CLI
11411141
File: osdk-installing-cli
1142-
- Name: Creating Go-based Operators
1142+
- Name: Go-based Operators
11431143
Dir: golang
11441144
Topics:
11451145
- Name: Quickstart
11461146
File: osdk-golang-quickstart
11471147
- Name: Tutorial
11481148
File: osdk-golang-tutorial
1149-
- Name: Creating Ansible-based Operators
1150-
File: osdk-ansible
1151-
- Name: Creating Helm-based Operators
1152-
File: osdk-helm
1153-
- Name: Generating a cluster service version (CSV)
1149+
- Name: Project layout
1150+
File: osdk-golang-project-layout
1151+
- Name: Ansible-based Operators
1152+
Dir: ansible
1153+
Topics:
1154+
- Name: Quickstart
1155+
File: osdk-ansible-quickstart
1156+
- Name: Tutorial
1157+
File: osdk-ansible-tutorial
1158+
- Name: Project layout
1159+
File: osdk-ansible-project-layout
1160+
- Name: Ansible support
1161+
File: osdk-ansible-support
1162+
- Name: Kubernetes Collection for Ansible
1163+
File: osdk-ansible-k8s-collection
1164+
- Name: Using Ansible inside an Operator
1165+
File: osdk-ansible-inside-operator
1166+
- Name: Custom resource status management
1167+
File: osdk-ansible-cr-status
1168+
- Name: Helm-based Operators
1169+
Dir: helm
1170+
Topics:
1171+
- Name: Quickstart
1172+
File: osdk-helm-quickstart
1173+
- Name: Tutorial
1174+
File: osdk-helm-tutorial
1175+
- Name: Project layout
1176+
File: osdk-helm-project-layout
1177+
- Name: Helm support
1178+
File: osdk-helm-support
1179+
- Name: Defining cluster service versions (CSVs)
11541180
File: osdk-generating-csvs
11551181
- Name: Working with bundle images
11561182
File: osdk-working-bundle-images
@@ -1165,8 +1191,6 @@ Topics:
11651191
- Name: Migrating to Operator SDK v0.1.0
11661192
File: osdk-migrating-to-v0-1-0
11671193
Distros: openshift-origin
1168-
- Name: Appendices
1169-
File: osdk-appendices
11701194
- Name: Red Hat Operators reference
11711195
File: operator-reference
11721196
---

modules/osdk-about-openapi-validation.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Module included in the following assemblies:
22
//
3-
// * operators/operator_sdk/osdk-golang-tutorial.adoc
3+
// * operators/operator_sdk/golang/osdk-golang-tutorial.adoc
44

55
[id="osdk-about-openapi-validation_{context}"]
66
= About OpenAPI validation
Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Module included in the following assemblies:
22
//
3-
// * operators/operator_sdk/osdk-ansible.adoc
3+
// * operators/operator_sdk/ansible/osdk-ansible-cr-status.adoc
44

5-
[id="osdk-ansible-managing-cr-status_{context}"]
6-
= Managing custom resource status using the `operator_sdk.util` Ansible collection
5+
[id="osdk-ansible-cr-status-about_{context}"]
6+
= About custom resource status in Ansible-based Operators
77

88
Ansible-based Operators automatically update custom resource (CR) link:https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#status-subresource[`status` subresources] with generic information about the previous Ansible run. This includes the number of successful and failed tasks and relevant error messages as shown:
99

1010
[source,yaml]
1111
----
1212
status:
1313
conditions:
14-
- ansibleResult:
14+
- ansibleResult:
1515
changed: 3
1616
completion: 2018-12-03T13:45:57.13329
1717
failures: 1
@@ -33,51 +33,3 @@ status:
3333
Ansible-based Operators also allow Operator authors to supply custom status values with the `k8s_status` Ansible module, which is included in the link:https://galaxy.ansible.com/operator_sdk/util[`operator_sdk.util` collection]. This allows the author to update the `status` from within Ansible with any key-value pair as desired.
3434

3535
By default, Ansible-based Operators always include the generic Ansible run output as shown above. If you would prefer your application did _not_ update the status with Ansible output, you can track the status manually from your application.
36-
37-
.Procedure
38-
39-
. To track CR status manually from your application, update the `watches.yaml` file with a `manageStatus` field set to `false`:
40-
+
41-
[source,yaml]
42-
----
43-
- version: v1
44-
group: api.example.com
45-
kind: Test1
46-
role: Test1
47-
manageStatus: false
48-
----
49-
50-
. Use the `operator_sdk.util.k8s_status` Ansible module to update the subresource. For example, to update with key `test1` and value `test2`, `operator_sdk.util` can be used as shown:
51-
+
52-
[source,yaml]
53-
----
54-
- operator_sdk.util.k8s_status:
55-
api_version: app.example.com/v1
56-
kind: Test1
57-
name: "{{ meta.name }}"
58-
namespace: "{{ meta.namespace }}"
59-
status:
60-
test1: test2
61-
----
62-
+
63-
Collections can also be declared in the `meta/main.yml` for the role, which is included for new scaffolded Ansible Operators:
64-
+
65-
[source,yaml]
66-
----
67-
collections:
68-
- operator_sdk.util
69-
----
70-
+
71-
Declaring collections in the role meta allows you to invoke the `k8s_status` module directly:
72-
+
73-
[source,yaml]
74-
----
75-
k8s_status:
76-
<snip>
77-
status:
78-
test1: test2
79-
----
80-
81-
.Additional resources
82-
83-
- For more details about user-driven status management from Ansible-based Operators, see the link:https://github.com/operator-framework/operator-sdk/blob/master/proposals/ansible-operator-status.md[Ansible-based Operator Status Proposal for Operator SDK].
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Module included in the following assemblies:
2+
//
3+
// * operators/operator_sdk/ansible/osdk-ansible-cr-status.adoc
4+
5+
[id="osdk-ansible-cr-status-manual_{context}"]
6+
= Tracking custom resource status manually
7+
8+
You can use the `operator_sdk.util` collection to modify your Ansible-based Operator to track custom resource (CR) status manually from your application.
9+
10+
.Prerequisites
11+
12+
* Ansible-based Operator project created by using the Operator SDK
13+
14+
.Procedure
15+
16+
. Update the `watches.yaml` file with a `manageStatus` field set to `false`:
17+
+
18+
[source,yaml]
19+
----
20+
- version: v1
21+
group: api.example.com
22+
kind: <kind>
23+
role: <role>
24+
manageStatus: false
25+
----
26+
27+
. Use the `operator_sdk.util.k8s_status` Ansible module to update the subresource. For example, to update with key `test` and value `data`, `operator_sdk.util` can be used as shown:
28+
+
29+
[source,yaml]
30+
----
31+
- operator_sdk.util.k8s_status:
32+
api_version: app.example.com/v1
33+
kind: <kind>
34+
name: "{{ ansible_operator_meta.name }}"
35+
namespace: "{{ ansible_operator_meta.namespace }}"
36+
status:
37+
test: data
38+
----
39+
40+
. You can declare collections in the `meta/main.yml` file for the role, which is included for scaffolded Ansible-based Operators:
41+
+
42+
[source,yaml]
43+
----
44+
collections:
45+
- operator_sdk.util
46+
----
47+
48+
. After declaring collections in the role meta, you can invoke the `k8s_status` module directly:
49+
+
50+
[source,yaml]
51+
----
52+
k8s_status:
53+
...
54+
status:
55+
key1: value1
56+
----
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Module included in the following assemblies:
2+
//
3+
// * operators/operator_sdk/ansible/osdk-ansible-tutorial.adoc
4+
5+
[id="osdk-ansible-create-api-controller_{context}"]
6+
= Creating an API
7+
8+
Use the Operator SDK CLI to create a Memcached API.
9+
10+
.Procedure
11+
12+
* Run the following command to create an API with group `cache`, version, `v1`, and kind `Memcached`:
13+
+
14+
[source,terminal]
15+
----
16+
$ operator-sdk create api \
17+
--group cache \
18+
--version v1 \
19+
--kind Memcached \
20+
--generate-role <1>
21+
----
22+
<1> Generates an Ansible role for the API.
23+
24+
After creating the API, your Operator project updates with the following structure:
25+
26+
Memcached CRD:: Includes a sample `Memcached` resource
27+
28+
Manager:: Program that reconciles the state of the cluster to the desired state by using:
29+
+
30+
--
31+
* A reconciler, either an Ansible role or playbook
32+
* A `watches.yaml` file, which connects the `Memcached` resource to the `memcached` Ansible role
33+
--

modules/osdk-ansible-custom-resource-files.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Module included in the following assemblies:
22
//
3-
// * operators/operator_sdk/osdk-ansible.adoc
3+
// * operators/operator_sdk/ansible/osdk-ansible-support.adoc
44

55
[id="osdk-ansible-custom-resource-files_{context}"]
66
= Custom resource files

modules/osdk-ansible-extra-variables.adoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Module included in the following assemblies:
22
//
3-
// * operators/operator_sdk/osdk-ansible.adoc
3+
// * operators/operator_sdk/ansible/osdk-ansible-support.adoc
44

55
[id="osdk-ansible-extra-variables_{context}"]
66
= Extra variables sent to Ansible
@@ -42,6 +42,7 @@ The `message` and `newParameter` fields are set in the top level as extra variab
4242

4343
[source,yaml]
4444
----
45+
---
4546
- debug:
46-
msg: "name: {{ meta.name }}, {{ meta.namespace }}"
47+
msg: "name: {{ ansible_operator_meta.name }}, {{ ansible_operator_meta.namespace }}"
4748
----
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Module included in the following assemblies:
2+
//
3+
// * operators/operator_sdk/ansible/osdk-ansible-inside-operator.adoc
4+
5+
[id="osdk-ansible-inside-operator-local_{context}"]
6+
= Testing an Ansible-based Operator locally
7+
8+
You can test the logic inside of an Ansible-based Operator running locally by using the `make run` command from the top-level directory of your Operator project. The `make run` Makefile target runs the `ansible-operator` binary locally, which reads from the `watches.yaml` file and uses your `~/.kube/config` file to communicate with a Kubernetes cluster just as the `k8s` modules do.
9+
10+
[NOTE]
11+
====
12+
You can customize the roles path by setting the environment variable `ANSIBLE_ROLES_PATH` or by using the `ansible-roles-path` flag. If the role is not found in the `ANSIBLE_ROLES_PATH` value, the Operator looks for it in `{{current directory}}/roles`.
13+
====
14+
15+
.Prerequisites
16+
17+
- link:https://ansible-runner.readthedocs.io/en/latest/install.html[Ansible Runner] version v1.1.0+
18+
- link:https://github.com/ansible/ansible-runner-http[Ansible Runner HTTP Event Emitter plug-in] version v1.0.0+
19+
- Performed the previous steps for testing the Kubernetes Collection locally
20+
21+
.Procedure
22+
23+
. Install your custom resource definition (CRD) and proper role-based access control (RBAC) definitions for your custom resource (CR):
24+
+
25+
[source,terminal]
26+
----
27+
$ make install
28+
----
29+
+
30+
.Example output
31+
[source,terminal]
32+
----
33+
/usr/bin/kustomize build config/crd | kubectl apply -f -
34+
customresourcedefinition.apiextensions.k8s.io/memcacheds.cache.example.com created
35+
----
36+
37+
. Run the `make run` command:
38+
+
39+
[source,terminal]
40+
----
41+
$ make run
42+
----
43+
+
44+
.Example output
45+
[source,terminal]
46+
----
47+
/home/user/memcached-operator/bin/ansible-operator run
48+
{"level":"info","ts":1612739145.2871568,"logger":"cmd","msg":"Version","Go Version":"go1.15.5","GOOS":"linux","GOARCH":"amd64","ansible-operator":"v1.3.0","commit":"1abf57985b43bf6a59dcd18147b3c574fa57d3f6"}
49+
...
50+
{"level":"info","ts":1612739148.347306,"logger":"controller-runtime.metrics","msg":"metrics server is starting to listen","addr":":8080"}
51+
{"level":"info","ts":1612739148.3488882,"logger":"watches","msg":"Environment variable not set; using default value","envVar":"ANSIBLE_VERBOSITY_MEMCACHED_CACHE_EXAMPLE_COM","default":2}
52+
{"level":"info","ts":1612739148.3490262,"logger":"cmd","msg":"Environment variable not set; using default value","Namespace":"","envVar":"ANSIBLE_DEBUG_LOGS","ANSIBLE_DEBUG_LOGS":false}
53+
{"level":"info","ts":1612739148.3490646,"logger":"ansible-controller","msg":"Watching resource","Options.Group":"cache.example.com","Options.Version":"v1","Options.Kind":"Memcached"}
54+
{"level":"info","ts":1612739148.350217,"logger":"proxy","msg":"Starting to serve","Address":"127.0.0.1:8888"}
55+
{"level":"info","ts":1612739148.3506632,"logger":"controller-runtime.manager","msg":"starting metrics server","path":"/metrics"}
56+
{"level":"info","ts":1612739148.350784,"logger":"controller-runtime.manager.controller.memcached-controller","msg":"Starting EventSource","source":"kind source: cache.example.com/v1, Kind=Memcached"}
57+
{"level":"info","ts":1612739148.5511978,"logger":"controller-runtime.manager.controller.memcached-controller","msg":"Starting Controller"}
58+
{"level":"info","ts":1612739148.5512562,"logger":"controller-runtime.manager.controller.memcached-controller","msg":"Starting workers","worker count":8}
59+
----
60+
+
61+
With the Operator now watching your CR for events, the creation of a CR will trigger your Ansible role to run.
62+
+
63+
[NOTE]
64+
====
65+
Consider an example `config/samples/<gvk>.yaml` CR manifest:
66+
67+
[source,yaml]
68+
----
69+
apiVersion: <group>.example.com/v1alpha1
70+
kind: <kind>
71+
metadata:
72+
name: "<kind>-sample"
73+
----
74+
75+
Because the `spec` field is not set, Ansible is invoked with no extra variables. Passing extra variables from a CR to Ansible is covered in another section. It is important to set reasonable defaults for the Operator.
76+
====
77+
78+
. Create an instance of your CR with the default variable `state` set to `present`:
79+
+
80+
[source,terminal]
81+
----
82+
$ oc apply -f config/samples/<gvk>.yaml
83+
----
84+
85+
. Check that the `example-config` config map was created:
86+
+
87+
[source,terminal]
88+
----
89+
$ oc get configmaps
90+
----
91+
+
92+
.Example output
93+
[source,terminal]
94+
----
95+
NAME STATUS AGE
96+
example-config Active 3s
97+
----
98+
99+
. Modify your `config/samples/<gvk>.yaml` file to set the `state` field to `absent`. For example:
100+
+
101+
[source,yaml]
102+
----
103+
apiVersion: cache.example.com/v1
104+
kind: Memcached
105+
metadata:
106+
name: memcached-sample
107+
spec:
108+
state: absent
109+
----
110+
111+
. Apply the changes:
112+
+
113+
[source,terminal]
114+
----
115+
$ oc apply -f config/samples/<gvk>.yaml
116+
----
117+
118+
. Confirm that the config map is deleted:
119+
+
120+
[source,terminal]
121+
----
122+
$ oc get configmap
123+
----

0 commit comments

Comments
 (0)