zip

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: BSD-3-Clause Imports: 9 Imported by: 12

README

The Zip API Object Framework

Go Report Card Latest release

This project contains the low-level object framework Zip API. It enables Kubernetes-inspired objects without the hassle of code generation.

Usage

To define your objects using the framework, simply import api.zip and declare a zip.Object along with a Spec and Status to get started.

import "api.zip"

type BookSpec struct {
	Name   string
	Author string
}

type BookState string

const (
	BookStateAvailable   = BookState("available")
	BookStateUnavailable = BookState("unavailable")
)

type BookStatus struct {
	State BookState
}

type (
	Book     = zip.Object[BookSpec, BookStatus]
	BookList = zip.ObjectList[BookSpec, BookStatus]
)

License

The Zip API Object Framework is licensed under BSD-3-Clause.

Documentation

Overview

SPDX-License-Identifier: BSD-3-Clause Copyright (c) 2022, The Zip API Object Framework Authors and Unikraft GmbH. Licensed under the BSD-3-Clause License (the "License"). You may not use this file except in compliance with the License.

SPDX-License-Identifier: BSD-3-Clause Copyright (c) 2022, The Zip API Object Framework Authors and Unikraft GmbH. Licensed under the BSD-3-Clause License (the "License"). You may not use this file except in compliance with the License.

Package api.zip enables Kubernetes-inspired objects without the hassle of code generation.

SPDX-License-Identifier: BSD-3-Clause Copyright (c) 2022, The Zip API Object Framework Authors and Unikraft GmbH. Licensed under the BSD-3-Clause License (the "License"). You may not use this file except in compliance with the License.

SPDX-License-Identifier: BSD-3-Clause Copyright (c) 2022, The Zip API Object Framework Authors and Unikraft GmbH. Licensed under the BSD-3-Clause License (the "License"). You may not use this file except in compliance with the License.

SPDX-License-Identifier: BSD-3-Clause Copyright (c) 2022, The Zip API Object Framework Authors and Unikraft GmbH. Licensed under the BSD-3-Clause License (the "License"). You may not use this file except in compliance with the License.

SPDX-License-Identifier: BSD-3-Clause Copyright (c) 2022, The Zip API Object Framework Authors and Unikraft GmbH. Licensed under the BSD-3-Clause License (the "License"). You may not use this file except in compliance with the License.

SPDX-License-Identifier: BSD-3-Clause Copyright (c) 2022, The Zip API Object Framework Authors and Unikraft GmbH. Licensed under the BSD-3-Clause License (the "License"). You may not use this file except in compliance with the License.

SPDX-License-Identifier: BSD-3-Clause Copyright (c) 2022, The Zip API Object Framework Authors and Unikraft GmbH. Licensed under the BSD-3-Clause License (the "License"). You may not use this file except in compliance with the License.

Index

Constants

View Source
const (
	// Always rehydrate the reference object
	StoreRehydrationAlways = StoreRehydrationMode("always")

	// Only rehydratae the reference object when its Spec is nil
	StoreRehydrationSpecNil = StoreRehydrationMode("specnil")

	// Never rehydrate the reference object
	StoreRehydrationNever = StoreRehydrationMode("never")
)

Variables

Functions

func Register added in v0.1.1

func Register(schemes ...AddToScheme) error

Register accepts a slice of AddToScheme methods which are then registered against the list of global schemes.

Types

type AddToScheme added in v0.1.1

type AddToScheme func(*runtime.Scheme) error

AddToScheme is a prototype to describe the method which can be used to append a new scheme to the list of global schemes.

type ClientConfig

type ClientConfig struct {
	// contains filtered or unexported fields
}

ClientConfig contains configuration which is passed to a Zip API client.

type ClientOption

type ClientOption func(*ClientConfig) error

ClientOption is a Zip API client config option-method handler.

func WithAfter added in v0.1.1

func WithAfter(after ...OnAfter) ClientOption

WithAfter provides post-call functions which manipulate the outbound object after the client has invoked its method strategy.

func WithBefore added in v0.1.1

func WithBefore(before ...OnBefore) ClientOption

WithBefore provides pre-call functions which manipulate the inbound object before the client invokes its method strategy.

func WithStore

func WithStore[Spec, Status any](store Store, mode StoreRehydrationMode) ClientOption

WithStore sets the Abstract client's store to the specified interface implementation. An additional positional argument for the rehydration mode is used to configure when the store on the before call is used and how it manipulates the value of the reference object.

type Method

type Method[In, Out ReferenceObject] func(context.Context, In) (Out, error)

Method represents a generic API function call.

type MethodClient

type MethodClient[In, Out ReferenceObject] struct {
	// contains filtered or unexported fields
}

MethodClient is the generic Zip API client.

func (*MethodClient[In, Out]) Do

func (client *MethodClient[In, Out]) Do(ctx context.Context, req In) (Out, error)

Do implements MethodStrategy.

type MethodStrategy

type MethodStrategy[In, Out ReferenceObject] interface {
	// Do performs the request by invoking the provided generic Method.
	Do(context.Context, In) (Out, error)
}

MethodStrategy is a generic method handler which standardizes implementation of a client such that the action performed on a specific endpoint for a given input type In which returns a known output type Out is consistently implemented.

func NewMethodClient

func NewMethodClient[In, Out ReferenceObject](
	ctx context.Context,
	fn Method[In, Out],
	opts ...ClientOption,
) (
	MethodStrategy[In, Out],
	error,
)

NewMethodClient instantiates a new Zip API method client.

type Object

type Object[Spec, Status any] struct {
	metav1.TypeMeta `json:",inline"`

	// Standard object's metadata.
	metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

	// Specification of the desired behavior of the Object.
	Spec Spec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`

	// Most recently observed status of the Object.
	Status Status `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}

Object represents a generic low-level Kubernetes-inspired object which contains information about the object type, its metadata, a specification which is used to describe the object and its current status.

func (*Object[Spec, Status]) DeepCopy

func (in *Object[Spec, Status]) DeepCopy() *Object[Spec, Status]

DeepCopy copies the receiver, creating a new Object.

func (*Object[Spec, Status]) DeepCopyInto

func (in *Object[Spec, Status]) DeepCopyInto(out *Object[Spec, Status])

DeepCopyInto is a deepcopy function, copying the receiver, writing into out.

func (Object[_, _]) DeepCopyObject

func (in Object[_, _]) DeepCopyObject() runtime.Object

DeepCopyObject implements runtime.Object.

func (*Object[_, _]) GetObjectKind

func (obj *Object[_, _]) GetObjectKind() schema.ObjectKind

GetObjectKind implements runtime.Object.

func (*Object[_, _]) GroupVersionKind

func (obj *Object[_, _]) GroupVersionKind() schema.GroupVersionKind

GroupVersionKind returns the API version and kind of the object reference.

func (*Object[_, _]) Reference

func (obj *Object[_, _]) Reference() (string, error)

Reference implements Referencable

func (*Object[_, _]) SetGroupVersionKind

func (obj *Object[_, _]) SetGroupVersionKind(gvk schema.GroupVersionKind)

SetGroupVersionKind sets the API version and kind of the object reference.

type ObjectList

type ObjectList[Spec, Status any] struct {
	metav1.TypeMeta `json:",inline"`

	// Standard object's metadata.
	metav1.ListMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

	// Items contains the list of machines.
	Items []Object[Spec, Status] `json:"items" protobuf:"bytes,2,rep,name=items"`
}

ObjectList represents a list of Objects.

func (*ObjectList[Spec, Status]) DeepCopy

func (list *ObjectList[Spec, Status]) DeepCopy() *ObjectList[Spec, Status]

DeepCopy copies the receiver, creating a new ObjectList.

func (*ObjectList[Spec, Status]) DeepCopyInto

func (in *ObjectList[Spec, Status]) DeepCopyInto(out *ObjectList[Spec, Status])

DeepCopyInto copies the receiver, writing into out. in must be non-nil.

func (*ObjectList[_, _]) DeepCopyObject

func (list *ObjectList[_, _]) DeepCopyObject() runtime.Object

DeepCopyObject implements runtime.Object.

func (*ObjectList[_, _]) GetObjectKind

func (list *ObjectList[_, _]) GetObjectKind() schema.ObjectKind

GetObjectKind implements runtime.Object.

func (*ObjectList[_, _]) GroupVersionKind

func (list *ObjectList[_, _]) GroupVersionKind() schema.GroupVersionKind

GroupVersionKind returns the API version and kind of the object reference

func (*ObjectList[_, _]) Reference

func (list *ObjectList[_, _]) Reference() (string, error)

Reference implements Referencable.

func (*ObjectList[_, _]) SetGroupVersionKind

func (list *ObjectList[_, _]) SetGroupVersionKind(gvk schema.GroupVersionKind)

SetGroupVersionKind sets the API version and kind of the object reference

type OnAfter

OnAfter is a prototype for a function that can be called on a referencable object after it has been returned by the client.

type OnBefore

type OnBefore func(context.Context, ReferenceObject) (any, error)

OnBefore is a prototype for a function that can be called on a referencable object before it is invoked by the client.

type Referencable

type Referencable interface {
	Reference() (string, error)
}

Referencable is an interface for allowing the object to return a unique reference for lookups.

type ReferenceObject

type ReferenceObject interface {
	Referencable
	runtime.Object
}

ReferenceObject combines the low-level Kubernetes-centric runtime.Object and the Zip API Referencable to allow mutable, copyable objects to be referenced at runtime.

type Store

type Store interface {
	// Returns Versioner associated with this interface.
	Versioner() storage.Versioner

	// Create adds a new object at a key unless it already exists. 'ttl' is
	// time-to-live in seconds (0 means forever). If no error is returned and out
	// is not nil, out will be set to the read value from database.
	Create(ctx context.Context, key string, obj, out runtime.Object, ttl uint64) error

	// Delete removes the specified key and returns the value that existed at that spot.
	// If key didn't exist, it will return NotFound storage error.
	// If 'cachedExistingObject' is non-nil, it can be used as a suggestion about the
	// current version of the object to avoid read operation from storage to get it.
	// However, the implementations have to retry in case suggestion is stale.
	Delete(
		ctx context.Context, key string, out runtime.Object, preconditions *storage.Preconditions,
		validateDeletion storage.ValidateObjectFunc, cachedExistingObject runtime.Object,
		opts storage.DeleteOptions) error

	// Watch begins watching the specified key. Events are decoded into API objects,
	// and any items selected by 'p' are sent down to returned watch.Interface.
	// resourceVersion may be used to specify what version to begin watching,
	// which should be the current resourceVersion, and no longer rv+1
	// (e.g. reconnecting without missing any updates).
	// If resource version is "0", this interface will get current object at given key
	// and send it in an "ADDED" event, before watch starts.
	Watch(ctx context.Context, key string, opts storage.ListOptions) (watch.Interface, error)

	// Get unmarshals object found at key into objPtr. On a not found error, will either
	// return a zero object of the requested type, or an error, depending on 'opts.ignoreNotFound'.
	// Treats empty responses and nil response nodes exactly like a not found error.
	// The returned contents may be delayed, but it is guaranteed that they will
	// match 'opts.ResourceVersion' according 'opts.ResourceVersionMatch'.
	Get(ctx context.Context, key string, opts storage.GetOptions, objPtr runtime.Object) error

	// GetList unmarshalls objects found at key into a *List api object (an object
	// that satisfies runtime.IsList definition).
	// If 'opts.Recursive' is false, 'key' is used as an exact match. If `opts.Recursive'
	// is true, 'key' is used as a prefix.
	// The returned contents may be delayed, but it is guaranteed that they will
	// match 'opts.ResourceVersion' according 'opts.ResourceVersionMatch'.
	GetList(ctx context.Context, key string, opts storage.ListOptions, listObj runtime.Object) error

	// GuaranteedUpdate keeps calling 'tryUpdate()' to update key 'key' (of type 'destination')
	// retrying the update until success if there is index conflict.
	// Note that object passed to tryUpdate may change across invocations of tryUpdate() if
	// other writers are simultaneously updating it, so tryUpdate() needs to take into account
	// the current contents of the object when deciding how the update object should look.
	// If the key doesn't exist, it will return NotFound storage error if ignoreNotFound=false
	// else `destination` will be set to the zero value of it's type.
	// If the eventual successful invocation of `tryUpdate` returns an output with the same serialized
	// contents as the input, it won't perform any update, but instead set `destination` to an object with those
	// contents.
	// If 'cachedExistingObject' is non-nil, it can be used as a suggestion about the
	// current version of the object to avoid read operation from storage to get it.
	// However, the implementations have to retry in case suggestion is stale.
	//
	// Example:
	//
	// s := /* implementation of Interface */
	// err := s.GuaranteedUpdate(
	//     "myKey", &MyType{}, true, preconditions,
	//     func(input runtime.Object, res ResponseMeta) (runtime.Object, *uint64, error) {
	//       // Before each invocation of the user defined function, "input" is reset to
	//       // current contents for "myKey" in database.
	//       curr := input.(*MyType)  // Guaranteed to succeed.
	//
	//       // Make the modification
	//       curr.Counter++
	//
	//       // Return the modified object - return an error to stop iterating. Return
	//       // a uint64 to alter the TTL on the object, or nil to keep it the same value.
	//       return cur, nil, nil
	//    }, cachedExistingObject
	// )
	GuaranteedUpdate(
		ctx context.Context, key string, destination runtime.Object, ignoreNotFound bool,
		preconditions *storage.Preconditions, tryUpdate storage.UpdateFunc, cachedExistingObject runtime.Object) error

	// Count returns number of different entries under the key (generally being path prefix).
	Count(key string) (int64, error)
}

Store offers a common interface for object marshaling/unmarshaling operations and hides all the storage-related operations behind it.

type StoreRehydrationMode added in v0.1.2

type StoreRehydrationMode string

StoreRehydrationMode is a switch that dictates how the store's before call is used to manipulate the

type Stream

type Stream[In ReferenceObject, Out any] func(context.Context, In) (chan Out, chan error, error)

Stream represents a generic stream-enabled API function call.

type StreamClient

type StreamClient[In ReferenceObject, Out any] struct {
	// contains filtered or unexported fields
}

StreamClient provides the interface for connecting to a remote service which returns a stream.

func (*StreamClient[In, Out]) Channel

func (client *StreamClient[In, Out]) Channel(
	ctx context.Context,
	req In,
) (
	chan Out,
	chan error,
	error,
)

Channel implements StreamStrategy

type StreamStrategy

type StreamStrategy[In ReferenceObject, Out any] interface {
	// Channel performs the request with the provide input In and returns a
	// channel which allows returning a stream of content.  Any issues from the
	// stream itself will be propagated back through the error channel.
	// Initialization errors are returned through the standard error.
	Channel(context.Context, In) (chan Out, chan error, error)
}

StreamMethodStrategy is a generic handler which standardizes the implementation of a streaming client which performs actions on an input In which resolves to a stream (implemented as a Go channel) Out.

func NewStreamClient

func NewStreamClient[In ReferenceObject, Out any](
	ctx context.Context,
	fn Stream[In, Out],
	opts ...ClientOption,
) (
	StreamStrategy[In, Out],
	error,
)

NewStreamClient instantiates a new stream-enable client for the given stream provider.

Jump to

Keyboard shortcuts

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