How does kubectl configure CRDs?

3/31/2020

There're already some CRDs defined in my kubernetes cluster. kubectl can create/update/delete the resources well.

When I tried to do those operations with program, the way I found by searching is to generate code with below tool:

https://github.com/kubernetes/code-generator

I'm wondering why kubectl can do it out-of-box without generating code for CRDs.

Is it necessary to generate code in order to add or delete a CRD resource?

Thanks!

-- Wei Huang
go
kubernetes
kubernetes-apiserver
kubernetes-custom-resources
kubernetes-go-client

1 Answer

3/31/2020

First lets understand what CRD is.

The CustomResourceDefinition API resource allows you to define custom resources. Defining a CRD object creates a new custom resource with a name and schema that you specify. The Kubernetes API serves and handles the storage of your custom resource. The name of a CRD object must be a valid DNS subdomain name.

This frees you from writing your own API server to handle the custom resource, but the generic nature of the implementation means you have less flexibility than with API server aggregation.

Why would one create Custom Resources:

A resource is an endpoint in the Kubernetes API 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 is an extension of the Kubernetes API that is not necessarily available in a default Kubernetes installation. It represents a customization of a particular Kubernetes installation. However, many core Kubernetes functions are now built using custom resources, making Kubernetes more modular.

Custom resources can appear and disappear in a running cluster through dynamic registration, and cluster admins can update custom resources independently of the cluster itself. Once a custom resource is installed, users can create and access its objects using kubectl, just as they do for built-in resources like Pods.

So to answer your question, if you need a functionality that is missing from Kubernetes you need to create it yourself using CRDs. Without it cluster won't know what you want and how to get it.

If you are looking for examples of usage of Kubernetes Client-go you can find them on the official GitHub Client-go/examples

-- Crou
Source: StackOverflow