Skip to content

Commit 373ebf3

Browse files
authored
Use the Kubernetes Ansible collection (operator-framework#2646)
* Use the Kubernetes Ansible collection
1 parent 11aa2af commit 373ebf3

24 files changed

+74
-31
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
### Changed
2121
- Ansible scaffolding has been rewritten to be simpler and make use of newer features of Ansible and Molecule.
22+
- The Kubernetes modules have migrated to the [Kubernetes Ansible collection](https://github.com/ansible-collections/kubernetes). All scaffolded code now references modules from this collection instead of Ansible Core. No immediate action is required for existing users of the modules from core, though it is recommended they switch to using the collection to continue to get non-critical bugfixes and features. The collection is now installed by default in the base image. ([#2646](https://github.com/operator-framework/operator-sdk/pull/2646))
2223
- No longer generates the build/test-framework directory or molecule/test-cluster scenario
2324
- Adds new `cluster` scenario that can be used to test against an existing cluster
2425
- There is no longer any Ansible templating done in the `deploy/` directory, any templates used for testing will be located in `molecule/templates/` instead.

doc/ansible/dev/apb_migration_guide.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ would become:
155155

156156
```yaml
157157
- name: Create bind credential secret
158-
k8s:
158+
community.kubernetes.k8s:
159159
definition:
160160
apiVersion: v1
161161
kind: Secret
@@ -171,7 +171,7 @@ would become:
171171
DB_NAME: "{{ postgresql_database | b64encode }}"
172172
173173
- name: Attach secret to CR status
174-
k8s_status:
174+
operator_sdk.util.k8s_status:
175175
api_version: apps.example.com/v1alpha1
176176
kind: PostgreSQL
177177
name: '{{ meta.name }}'
@@ -182,8 +182,8 @@ would become:
182182

183183
### ansible_kubernetes_modules
184184
The `ansible_kubernetes_modules` role and the generated modules are now deprecated.
185-
The `k8s` module was added in Ansible 2.6 and is the supported way to interact with Kubernetes from Ansible.
186-
The `k8s` module takes normal kubernetes manifests, so if you currently rely on the old generated modules some refactoring will be required.
185+
The `community.kubernetes` Ansible collection supports Ansible 2.9+ and is the supported way to interact with Kubernetes from Ansible.
186+
The `community.kubernetes.k8s` module takes normal kubernetes manifests, so if you currently rely on the old generated modules some refactoring will be required.
187187

188188

189189
# convert.py

doc/ansible/dev/developer_guide.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ and test them using a playbook.
1919

2020
### Installing the k8s Ansible modules
2121

22-
To install the k8s Ansible modules, one must first install Ansible 2.6+. On
22+
To install the k8s Ansible modules, one must first install Ansible 2.9+. On
2323
Fedora/Centos:
2424
```bash
2525
$ sudo dnf install ansible
@@ -31,6 +31,11 @@ Python][openshift_restclient_python] package. This can be installed from pip:
3131
$ pip3 install openshift
3232
```
3333

34+
Finally, a user must install the Ansible Kubernetes collection from ansible-galaxy:
35+
```bash
36+
$ ansible-galaxy collection install community.kubernetes
37+
```
38+
3439
### Testing the k8s Ansible modules locally
3540

3641
Sometimes it is beneficial for a developer to run the Ansible code from their
@@ -61,7 +66,7 @@ we will create and delete a namespace with the switch of a variable:
6166
```yaml
6267
---
6368
- name: set example-memcached namespace to {{ state }}
64-
k8s:
69+
community.kubernetes.k8s:
6570
api_version: v1
6671
kind: Namespace
6772
name: example-memcached

doc/ansible/dev/retroactively-owned-resources.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ This file can be used as-is without user adjustments.
133133
- name: Import user variables
134134
include_vars: vars.yml
135135
- name: Retrieve owning resource
136-
k8s_info:
136+
community.kubernetes.k8s_info:
137137
api_version: "{{ owning_resource.apiVersion }}"
138138
kind: "{{ owning_resource.kind }}"
139139
name: "{{ owning_resource.name }}"
@@ -144,7 +144,7 @@ This file can be used as-is without user adjustments.
144144
include_tasks: each_resource.yml
145145
loop: "{{ resources_to_own }}"
146146
vars:
147-
to_be_owned: '{{ q("k8s",
147+
to_be_owned: '{{ q("community.kubernetes.k8s",
148148
api_version=item.apiVersion,
149149
kind=item.kind,
150150
resource_name=item.name,
@@ -168,7 +168,7 @@ This file can be used as-is without user adjustments.
168168
- to_be_owned.metadata.namespace == owning_resource.namespace
169169
- (to_be_owned.metadata.ownerReferences is not defined) or
170170
(owner_reference not in to_be_owned.metadata.ownerReferences)
171-
k8s:
171+
community.kubernetes.k8s:
172172
state: present
173173
resource_definition:
174174
apiVersion: "{{ to_be_owned.apiVersion }}"
@@ -180,7 +180,7 @@ This file can be used as-is without user adjustments.
180180
181181
- name: Patch resource with owner annotation
182182
when: to_be_owned.namespace is not defined or to_be_owned.namespace != owning_resource.namespace
183-
k8s:
183+
community.kubernetes.k8s:
184184
state: present
185185
resource_definition:
186186
apiVersion: "{{ to_be_owned.apiVersion }}"

doc/ansible/dev/testing_guide.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ We'll be adding the task to `roles/example/tasks/main.yml`, which should now loo
218218
---
219219
# tasks file for exampleapp
220220
- name: create Example configmap
221-
k8s:
221+
community.kubernetes.k8s:
222222
definition:
223223
apiVersion: v1
224224
kind: ConfigMap
@@ -246,11 +246,11 @@ The file should now look like this:
246246
tasks:
247247
- debug: var=cm
248248
vars:
249-
cm: '{{ lookup("k8s", api_version="v1", kind="ConfigMap", namespace=namespace, resource_name="test-data") }}'
249+
cm: '{{ lookup("community.kubernetes.k8s", api_version="v1", kind="ConfigMap", namespace=namespace, resource_name="test-data") }}'
250250
- assert:
251251
that: cm.data.hello == 'world'
252252
vars:
253-
cm: '{{ lookup("k8s", api_version="v1", kind="ConfigMap", namespace=namespace, resource_name="test-data") }}'
253+
cm: '{{ lookup("community.kubernetes.k8s", api_version="v1", kind="ConfigMap", namespace=namespace, resource_name="test-data") }}'
254254
```
255255
256256
Now that we have a functional Operator, and an assertion of its behavior, we can verify that everything is working

doc/ansible/user-guide.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ Modify `roles/memcached/tasks/main.yml` to look like the following:
201201
```yaml
202202
---
203203
- name: start memcached
204-
k8s:
204+
community.kubernetes.k8s:
205205
definition:
206206
kind: Deployment
207207
apiVersion: apps/v1

internal/scaffold/ansible/dockerfilehybrid.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ RUN yum clean all && rm -rf /var/cache/yum/* \
7979
&& yum remove -y gcc libffi-devel openssl-devel python36-devel \
8080
&& yum clean all \
8181
&& rm -rf /var/cache/yum \
82-
&& ansible-galaxy collection install operator_sdk.util
82+
&& ansible-galaxy collection install operator_sdk.util community.kubernetes
8383
8484
COPY build/_output/bin/[[.ProjectName]] ${OPERATOR}
8585
COPY bin /usr/local/bin

internal/scaffold/ansible/molecule_cluster_destroy.go

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const moleculeClusterDestroyAnsibleTmpl = `---
4646
connection: local
4747
gather_facts: false
4848
no_log: "{{ molecule_no_log }}"
49+
collections:
50+
- community.kubernetes
4951
5052
tasks:
5153
- name: Delete namespace

internal/scaffold/ansible/molecule_cluster_playbook.go

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const moleculeClusterPlaybookAnsibleTmpl = `---
4141
hosts: localhost
4242
connection: local
4343
gather_facts: no
44+
collections:
45+
- community.kubernetes
4446
4547
tasks:
4648
- name: Ensure operator image is set

internal/scaffold/ansible/molecule_cluster_prepare.go

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const moleculeClusterPrepareAnsibleTmpl = `---
4646
connection: local
4747
gather_facts: false
4848
no_log: "{{ molecule_no_log }}"
49+
collections:
50+
- community.kubernetes
51+
4952
vars:
5053
deploy_dir: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/deploy"
5154

internal/scaffold/ansible/molecule_cluster_verify.go

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const moleculeClusterVerifyAnsibleTmpl = `---
4646
hosts: localhost
4747
connection: local
4848
gather_facts: no
49+
collections:
50+
- community.kubernetes
51+
4952
vars:
5053
custom_resource: "{{ lookup('template', '/'.join([deploy_dir, 'crds/[[.Resource.FullGroup]]_[[.Resource.Version]]_[[.Resource.LowerKind]]_cr.yaml'])) | from_yaml }}"
5154

internal/scaffold/ansible/molecule_test_local_playbook.go

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func (m *MoleculeTestLocalPlaybook) GetInput() (input.Input, error) {
4343
const moleculeTestLocalPlaybookAnsibleTmpl = `---
4444
- name: Build Operator in Kubernetes docker container
4545
hosts: k8s
46+
collections:
47+
- community.kubernetes
48+
4649
vars:
4750
image: [[.Resource.FullGroup]]/[[.ProjectName]]:testing
4851
@@ -64,6 +67,9 @@ const moleculeTestLocalPlaybookAnsibleTmpl = `---
6467
- name: Converge
6568
hosts: localhost
6669
connection: local
70+
collections:
71+
- community.kubernetes
72+
6773
vars:
6874
image: [[.Resource.FullGroup]]/[[.ProjectName]]:testing
6975
operator_template: "{{ '/'.join([template_dir, 'operator.yaml.j2']) }}"

internal/scaffold/ansible/playbook.go

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ func (p *Playbook) GetInput() (input.Input, error) {
4040
const playbookTmpl = `---
4141
- hosts: localhost
4242
gather_facts: no
43+
collections:
44+
- community.kubernetes
45+
- operator_sdk.util
4346
4447
tasks:
4548
- import_role:

internal/scaffold/ansible/roles_meta_main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,6 @@ dependencies: []
100100
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
101101
# if you add dependencies to this list.
102102
collections:
103-
- operator_sdk.util`
103+
- operator_sdk.util
104+
- community.kubernetes
105+
`

test/ansible-memcached/memfin/tasks/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
- name: delete configmap for test
2-
k8s:
2+
community.kubernetes.k8s:
33
kind: ConfigMap
44
api_version: v1
55
name: deleteme

test/ansible-memcached/secret/tasks/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
- name: Create test service
2-
k8s:
2+
community.kubernetes.k8s:
33
definition:
44
kind: Service
55
api_version: v1

test/ansible-memcached/tasks.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
- name: start memcached
3-
k8s:
3+
community.kubernetes.k8s:
44
definition:
55
kind: Deployment
66
apiVersion: apps/v1
@@ -44,7 +44,7 @@
4444
status:
4545
test: "hello world"
4646

47-
- k8s:
47+
- community.kubernetes.k8s:
4848
definition:
4949
kind: Secret
5050
apiVersion: v1
@@ -55,10 +55,10 @@
5555
test: aGVsbG8K
5656
- name: Get cluster api_groups
5757
set_fact:
58-
api_groups: "{{ lookup('k8s', cluster_info='api_groups', kubeconfig=lookup('env', 'K8S_AUTH_KUBECONFIG')) }}"
58+
api_groups: "{{ lookup('community.kubernetes.k8s', cluster_info='api_groups', kubeconfig=lookup('env', 'K8S_AUTH_KUBECONFIG')) }}"
5959

6060
- name: create project if projects are available
61-
k8s:
61+
community.kubernetes.k8s:
6262
definition:
6363
apiVersion: project.openshift.io/v1
6464
kind: Project
@@ -67,7 +67,7 @@
6767
when: "'project.openshift.io' in api_groups"
6868

6969
- name: Create ConfigMap to test blacklisted watches
70-
k8s:
70+
community.kubernetes.k8s:
7171
definition:
7272
kind: ConfigMap
7373
apiVersion: v1

test/ansible-memcached/verify.yml

+4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
- name: Verify
44
hosts: localhost
55
connection: local
6+
collections:
7+
- community.kubernetes
8+
69
vars:
710
ansible_python_interpreter: '{{ ansible_playbook_python }}'
811
deploy_dir: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/deploy"
912
custom_resource: "{{ lookup('file', '/'.join([deploy_dir, 'crds/ansible.example.com_v1alpha1_memcached_cr.yaml'])) | from_yaml }}"
13+
1014
tasks:
1115
- block:
1216
- name: debug memcached lookup

test/ansible/build/Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ COPY fixture_collection/ /tmp/fixture_collection/
1111
USER root
1212
RUN chmod -R ug+rwx /tmp/fixture_collection
1313
USER 1001
14-
RUN ansible-galaxy collection build /tmp/fixture_collection/ --output-path /tmp/fixture_collection/
15-
RUN ansible-galaxy collection install /tmp/fixture_collection/operator_sdk-test_fixtures-0.0.0.tar.gz
14+
RUN ansible-galaxy collection build /tmp/fixture_collection/ --output-path /tmp/fixture_collection/ \
15+
&& ansible-galaxy collection install /tmp/fixture_collection/operator_sdk-test_fixtures-0.0.0.tar.gz

test/ansible/fixture_collection/roles/dummy/tasks/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
- name: Create ConfigMap
3-
k8s:
3+
community.kubernetes.k8s:
44
definition:
55
kind: ConfigMap
66
apiVersion: v1

test/ansible/molecule/cluster/playbook.yml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
hosts: localhost
44
connection: local
55
gather_facts: no
6+
collections:
7+
- community.kubernetes
68

79
tasks:
810
- name: Ensure operator image is set

test/ansible/molecule/cluster/verify.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
hosts: localhost
44
connection: local
55
gather_facts: no
6+
collections:
7+
- community.kubernetes
68
tasks:
7-
- import_tasks: tasks/inventory_test.yml
8-
- import_tasks: tasks/servicemonitor_test.yml
9-
- import_tasks: tasks/collections_test.yml
9+
- name: Import all test files from tasks/
10+
include_tasks: '{{ item }}'
11+
with_fileglob:
12+
- tasks/*_test.yml

test/ansible/molecule/test-local/playbook.yml

+7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
---
22
- name: Build Operator in Kubernetes docker container
33
hosts: k8s
4+
collections:
5+
- community.kubernetes
6+
47
vars:
58
image: test.example.com/ansible:testing
9+
610
tasks:
711
# using command so we don't need to install any dependencies
812
- name: Get existing image hash
@@ -21,6 +25,9 @@
2125
- name: Converge
2226
hosts: localhost
2327
connection: local
28+
collections:
29+
- community.kubernetes
30+
2431
vars:
2532
image: test.example.com/ansible:testing
2633
operator_template: "{{ '/'.join([template_dir, 'operator.yaml.j2']) }}"
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
- when: sentinel | test
33
block:
4-
- k8s:
4+
- community.kubernetes.k8s:
55
definition:
66
apiVersion: v1
77
kind: ConfigMap
@@ -10,4 +10,4 @@
1010
namespace: '{{ meta.namespace }}'
1111
data:
1212
sentinel: '{{ sentinel }}'
13-
groups: '{{ groups | to_nice_yaml }}'
13+
groups: '{{ groups | to_nice_yaml }}'

0 commit comments

Comments
 (0)