generate
import "github.com/ice-bergtech/kr8/pkg/generate"
Package generate implements the logic for generating output files based on input data.
Combines:
- a directory of cluster configurations
- a directory of components
- a optional directory of Jsonnet library files
to generate output files. Output files can be structured yaml, or files generated from go templates.
The package prepares a Jsonnet VM and loads the necessary libraries and extVars. A new VM is created for each component.
Index
- func CalculateClusterComponentList(clusterComponents map[string]gjson.Result, filters util.PathFilterOptions) []string
- func CheckComponentCache(cache *kr8_cache.DeploymentCache, compSpec kr8_types.Kr8ComponentSpec, config string, componentName string, baseDir string, logger zerolog.Logger) (bool, *kr8_cache.ComponentCache, error)
- func CheckIfUpdateNeeded(outFile string, outStr string) (bool, error)
- func CleanOutputDir(outputFileMap map[string]bool, componentOutputDir string) error
- func CleanupOldComponentDirs(existingComponents []string, clusterComponents map[string]gjson.Result, kr8Spec *kr8_types.Kr8ClusterSpec, logger zerolog.Logger)
- func CompileClusterConfiguration(clusterName, clusterDir string, kr8Opts types.Kr8Opts, vmConfig types.VMConfig, generateDirOverride string, logger zerolog.Logger) (*kr8_types.Kr8ClusterSpec, map[string]gjson.Result, error)
- func CreateClusterGenerateDirs(kr8Spec kr8_types.Kr8ClusterSpec) ([]string, error)
- func GatherClusterConfig(clusterName, clusterDir string, kr8Opts types.Kr8Opts, vmConfig types.VMConfig, generateDirOverride string, filters util.PathFilterOptions, clusterParamsFile string, noop bool, logger zerolog.Logger) (*kr8_types.Kr8ClusterSpec, []string, string, error)
- func GenProcessCluster(clusterConfig *GenerateProcessRootConfig, pool *ants.Pool, logger zerolog.Logger) error
- func GenProcessComponent(vmConfig types.VMConfig, componentName string, kr8Spec kr8_types.Kr8ClusterSpec, kr8Opts types.Kr8Opts, config string, allConfig *SafeString, filters util.PathFilterOptions, paramsFile string, cache *kr8_cache.DeploymentCache, logger zerolog.Logger) (bool, *kr8_cache.ComponentCache, error)
- func GenerateIncludesFiles(includesFiles []kr8_types.Kr8ComponentSpecIncludeObject, kr8Spec kr8_types.Kr8ClusterSpec, kr8Opts types.Kr8Opts, config string, componentName string, compPath string, componentOutputDir string, jvm *jsonnet.VM, logger zerolog.Logger) (map[string]bool, error)
- func GetAllClusterParams(clusterDir string, vmConfig types.VMConfig, jvm *jsonnet.VM, logger zerolog.Logger) error
- func GetClusterComponentParamsThreadSafe(allConfig *SafeString, config string, vmConfig types.VMConfig, kr8Spec kr8_types.Kr8ClusterSpec, filters util.PathFilterOptions, paramsFile string, jvm *jsonnet.VM, logger zerolog.Logger) error
- func GetClusterParams(clusterDir string, vmConfig types.VMConfig, logger zerolog.Logger) (map[string]string, error)
- func GetComponentFiles(compSpec kr8_types.Kr8ComponentSpec) []string
- func GetComponentPath(config string, componentName string) string
- func LoadClusterCache(kr8Spec *kr8_types.Kr8ClusterSpec, logger zerolog.Logger) (*kr8_cache.DeploymentCache, string)
- func ProcessComponentFinalizer(compSpec kr8_types.Kr8ComponentSpec, componentOutputDir string, outputFileMap map[string]bool) error
- func ProcessFile(inputFile string, outputFile string, kr8Spec kr8_types.Kr8ClusterSpec, componentName string, config string, incInfo kr8_types.Kr8ComponentSpecIncludeObject, jvm *jsonnet.VM, logger zerolog.Logger) (string, error)
- func ProcessJsonnetToYaml(jvm *jsonnet.VM, input string, snippetFilename string) (string, error)
- func ProcessTemplate(filename string, data gjson.Result) (string, error)
- func RenderComponents(config string, vmConfig types.VMConfig, kr8Spec kr8_types.Kr8ClusterSpec, cache *kr8_cache.DeploymentCache, compList []string, clusterParamsFile string, pool *ants.Pool, kr8Opts types.Kr8Opts, filters util.PathFilterOptions, logger zerolog.Logger) (map[string]kr8_cache.ComponentCache, error)
- func SetupBaseComponentJvm(vmconfig types.VMConfig, config string, kr8Spec kr8_types.Kr8ClusterSpec) (*jsonnet.VM, error)
- func SetupComponentVM(vmConfig types.VMConfig, config string, kr8Spec kr8_types.Kr8ClusterSpec, componentName string, compSpec kr8_types.Kr8ComponentSpec, allConfig *SafeString, filters util.PathFilterOptions, paramsFile string, kr8Opts types.Kr8Opts, logger zerolog.Logger) (*jsonnet.VM, string, error)
- func SetupJvmForComponent(jvm *jsonnet.VM, config string, kr8Spec kr8_types.Kr8ClusterSpec, componentName string)
- func ValidateOrCreateCache(cache *kr8_cache.DeploymentCache, config string, logger zerolog.Logger) *kr8_cache.DeploymentCache
- type GenerateProcessRootConfig
- type SafeString
func CalculateClusterComponentList
func CalculateClusterComponentList(clusterComponents map[string]gjson.Result, filters util.PathFilterOptions) []string
Calculates which components should be generated based on filters. Only processes specified component if it's defined in the cluster. Processes components in string sorted order. Sorts out orphaned, generated components directories.
func CheckComponentCache
func CheckComponentCache(cache *kr8_cache.DeploymentCache, compSpec kr8_types.Kr8ComponentSpec, config string, componentName string, baseDir string, logger zerolog.Logger) (bool, *kr8_cache.ComponentCache, error)
Compares a component's current state to a cache entry. Returns an up-to-date cache entry for the component. If the cache pointer is nil or cache invalid, a fresh cache entry will be generated to return.
func CheckIfUpdateNeeded
func CheckIfUpdateNeeded(outFile string, outStr string) (bool, error)
Check if a file needs updating based on its current contents and potential new contents.
func CleanOutputDir
func CleanOutputDir(outputFileMap map[string]bool, componentOutputDir string) error
Removes all files not present in outputFileMap from componentOutputDir. checks if each file in the directory is present in the map, ignoring the bool value.
func CleanupOldComponentDirs
func CleanupOldComponentDirs(existingComponents []string, clusterComponents map[string]gjson.Result, kr8Spec *kr8_types.Kr8ClusterSpec, logger zerolog.Logger)
Go through each item in existingComponents and remove the file if it isn't in clusterComponents. Skips removing `.kr8_cache` files.
func CompileClusterConfiguration
func CompileClusterConfiguration(clusterName, clusterDir string, kr8Opts types.Kr8Opts, vmConfig types.VMConfig, generateDirOverride string, logger zerolog.Logger) (*kr8_types.Kr8ClusterSpec, map[string]gjson.Result, error)
Build the list of parameter files to combine for the final cluster config by walking folder tree leaf to root.
func CreateClusterGenerateDirs
func CreateClusterGenerateDirs(kr8Spec kr8_types.Kr8ClusterSpec) ([]string, error)
Create the root cluster output directory. Returns a list of cluster component output directories that already existed.
func GatherClusterConfig
func GatherClusterConfig(clusterName, clusterDir string, kr8Opts types.Kr8Opts, vmConfig types.VMConfig, generateDirOverride string, filters util.PathFilterOptions, clusterParamsFile string, noop bool, logger zerolog.Logger) (*kr8_types.Kr8ClusterSpec, []string, string, error)
Compiles configuration for each cluster. Creates and cleans output directories for generated cluster components. Uses the filter to determine which components to process. Renders the cluster-level configuration for each component.
func GenProcessCluster
func GenProcessCluster(clusterConfig *GenerateProcessRootConfig, pool *ants.Pool, logger zerolog.Logger) error
The root function for generating a cluster. Prepares and builds the cluster config. Build and processes the list of components.
func GenProcessComponent
func GenProcessComponent(vmConfig types.VMConfig, componentName string, kr8Spec kr8_types.Kr8ClusterSpec, kr8Opts types.Kr8Opts, config string, allConfig *SafeString, filters util.PathFilterOptions, paramsFile string, cache *kr8_cache.DeploymentCache, logger zerolog.Logger) (bool, *kr8_cache.ComponentCache, error)
Root function for processing a kr8+ component. Processes a component through a jsonnet VM to generate output files.
func GenerateIncludesFiles
func GenerateIncludesFiles(includesFiles []kr8_types.Kr8ComponentSpecIncludeObject, kr8Spec kr8_types.Kr8ClusterSpec, kr8Opts types.Kr8Opts, config string, componentName string, compPath string, componentOutputDir string, jvm *jsonnet.VM, logger zerolog.Logger) (map[string]bool, error)
Generates the list of includes files for a component. Processes each includes file using the component's config. Returns an error if there's an issue with ANY includes file.
func GetAllClusterParams
func GetAllClusterParams(clusterDir string, vmConfig types.VMConfig, jvm *jsonnet.VM, logger zerolog.Logger) error
Combine all the cluster params into a single object indexed by cluster name.
func GetClusterComponentParamsThreadSafe
func GetClusterComponentParamsThreadSafe(allConfig *SafeString, config string, vmConfig types.VMConfig, kr8Spec kr8_types.Kr8ClusterSpec, filters util.PathFilterOptions, paramsFile string, jvm *jsonnet.VM, logger zerolog.Logger) error
Include full render of all component params for cluster. Only do this if we have not already cached it and don't already have it stored.
func GetClusterParams
func GetClusterParams(clusterDir string, vmConfig types.VMConfig, logger zerolog.Logger) (map[string]string, error)
Given a base directory, generates cluster-level configuration for each cluster found. Gets list of clusters from `util.GetClusterFilenames(clusterDir)`.
func GetComponentFiles
func GetComponentFiles(compSpec kr8_types.Kr8ComponentSpec) []string
func GetComponentPath
func GetComponentPath(config string, componentName string) string
Fetch a component path from raw cluster config.
func LoadClusterCache
func LoadClusterCache(kr8Spec *kr8_types.Kr8ClusterSpec, logger zerolog.Logger) (*kr8_cache.DeploymentCache, string)
Loads cluster cache based on a cluster spec. If cache is disabled, a nil deployment cache pointer is returned.
func ProcessComponentFinalizer
func ProcessComponentFinalizer(compSpec kr8_types.Kr8ComponentSpec, componentOutputDir string, outputFileMap map[string]bool) error
Final actions performed once a component is generated. Cleans extra files from output dir if not disabled in component spec.
func ProcessFile
func ProcessFile(inputFile string, outputFile string, kr8Spec kr8_types.Kr8ClusterSpec, componentName string, config string, incInfo kr8_types.Kr8ComponentSpecIncludeObject, jvm *jsonnet.VM, logger zerolog.Logger) (string, error)
Process an includes file. Based on the extension, the file is processed differently.
- .jsonnet: Imported and processed using jsonnet VM.
- .yml, .yaml: Imported and processed through native function ParseYaml.
- .tpl, .tmpl: Processed using component config and Sprig templating.
func ProcessJsonnetToYaml
func ProcessJsonnetToYaml(jvm *jsonnet.VM, input string, snippetFilename string) (string, error)
Processes an input string through the jsonnet VM and handles extracting the output into a yaml string. snippetFilename is used for error messages.
func ProcessTemplate
func ProcessTemplate(filename string, data gjson.Result) (string, error)
Processes a template file with the given data. Loads file, parses template, then executes template.
func RenderComponents
func RenderComponents(config string, vmConfig types.VMConfig, kr8Spec kr8_types.Kr8ClusterSpec, cache *kr8_cache.DeploymentCache, compList []string, clusterParamsFile string, pool *ants.Pool, kr8Opts types.Kr8Opts, filters util.PathFilterOptions, logger zerolog.Logger) (map[string]kr8_cache.ComponentCache, error)
Renders a list of components with a given Kr8ClusterSpec configuration. Each component is added to a sync.WaitGroup to be processed by the ants.Pool. Returns the cache results for all successfully generated components.
func SetupBaseComponentJvm
func SetupBaseComponentJvm(vmconfig types.VMConfig, config string, kr8Spec kr8_types.Kr8ClusterSpec) (*jsonnet.VM, error)
This function sets up the JVM for a given component. It sets up post-processing, and prunes parameters as required. It's faster to create this VM for each component, rather than re-use. Default postprocessor just copies input to output.
func SetupComponentVM
func SetupComponentVM(vmConfig types.VMConfig, config string, kr8Spec kr8_types.Kr8ClusterSpec, componentName string, compSpec kr8_types.Kr8ComponentSpec, allConfig *SafeString, filters util.PathFilterOptions, paramsFile string, kr8Opts types.Kr8Opts, logger zerolog.Logger) (*jsonnet.VM, string, error)
Setup and configures a jsonnet VM for processing kr8+ resources. Creates a new VM and does the following:
- loads cluster and component config
- loads jsonnet library files
- loads external file references
func SetupJvmForComponent
func SetupJvmForComponent(jvm *jsonnet.VM, config string, kr8Spec kr8_types.Kr8ClusterSpec, componentName string)
This function sets up component-specific external code in the JVM. It makes the component config available to the jvm under the `kr8` extVar.
func ValidateOrCreateCache
func ValidateOrCreateCache(cache *kr8_cache.DeploymentCache, config string, logger zerolog.Logger) *kr8_cache.DeploymentCache
For provided config, validates the cache object matches. If the cache is valid, it is returned. If cache is not valid, an empty deployment cache returned.
type GenerateProcessRootConfig
type GenerateProcessRootConfig struct {
ClusterName string
ClusterDir string
BaseDir string
GenerateDir string
Kr8Opts types.Kr8Opts
ClusterParamsFile string
Filters util.PathFilterOptions
VmConfig types.VMConfig
Noop bool
}
type SafeString
A thread-safe string that can be used to store and retrieve configuration data.
type SafeString struct {
// contains filtered or unexported fields
}