Skip to content

Commit e48552e

Browse files
authored
Stop attempting to cache all subresources in Ansible Operator proxy (#3103)
* Add failing exec test * Only cache the status subresource request, otherwise pass it through * add changelog fragment * Fix tests and improve output * ensure kubernetes collection is installed for molecule CI tests
1 parent 1d53403 commit e48552e

File tree

9 files changed

+103
-43
lines changed

9 files changed

+103
-43
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
entries:
2+
- description: >
3+
The Ansible Operator proxy no longer will attempt to cache non-status
4+
subresource requests. This will fix the issue where attempting to get
5+
Pod logs returns the API Pod resource instead of the log contents.
6+
7+
kind: "bugfix"
8+
9+
breaking: false

hack/tests/e2e-ansible-molecule.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pip3 install --user pyasn1==0.4.7 pyasn1-modules==0.2.6 idna==2.8 ipaddress==1.0
1414
pip3 install --user molecule==3.0.2
1515
pip3 install --user ansible-lint yamllint
1616
pip3 install --user docker openshift jmespath
17+
ansible-galaxy collection install community.kubernetes
1718

1819
deploy_prereqs() {
1920
header_text "Deploying resources"

pkg/ansible/proxy/cache_response.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func (c *cacheResponseHandler) ServeHTTP(w http.ResponseWriter, req *http.Reques
6767
break
6868
}
6969

70-
// Skip cache for non-resource requests, not a part of skipCacheLookup for performance.
71-
if !r.IsResourceRequest {
70+
// Skip cache for non-cacheable requests, not a part of skipCacheLookup for performance.
71+
if !r.IsResourceRequest || !(r.Subresource == "" || r.Subresource == "status") {
7272
log.Info("Skipping cache lookup", "resource", r)
7373
break
7474
}

test/ansible/deploy/crds/test.example.com_exectests_crd.yaml renamed to test/ansible/deploy/crds/test.example.com_subresourcestests_crd.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
apiVersion: apiextensions.k8s.io/v1beta1
22
kind: CustomResourceDefinition
33
metadata:
4-
name: exectests.test.example.com
4+
name: subresourcestests.test.example.com
55
spec:
66
group: test.example.com
77
names:
8-
kind: ExecTest
9-
listKind: ExecTestList
10-
plural: exectests
11-
singular: exectest
8+
kind: SubresourcesTest
9+
listKind: SubresourcesTestList
10+
plural: subresourcestests
11+
singular: subresourcestest
1212
scope: Namespaced
1313
subresources:
1414
status: {}

test/ansible/molecule/cluster/tasks/exec_test.yml

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
- name: Create the test.example.com/v1alpha1.SubresourcesTest
3+
k8s:
4+
state: present
5+
definition:
6+
apiVersion: test.example.com/v1alpha1
7+
kind: SubresourcesTest
8+
metadata:
9+
name: subresources-test
10+
namespace: '{{ namespace }}'
11+
spec:
12+
execCommand: "echo 'hello world'"
13+
logMessage: "Running..."
14+
wait: yes
15+
wait_timeout: 300
16+
wait_condition:
17+
type: Running
18+
reason: Successful
19+
status: "True"
20+
register: subresources_test
21+
22+
- debug: var=subresources_test
23+
24+
- name: Assert stdout and stderr are properly set in status
25+
assert:
26+
that:
27+
- subresources_test.result.status.execCommandStderr == ""
28+
- subresources_test.result.status.execCommandStdout == "hello world"
29+
- "'Running' in subresources_test.result.status.logs"

test/ansible/molecule/cluster/verify.yml

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,48 @@
66
collections:
77
- community.kubernetes
88
tasks:
9-
- name: Import all test files from tasks/
10-
include_tasks: '{{ item }}'
11-
with_fileglob:
12-
- tasks/*_test.yml
9+
- block:
10+
- name: Import all test files from tasks/
11+
include_tasks: '{{ item }}'
12+
with_fileglob:
13+
- tasks/*_test.yml
14+
rescue:
15+
- name: Retrieve relevant resources
16+
k8s_info:
17+
api_version: '{{ item.api_version }}'
18+
kind: '{{ item.kind }}'
19+
namespace: '{{ namespace }}'
20+
loop:
21+
- api_version: v1
22+
kind: Pod
23+
- api_version: apps/v1
24+
kind: Deployment
25+
- api_version: v1
26+
kind: Secret
27+
- api_version: v1
28+
kind: ConfigMap
29+
register: debug_resources
30+
31+
- name: Retrieve Pod logs
32+
k8s_log:
33+
name: '{{ item.metadata.name }}'
34+
namespace: '{{ namespace }}'
35+
loop: '{{ q("k8s", api_version="v1", kind="Pod", namespace=namespace) }}'
36+
register: debug_logs
37+
38+
- name: Ouput gathered resources
39+
debug:
40+
var: debug_resources
41+
42+
- name: Output gathered logs
43+
debug:
44+
var: item.log_lines
45+
loop: '{{ debug_logs.results }}'
46+
47+
- name: Re-emit failure
48+
vars:
49+
failed_task:
50+
name: '{{ ansible_failed_task.name }}'
51+
result: '{{ ansible_failed_result }}'
52+
fail:
53+
msg: '{{ failed_task }}'

test/ansible/playbooks/exec.yml renamed to test/ansible/playbooks/subresources.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
args:
2222
- "/bin/sh"
2323
- "-c"
24-
- "while true;do date;sleep 5;done"
24+
- "while true ; do echo '{{ log_message }}' ; sleep 5 ; done"
2525
wait: yes
2626

2727
- name: Execute command in busybox pod
@@ -33,12 +33,19 @@
3333

3434
- debug: var=exec_result
3535

36-
- name: Write result to resource status
36+
- name: Get logs from busybox pod
37+
k8s_log:
38+
name: '{{ meta.name }}-busybox'
39+
namespace: '{{ meta.namespace }}'
40+
register: log_result
41+
42+
- name: Write results to resource status
3743
k8s_status:
3844
api_version: test.example.com/v1alpha1
39-
kind: ExecTest
45+
kind: SubresourcesTest
4046
name: '{{ meta.name }}'
4147
namespace: '{{ meta.namespace }}'
4248
status:
4349
execCommandStdout: '{{ exec_result.stdout.strip() }}'
4450
execCommandStderr: '{{ exec_result.stderr.strip() }}'
51+
logs: '{{ log_result.log }}'

test/ansible/watches.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
- version: v1alpha1
1313
group: test.example.com
14-
kind: ExecTest
15-
playbook: playbooks/exec.yml
14+
kind: SubresourcesTest
15+
playbook: playbooks/subresources.yml
1616

1717
- version: v1
1818
group: ""

0 commit comments

Comments
 (0)