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

[Docs]: Functions Example in Docs #957

Open
1 task done
vfarcic opened this issue Feb 22, 2024 · 16 comments
Open
1 task done

[Docs]: Functions Example in Docs #957

vfarcic opened this issue Feb 22, 2024 · 16 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@vfarcic
Copy link

vfarcic commented Feb 22, 2024

Problem Statement

It's not clear how to use functions based on https://kyverno.github.io/chainsaw/latest/jp/functions/.

Solution Description

Add a few examples to the docs.

Alternatives

No response

Additional Context

No response

Slack discussion

No response

Research

  • I have searched other issues in this repository and mine is not recorded.
@vfarcic vfarcic added the enhancement New feature or request label Feb 22, 2024
@shubham-cmyk
Copy link
Member

@vfarcic We have some examples here.
https://github.com/kyverno/chainsaw/tree/main/testdata/e2e/examples

we can make a specific directory here functions to display the use of everyfunction :)

@vfarcic
Copy link
Author

vfarcic commented Feb 22, 2024

That would be great. What I'm looking for is an example of contains. I'm hoping that I could use it to validate whether an array contains specific entries. My arrays have tens or more entries and listing them all tends to be cumbersome.

@shubham-cmyk
Copy link
Member

@vfarcic
Copy link
Author

vfarcic commented Feb 22, 2024

@shubham-cmyk Would that work without a script? I'm trying to add something like that to the assert manifest.

@shubham-cmyk shubham-cmyk changed the title Functions in docs [Feature] [Docs]: Functions Example in Docs Feb 22, 2024
@shubham-cmyk
Copy link
Member

assert manifest ?
why would you need a check on the assert manifest.

@eddycharly
Copy link
Member

currently we generate the docs from the code... i will see if i can improve that in the jp lib, i agree this is a bit poor ;)

@shubham-cmyk
Copy link
Member

i think we can.

@vfarcic
Copy link
Author

vfarcic commented Feb 22, 2024

Here's a manifest:

apiVersion: devopstoolkitseries.com/v1alpha1
kind: SQL
metadata:
  name: xyz
spec:
  claimRef:
    apiVersion: devopstoolkitseries.com/v1alpha1
    kind: SQLClaim
    name: my-db
  compositionRef:
    name: aws-postgresql
  compositionSelector:
    matchLabels:
      db: postgresql
      provider: aws
  compositionUpdatePolicy: Automatic
  id: my-db-aws
  parameters:
    size: medium
    version: "13.4"
  resourceRefs:
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: InternetGateway
    name: my-db-aws
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: MainRouteTableAssociation
    name: my-db-aws
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: RouteTableAssociation
    name: my-db-aws-1a
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: RouteTableAssociation
    name: my-db-aws-1b
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: RouteTableAssociation
    name: my-db-aws-1c
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: RouteTable
    name: my-db-aws
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: Route
    name: my-db-aws
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: SecurityGroupRule
    name: my-db-aws
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: SecurityGroup
    name: my-db-aws
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: Subnet
    name: my-db-aws-a
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: Subnet
    name: my-db-aws-b
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: Subnet
    name: my-db-aws-c
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: VPC
    name: my-db-aws
  - apiVersion: kubernetes.crossplane.io/v1alpha1
    kind: ProviderConfig
    name: my-db-aws-sql
  - apiVersion: kubernetes.crossplane.io/v1alpha2
    kind: Object
    name: my-db-aws-secret
  - apiVersion: postgresql.sql.crossplane.io/v1alpha1
    kind: Database
    name: my-db-aws-db-01
  - apiVersion: postgresql.sql.crossplane.io/v1alpha1
    kind: Database
    name: my-db-aws-db-02
  - apiVersion: postgresql.sql.crossplane.io/v1alpha1
    kind: Database
    name: my-db-aws-db-03
  - apiVersion: postgresql.sql.crossplane.io/v1alpha1
    kind: ProviderConfig
    name: my-db-aws
  - apiVersion: rds.aws.upbound.io/v1beta1
    kind: Instance
    name: my-db-aws
  - apiVersion: rds.aws.upbound.io/v1beta1
    kind: SubnetGroup
    name: my-db-aws

I'd like to check that, among other things, that resource contains spec.resourceRefs[].kind: InternetGateway without having to specify all the items in spec.resourceRefs.

@shubham-cmyk
Copy link
Member


apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
  name: example
spec:
  steps:
  - try:
    # ...
    - assert:
        resource:
          apiVersion: v1
          kind: Deployment
          metadata:
            name: foo
          spec:
            (replicas > 3): true
    # ...

refer : https://kyverno.github.io/chainsaw/main/operations/assert/#usage-in-test

Would this example help

@eddycharly
Copy link
Member

eddycharly commented Feb 22, 2024

@vfarcic you can do something like this:

(contains(spec.resourceRefs[?kind == InternetGateway].[name], ['my-db-aws'])): true

It's called multiselect in jmespath term.

(you can do multiselect hash but harder to use because it uses : and conflicts with the yaml syntax unless the key is quoted)

@vfarcic
Copy link
Author

vfarcic commented Feb 22, 2024

Would something like the following be it?

apiVersion: devopstoolkitseries.com/v1alpha1
kind: SQL
metadata:
  labels:
    crossplane.io/claim-name: my-db
spec:
  claimRef:
    apiVersion: devopstoolkitseries.com/v1alpha1
    kind: SQLClaim
    name: my-db
  compositionRef:
    name: aws-postgresql
  compositionSelector:
    matchLabels:
      db: postgresql
      provider: aws
  compositionUpdatePolicy: Automatic
  id: my-db-aws
  parameters:
    size: medium
    version: "13.4"
  (contains(resourceRefs[?kind == InternetGateway].[name], ['my-db-aws'])): true
  resourceRefs:
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: InternetGateway
    name: my-db-aws
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: MainRouteTableAssociation
    name: my-db-aws
  - apiVersion: ec2.aws.upbound.io/v1beta1
    kind: RouteTableAssociation
    name: my-db-aws-1a

That one is throwing * spec.(contains(resourceRefs[?kind == InternetGateway].[name], ['my-db-aws'])): Invalid value: false: Expected value: true.

I left the existing resourceRef there as a reference. Without the contains line it works so the InternetGateway is definitely there.

@eddycharly
Copy link
Member

(contains(resourceRefs[?kind == 'InternetGateway'].[name], ['my-db-aws'])) (needs quotes)

@eddycharly
Copy link
Member

eddycharly commented Feb 22, 2024

Tried it, with quotes it passes:

apiVersion: devopstoolkitseries.com/v1alpha1
kind: SQL
metadata:
  labels:
    crossplane.io/claim-name: my-db
spec:
  claimRef:
    apiVersion: devopstoolkitseries.com/v1alpha1
    kind: SQLClaim
    name: my-db
  compositionRef:
    name: aws-postgresql
  compositionSelector:
    matchLabels:
      db: postgresql
      provider: aws
  compositionUpdatePolicy: Automatic
  id: my-db-aws
  parameters:
    size: medium
    version: "13.4"
  (length(resourceRefs[?kind == 'InternetGateway'])): 1
  (contains(resourceRefs[?kind == 'InternetGateway'].[name], ['my-db-aws'])): true
chainsaw assert -r ./_resource.yaml -f ./_assert.yaml
Assertion(s) PASSED

@eddycharly
Copy link
Member

@vfarcic there's a simpler and more elegant solution:

apiVersion: devopstoolkitseries.com/v1alpha1
kind: SQL
metadata:
  labels:
    crossplane.io/claim-name: my-db
spec:
  claimRef:
    apiVersion: devopstoolkitseries.com/v1alpha1
    kind: SQLClaim
    name: my-db
  compositionRef:
    name: aws-postgresql
  compositionSelector:
    matchLabels:
      db: postgresql
      provider: aws
  compositionUpdatePolicy: Automatic
  id: my-db-aws
  parameters:
    size: medium
    version: "13.4"
  (resourceRefs[?kind == 'InternetGateway']):
  - name: my-db-aws

@eddycharly eddycharly added the documentation Improvements or additions to documentation label Feb 24, 2024
@vfarcic
Copy link
Author

vfarcic commented Feb 24, 2024

That's the one I like the most. @eddycharly. It's easy to read than the previous example.

@eddycharly
Copy link
Member

For reference grafana/tempo-operator#786 (comment)

@eddycharly eddycharly added this to the v0.2.0 milestone Mar 6, 2024
@eddycharly eddycharly modified the milestones: v0.2.0, v0.2.1 Apr 19, 2024
@eddycharly eddycharly modified the milestones: v0.2.1, v0.2.2, v0.2.3 May 15, 2024
@eddycharly eddycharly modified the milestones: v0.2.3, v0.2.4 May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants