Skip to content

Instantly share code, notes, and snippets.

@MadVikingGod
Created March 9, 2022 16:43
Show Gist options
  • Save MadVikingGod/47dc002a23fcc0df078ea87ae4d46f43 to your computer and use it in GitHub Desktop.
Save MadVikingGod/47dc002a23fcc0df078ea87ae4d46f43 to your computer and use it in GitHub Desktop.

Registration of Kubernetes API objects

Definition of terms: API Object - This is anything that a Kubernetes cluster would know about, examples are Pods, Deployments, Services, DeploymentLists. It can be extended by Custom Resource Definitions (CRDs) GroupVersionKind (GVK) - This is the identifier of an API Object. eg Core/V1/Pod GroupVersionResource (GVR) - This is an identifier for one particular GVK pods.core.v1 Schema - This is an object that understands how to translate a Go struct into the GroupVersionKind for the API object that struct represents and back. This is used in the encoding and decoding process.

When you use Kubernetes types in a go program you will import somthing like "k8s.io/apimachinery/pkg/apis/core/v1". In that package it will have something to register that API's known types. This SchemaBuilder, and the schemas from other types are used by the kubernetes client (and the generated clientset) to connect the struct type to the GVK. This enables things like

resource := schema.GroupVersionResource{Group: "core", Version: "v1", Resource: "pods"}
dynamicClient.Resource(resource).Namespace(ns).Get(...)
# Or in the generated client 
client.CoreV1().Pods(ns).Get(...)

Another place that I've experimented with this concept is registering the exporters to enable envvar swapping of the components. https://github.com/MadVikingGod/opentelemetry-registration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment