Ansible and inserted underscores in extravars
Bug Report
What did you do? I created a cr with, in the spec the "create_k8s_namespace" key
What did you expect to see? I expect to see "create_k8s_namespace" as an extravars. Confirmed reading /tmp/ansible-operator/runner/XYZ/v1alpha1/ASD/default/test-2/env/extravars
What did you see instead? Under which circumstances? I see "create_k_8_s_namespace" (with inserted underscores)
Environment
-
operator-sdk version: 1.14.0
-
Kubernetes version information: v1.15.4-gke.22
-
Kubernetes cluster kind: gke
-
Are you writing your operator in ansible, helm, or go? ansible
HI @johnnyrun,
Could you provide step by step all that we need to reproduce this scenario?
Something as for example:
- Create a project following the ansible getting started ( memcached )
- Then update the file X with the following code:
- Then perform N
- Then, check Y
Also, could you please add the logs/output and how you are checking it and where.
Really thank you for your collaboration and attention.
Hi @camilamacedo86, this is the scenario. Thanks for the attention
Create the operator using cli
$ operator-sdk version
operator-sdk version: "v0.14.0", commit: "4fb185b9d45682473b4951c9788f845ba2fa5b12", go version: "go1.13.6 darwin/amd64"
$ operator-sdk new bugtest-operator --api-version=johnnyrun.github.com/v1alpha1 --kind=Bugtest --type=ansible
$ cd bugtest-operator
setup a spec key/value changing the default "size" param
$ sed -i s/size/size_k8s_bugtest/ deploy/crds/johnnyrun.github.com_v1alpha1_bugtest_cr.yaml
setup a custom playbook, copy inside the image and setup watches.yaml
#main playbook
$ cat dumpvar.yaml
---
- hosts: localhost
tasks:
- name: debug size_k8s_bugtest string
debug: var=size_k8s_bugtest
- name: debug size_k_8_s_bugtest string
debug: var=size_k_8_s_bugtest
#copy this playbook inside the image
$ cat build/Dockerfile
FROM quay.io/operator-framework/ansible-operator:v0.14.0
COPY watches.yaml ${HOME}/watches.yaml
COPY dumpvar.yaml /opt/ansible/
# watches refers to this playbook
$ cat watches.yaml
---
- version: v1alpha1
group: johnnyrun.github.com
kind: Bugtest
playbook: /opt/ansible/dumpvar.yaml
build:
$operator-sdk build removeme
create crd and cr in the cluster
$ kubectl apply -f deploy/crds
run locally but inside docker, putting the kubernetes config inside the container:
docker run -ti --rm -e OPERATOR_NAME=bugtest-operator -e KUBECONFIG=/tmp/config -v /tmp/config:/tmp/config removeme
{"level":"info","ts":1578845585.5585523,"logger":"cmd","msg":"Go Version: go1.13.6"}
{"level":"info","ts":1578845585.5589025,"logger":"cmd","msg":"Go OS/Arch: linux/amd64"}
{"level":"info","ts":1578845585.559011,"logger":"cmd","msg":"Version of operator-sdk: v0.14.0"}
{"level":"info","ts":1578845585.5591285,"logger":"cmd","msg":"WATCH_NAMESPACE environment variable not set. This operator is watching all namespaces.","Namespace":""}
{"level":"info","ts":1578845586.3889246,"logger":"controller-runtime.metrics","msg":"metrics server is starting to listen","addr":"0.0.0.0:8383"}
{"level":"info","ts":1578845586.390343,"logger":"watches","msg":"Environment variable not set; using default value","envVar":"WORKER_BUGTEST_JOHNNYRUN_GITHUB_COM","default":1}
{"level":"info","ts":1578845586.390622,"logger":"watches","msg":"Environment variable not set; using default value","envVar":"ANSIBLE_VERBOSITY_BUGTEST_JOHNNYRUN_GITHUB_COM","default":2}
{"level":"info","ts":1578845586.3907392,"logger":"ansible-controller","msg":"Watching resource","Options.Group":"johnnyrun.github.com","Options.Version":"v1alpha1","Options.Kind":"Bugtest"}
{"level":"info","ts":1578845586.3913076,"logger":"leader","msg":"Trying to become the leader."}
{"level":"info","ts":1578845586.3916688,"logger":"leader","msg":"Skipping leader election; not running in a cluster."}
{"level":"info","ts":1578845587.553924,"logger":"metrics","msg":"Skipping metrics Service creation; not running in a cluster."}
{"level":"info","ts":1578845587.5560038,"logger":"proxy","msg":"Starting to serve","Address":"127.0.0.1:8888"}
{"level":"info","ts":1578845587.5565903,"logger":"controller-runtime.manager","msg":"starting metrics server","path":"/metrics"}
{"level":"info","ts":1578845587.5569623,"logger":"controller-runtime.controller","msg":"Starting EventSource","controller":"bugtest-controller","source":"kind source: johnnyrun.github.com/v1alpha1, Kind=Bugtest"}
{"level":"info","ts":1578845587.6589544,"logger":"controller-runtime.controller","msg":"Starting Controller","controller":"bugtest-controller"}
{"level":"info","ts":1578845587.659084,"logger":"controller-runtime.controller","msg":"Starting workers","controller":"bugtest-controller","worker count":1}
{"level":"info","ts":1578845590.506479,"logger":"logging_event_handler","msg":"[playbook task]","name":"example-bugtest","namespace":"default","gvk":"johnnyrun.github.com/v1alpha1, Kind=Bugtest","event_type":"playbook_on_task_start","job":"6129484611666145821","EventData.Name":"Gathering Facts"}
--------------------------- Ansible Task StdOut -------------------------------
TASK [Gathering Facts] *********************************************************
task path: /opt/ansible/dumpvar.yaml:2
-------------------------------------------------------------------------------
{"level":"info","ts":1578845592.3572443,"logger":"logging_event_handler","msg":"[playbook debug]","name":"example-bugtest","namespace":"default","gvk":"johnnyrun.github.com/v1alpha1, Kind=Bugtest","event_type":"runner_on_ok","job":"6129484611666145821","EventData.TaskArgs":""}
--------------------------- Ansible Task StdOut -------------------------------
TASK [debug size_k8s_bugtest string] ********************************
ok: [localhost] => {
"size_k8s_bugtest": "VARIABLE IS NOT DEFINED!: 'size_k8s_bugtest' is undefined"
}
-------------------------------------------------------------------------------
{"level":"info","ts":1578845592.397763,"logger":"logging_event_handler","msg":"[playbook debug]","name":"example-bugtest","namespace":"default","gvk":"johnnyrun.github.com/v1alpha1, Kind=Bugtest","event_type":"runner_on_ok","job":"6129484611666145821","EventData.TaskArgs":""}
--------------------------- Ansible Task StdOut -------------------------------
TASK [debug size_k_8_s_bugtest string] ********************************
ok: [localhost] => {
"size_k_8_s_bugtest": 3
}
-------------------------------------------------------------------------------
debug extravars generated file (look at the end of the line pls)
$ docker exec -ti admiring_lewin cat /tmp/ansible-operator/runner/johnnyrun.github.com/v1alpha1/Bugtest/default/example-bugtest/env/extravars
{"_johnnyrun_github_com_bugtest":{"apiVersion":"johnnyrun.github.com/v1alpha1","kind":"Bugtest","metadata":{"annotations":{"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"johnnyrun.github.com/v1alpha1\",\"kind\":\"Bugtest\",\"metadata\":{\"annotations\":{},\"name\":\"example-bugtest\",\"namespace\":\"default\"},\"spec\":{\"size_k8s_bugtest\":3}}\n"},"creationTimestamp":"2020-01-12T16:10:20Z","generation":1,"name":"example-bugtest","namespace":"default","resourceVersion":"1081259","selfLink":"/apis/johnnyrun.github.com/v1alpha1/namespaces/default/bugtests/example-bugtest/status","uid":"3fe65202-c13f-4280-823a-463152433792"},"spec":{"size_k8s_bugtest":3},"status":{"conditions":[{"lastTransitionTime":"2020-01-12T16:10:41Z","message":"Running reconciliation","reason":"Running","status":"True","type":"Running"}]}},"_johnnyrun_github_com_bugtest_spec":{"size_k8s_bugtest":3},"meta":{"name":"example-bugtest","namespace":"default"},"size_k_8_s_bugtest":3}
The same using your vanilla repo ansible-memcached-operator and adding only the spec.
git clone https://github.com/camilamacedo86/ansible-memcached-operator
cd ansible-memcached-operator
sed -i s/size/test_k8s_size/ deploy/crds/cache.example.com_v1alpha1_memcached_cr.yaml
operator-sdk build removeme
kubectl apply -f deploy/crds
docker run -ti --rm -e OPERATOR_NAME=ansible-memcached-operator -e KUBECONFIG=/tmp/config -v /tmp/config:/tmp/config removeme
docker exec affectionate_lederberg cat /tmp/ansible-operator/runner/cache.example.com/v1alpha1/Memcached/default/example-memcached/env/extravars |jq '.'
{
"_cache_example_com_memcached": {
"apiVersion": "cache.example.com/v1alpha1",
"kind": "Memcached",
"metadata": {
"annotations": {
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"cache.example.com/v1alpha1\",\"kind\":\"Memcached\",\"metadata\":{\"annotations\":{},\"name\":\"example-memcached\",\"namespace\":\"default\"},\"spec\":{\"test_k8s_size\":3}}\n"
},
"creationTimestamp": "2020-01-12T16:36:57Z",
"generation": 1,
"name": "example-memcached",
"namespace": "default",
"resourceVersion": "1091611",
"selfLink": "/apis/cache.example.com/v1alpha1/namespaces/default/memcacheds/example-memcached/status",
"uid": "74abb108-c45e-412c-9b4a-82182ba75cab"
},
"spec": {
"test_k8s_size": 3
},
"status": {
"conditions": [
{
"lastTransitionTime": "2020-01-12T16:37:23Z",
"message": "Running reconciliation",
"reason": "Running",
"status": "True",
"type": "Running"
}
]
}
},
"_cache_example_com_memcached_spec": {
"test_k8s_size": 3
},
"meta": {
"name": "example-memcached",
"namespace": "default"
},
"test_k_8_s_size": 3
}
https://github.com/operator-framework/operator-sdk/blob/92d78a2c25f0edbe039c4906a88fbb38c022018c/pkg/ansible/runner/runner.go#L287
there is a call to mapToSnake, that go to ToSnake() and finally to addWordBoundariesToNumbers(), that intentionally do the conversion:
https://github.com/operator-framework/operator-sdk/blob/92d78a2c25f0edbe039c4906a88fbb38c022018c/pkg/ansible/paramconv/paramconv.go#L75
@johnnyrun, thanks for filing, I can see why that behavior is unexpected. Does your variable name need to have "k8s" in it? My gut says we should treat this as correct behavior and add something to the docs so others aren't surprised.
Thank you @johnnyrun for filing this. We've been banging our heads for days now trying to understand why variable resolution didn't work in our ansible operator where we used names like: db2_port.
The confusing part about current behavior is that snake case conversion isn't very well documented especially around actions when a string contains numbers.
Most examples on the web only demonstrate conversions like: CamelCase -> camel_case
I was not able to find any official documentation on numbers processing during conversion to snake case.
Please add documentation to Operator SDK regarding this aspect and provide good examples including combinations of capital letters and numbers used in variable names.
sorry @asmacdo I lost your reply. One plus of the operator-sdk in flavour ansible is that you can use existing playbook in k8s. But in this case it is not possible due to I need to modify it.
example: want to use kubespray inside ansible-sdk? At the moment you can't without forking kubespray.
example: https://github.com/kubernetes-sigs/kubespray/blob/master/inventory/sample/group_vars/k8s-cluster/k8s-cluster.yml#L170
@dubauski Thanks. At the moment I wrap the role I need to run in a playbook that begins with:
- hosts: localhost
vars:
k8s_namespace: "{{ k_8_s_namespace }}"
In order to workaround it. @asmacdo says this is the correct behavior. I don't agree.. I'll fork later but for the moment I use this workaround.
@johnnyrun, see #1770 (specifically https://github.com/operator-framework/operator-sdk/issues/1770#issuecomment-573022243). I had the same problem this morning and was able to get the unaltered spec of my CR using the _<group_name>_<kind>_spec variable that is exposed to my Ansible playbook through the operator.
@johnnyrun, see #1770 (specifically #1770 (comment)). I had the same problem this morning and was able to get the unaltered
specof my CR using the_<group_name>_<kind>_specvariable that is exposed to my Ansible playbook through the operator.
Thank you @jValdron for pointing out the hidden variable that can provide original values. This may prove helpful.
I could reproduce the scenario with 0.16.0 SDK version on top of Ansible Memcached Sample by performing the following changes
- Update the CR:
apiVersion: cache.example.com/v1alpha1
kind: Memcached
metadata:
name: example-memcached
spec:
# Add fields here
size: 3
size_k8s_bugtest: test1
size_k_8_s_bugtest: test2
- Add the tasks to debug their values:
---
# tasks file for Memcached
- name: start memcached
k8s:
...
- name: debug size_k8s_bugtest string
debug: var=size_k8s_bugtest
- name: debug size_k_8_s_bugtest string
debug: var=size_k_8_s_bugtest
- Running
make installand checking its result:
TASK [memcached : debug size_k8s_bugtest string] *******************************
task path: /opt/ansible/roles/memcached/tasks/main.yml:33
ok: [localhost] => {
"size_k8s_bugtest": "VARIABLE IS NOT DEFINED!: 'size_k8s_bugtest' is undefined"
}
TASK [memcached : debug size_k_8_s_bugtest string] *****************************
task path: /opt/ansible/roles/memcached/tasks/main.yml:35
ok: [localhost] => {
"size_k_8_s_bugtest": "test1"
}
META: ran handlers
META: ran handlers