jkube icon indicating copy to clipboard operation
jkube copied to clipboard

Add support or examples for generating helm test resources

Open bryopsida opened this issue 2 years ago • 1 comments

Component

JKube Kit

Is your enhancement related to a problem? Please describe

I want to create a test resource that's created when helm test is executed on my chart.

https://helm.sh/docs/topics/chart_tests/#example-test

I don't see any examples or references to doing this in the current documentation.

Describe the solution you'd like

As a user it would be nice if I could create resources under src/test/jkube in the same way I create src/main/jkube resources. Except they are created with the appropriate test annotations for helm test to allow consumers of the chart to run the tests from the chart.

A corresponding k8sHelmTest task could be added to run helm test through the project.

For those not using helm, k8sTest task could be added which generates the resources, applies and watches for completion of resources and then cleans up.

Describe alternatives you've considered

A quick start example of the preferred mechanism to go about creating helm test resources.

Additional context

The specific use case I'm trying to solve is having a test pod that runs a postman collection, so I want to create a pod with the collection json file and env json file with the correct annotations set for running on helm test.

I believe I can accomplish this via providing a mostly complete pod template and ConfigMap template, but am combing through the docs trying to piece together the pieces needed to get it working.

bryopsida avatar Sep 10 '23 12:09 bryopsida

OK this ticket is close enough, hopefully I'm not hijacking it.

Here is how I'm currently doing helm tests in a maven project.

I added a kubernetes fragment in src/main/jkube/test-prometheus.yml

apiVersion: v1
kind: Pod
metadata:
  namespace: ${common.namespace}
  name: ${project.artifactId}-test-prometheus
  annotations:
    "helm.sh/hook": test
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
  containers:
    - name: wget
      image: busybox
      command: ['wget']
      args: ['http://${project.artifactId}:${service.server.port}/restprivate/prometheus']
  restartPolicy: Never

I directly added the helm test annotations.

This works fine for helm / production and I can do

./mvnw -pl microservice-app k8s:resource k8s:helm
helm install --wait -n dev microservice-app microservice-app/target/jkube/helm/microservice-app/kubernetes
helm test -n dev microservice-app

However those pods are also automatically deployed when doing a ./mvnw -pl microservice-app k8s:apply which is not intended. So the inner loop setup is not perfect in that case.

Adding those helm test fragments in src/test/jkube/ so that they can be added for helm, and ignored for direct k8s apply is a neat idea...

ajeans avatar Dec 05 '23 13:12 ajeans

I have a few questions before proceeding with its implementation:

  • Should test resource directory ${basedir}/src/test/jkube be configurable like resource dir? If yes, what should be the name/property of the field ?

  • In which directory should we generate these test resources, Should it be in the same directory as current output of k8s:resource ${targetDir}/classes/META-INF/jkube/ or somewhere else ${project.build.outputDirectory}/generated-test-sources

  • watches for completion of resources

    How shall we determine when the test is completed? Shall we just wait until application pod becomes ready? Should waiting time configuratble via property?

rohanKanojia avatar Aug 27 '24 05:08 rohanKanojia

Should test resource directory ${basedir}/src/test/jkube be configurable like resource dir? If yes, what should be the name/property of the field ?

Same procedure as helm standard resource location. If main is not part of that standard (helm) config, then it's even easier, since test resources should go to test/jkube and production resources should go to main/jkube.

However, since these files will eventually be distributed as part of a distributes package, we might want to crate these files in src/main/jkube with a suffix .test.helm.yaml similar to what we do with the values and Chart yaml files.

Update

As agreed in internal meeting, we will consider all files in src/main/jkube (or configured directory for Helm resources) with the suffix .test.helm.y(a)ml as Helm test fragments.

In which directory should we generate these test resources, Should it be in the same directory as current output of k8s:resource ${targetDir}/classes/META-INF/jkube/ or somewhere else ${project.build.outputDirectory}/generated-test-sources

We need to check how the helm client handles this. AFAIR these go into templates/tests so that should be easy enough.

How shall we determine when the test is completed? Shall we just wait until application pod becomes ready? Should waiting time configuratble via property?

I think Helm Java offers this option, is part of the helm CLI options AFAIR. But I guess this is part of the other issue (#2667)

manusa avatar Aug 27 '24 08:08 manusa