Create Custom Objects

After the CustomResourceDefinition object has been created, you can create custom objects. Custom objects can contain custom fields. These fields can contain arbitrary JSON. In the following example, the cronSpec and image custom fields are set in a custom object of kind CronTab. The kind CronTab comes from the spec of the CustomResourceDefinition object you created above.

If you save the following YAML to my-crontab.yaml:

cat <<EoF > ~/environment/my-crontab.yaml
apiVersion: ""
kind: CronTab
  name: my-new-cron-object
  cronSpec: "* * * * */5"
  image: my-awesome-cron-image

and create it:

kubectl apply -f my-crontab.yaml

You can then manage your CronTab objects using kubectl. For example:

kubectl get crontab

Should print a list like this:

NAME                 AGE
my-new-cron-object   6s

Resource names are not case-sensitive when using kubectl, and you can use either the singular or plural forms defined in the CRD, as well as any short names.

You can also view the raw YAML data:

kubectl get ct -o yaml

You should see that it contains the custom cronSpec and image fields from the yaml you used to create it:

apiVersion: v1
- apiVersion:
  kind: CronTab
    creationTimestamp: 2017-05-31T12:56:35Z
    generation: 1
    name: my-new-cron-object
    namespace: default
    resourceVersion: "285"
    selfLink: /apis/
    uid: 9423255b-4600-11e7-af6a-28d2447dc82b
    cronSpec: '* * * * */5'
    image: my-awesome-cron-image
kind: List
  resourceVersion: ""
  selfLink: ""

We can also describe the custom object with kubectl:

kubectl describe crontab

The output being something like this:

Name:         my-new-cron-object
Namespace:    default
Labels:       <none>
API Version:
Kind:         CronTab
  Creation Timestamp:  2019-05-09T18:10:35Z
  Generation:          1
  Resource Version:    3274450
  Self Link:           /apis/
  UID:                 bdc71d84-7285-11e9-a54d-0615623ca50e
  Cron Spec:  * * * * */5
  Image:      my-awesome-cron-image
Events:       <none>

Or we can check the resource directly from the Kubernetes API. First, we start the proxy in one tab of the Cloud9 environment:

kubectl proxy --port=8080 --address='' --disable-filter=true

And in another tab we check the existance of the Custom Resource

curl -i

With the output:

HTTP/1.1 200 OK
Audit-Id: 04c5ce6e-5a45-4064-8139-6c2b848bc467
Content-Length: 707
Content-Type: application/json
Date: Thu, 09 May 2019 18:18:21 GMT

{"apiVersion":"","kind":"CronTab","metadata":{"annotations":{"":"{\"apiVersion\":\"\",\"kind\":\"CronTab\",\"metadata\":{\"annotations\":{},\"name\":\"my-new-cron-object\",\"namespace\":\"default\"},\"spec\":{\"cronSpec\":\"* * * * */5\",\"image\":\"my-awesome-cron-image\"}}\n"},"creationTimestamp":"2019-05-09T18:10:35Z","generation":1,"name":"my-new-cron-object","namespace":"default","resourceVersion":"3274450","selfLink":"/apis/","uid":"bdc71d84-7285-11e9-a54d-0615623ca50e"},"spec":{"cronSpec":"* * * * */5","image":"my-awesome-cron-image"}}