Query Resources

VelaQL(Vela Query Language) is a resource query language for KubeVela, used to query status of any extended resources in application-level.

KubeVela’s Application encapsulates the underlying resources from the infrastructure. It brings the infrastructure agonistic experience to app developers but also makes challenges for platform builders to maintain. For example, the monitoring of the resource status created by KubeVela Application can be hard. Moreover, the status information within Application is not detailed enough for trouble shooting and the real-time feedback is poor.

The purpose of VelaQL is to help users and platform builders to uncover the mysterious of the Application. Users can query application deployment status through VelaQL, or use the extensible interface provided by VelaQL to customize query information to improve the observability of one Application.

KubeVela command has supported a vela ql command to query resources.

You can specify a query file in cue like below:

  1. import (
  2. "vela/ql"
  3. )
  4. configmap: ql.#Read & {
  5. value: {
  6. kind: "ConfigMap"
  7. apiVersion: "v1"
  8. metadata: {
  9. name: "vela-addon-registry"
  10. namespace: "vela-system"
  11. }
  12. }
  13. }
  14. status: configmap.value.data["registries"]

Then use the following command to query:

  1. $ vela ql -f ql.cue
  2. { \"KubeVela\":{ \"name\": \"KubeVela\", \"helm\": { \"url\": \"https://addons.kubevela.net\" } } }

It will print the value in the field status which is a keyword of VelaQL.

There’re some other op CUE operations you can use in VelaQL.

You can apply the view to server for re-use, either use vela ql apply command (recommended, requires CLI v1.5 or later) or wrap it in ConfigMap.

Instructions on using ConfigMaps

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: query-config-map
  5. namespace: vela-system
  6. data:
  7. template: |
  8. import (
  9. "vela/ql"
  10. )
  11. parameter: {
  12. name: string
  13. namespace: string
  14. key: string
  15. }
  16. configmap: ql.#Read & {
  17. value: {
  18. kind: "ConfigMap"
  19. apiVersion: "v1"
  20. metadata: {
  21. name: parameter.name
  22. namespace: parameter.namespace
  23. }
  24. }
  25. }
  26. status: configmap.value.data["\(parameter.key)"]

There’re two keywords:

  • The parameter, indicates which parameters can use in the query.
  • The status, indicates which fields of resources will be exported.

Apply the yaml into Kubernetes:

  1. vela kube apply -f query-configmap.yaml

vela kube apply is the same with kubectl apply in case you don’t use kubectl command line.

Create a file named my-view.cue with:

  1. import (
  2. "vela/ql"
  3. )
  4. parameter: {
  5. name: string
  6. namespace: string
  7. key: string
  8. }
  9. configmap: ql.#Read & {
  10. value: {
  11. kind: "ConfigMap"
  12. apiVersion: "v1"
  13. metadata: {
  14. name: parameter.name
  15. namespace: parameter.namespace
  16. }
  17. }
  18. }
  19. status: configmap.value.data["\(parameter.key)"]

There’re two keywords:

  • The parameter, indicates which parameters can use in the query.
  • The status, indicates which fields of resources will be exported.

Create the view in Kubernetes for later use:

  1. $ vela ql apply -f my-view.cue

A view named my-view will be created. You can use it later.

Then everyone who has access to the cluster can query by re-use the view, the basic syntax is:

  1. <view-name>{parameter1=value1,parameter2=value2}

In our example, you can query in the following command:

  1. vela ql -q "query-config-map{name=vela-addon-registry,namespace=vela-system,key=registries}"

The result is the same.

VelaUX has use VelaQL for all it’s resources query. You can also integrate in this way, just install VelaUX and query the resource status with the help of its api server. You can install VelaUX addon by:

  1. vela addon enable velaux

You can refer to the velaux doc to learn how to expose endpoint. Assuming we started the apiserver at http://127.0.0.1:8000, you can use VelaQL like:

  1. http://127.0.0.1:8000/api/v1/query?velaql="write VelaQL here"

The basic syntax is the same with command line.

  1. view{parameter1=value1,parameter2=value2}

view represents a query view, which can be compared to the concept of a view in a database. The view in VelaQL is a collection of ConfigMap resources in the Kubernetes system, the name of the ConfigMap is also the name of the view.

Filter conditions are placed in {} as the form of key-value pairs, currently the value type only supports: string, int, float, boolean.

You can refer to velaux addon repo for more view examples.

In many scenarios, the built-in views cannot meet our needs, and the resources encapsulated under Application are not just the native resources of k8s. For many custom resources, users will have different query requirements. At this time, you need to write your own specific views to complete the query. This section will tell you how to write a custom view.

The current view in VelaQL relies on configMap in k8s as a storage medium, You can refer to https://github.com/kubevela/kubevela/blob/master/test/e2e-apiserver-test/testdata/component-pod-view.yaml. The template in the configMap data field stores the core logic of the view, template is a query statement implemented in cue language.

Every time you use VelaQL, the system will look for the configMap with the same name as the view under the vela-system namespace, and extract the template for query operations, so please ensure that your custom view is stored under vela-system. ​ The structure of a template is as follows:

  1. import (
  2. "vela/ql"
  3. )
  4. // The parameter here corresponds to the parameter in VelaQL
  5. parameter: {
  6. appName: string
  7. appNs: string
  8. name?: string
  9. cluster?: string
  10. clusterNs?: string
  11. }
  12. // query statement implemented with cue
  13. ...
  14. // The query result of VelaQL will return the content of the status field by default,
  15. // so please summarize the results you need to query under the status field
  16. status: podList: {...}

We provide the vela/ql package to help you query resources. The following explains the cue operators that can be used:

List all resources created by Application

  • app: fill in the basic information of the application that needs to be queried, including the application name and application namespace,The filter field is used to filter the resources created by the application. The options include the cluster where the resource is located, the cluster namespace, and the component to which it belongs
  • list: after the operation is successful, this field will be filled. list is a list of all resources, the k8s description of the resource is stored in the object field.
  • err: if an error occurs in the operation, the error message will be displayed as a string.
  1. #ListResourcesInApp: {
  2. app: {
  3. name: string
  4. namespace: string
  5. filter?: {
  6. cluster?: string
  7. clusterNamespace?: string
  8. components?: [...string]
  9. }
  10. }
  11. list?: [...{
  12. cluster: string
  13. component: string
  14. revision: string
  15. object: {...}
  16. }]
  17. err?: string
  18. }
  1. import (
  2. "vela/ql"
  3. )
  4. // The parameter here corresponds to the parameter in VelaQL
  5. parameter: {
  6. appName: string
  7. appNs: string
  8. name?: string
  9. cluster?: string
  10. clusterNs?: string
  11. }
  12. resources: ql.#ListResourcesInApp & {
  13. app: {
  14. name: parameter.appName
  15. namespace: parameter.appNs
  16. filter: {
  17. if parameter.cluster != _|_ {
  18. cluster: parameter.cluster
  19. }
  20. if parameter.clusterNs != _|_ {
  21. clusterNamespace: parameter.clusterNs
  22. }
  23. if parameter.name != _|_ {
  24. components: [parameter.name]
  25. }
  26. }
  27. }
  28. }
  29. // VelaQL returns the value of the status field by default
  30. status: resourcesList: resources.list

List all pods created by the workload

  • value: definition of the workload resource you want to query
  • cluster: cluster
  • list: after the operation is successful, this field will be filled. list is a list of all pod resources
  • err: if an error occurs in the operation, the error message will be displayed as a string.
  1. #CollectPods: {
  2. value: {...}
  3. cluster: string
  4. list?:[... v1.Pod]
  5. err?: string
  6. }
  1. import (
  2. "vela/ql"
  3. )
  4. parameter: {
  5. name: string
  6. }
  7. resources: ql.#CollectPods & {
  8. value: {
  9. kind: "Deployment"
  10. apiVersion: "apps/v1"
  11. metadata: name: parameter.name
  12. }
  13. }
  14. status: pods: resources.list

Get resource in Kubernetes cluster.

  • value: the resource metadata to be get. And after successful execution, value will be updated with resource definition in cluster.
  • err: if an error occurs, the err will contain the error message.
  1. #Read: {
  2. value: {}
  3. err?: string
  4. }
  1. // You can use configmap.value.data after this action.
  2. configmap: ql.#Read & {
  3. value: {
  4. kind: "ConfigMap"
  5. apiVersion: "v1"
  6. metadata: {
  7. name: "configmap-name"
  8. namespace: "configmap-ns"
  9. }
  10. }
  11. }

List resources in the k8s cluster.

  • resource: the resource metadata to be get
  • filter: namespace is used to select a namespace, and the matchingLabels field is used to filter labels
  • err: if an error occurs, the err will contain the error message.
  1. #List: {
  2. cluster: *"" | string
  3. resource: {
  4. apiVersion: string
  5. kind: string
  6. }
  7. filter?: {
  8. namespace?: *"" | string
  9. matchingLabels?: {...}
  10. }
  11. list?: {...}
  12. err?: string
  13. }

Happy enjoy the extensibility of VelaQL for query resources in Kubernetes with CUE.

Last updated on Aug 4, 2023 by Daniel Higuero