Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remove-node by removing jq usage #7405

Merged
merged 1 commit into from
Mar 26, 2021
Merged
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
20 changes: 5 additions & 15 deletions roles/remove-node/pre-remove/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,13 @@
set_fact:
nodes_to_drain: []

- name: remove-node | Identify nodes to drain, ignore non-cluster nodes
shell: |
set -o pipefail
{{ bin_dir }}/kubectl get nodes -o json \
| jq .items[].metadata.name \
| jq "select(. | test(\"^{{ hostvars[item]['kube_override_hostname']|default(item) }}$\"))"
loop: "{{ node.split(',') | default(groups['kube-node']) }}"
- name: remove-node | List nodes
command: kubectl get nodes -o go-template={% raw %}'{{ range .items }}{{ .metadata.name }}{{ "\n" }}{{ end }}'{% endraw %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the above command locally, the following error is output

$ kubectl get nodes -o go-template={% raw %}'{{ range .items }}{{ .metadata.name }}{{ "\n" }}{{ end }}'{% endraw %}
{%Error from server (NotFound): nodes "raw" not found
invalid resource name "%}{{ range .items }}{{ .metadata.name }}{{ \"\\n\" }}{{ end }}{%": [may not contain '%']
Error from server (NotFound): nodes "endraw" not found
invalid resource name "%}": [may not contain '%']

After changing here like

kubectl get nodes -o go-template='{% raw %}{{ range .items }}{{ .metadata.name }}{{ "\n" }}{{ end }}{% endraw %}'

by changing ' positions, the command outputs the list of nodes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ansible (Jinja) will remove the {% raw %} / {% endraw %}, I can use !unsafe if it's more clear

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or I can put the {% raw %} / {% endraw %} around the whole line

Copy link
Member

@floryut floryut Mar 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum I don't think you have to change anything, Ansible will ltake care of %raw%/%endraw%
Other commands with raw/endraw are also not 'run-able' as is and require ansible processing.

eg docker images -q | xargs -i {{ '{{' }} docker_bin_dir }}/docker inspect -f {% raw %}'{{ '{{' }} if .RepoTags }}{{ '{{' }} join .RepoTags \",\" }}{{ '{{' }} end }}{{ '{{' }} if .RepoDigests }},{{ '{{' }} join .RepoDigests \",\" }}{{ '{{' }} end }}' {% endraw %} {} | tr '\n' ','

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oomichi your call, I don't have strong opinions on this one

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your explanation, I just wanted to confirm that works fine as expected.
It is fine to keep this as it if working.

/lgtm

register: nodes
delegate_to: "{{ groups['kube_control_plane']|first }}"
changed_when: false
run_once: true

- name: remove-node | Generate list of nodes to drain
set_fact:
nodes_to_drain: "{{ nodes_to_drain }} + [ '{{ item.stdout | regex_replace('\"', '') }}' ]"
loop: "{{ nodes.results }}"
when: item.stdout | length != 0
run_once: true

- name: remove-node | Drain node except daemonsets resource # noqa 301
command: >-
{{ bin_dir }}/kubectl --kubeconfig /etc/kubernetes/admin.conf drain
Expand All @@ -30,7 +18,9 @@
--grace-period {{ drain_grace_period }}
--timeout {{ drain_timeout }}
--delete-local-data {{ hostvars[item]['kube_override_hostname']|default(item) }}
loop: "{{ nodes_to_drain }}"
loop: "{{ node.split(',') | default(groups['kube-node']) }}"
# ignore servers that are not nodes
when: hostvars[item]['kube_override_hostname']|default(item) in nodes.stdout_lines
register: result
failed_when: result.rc != 0 and not allow_ungraceful_removal
delegate_to: "{{ groups['kube_control_plane']|first }}"
Expand Down