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

[Feature] Allow to use binding/functions in use.template in test step #1846

Open
jingav opened this issue Aug 12, 2024 · 10 comments
Open

[Feature] Allow to use binding/functions in use.template in test step #1846

jingav opened this issue Aug 12, 2024 · 10 comments
Labels
question Further information is requested

Comments

@jingav
Copy link

jingav commented Aug 12, 2024

Describe your question

Hi there,

I'm trying to create reusable (StepTemplate) building blocks and have the following:

apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
  name: basic-test
spec:
  bindings:
  - name: rootPath
    value: ../..
  - name: templatesPath
    value: (join('/', [$rootPath, 'templates']))
  steps:
  - name:  Print path # Works fine
    try:
    - script:
        env:
        - name: TEMPLATES_PATH
          value: (join('/', [$templatesPath, 'example/custom_bindings.yaml']))
        content: |
          echo $TEMPLATES_PATH
  - name: Test custom bindings
    bindings:
    - name: filePath
      value: (join('/', [$templatesPath, 'example/custom_bindings.yaml']))
    use: 
      # template: ../../templates/example/custom_bindings.yaml
      template: $filePath # does NOT work
      # template: (join('/', [$templatesPath, 'example/custom_bindings.yaml'])) # does NOT work
Version: 0.2.8
No configuration provided but found default file: .chainsaw.yaml
Loading config (.chainsaw.yaml)...
Loading tests...
Running tests...
=== RUN   chainsaw
=== PAUSE chainsaw
=== CONT  chainsaw
=== RUN   chainsaw/my-test
    | 18:46:04 | basic-test | Paths                            | TRY       | RUN   |
    | 18:46:04 | basic-test | Paths                            | SCRIPT    | RUN   |
        === COMMAND
        /usr/bin/sh -c echo $TEMPLATES_PATH
    | 18:46:04 | basic-test | Paths                            | SCRIPT    | LOG   |
        === STDOUT
        ../../templates/example/custom_bindings.yaml

Version: 0.2.8
No configuration provided but found default file: .chainsaw.yaml
Loading config (.chainsaw.yaml)...
Loading tests...
Error: open tests/basic-test/$filePath: no such file or directory

It seems it's not currently supported or did I miss anything?

Would it be possible to add support for this?
There are more use cases for this, the above is just a quick example.

chainsaw version Version

v0.2.8

Additional context

No response

@jingav jingav added the question Further information is requested label Aug 12, 2024
@jingav
Copy link
Author

jingav commented Aug 12, 2024

Similarly for apply.file

apiVersion: chainsaw.kyverno.io/v1alpha1
kind: StepTemplate
metadata:
  name: generic-step-apply-claim
spec:
  bindings:
    # e.g. "../.."
  - name: rootPath
    value: ($rootPath)
    # e.g. "../../common/claims"
  - name: claimsPath
    value: (join('/', [$rootPath, 'common/claims']))
    # e.g. "azure-subscription.yaml"
  - name: claimFileName
    value: ($claimFileName)
    # e.g. "../../common/claims/azure-subscription.yaml"
  - name: claimFileNamePath
    value: (join('/', [$claimsPath, $claimFileName]))
    # e.g. "my-azure-subscription-claim"
  - name: claimName
    value: (to_string($claimName))
  try:
  - apply:
      # file: ../../common/claims/azure-subscription.yaml
      file: ($claimFileNamePath)
   catch:
     .....
   finally:
     .....

So, it looks like no interpolation for any paths anywhere?

@eddycharly
Copy link
Member

@jingav it will be supported in file in the next version (v0.2.9).

There's no plan to support it in template at this point. Step templates are resolved at discovery time and no binding are evaluated at that time, only when the test executes. We could delay the step template loading until execution but again, there's no plan for that yet.

@jingav
Copy link
Author

jingav commented Aug 14, 2024

@eddycharly , ok thanks for sharing the details.

Shall I then change this issue to Enhancement type for the template part?

@eddycharly
Copy link
Member

Yes you can change it to a feature request.

@jingav jingav changed the title [Question] Is it possible to use custom binding in use.template in test step? [Feature] Allow to use binding/functions in use.template in test step Aug 15, 2024
@jingav
Copy link
Author

jingav commented Aug 15, 2024

I'm not able to change label from question to enhancement.
Please change it if you can.

@nkuacac
Copy link

nkuacac commented Aug 16, 2024

how about use can support from workdir and build-in workdir

// Use defines a reference to a step template.
type Use struct {
	// Template references a step template.
	Template string `json:"template,omitempty"`

	// TemplateFromWorkDir references a step template from workdir.
	TemplateFromWorkDir string `json:"templateFromWorkDir,omitempty"`
}

@jingav
Copy link
Author

jingav commented Aug 22, 2024

@eddycharly, I have one more case, for wait and its selector.

  bindings:
  - name: dnsClaimName
    value: (lower(join('-', ['private-dns', $dnsSuffix, $branchName])))
  steps:  
  - wait:
      timeout: 5m
      namespace: ($namespace)
      apiVersion: ($resourceApiVersion)
      kind: ($resourceKind)
      # name: ($dnsSpokeVnetLinkExtName)
      selector: crossplane.io/claim-name=($dnsClaimName)
      for:
        condition:
          name: LastAsyncOperation
          value: 'false'

Response

=== STDERR
        Error from server (BadRequest): Unable to find "network.azure.upbound.io/v1beta1, Resource=privatednszonevirtualnetworklinks" that match label selector "crossplane.io/claim-name=()", field selector "": unable to parse requirement: found '(', expected: identifier

Any trick to use or chance this would get supported in the next version (v0.2.9)?

Thanks

@nkuacac
Copy link

nkuacac commented Aug 23, 2024

selector: crossplane.io/claim-name=($dnsClaimName)

@jingav try
selector: (join('=', ['crossplane.io/claim-name',$dnsClaimName]))

@jingav
Copy link
Author

jingav commented Aug 23, 2024

selector: crossplane.io/claim-name=($dnsClaimName)

@jingav try selector: (join('=', ['crossplane.io/claim-name',$dnsClaimName]))

Ah, yes, thanks.

For multi selection, something like this
selector: (join(',', ['scc.cloud.abcd/usage=dns-link-hub', (join('=', ['crossplane.io/claim-name', $dnsClaimName]))]))

@eddycharly
Copy link
Member

Sorry folks, i didn't follow the issue here, is there anything left to discuss ?

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

No branches or pull requests

3 participants