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

Add validate argument to helm_template #587

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pauvos
Copy link
Contributor

@pauvos pauvos commented Feb 18, 2023

SUMMARY

Support helm template --validate:

--validate

validate your manifests against the Kubernetes cluster you are currently pointing at. This is the same validation performed on an install

It's sometimes necessary to render templates against the kubernetes API because helm charts increasingly start to use conditions:

{{- if and (.Capabilities.APIVersions.Has "networking.k8s.io/v1") (semverCompare ">= v1.19.0-0" .Capabilities.KubeVersion.Version) -}}
  {{- $apiVersion = "networking.k8s.io/v1" -}}
{{- else if .Capabilities.APIVersions.Has "networking.k8s.io/v1beta1" -}}
  {{- $apiVersion = "networking.k8s.io/v1beta1" -}}
{{- else -}}
  {{- $apiVersion = "extensions/v1beta1" -}}
{{- end }}
apiVersion: {{ $apiVersion }}
kind: Ingress

Without checking the capabilities of the cluster the above template will render a deprecated extensions/v1beta1 Ingress resource.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

kubernetes.core.helm_template

ADDITIONAL INFORMATION

@pauvos
Copy link
Contributor Author

pauvos commented Feb 18, 2023

I would like to add a unittest but I couldn't figure out how to run them.

Does this make sense?

# tests/unit/modules/test_helm_template.py

def test_template_with_validate():
    my_chart_ref = "testref"
    helm_cmd = "helm"
    parser = argparse.ArgumentParser()
    # to "simulate" helm template options, include two optional parameters NAME and CHART.
    # if parsed string contains only one parameter, the value will be passed
    # to CHART and NAME will be set to default value "release-name" as in helm template
    parser.add_argument("cmd")
    parser.add_argument("template")
    parser.add_argument("NAME", nargs="?", default="release-name")
    parser.add_argument("CHART", nargs="+")
    parser.add_argument("--validate", dest="validate", action="store_true")
    parser.set_defaults(validate=False)

    mytemplate = template(cmd=helm_cmd, chart_ref=my_chart_ref, validate=True)

    args, unknown = parser.parse_known_args(mytemplate.split())

    assert args.validate is True

@softwarefactory-project-zuul
Copy link

Build failed.
https://ansible.softwarefactory-project.io/zuul/buildset/a77532e781d540c28106cfb10deca60e

✔️ build-ansible-collection SUCCESS in 6m 37s
ansible-test-sanity-docker-devel FAILURE in 20m 41s (non-voting)
ansible-test-sanity-docker-milestone FAILURE in 12m 18s (non-voting)
ansible-test-sanity-docker-stable-2.9 RETRY_LIMIT in 1m 31s
✔️ ansible-test-sanity-docker-stable-2.11 SUCCESS in 9m 41s
ansible-test-sanity-docker-stable-2.12 RETRY_LIMIT in 18s
ansible-test-units-kubernetes-core-python38 RETRY_LIMIT in 5m 14s
✔️ ansible-test-splitter SUCCESS in 4m 35s
✔️ integration-kubernetes.core-devel-1 SUCCESS in 12m 28s (non-voting)
✔️ integration-kubernetes.core-devel-2 SUCCESS in 7m 25s (non-voting)
✔️ integration-kubernetes.core-milestone-1 SUCCESS in 13m 58s
✔️ integration-kubernetes.core-milestone-2 SUCCESS in 7m 13s
✔️ integration-kubernetes.core-2.9-1 SUCCESS in 12m 49s
✔️ integration-kubernetes.core-2.9-2 SUCCESS in 7m 37s
✔️ integration-kubernetes.core-2.10-1 SUCCESS in 12m 59s
✔️ integration-kubernetes.core-2.10-2 SUCCESS in 7m 46s
✔️ integration-kubernetes.core-with-turbo-1 SUCCESS in 13m 07s
✔️ integration-kubernetes.core-with-turbo-2 SUCCESS in 6m 41s
✔️ ansible-tox-linters SUCCESS in 10m 14s
ansible-test-changelog FAILURE in 4m 12s
✔️ ansible-galaxy-importer SUCCESS in 3m 21s
Skipped 5 jobs

@pauvos pauvos mentioned this pull request Feb 18, 2023
Copy link
Contributor

@abikouo abikouo left a comment

Choose a reason for hiding this comment

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

plugins/modules/helm_template.py Show resolved Hide resolved
@pauvos
Copy link
Contributor Author

pauvos commented Feb 20, 2023

@abikouo , yes sure thing!

I have 2 other pull requests:

Should i merge everything in one pull requests or create individual changelog fragments?

@pauvos pauvos force-pushed the helm-template-validate branch from 71e9d81 to aaeeb1d Compare February 20, 2023 10:56
@softwarefactory-project-zuul
Copy link

Build failed.
https://ansible.softwarefactory-project.io/zuul/buildset/b0a6cf3c5b284f8698f482c4133cdf1c

✔️ build-ansible-collection SUCCESS in 6m 47s
ansible-test-sanity-docker-devel FAILURE in 10m 35s (non-voting)
ansible-test-sanity-docker-milestone FAILURE in 7m 32s (non-voting)
✔️ ansible-test-sanity-docker-stable-2.9 SUCCESS in 9m 58s
✔️ ansible-test-sanity-docker-stable-2.11 SUCCESS in 9m 51s
✔️ ansible-test-sanity-docker-stable-2.12 SUCCESS in 8m 35s
✔️ ansible-test-units-kubernetes-core-python38 SUCCESS in 6m 32s
✔️ ansible-test-splitter SUCCESS in 4m 35s
✔️ integration-kubernetes.core-devel-1 SUCCESS in 13m 07s (non-voting)
✔️ integration-kubernetes.core-devel-2 SUCCESS in 7m 44s (non-voting)
✔️ integration-kubernetes.core-milestone-1 SUCCESS in 14m 47s
✔️ integration-kubernetes.core-milestone-2 SUCCESS in 10m 47s
✔️ integration-kubernetes.core-2.9-1 SUCCESS in 16m 55s
✔️ integration-kubernetes.core-2.9-2 SUCCESS in 10m 46s
✔️ integration-kubernetes.core-2.10-1 SUCCESS in 15m 09s
✔️ integration-kubernetes.core-2.10-2 SUCCESS in 6m 46s
✔️ integration-kubernetes.core-with-turbo-1 SUCCESS in 14m 35s
✔️ integration-kubernetes.core-with-turbo-2 SUCCESS in 6m 43s
✔️ ansible-tox-linters SUCCESS in 10m 29s
✔️ ansible-test-changelog SUCCESS in 4m 22s
ansible-galaxy-importer RETRY_LIMIT in 16s
Skipped 5 jobs

Copy link
Member

@gravesm gravesm left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, and sorry for taking so long to get back to this. Since this is going to connect to a cluster, you'll need to add some connection parameters to the module. This should be pretty easy, though, and you can look at how the helm module does it. I think it should be sufficient to just extend the doc string as it's done in

extends_documentation_fragment:
- kubernetes.core.helm_common_options
and then add those args to the arg spec for the module, similar to how it's done in
arg_spec = copy.deepcopy(HELM_AUTH_ARG_SPEC)
.

Since this is adding functionality to connect to a cluster, it would be a good idea to add an integration test or two for this. You can add those in

and just use that as a guide.

Your suggestion of the unit test seems reasonable. If you want to run unit tests locally you can do so with ansible-test units --docker.

@willthames
Copy link
Collaborator

willthames commented Sep 15, 2023

There is an alternative way to achieve the desired results here which is to add an api versions parameter to the helm_template module (this is often useful when there isn't a kubernetes cluster to connect to e.g. during CI - this is how we generate all of our templates)

e.g. for the alb-controller we pass -a cert-manager.io/v1 -a admissionregistration.k8s.io/v1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants