Managing resources from custom resource definitions

This guide describes how developers can manage custom resources (CRs) that come from custom resource definitions (CRDs).

Custom resource definitions

In the Kubernetes API, a resource is an endpoint that stores a collection of API objects of a certain kind. For example, the built-in Pods resource contains a collection of Pod objects.

A custom resource definition (CRD) object defines a new, unique object type, called a kind, in the cluster and lets the Kubernetes API server handle its entire lifecycle.

Custom resource (CR) objects are created from CRDs that have been added to the cluster by a cluster administrator, allowing all cluster users to add the new resource type into projects.

Operators in particular make use of CRDs by packaging them with any required RBAC policy and other software-specific logic. Cluster administrators can also add CRDs manually to the cluster outside of the lifecycle of an Operator, making them available to all users.

While only cluster administrators can create CRDs, developers can create the CR from an existing CRD if they have read and write permission to it.

Creating custom resources from a file

After a custom resource definition (CRD) has been added to the cluster, custom resources (CRs) can be created with the CLI from a file using the CR specification.

Prerequisites

  • CRD added to the cluster by a cluster administrator.

Procedure

  1. Create a YAML file for the CR. In the following example definition, the cronSpec and image custom fields are set in a CR of Kind: CronTab. The Kind comes from the spec.kind field of the CRD object:

    Example YAML file for a CR

    1. apiVersion: "stable.example.com/v1" (1)
    2. kind: CronTab (2)
    3. metadata:
    4. name: my-new-cron-object (3)
    5. finalizers: (4)
    6. - finalizer.stable.example.com
    7. spec: (5)
    8. cronSpec: "* * * * /5"
    9. image: my-awesome-cron-image
    1Specify the group name and API version (name/version) from the CRD.
    2Specify the type in the CRD.
    3Specify a name for the object.
    4Specify the finalizers for the object, if any. Finalizers allow controllers to implement conditions that must be completed before the object can be deleted.
    5Specify conditions specific to the type of object.
  2. After you create the file, create the object:

    1. $ oc create -f <file_name>.yaml

Inspecting custom resources

You can inspect custom resource (CR) objects that exist in your cluster using the CLI.

Prerequisites

  • A CR object exists in a namespace to which you have access.

Procedure

  1. To get information on a specific kind of a CR, run:

    1. $ oc get <kind>

    For example:

    1. $ oc get crontab

    Example output

    1. NAME KIND
    2. my-new-cron-object CronTab.v1.stable.example.com

    Resource names are not case-sensitive, and you can use either the singular or plural forms defined in the CRD, as well as any short name. For example:

    1. $ oc get crontabs
    1. $ oc get crontab
    1. $ oc get ct
  2. You can also view the raw YAML data for a CR:

    1. $ oc get <kind> -o yaml

    For example:

    1. $ oc get ct -o yaml

    Example output

    1. apiVersion: v1
    2. items:
    3. - apiVersion: stable.example.com/v1
    4. kind: CronTab
    5. metadata:
    6. clusterName: ""
    7. creationTimestamp: 2017-05-31T12:56:35Z
    8. deletionGracePeriodSeconds: null
    9. deletionTimestamp: null
    10. name: my-new-cron-object
    11. namespace: default
    12. resourceVersion: "285"
    13. selfLink: /apis/stable.example.com/v1/namespaces/default/crontabs/my-new-cron-object
    14. uid: 9423255b-4600-11e7-af6a-28d2447dc82b
    15. spec:
    16. cronSpec: '* * * * /5' (1)
    17. image: my-awesome-cron-image (1)
    1Custom data from the YAML that you used to create the object displays.