Path Mapping
Path mapping is a concept that transformers use to affect the output of Move2Kube. Usually transformers deal with artifacts. They take artifacts as input and output new artifacts. However this does nothing to change the output of Move2Kube since all transformers are run inside temporary directories. In order to affect the output directory, transformers need to return path mappings indicating the type of change to be made. For example: Consider a transformer that adds an annotation to Kubernetes Ingress YAML files. The transformer reads the file, adds the annotation and then writes it back out. However this modified file is only present inside the temporary directory and does not appear in the output directory of Move2Kube. To copy this file over to the output directory we can create a path mapping:
{
"type": "Source",
"sourcePath": "annotated-ingress.yaml",
"destinationPath": "deploy/yamls/ingress.yaml"
}
and return this from our transformer. Once the transformer is finished, Move2Kube will look at the path mapping our transformer returned and copy over the file to the output directory.
The above example shows the simplest use case for path mappings. However path mappings are capable of much more advanced uses, for example: the source file is a template and needs to be filled in before being copied to the output. Another example is when the source and destination paths are template strings that need to be filled in order to get the actual paths.
Different type of path mappings
// PathMappingType refers to the Path Mapping type
type PathMappingType = string
const (
// DefaultPathMappingType allows normal copy with overwrite
DefaultPathMappingType PathMappingType = "Default" // Normal Copy with overwrite
// TemplatePathMappingType allows copy of source to destination and applying of template
TemplatePathMappingType PathMappingType = "Template" // Source path when relative, is relative to yaml file location
// SourcePathMappingType allows for copying of source directory to another directory
SourcePathMappingType PathMappingType = "Source" // Source path becomes relative to source directory
// DeletePathMappingType allows for deleting of files or directories
DeletePathMappingType PathMappingType = "Delete" // Delete path becomes relative to source directory
// ModifiedSourcePathMappingType allows for copying of deltas wrt source
ModifiedSourcePathMappingType PathMappingType = "SourceDiff" // Source path becomes relative to source directory
// PathTemplatePathMappingType allows for path template registration
PathTemplatePathMappingType PathMappingType = "PathTemplate" // Path Template type
// SpecialTemplatePathMappingType allows copy of source to destination and applying of template with custom delimiter
SpecialTemplatePathMappingType PathMappingType = "SpecialTemplate" // Source path when relative, is relative to yaml file location
)
// PathMapping is the mapping between source and intermediate files and output files
type PathMapping struct {
Type PathMappingType `yaml:"type,omitempty" json:"type,omitempty"` // Default - Normal copy
SrcPath string `yaml:"sourcePath" json:"sourcePath" m2kpath:"normal"`
DestPath string `yaml:"destinationPath" json:"destinationPath" m2kpath:"normal"` // Relative to output directory
TemplateConfig interface{} `yaml:"templateConfig" json:"templateConfig"`
}
Below we explain the different types of path mappings:
Default
-sourcePath
must be an absolute path.destinationPath
must be a relative path, relative to Move2Kube’s output directory. Copies the directory/file specified insourcePath
todestinationPath
.Template
-sourcePath
must be a relative path, relative to the templates directory of your transformer.destinationPath
must be a relative path, relative to Move2Kube’s output directory. Fills the template in the file given bysourcePath
and copies the filled template todestinationPath
. The values for filling the template are be given intemplateConfig
.Source
- Same asDefault
path mapping except now thesourcePath
can now be a relative path, relative to the temporary directory where the transformer is running.Delete
-sourcePath
must be a relative path, relative to Move2Kube’s output directory. The directory/file specified bysourcePath
will be deleted.SourceDiff
- TODOPathTemplate
- The path itself becomes a template.sourcePath
contains the templated path.templateConfig
can be used to set a name for this templated path.SpecialTemplate
- Same asTemplate
path mapping except now the template has a different syntax. The delimiters used in normal templates are{{
and}}
. In special templates, the delimiters are<~
and~>
. Same as before, the values for filling the template are be given intemplateConfig
.