Camel Components

Kubernetes Custom Resources

Since Camel 3.7

Both producer and consumer are supported

The Kubernetes Custom Resources component is one of Kubernetes Components which provides a producer to execute Kubernetes Custom Resources operations and a consumer to consume events related to custom resources.

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The Kubernetes Custom Resources component supports the following options which are listed below.

Name Description Default Type

kubernetesClient (common)

Autowired An existing Kubernetes client for the endpoints of this component that do not set their own kubernetesClient option. The endpoints then do not use the options that configure the client (masterUrl, credentials, certificates, trustCerts, apiVersion, connectionTimeout).

KubernetesClient

bridgeErrorHandler (consumer)

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

Endpoint Options

The Kubernetes Custom Resources endpoint is configured using URI syntax:

kubernetes-custom-resources:masterUrl

With the following path and query parameters:

Path Parameters

Name Description Default Type

masterUrl (common)

Required The URL of the Kubernetes API server, such as https://kubernetes.default.svc. Use local or client to create the Kubernetes client from the auto-detected configuration (kubeconfig file, in-cluster service account, system properties and environment variables) without applying the options that configure the client (credentials, certificates, trustCerts, apiVersion, connectionTimeout). With a URL, the client also starts from the auto-detected configuration, and these options override it. Not used when the kubernetesClient option is set.

String

Query Parameters

Name Description Default Type

apiVersion (common)

The default API version of the Kubernetes client (the client default is v1). The client uses it only for a resource type that does not give its own version, and the resource types of these components all give theirs.

String

dnsDomain (common)

Not used. It was the DNS domain for the ServiceCall EIP, which was removed in Camel 4.19.

String

kubernetesClient (common)

An existing Kubernetes client to use. The options that configure the client (masterUrl, credentials, certificates, trustCerts, apiVersion, connectionTimeout) are then not used, and Camel does not close the client when the endpoint stops.

KubernetesClient

namespace (common)

The namespace of the resources. For the producer, the CamelKubernetesNamespaceName header takes precedence. When neither is set, the list operations and the consumer cover all namespaces, and the operations on a single resource fail; openshift-deploymentconfigs then uses the namespace of the Kubernetes client, and kubernetes-custom-resources fails. Not used by kubernetes-namespaces, kubernetes-nodes and kubernetes-persistent-volumes, whose resources are cluster-scoped. When Camel creates the client from a URL, it is also the default namespace of the client.

String

portName (common)

Not used. It was the port name for the ServiceCall EIP, which was removed in Camel 4.19.

String

portProtocol (common)

Not used. It was the port protocol for the ServiceCall EIP, which was removed in Camel 4.19.

tcp

String

crdGroup (consumer)

The API group of the custom resource, such as sources.knative.dev. Used only by kubernetes-custom-resources: the consumer requires it, and the producer uses the CamelKubernetesCRDGroup header when it is not set.

String

crdName (consumer)

The name of the custom resource definition (CRD), such as githubsources.sources.knative.dev. Used only by kubernetes-custom-resources: the consumer requires it, and the producer uses the CamelKubernetesCRDName header when it is not set.

String

crdPlural (consumer)

The plural name of the custom resource, as used in its API path, such as githubsources. Used only by kubernetes-custom-resources: the consumer requires it, and the producer uses the CamelKubernetesCRDPlural header when it is not set.

String

crdScope (consumer)

The scope of the custom resource: Namespaced or Cluster (the client takes any value other than Namespaced as cluster-scoped). Used only by kubernetes-custom-resources: the consumer requires it, and the producer uses the CamelKubernetesCRDScope header when it is not set.

String

crdVersion (consumer)

The version of the custom resource API, such as v1alpha1. Used only by kubernetes-custom-resources: the consumer requires it, and the producer uses the CamelKubernetesCRDVersion header when it is not set.

String

labelKey (consumer)

The label key that the consumer filters the watched resources on. Used only together with labelValue, and it takes precedence over resourceName. Not used by kubernetes-custom-resources.

String

labelValue (consumer)

The label value that the consumer filters the watched resources on. Used only together with labelKey. Not used by kubernetes-custom-resources.

String

poolSize (consumer)

The number of threads of the consumer that create the watch, and watch again when the Kubernetes client closed it with an error. The client delivers the events one at a time on its own threads, so one thread is enough.

1

int

resourceName (consumer)

The name of the single resource that the consumer watches. Not used when labelKey and labelValue are set, nor when namespace is not set, except by kubernetes-namespaces and kubernetes-nodes, whose resources are cluster-scoped. Not used by kubernetes-custom-resources.

String

bridgeErrorHandler (consumer (advanced))

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

exceptionHandler (consumer (advanced))

To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored.

ExceptionHandler

exchangePattern (consumer (advanced))

Sets the exchange pattern when the consumer creates an exchange.

Enum values:

  • InOnly

  • InOut

ExchangePattern

operation (producer)

The producer operation to perform, such as listPods or createConfigMap; the page of each component lists its operations. When it is not set, the CamelKubernetesOperation header gives the operation; the option takes precedence over the header.

String

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

connectionTimeout (advanced)

The timeout in milliseconds to establish a connection to the API server. When it is not set, the Kubernetes client default of 10000 applies; 0 or less leaves the timeout to the HTTP client implementation.

Integer

caCertData (security)

The certificate of the certificate authority (CA) to trust for the API server, as Base64-encoded PEM data. Takes precedence over caCertFile.

String

caCertFile (security)

The path of a PEM file with the certificate of the certificate authority (CA) to trust for the API server. caCertData takes precedence over it.

String

clientCertData (security)

The client certificate for TLS client authentication, as Base64-encoded PEM data. Takes precedence over clientCertFile, and is used only together with clientKeyData or clientKeyFile.

String

clientCertFile (security)

The path of a PEM file with the client certificate for TLS client authentication. Used only together with clientKeyData or clientKeyFile; clientCertData takes precedence over it.

String

clientKeyAlgo (security)

The algorithm of the client private key: RSA (the default of the Kubernetes client) or EC.

String

clientKeyData (security)

The private key for TLS client authentication, as Base64-encoded unencrypted PEM data. Takes precedence over clientKeyFile.

String

clientKeyFile (security)

The path of a file with the private key for TLS client authentication, as unencrypted PEM. clientKeyData takes precedence over it.

String

clientKeyPassphrase (security)

The password with which the Kubernetes client protects the client private key in the key store that it builds in memory (the client default is changeit). It does not decrypt the key, which must be unencrypted.

String

oauthToken (security)

The bearer token to authenticate to the API server, such as a service account token or an OAuth access token. When it is set, username and password are not used.

String

password (security)

The password for HTTP basic authentication to the API server. Used only together with username, and not when oauthToken is set.

String

trustCerts (security)

Whether to trust any certificate of the API server without checking it. This is insecure: set caCertData or caCertFile instead. Setting it to false does not override true from the auto-detected configuration.

Boolean

username (security)

The username for HTTP basic authentication to the API server. Used only together with password, and not when oauthToken is set.

String

Message Headers

The Kubernetes Custom Resources component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelKubernetesOperation (producer)

Constant: KUBERNETES_OPERATION

The operation of the producer, such as listPods or createConfigMap. Used only when the operation option of the endpoint is not set; one of the two is required.

String

CamelKubernetesNamespaceName (producer)

Constant: KUBERNETES_NAMESPACE_NAME

The namespace of the resource, which takes precedence over the namespace option of the endpoint. For kubernetes-namespaces, it is instead the name of the namespace to get, create, update or delete, where it is required. When no namespace is set, the list operations cover all namespaces (openshift-deploymentconfigs: the namespace of the Kubernetes client), getConfigMap, getDeployment and getDeploymentConfig use the namespace of the Kubernetes client, and the other operations fail; kubernetes-custom-resources requires a namespace for every operation.

String

CamelKubernetesCRDInstanceName (producer)

Constant: KUBERNETES_CRD_INSTANCE_NAME

The name of the custom resource for getCustomResource and deleteCustomResource, where it is required. createCustomResource and updateCustomResource take the name in the manifest of the CamelKubernetesCRDInstance header.

String

CamelKubernetesCRDEventTimestamp (consumer)

Constant: KUBERNETES_CRD_EVENT_TIMESTAMP

The time when the consumer received the watch event, in milliseconds since the Unix epoch (not the time of the change in Kubernetes).

long

CamelKubernetesCRDEventAction (consumer)

Constant: KUBERNETES_CRD_EVENT_ACTION

The action of the watch event that the consumer received: ADDED, MODIFIED or DELETED. The message body is the custom resource as a JSON string.

Enum values:

  • ADDED

  • MODIFIED

  • DELETED

  • ERROR

  • BOOKMARK

Action

CamelKubernetesCRDName (producer)

Constant: KUBERNETES_CRD_NAME

The name of the custom resource definition (its plural name and group joined by a dot), such as githubsources.sources.knative.dev. Used by the producer when the crdName option is not set (the option takes precedence); every operation needs all five custom resource definition values (name, group, scope, version and plural), from the options or the headers.

String

CamelKubernetesCRDGroup (producer)

Constant: KUBERNETES_CRD_GROUP

The API group of the custom resources, such as sources.knative.dev. Used by the producer when the crdGroup option is not set (the option takes precedence); every operation needs all five custom resource definition values (name, group, scope, version and plural), from the options or the headers.

String

CamelKubernetesCRDScope (producer)

Constant: KUBERNETES_CRD_SCOPE

The scope of the custom resources, Namespaced or Cluster. Used by the producer when the crdScope option is not set (the option takes precedence); every operation needs all five custom resource definition values (name, group, scope, version and plural), from the options or the headers.

String

CamelKubernetesCRDVersion (producer)

Constant: KUBERNETES_CRD_VERSION

The API version of the custom resources, such as v1alpha1. Used by the producer when the crdVersion option is not set (the option takes precedence); every operation needs all five custom resource definition values (name, group, scope, version and plural), from the options or the headers.

String

CamelKubernetesCRDPlural (producer)

Constant: KUBERNETES_CRD_PLURAL

The plural name of the custom resources, as in the URL path of the API, such as githubsources. Used by the producer when the crdPlural option is not set (the option takes precedence); every operation needs all five custom resource definition values (name, group, scope, version and plural), from the options or the headers.

String

CamelKubernetesCRDLabels (producer)

Constant: KUBERNETES_CRD_LABELS

The labels, as a map of label keys to values: listCustomResourcesByLabels (where they are required) lists only the custom resources that have all of them.

Map

CamelKubernetesCRDInstance (producer)

Constant: KUBERNETES_CRD_INSTANCE

The manifest of the custom resource to create or update, as a JSON or YAML string (the Kubernetes client reads it as YAML unless it starts with \{ or [); required for createCustomResource and updateCustomResource.

String

CamelKubernetesDeleteResult (producer)

Constant: KUBERNETES_DELETE_RESULT

Set by deleteCustomResource: true when the custom resource was deleted, false when it did not exist. The message body is not changed.

boolean

Message Body

The message body at each side of the Kubernetes Custom Resources component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Consumer

text (java.lang.String), as json

The custom resource that changed as JSON text, with the action (ADDED, MODIFIED or DELETED) in the CamelKubernetesCRDEventAction header.

Producer input

any

Not used: the operations take their input from headers, such as the name of the instance in CamelKubernetesCRDInstanceName and, for createCustomResource and updateCustomResource, the instance as JSON text in CamelKubernetesCRDInstance.

Producer output

list, map, pojo or unchanged

A JsonArray of the GenericKubernetesResource of the instances for the list operations, a JsonObject of the instance for getCustomResource (an empty one when it is not found), and the GenericKubernetesResource of create and update. deleteCustomResource leaves the body as it was, with whether it deleted the instance in the CamelKubernetesDeleteResult header.

  • With operation=listCustomResources: list (org.apache.camel.util.json.JsonArray)

  • With operation=listCustomResourcesByLabels: list (org.apache.camel.util.json.JsonArray)

  • With operation=getCustomResource: map (org.apache.camel.util.json.JsonObject)

  • With operation=createCustomResource: pojo (io.fabric8.kubernetes.api.model.GenericKubernetesResource)

  • With operation=updateCustomResource: pojo (io.fabric8.kubernetes.api.model.GenericKubernetesResource)

  • With operation=deleteCustomResource: unchanged

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Supported producer operation

  • listCustomResources

  • listCustomResourcesByLabels

  • getCustomResource

  • deleteCustomResource

  • createCustomResource

  • updateCustomResource

Example

The following route lists the custom resources of one kind in the default namespace. The headers name the custom resource definition, here the GitHub source of Knative:

- route:
    from:
      uri: direct:listSources
      steps:
        - setHeader:
            name: CamelKubernetesNamespaceName
            expression:
              constant:
                expression: default
        - setHeader:
            name: CamelKubernetesCRDName
            expression:
              constant:
                expression: githubsources.sources.knative.dev
        - setHeader:
            name: CamelKubernetesCRDGroup
            expression:
              constant:
                expression: sources.knative.dev
        - setHeader:
            name: CamelKubernetesCRDScope
            expression:
              constant:
                expression: Namespaced
        - setHeader:
            name: CamelKubernetesCRDVersion
            expression:
              constant:
                expression: v1alpha1
        - setHeader:
            name: CamelKubernetesCRDPlural
            expression:
              constant:
                expression: githubsources
        - to:
            uri: kubernetes-custom-resources:///
            parameters:
              kubernetesClient: "#kubernetesClient"
              operation: listCustomResources
        - log:
            message: "Found ${body.size()} GitHub sources"