Documentation ¶
Overview ¶
Package yamlfmt contains libraries for formatting yaml files containing Kubernetes Resource configuration.
Yaml files are formatted by: - Sorting fields and map values - Sorting unordered lists for whitelisted types - Applying a canonical yaml Style
Fields are ordered using a relative ordering applied to commonly encountered Resource fields. All Resources, including non-builtin Resources such as CRDs, share the same field precedence.
Fields that do not appear in the explicit ordering are ordered lexicographically.
A subset of well known known unordered lists are sorted by element field values.
Package merge contains libraries for merging Resources and Patches
Index ¶
- Constants
- Variables
- func FormatFileOrDirectory(path string) error
- func FormatInput(input io.Reader) (*bytes.Buffer, error)
- type ContainerFilter
- type ContainerNetwork
- type ContainerSpec
- type FileSetter
- type FilenameFmtVerb
- type FormatFilter
- type FunctionSpec
- type GrepFilter
- type GrepType
- type IsLocalConfig
- type IsReconcilerFilter
- type KFilter
- type MatchFilter
- type MatchModifyFilter
- type Merge3
- type MergeFilter
- type Modifier
- type StarlarkSpec
- type StorageMount
- type StripCommentsFilter
Constants ¶
const DefaultFilenamePattern = "%n_%k.yaml"
const (
FunctionAnnotationKey = "config.kubernetes.io/function"
)
const LocalConfigAnnotation = "config.kubernetes.io/local-config"
Variables ¶
var Filters = map[string]func() kio.Filter{ "FileSetter": func() kio.Filter { return &FileSetter{} }, "FormatFilter": func() kio.Filter { return &FormatFilter{} }, "GrepFilter": func() kio.Filter { return GrepFilter{} }, "MatchModifier": func() kio.Filter { return &MatchModifyFilter{} }, "Modifier": func() kio.Filter { return &Modifier{} }, }
Filters are the list of known filters for unmarshalling a filter into a concrete implementation.
Functions ¶
func FormatFileOrDirectory ¶
FormatFileOrDirectory reads the file or directory and formats each file's contents by writing it back to the file.
Types ¶
type ContainerFilter ¶
type ContainerFilter struct { // Image is the container image to use to create a container. Image string `yaml:"image,omitempty"` // Network is the container network to use. Network string `yaml:"network,omitempty"` // StorageMounts is a list of storage options that the container will have mounted. StorageMounts []StorageMount // Config is the API configuration for the container and passed through the // API_CONFIG env var to the container. // Typically a Kubernetes style Resource Config. Config *yaml.RNode `yaml:"config,omitempty"` // GlobalScope will cause the function to be run against all input // nodes instead of only nodes scoped under the function. GlobalScope bool // contains filtered or unexported fields }
ContainerFilter filters Resources using a container image. The container must start a process that reads the list of input Resources from stdin, reads the Configuration from the env API_CONFIG, and writes the filtered Resources to stdout. If there is a error or validation failure, the process must exit non-zero. The full set of environment variables from the parent process are passed to the container.
Function Scoping: ContainerFilter applies the function only to Resources to which it is scoped.
Resources are scoped to a function if any of the following are true:
- the Resource were read from the same directory as the function config
- the Resource were read from a subdirectory of the function config directory
- the function config is in a directory named "functions" and they were read from a subdirectory of "functions" parent
- the function config doesn't have a path annotation (considered globally scoped)
- the ContainerFilter has GlobalScope == true
In Scope Examples:
Example 1: deployment.yaml and service.yaml in function.yaml scope
same directory as the function config directory . ├── function.yaml ├── deployment.yaml └── service.yaml
Example 2: apps/deployment.yaml and apps/service.yaml in function.yaml scope
subdirectory of the function config directory . ├── function.yaml └── apps ├── deployment.yaml └── service.yaml
Example 3: apps/deployment.yaml and apps/service.yaml in functions/function.yaml scope
function config is in a directory named "functions" . ├── functions │ └── function.yaml └── apps ├── deployment.yaml └── service.yaml
Out of Scope Examples:
Example 1: apps/deployment.yaml and apps/service.yaml NOT in stuff/function.yaml scope
. ├── stuff │ └── function.yaml └── apps ├── deployment.yaml └── service.yaml
Example 2: apps/deployment.yaml and apps/service.yaml NOT in stuff/functions/function.yaml scope
. ├── stuff │ └── functions │ └── function.yaml └── apps ├── deployment.yaml └── service.yaml
Default Paths: Resources emitted by functions will have default path applied as annotations if none is present. The default path will be the function-dir/ (or parent directory in the case of "functions") + function-file-name/ + namespace/ + kind_name.yaml
Example 1: Given a function in fn.yaml that produces a Deployment name foo and a Service named bar
dir └── fn.yaml
Would default newly generated Resources to:
dir ├── fn.yaml └── fn ├── deployment_foo.yaml └── service_bar.yaml
Example 2: Given a function in functions/fn.yaml that produces a Deployment name foo and a Service named bar
dir └── fn.yaml
Would default newly generated Resources to:
dir ├── functions │ └── fn.yaml └── fn ├── deployment_foo.yaml └── service_bar.yaml
Example 3: Given a function in fn.yaml that produces a Deployment name foo, namespace baz and a Service named bar namespace baz
dir └── fn.yaml
Would default newly generated Resources to:
dir ├── fn.yaml └── fn └── baz ├── deployment_foo.yaml └── service_bar.yaml
func (ContainerFilter) String ¶ added in v0.0.8
func (c ContainerFilter) String() string
type ContainerNetwork ¶ added in v0.1.3
type ContainerNetwork struct { // Required specifies that function requires a network Required bool `json:"required,omitempty" yaml:"required,omitempty"` }
ContainerNetwork
type ContainerSpec ¶ added in v0.1.3
type ContainerSpec struct { // Image is the container image to run Image string `json:"image,omitempty" yaml:"image,omitempty"` // Network defines network specific configuration Network ContainerNetwork `json:"network,omitempty" yaml:"network,omitempty"` }
ContainerSpec defines a spec for running a function as a container
type FileSetter ¶
type FileSetter struct { Kind string `yaml:"kind,omitempty"` // FilenamePattern is the pattern to use for generating filenames. FilenameFmtVerb // FielnameFmtVerbs may be specified to substitute Resource metadata into the filename. FilenamePattern string `yaml:"filenamePattern,omitempty"` // Mode is the filemode to write. Mode string `yaml:"mode,omitempty"` // Override will override the existing filename if it is set on the pattern. // Otherwise the existing filename is kept. Override bool `yaml:"override,omitempty"` }
FileSetter sets the file name and mode annotations on Resources.
type FilenameFmtVerb ¶
type FilenameFmtVerb string
const ( // KindFmt substitutes kind KindFmt FilenameFmtVerb = "%k" // NameFmt substitutes metadata.name NameFmt FilenameFmtVerb = "%n" // NamespaceFmt substitutes metdata.namespace NamespaceFmt FilenameFmtVerb = "%s" )
type FormatFilter ¶
type FunctionSpec ¶ added in v0.1.3
type FunctionSpec struct { // Path defines the path for scoped functions Path string `json:"path,omitempty" yaml:"path,omitempty"` // Network is the name of the network to use from a container Network string `json:"network,omitempty" yaml:"network,omitempty"` // Container is the spec for running a function as a container Container ContainerSpec `json:"container,omitempty" yaml:"container,omitempty"` // Starlark is the spec for running a function as a starlark script Starlark StarlarkSpec `json:"starlark,omitempty" yaml:"starlark,omitempty"` }
FunctionSpec defines a spec for running a function
func GetFunctionSpec ¶ added in v0.1.3
func GetFunctionSpec(n *yaml.RNode) *FunctionSpec
GetFunctionSpec returns the FunctionSpec for a resource. Returns nil if the resource does not have a FunctionSpec.
The FunctionSpec is read from the resource metadata.annotation "config.kubernetes.io/function"
type GrepFilter ¶
type GrepFilter struct { Path []string `yaml:"path,omitempty"` Value string `yaml:"value,omitempty"` MatchType GrepType `yaml:"matchType,omitempty"` InvertMatch bool `yaml:"invertMatch,omitempty"` Compare func(a, b string) (int, error) }
GrepFilter filters RNodes with a matching field
type IsLocalConfig ¶
type IsLocalConfig struct { // IncludeLocalConfig will include local-config if set to true IncludeLocalConfig bool `yaml:"includeLocalConfig,omitempty"` // ExcludeNonLocalConfig will exclude non local-config if set to true ExcludeNonLocalConfig bool `yaml:"excludeNonLocalConfig,omitempty"` }
IsLocalConfig filters Resources using the config.kubernetes.io/local-config annotation
type IsReconcilerFilter ¶
type IsReconcilerFilter struct { // ExcludeReconcilers if set to true, then Reconcilers will be excluded -- e.g. // Resources with a reconcile container through the apiVersion (gcr.io prefix) or // through the annotations ExcludeReconcilers bool `yaml:"excludeReconcilers,omitempty"` // IncludeNonReconcilers if set to true, the NonReconciler will be included. IncludeNonReconcilers bool `yaml:"includeNonReconcilers,omitempty"` }
IsReconcilerFilter filters Resources based on whether or not they are Reconciler Resource. Resources with an apiVersion starting with '*.gcr.io', 'gcr.io' or 'docker.io' are considered Reconciler Resources.
type KFilter ¶
filter wraps a kio.filter so that it can be unmarshalled from yaml.
func (KFilter) MarshalYAML ¶
func (*KFilter) UnmarshalYAML ¶
type MatchFilter ¶
type MatchModifyFilter ¶
type Merge3 ¶
type Merge3 struct { OriginalPath string UpdatedPath string DestPath string MatchFilesGlob []string // MergeOnPath will use the relative filepath as part of the merge key. // This may be necessary if the directory contains multiple copies of // the same resource, or resources patches. MergeOnPath bool }
Merge3 performs a 3-way merge on the original, updated, and destination packages.
type MergeFilter ¶
type MergeFilter struct {
Reverse bool
}
GrepFilter merges Resources with the Group/Version/Kind/Namespace/Name together using a 2-way merge strategy.
- Fields set to null in the source will be cleared from the destination - Fields with matching keys will be merged recursively - Lists with an associative key (e.g. name) will have their elements merged using the key - List without an associative key will have the dest list replaced by the source list
type Modifier ¶
type Modifier struct { Kind string `yaml:"kind,omitempty"` Filters yaml.YFilters `yaml:"pipeline,omitempty"` }
Modifier modifies the input Resources by invoking the provided pipeline. Modifier will return any Resources for which the pipeline does not return an error.
type StarlarkSpec ¶ added in v0.1.3
type StarlarkSpec struct { Name string `json:"name,omitempty" yaml:"name,omitempty"` // Path specifies a path to a starlark script Path string `json:"path,omitempty" yaml:"path,omitempty"` }
StarlarkSpec defines how to run a function as a starlark program
type StorageMount ¶
type StorageMount struct { // Type of mount e.g. bind mount, local volume, etc. MountType string // Source for the storage to be mounted. // For named volumes, this is the name of the volume. // For anonymous volumes, this field is omitted (empty string). // For bind mounts, this is the path to the file or directory on the host. Src string // The path where the file or directory is mounted in the container. DstPath string }
StorageMount represents a container's mounted storage option(s)
func (*StorageMount) String ¶
func (s *StorageMount) String() string
type StripCommentsFilter ¶
type StripCommentsFilter struct{}