conversion

package
v1.16.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2026 License: Apache-2.0 Imports: 13 Imported by: 21

README

pkg/server/conversion

Intention

pkg/server/conversion is the canonical shared conversion layer for the generic part of API handler conversion. Service handlers still own type-specific conversion for their domain objects, but they should build the common resource envelope through this package rather than reimplementing shared metadata, status, scoping, and tag handling locally.

This package bridges the repository's shared metadata vocabulary from pkg/constants and shared API resource types from pkg/openapi into concrete handler conversion code.

It combines four closely related responsibilities:

  • projecting shared Kubernetes object metadata into common API resource metadata for read paths
  • translating common internal provisioning and health condition state into public API status enums
  • assembling and mutating shared Kubernetes object metadata for write paths
  • converting shared tag representations between Kubernetes and OpenAPI forms, and logging update patches for debugging

Invariants And Guard Rails

  • This package covers the generic conversion layer only. Service-specific converters must still handle domain fields and resource-specific semantics on top.
  • ResourceReadMetadata, OrganizationScopedResourceReadMetadata, and ProjectScopedResourceReadMetadata are the standard way to build the shared API resource envelope from Kubernetes objects.
  • NewObjectMetadata is the standard base path for constructing shared object metadata from API write metadata when a service needs to create a new Kubernetes resource. Callers are expected to layer scoping and resource-specific labels on top with the builder methods.
  • UpdateObjectMetadata is the common path for applying shared metadata mutation behavior on update, including the modified timestamp annotation. It is intentionally composable and callers commonly provide additional service-specific mutators on top of the generic behavior.
  • Provisioning and health status mapping here is repository-specific policy based on Unikorn status conditions. Callers should not improvise their own generic status mapping for the same resource envelope.
  • Deletion takes precedence for provisioning state. If a resource is being deleted, the public provisioning status is reported as deprovisioning immediately.
  • Tag conversion helpers here are the shared bridge between Kubernetes tag lists and OpenAPI tag lists. Type-specific converters should reuse them rather than duplicating field-by-field translation.

Caveats

  • The package centralizes generic conversion logic, but it still mixes read projection, write metadata assembly, tag translation, and update logging in one place.
  • The scoped read-metadata helpers use JSON marshal and unmarshal to copy the shared base metadata into larger generated OpenAPI structs. That is pragmatic generated-type glue rather than an especially elegant conversion model.
  • Generic metadata extraction is intentionally tolerant. Missing labels or annotations usually degrade to empty or absent API fields rather than producing hard conversion failures, which means scoping or attribution data can disappear from the outward API view without this layer rejecting the conversion.
  • The status mapping is only as expressive as the shared condition vocabulary. Resources with richer or different lifecycle semantics still need service-specific conversion logic around this generic layer.
  • LogUpdate is a debugging aid that logs a merge-patch-style diff of the resource update. It is useful for visibility, but it is not a patch application mechanism or a general audit system.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAnnotation = errors.New("a required annotation was missing")
)

Functions

func ConvertTag added in v0.1.84

func ConvertTag(in unikornv1.Tag) openapi.Tag

func ConvertTags added in v0.1.84

func ConvertTags(in unikornv1.TagList) openapi.TagList

func GenerateTag added in v0.1.84

func GenerateTag(in openapi.Tag) unikornv1.Tag

func GenerateTagList added in v0.1.84

func GenerateTagList(in *openapi.TagList) unikornv1.TagList

func LogUpdate added in v1.8.0

func LogUpdate(ctx context.Context, current, required metav1.Object) error

LogUpdate takes a diff of two resources and logs them.

func OrganizationScopedResourceReadMetadata

func OrganizationScopedResourceReadMetadata(in metav1.Object, tags unikornv1.TagList) openapi.OrganizationScopedResourceReadMetadata

OrganizationScopedResourceReadMetadata extracts organization scoped metdata from a resource for GET APIS.

func ProjectScopedResourceReadMetadata

func ProjectScopedResourceReadMetadata(in metav1.Object, tags unikornv1.TagList) openapi.ProjectScopedResourceReadMetadata

ProjectScopedResourceReadMetadata extracts project scoped metdata from a resource for GET APIs.

func ResourceReadMetadata

func ResourceReadMetadata(in metav1.Object, tags unikornv1.TagList) openapi.ResourceReadMetadata

ResourceReadMetadata extracts generic metadata from a resource for GET APIs.

func UpdateObjectMetadata

func UpdateObjectMetadata(required, current metav1.Object, mutators ...MetadataMutationFunc) error

UpdateObjectMetadata abstracts away metadata updates.

Types

type MetadataMutationFunc added in v1.4.0

type MetadataMutationFunc func(required, current metav1.Object) error

MetadataMutationFunc is used to mutate metadata on update.

type ObjectMetadata

type ObjectMetadata metav1.ObjectMeta

ObjectMetadata implements a builder pattern.

func NewObjectMetadata

func NewObjectMetadata(metadata *openapi.ResourceWriteMetadata, namespace string) *ObjectMetadata

NewObjectMetadata requests the bare minimum to build an object metadata object.

func (*ObjectMetadata) Get

func (o *ObjectMetadata) Get() metav1.ObjectMeta

Get renders the object metadata ready for inclusion into a Kubernetes resource.

func (*ObjectMetadata) WithLabel

func (o *ObjectMetadata) WithLabel(key, value string) *ObjectMetadata

WithLabel allows non-generic labels to be attached to a resource.

func (*ObjectMetadata) WithOrganization

func (o *ObjectMetadata) WithOrganization(id string) *ObjectMetadata

WithOrganization adds an organization for scoped resources.

func (*ObjectMetadata) WithProject

func (o *ObjectMetadata) WithProject(id string) *ObjectMetadata

WithProject adds a project for scoped resources.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL