|
| 1 | +# Groups and Versions and Kinds, oh my! |
| 2 | + |
| 3 | +Actually, before we get started with our API, we should talk terminology |
| 4 | +a bit. |
| 5 | + |
| 6 | +When we talk about APIs in Kubernetes, we often use 4 terms: *groups*, |
| 7 | +*versions*, *kinds*, and *resources*. |
| 8 | + |
| 9 | +## Groups and Versions |
| 10 | + |
| 11 | +An *API Group* in Kubernetes is simply a collection of related |
| 12 | +functionality. Each group has one or more *versions*, which, as the name |
| 13 | +suggests, allow us to change how an API works over time. |
| 14 | + |
| 15 | +## Kinds and Resources |
| 16 | + |
| 17 | +Each API group-version contains one or more API types, which we call |
| 18 | +*Kinds*. While a Kind may change forms between versions, each form must |
| 19 | +be able to store all the data of the other forms, somehow (we can store |
| 20 | +the data in fields, or in annotations). This means that using an older |
| 21 | +API version won't cause newer data to be lost or corrupted. See the |
| 22 | +[Kubernetes API guidelines](../TODO.md) for more information. |
| 23 | + |
| 24 | +You'll also hear mention of *resources* on occaison. A resource is simply |
| 25 | +a use of a Kind in the API. Often, there's a one-to-one mapping between |
| 26 | +Kinds and resources. For instance, the `pods` resource corresponds to the |
| 27 | +`Pod` Kind. However, sometimes, the same Kind may be returned by multiple |
| 28 | +resources. For instance, the `Scale` Kind is returned by all scale |
| 29 | +subresources, like `deployments/scale` or `replicasets/scale`. This is |
| 30 | +what allows the Kubernetes HorizontalPodAutoscaler to interact with |
| 31 | +different resources. With CRDs, however, each Kind will correspond to |
| 32 | +a single resource. |
| 33 | + |
| 34 | +Notice that resources are always lowercase, and by convention are the |
| 35 | +lowercase form of the Kind. |
| 36 | + |
| 37 | +## So, how does that correspond to Go? |
| 38 | + |
| 39 | +When we refer to a kind in a particular group-version, we'll call it |
| 40 | +a *GroupVersionKind*, or GVK for short. Same with resources and GVR. As |
| 41 | +we'll see shortly, each GVK corresponds to a given root Go type in |
| 42 | +a package. |
| 43 | + |
| 44 | +Now that we have our terminology straight, we can *actually* create our |
| 45 | +API! |
| 46 | + |
| 47 | +## Err, but what's that Scheme thing? |
| 48 | + |
| 49 | +The `Scheme` we saw before is simply a way to keep track of what Go type |
| 50 | +corresponds to a given GVK. |
| 51 | + |
| 52 | +For instance, suppose we mark that the |
| 53 | +`"tutorial.kubebuilder.io/api/v1".CronJob{}` type as being in the |
| 54 | +`batch.tutorial.kubebuilder.io/v1` API group (implicitly saying it has the |
| 55 | +Kind `CronJob`). |
| 56 | + |
| 57 | +Then, we can later construct a new `&CronJob{}` given some JSON from the |
| 58 | +API server that says |
| 59 | + |
| 60 | +```json |
| 61 | +{ |
| 62 | + "kind": "CronJob", |
| 63 | + "apiVersion": "batch.tutorial.kubebuilder.io/v1", |
| 64 | + ... |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +or properly look up the group-version when we go to submit a `&CronJob{}` |
| 69 | +in an update. |
0 commit comments