TypeMeta Struct

TypeMeta Struct

Definition of Struct TypeMeta: This structure is used in Kubernetes to describe an object’s type and API version. It consists of two fields: Kind and APIVersion. The former denotes the object category (e.g., Pod, Service), while the latter defines its schema with a version number.

Embedding TypeMeta: In Go, one struct can be inserted into another. For instance, in order to gain access to its own properties (Kind and APIVersion), each Kubernetes entity like a Pod needs only embed the TypeMeta struct.

JSON tags and inline options: json:"…" tags tell Go which instructions should be followed when encoding or decoding JSON values for structs. Meanwhile, given that serialization/deserialization may affect both inner (TypeMeta) and outer layers of data representation simultaneously during these processes (as specified by inline tag),

YAML encoding peculiarities: Although it is simple enough to encode JSON using standard means provided by Go packages, YAML requires some extra care here. Specifically, ",inline" directive from early versions of Kubernetes API machinery will not work as expected with go-yaml/yaml encoder produced today (check out text). Accordingly, sigs.k8s.io/yaml package should be employed for Golang API structs so that they are serialized via JSON encoding/decoding – this applies to YAML encoding too

API versioning and groups: To arrange an increasing range of resources, Kubernetes came up with API groups. In Kubernetes objects, the apiVersion field stores the API group name and the version string as well. For objects that were created early in Kubernetes history, which belong to the core group, this field has an empty string for the group name.

Populating TypeMeta fields: While interacting with Kubernetes API through client-go library, the Kind and APIVersion fields are usually empty in memory representation of objects – they get filled in with real values during marshalling to JSON or protobufs. This is done automatically by a client.

Matching Go types against kinds and group names: Based on Go type names and package names, the kind and API group of an object can be inferred by a client. Although it works most of the time including custom resources there could be some exceptions.