ansible-collections / community.kubernetes

Kubernetes Collection for Ansible
https://galaxy.ansible.com/community/kubernetes
GNU General Public License v3.0
265 stars 104 forks source link

Helm plugin ignores `context` and `K8S_AUTH_CONTEXT` setting #385

Closed mwalser closed 3 years ago

mwalser commented 3 years ago
SUMMARY

For the community.kubernetes.helm module it is documented that the context parameter, its alias kube_context and alternatively the K8S_AUTH_CONTEXT environment variable can be used to control the K8S context used for executing the helm command.

Except for kube_context these configuration options are, however, ignored.

It appears the problem was introduced with PR #324 which made context the primary configuration paramter and kube_context its alias. In this PR it was forgotten to change the prepare_helm_environ_update method as well.

Whats especially devious is that executing Ansible with -vvv reports the correct context being used when this is actually not the case.

-- This is really dangerous and could result in deployments to the wrong environment. --

ISSUE TYPE
COMPONENT NAME

community.kubernetes.helm

ANSIBLE VERSION
ansible 2.10.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/walsermi/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/walsermi/.local/lib/python3.6/site-packages/ansible
  executable location = /home/walsermi/.local/bin/ansible
  python version = 3.6.9 (default, Jan 26 2021, 15:33:00) [GCC 8.4.0]
CONFIGURATION
(empty)
OS / ENVIRONMENT

Linux

STEPS TO REPRODUCE
  1. Set local K8S context to "context-a": kubectl config use-context context-a
  2. Run the following task
    - name: Install prometheus stack
    community.kubernetes.helm:
    release_name: prometheus-stack
    release_namespace: default
    chart_ref: kube-prometheus-stack
    chart_repo_url: https://prometheus-community.github.io/helm-charts
    chart_version: 14.0.1
    context: context-b # <--- note "context-b" here
EXPECTED RESULTS

Helm chart is installed to "context-b", as specified in the task properties.

ACTUAL RESULTS

Helm chart is installed to whatever happens to be the current kubectl context at the time. In this case "context-a".

TASK [helm : Install prometheus stack] ************************************************************************************************************************************************************************************************************
task path: /home/walsermi/dev/project/ms_environments/k8s_ansible/roles/helm/tasks/main.yml:25
redirecting (type: action) community.kubernetes.helm to community.kubernetes.k8s_info
redirecting (type: action) community.kubernetes.helm to community.kubernetes.k8s_info
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: walsermi
<localhost> EXEC /bin/sh -c 'echo ~walsermi && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/walsermi/.ansible/tmp `"&& mkdir "` echo /home/walsermi/.ansible/tmp/ansible-tmp-1615285258.2431972-23687-7362420236700 `" && echo ansible-tmp-1615285258.2431972-23687-7362420236700="` echo /home/walsermi/.ansible/tmp/ansible-tmp-1615285258.2431972-23687-7362420236700 `" ) && sleep 0'
Using module file /home/walsermi/.ansible/collections/ansible_collections/community/kubernetes/plugins/modules/helm.py
Pipelining is enabled.
<localhost> EXEC /bin/sh -c '/usr/bin/python3 && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /home/walsermi/.ansible/tmp/ansible-tmp-1615285258.2431972-23687-7362420236700/ > /dev/null 2>&1 && sleep 0'
changed: [k8s-product-eval] => {
    "changed": true,
    "command": "/home/walsermi/bin/helm/3/linux-amd64/helm --version=14.0.1 --repo=https://prometheus-community.github.io/helm-charts upgrade -i --reset-values prometheus-stack kube-prometheus-stack",
    "invocation": {
        "module_args": {
            "api_key": null,
            "atomic": false,
            "binary_path": null,
            "ca_cert": null,
            "chart_ref": "kube-prometheus-stack",
            "chart_repo_url": "https://prometheus-community.github.io/helm-charts",
            "chart_version": "14.0.1",
            "context": "context-b", # <--- Correct context reported here, but actual install goes to "context-a"
            "create_namespace": false,
            "disable_hook": false,
            "force": false,
            "host": null,
            "kubeconfig": null,
            "purge": true,
            "release_name": "prometheus-stack",
            "release_namespace": "default",
            "release_state": "present",
            "release_values": {},
            "replace": false,
            "skip_crds": false,
            "update_repo_cache": false,
            "validate_certs": true,
            "values_files": [],
            "wait": false,
            "wait_timeout": null
        }
    },
    "status": {
        "app_version": "0.46.0",
        "chart": "kube-prometheus-stack-14.0.1",
        "name": "prometheus-stack",
        "namespace": "default",
        "revision": "1",
        "status": "deployed",
        "updated": "2021-03-09 11:21:07.666804448 +0100 CET",
        "values": {}
    },
    "stderr": "",
    "stderr_lines": [],
    "stdout": "Release \"prometheus-stack\" does not exist. Installing it now.\nNAME: prometheus-stack\nLAST DEPLOYED: Tue Mar  9 11:21:07 2021\nNAMESPACE: default\nSTATUS: deployed\nREVISION: 1\nNOTES:\nkube-prometheus-stack has been installed. Check its status by running:\n  kubectl --namespace default get pods -l \"release=prometheus-stack\"\n\nVisit https://github.com/prometheus-operator/kube-prometheus for instructions on how to create & configure Alertmanager and Prometheus instances using the Operator.\n",
    "stdout_lines": [
        "Release \"prometheus-stack\" does not exist. Installing it now.",
        "NAME: prometheus-stack",
        "LAST DEPLOYED: Tue Mar  9 11:21:07 2021",
        "NAMESPACE: default",
        "STATUS: deployed",
        "REVISION: 1",
        "NOTES:",
        "kube-prometheus-stack has been installed. Check its status by running:",
        "  kubectl --namespace default get pods -l \"release=prometheus-stack\"",
        "",
        "Visit https://github.com/prometheus-operator/kube-prometheus for instructions on how to create & configure Alertmanager and Prometheus instances using the Operator."
    ]
}
META: ran handlers
META: ran handlers