codec

package
v0.0.0-...-6c2d9fc Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2017 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package codec provides a JSON/YAML codec for Manifests.

Usage:

// Decode one manifest from a JSON file.
man, err := JSON.Decode(b).One()
// Decode all of the manifests out of this file.
manifests, err := YAML.Decode(b).All()
err := YAML.Encode(filename).One("hello")
// Encode multiple objects to one file (as separate docs).
err := YAML.Encode(filename).All("one", "two", "three")

Index

Constants

This section is empty.

Variables

View Source
var JSON jsonCodec

JSON is the default JSON encoder/decoder.

View Source
var YAML yamlCodec

YAML is the default YAML encoder/decoder.

Functions

func SplitYAMLDocument

func SplitYAMLDocument(data []byte, atEOF bool) (advance int, token []byte, err error)

SplitYAMLDocument is a bufio.SplitFunc for splitting a YAML document into individual documents.

This is from Kubernetes' 'pkg/util/yaml'.splitYAMLDocument, which is unfortunately not exported.

Types

type Codec

type Codec interface {
	Encode(io.Writer) Encoder
	Decode([]byte) Decoder
}

Codec has an encoder and a decoder for a particular encoding.

type DecodeFunc

type DecodeFunc func([]byte, interface{}) error

DecodeFunc is a func that can decode bytes into an interface.

type Decoder

type Decoder interface {
	// Get one object from a file.
	One() (*Object, error)
	// Get all objects from a file.
	All() ([]*Object, error)
}

Decoder decodes an encoded representation into one or many objects.

func DecodeFile

func DecodeFile(filename string, c Codec) (Decoder, error)

DecodeFile returns a decoder pre-populated with the file contents.

type Encoder

type Encoder interface {
	// Write one object to one file
	One(interface{}) error
	// Write all objects to one file
	All(...interface{}) error
}

Encoder describes something capable of encoding to a given format.

An Encoder should be able to encode one object to an output stream, or many objects to an output stream.

For example, a single YAML file can contain multiple YAML objects, and a single JSONList file can contain many JSON objects.

type Metadata

type Metadata struct {
	unversioned.TypeMeta `json:",inline"`
	api.ObjectMeta       `json:"metadata,omitempty"`
}

Metadata provides just the basic metadata fields of an object.

It contains more data than an ObjectReference, since it includes standard metadata values like Name, Labels, and Annotations.

This is a readable structure, but should not be used to write changes.

type Object

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

Object describes some discrete instance of a Kind.

Objects are tracked as raw []byte data. They can be decoded into a variety of forms, but the decoded version is never stored on the object itself.

Mutators like AddLabels typically decode the object, mutate it, and then re-encode it. The reason for this is that those operations can assume a very simply and generic type and trust that the decoder/encoder pair can preserve structure.

Some operations, like Ref() and Meta(), can produce structured subsets of the original data, but will not preserve the entire object. These can be used to read data easily, but should not be used to mutate an object.

Operations like JSON() and YAML() will decode the data into a generic form and then re-encode the data.

func (*Object) AddAnnotations

func (m *Object) AddAnnotations(ann map[string]string) error

AddAnnotations adds the map of annotations to an object, regardless of kind.

This looks for a top-level metadata entry and adds annotations inside of that.

See the notes on AddLabels for performance implications.

func (*Object) AddLabels

func (m *Object) AddLabels(labels map[string]string) error

AddLabels adds the map of labels to an object, regardless of kind.

This looks for a top-level metadata entry, and adds labels there. Because this decodes the object into a generic format and then re-encodes it, this is a more costly operation than unmarshaling into a specific type and mutating the properties. However, it works on all types.

func (*Object) ConfigMap

func (m *Object) ConfigMap() (*v1.ConfigMap, error)

ConfigMap decodes a manifest into a ConfigMap.

func (*Object) DaemonSet

func (m *Object) DaemonSet() (*v1beta1.DaemonSet, error)

DaemonSet decodes a manifest into a DaemonSet.

func (*Object) Deployment

func (m *Object) Deployment() (*v1beta1.Deployment, error)

Deployment decodes a manifest into a Deployment.

func (*Object) HorizontalPodAutoscaler

func (m *Object) HorizontalPodAutoscaler() (*v1beta1.HorizontalPodAutoscaler, error)

HorizontalPodAutoscaler decodes a manifest into a HorizontalPodAutoscaler.

func (*Object) Ingress

func (m *Object) Ingress() (*v1beta1.Ingress, error)

Ingress decodes a manifest into a Ingress.

func (*Object) JSON

func (m *Object) JSON() ([]byte, error)

JSON re-encodes the data on this object into JSON format.

func (*Object) Job

func (m *Object) Job() (*v1beta1.Job, error)

Job decodes a manifest into a Job.

func (*Object) Meta

func (m *Object) Meta() (*Metadata, error)

Meta returns a *Metadata

This contains more information than an ObjectReference. It is valid for the core Kubernetes kinds, but is not guaranteed to work for all possible kinds.

func (*Object) Namespace

func (m *Object) Namespace() (*v1.Namespace, error)

Namespace decodes a manifest into a Namespace

func (*Object) Object

func (m *Object) Object(v interface{}) error

Object decodes the manifest into the given object.

You can use ObjectReference.Kind to figure out what kind of object to decode into.

There are several shortcut methods that will allow you to decode directly to one of the common types, like Pod(), RC(), and Service().

func (*Object) PersistentVolume

func (m *Object) PersistentVolume() (*v1.PersistentVolume, error)

PersistentVolume decodes a manifest into a PersistentVolume

func (*Object) Pod

func (m *Object) Pod() (*v1.Pod, error)

Pod decodes a manifest into a Pod.

func (*Object) RC

func (m *Object) RC() (*v1.ReplicationController, error)

RC decodes a manifest into a ReplicationController.

func (*Object) Ref

func (m *Object) Ref() (*v1.ObjectReference, error)

Ref returns an ObjectReference with basic information about the object.

This can be used to perform simple operations, as well as to instrospect a record enough to know how to unmarshal it.

func (*Object) Secret

func (m *Object) Secret() (*v1.Secret, error)

Secret decodes a manifest into a Secret

func (*Object) Service

func (m *Object) Service() (*v1.Service, error)

Service decodes a manifest into a Service

func (*Object) ServiceAccount

func (m *Object) ServiceAccount() (*v1.ServiceAccount, error)

ServiceAccount decodes a manifest into a ServiceAccount.

func (*Object) YAML

func (m *Object) YAML() ([]byte, error)

YAML takes the raw data and (re-)encodes it as YAML.

Jump to

Keyboard shortcuts

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