Kustomize

repository·master·Indexed 11 days ago

https://github.com/kubernetes-sigs/kustomize

A tool for customizing raw, template-free YAML files for Kubernetes. It uses a base and overlay pattern to manage multiple environment configurations (such as dev, staging, and prod) by applying patches, name prefixes, and common labels to a common base without modifying original files.

Tokens
115.6K
Snippets
422
Records
548
Agent score
94%

What's inside Kustomize

  1. What is kustomize

    master
    kustomize is a tool for customizing raw, template-free YAML files for Kubernetes. It allows you to modify Kubernetes API objects using patches and other customization rules without altering the original source YAML files. This makes it possible to maintain a clean 'base' configuration while generating environment-specific variants.
  2. Overview of Kustomize Go Modules

    master

    The Kustomize repository is organized into several semantically versioned Go modules:

    ModulePurpose
    apiHigh-level Kustomize code suitable for import by other programs (e.g., kubectl). Contains krusty, filters, loader, and resmap
    kustomizeThe main CLI entry point containing main.go and Cobra command implementations
    kyamlA Kubernetes-focused enhancement of go-yaml used for low-level YAML manipulation and RNode handling
    cmd/configProvides Unix-like file manipulation commands (like grep and tree) for Kubernetes YAML files
  3. What is Kustomize and how does it work?

    master

    Kustomize is a tool for customizing Kubernetes resource configurations without using templates or Domain Specific Languages (DSLs). It works by taking raw, template-free YAML files and applying customizations to them while leaving the original files untouched.

    Key characteristics:

    • Template-free: You work with standard Kubernetes API objects rather than complex templating engines.
    • Patch-based: It uses patches to introduce environment-specific changes to existing standard configuration files.
    • Declarative: Like make, what Kustomize does is declared in a configuration file.
    • Transformation-oriented: Like sed, it emits edited text (YAML) based on the input and instructions.
  4. The Kustomize build lifecycle and ordering

    master

    Kustomize processes a kustomization file in a specific sequence of stages. Understanding this order is critical for predicting how resources are modified:

    1. Process resources: Kustomize reads the YAML files or hydrates recursive kustomizations to create the initial list of input objects.
    2. Process generators: Kustomize executes generators, adding the newly created KRM objects to the resource list.
    3. Process transformers: Kustomize applies transformers to modify, add, remove, or reorder the current list of objects.
    4. Process validators: Kustomize runs validators to check the final list for errors. Validators can fail the build but are intended to be non-mutating.
  5. How Validator Plugins work in Kustomize

    master

    A Validator Plugin is a specialized plugin type used to validate the resulting YAML output of a Kustomize build. Unlike transformers, validators are intended to inspect resources rather than modify them.

    Key Assumptions and Constraints

    • Input/Output: Resources are passed to the plugin via stdin. The plugin must write the validated resources to stdout. If no changes are needed, the plugin can print nothing to stdout.
    • Configuration: The plugin's configuration file is passed as the first argument.
    • Working Directory: The plugin's working directory is the kustomization directory where it is being invoked.
    • Modification Restrictions: Validators cannot modify the input YAML content. The only permitted modification is adding a label named validated-by (case-sensitive) to the top-level resources. Any other modifications will cause Kustomize to throw an error.
    • Failure Signaling: A non-zero exit code from the plugin indicates a validation failure. Kustomize will treat this as a build error and report the stderr output.

    You can implement validators using either an exec plugin (e.g., a bash script) or a Go plugin.

  6. Understand the `kustomize localize` command

    master
    The kustomize localize command is designed to download all remote resources referenced in a kustomization file and save them into a local directory. This allows a kustomization layer to become self-contained, making it possible to version control a fully localized directory without relying on external network calls during kustomize build.
  7. Perform partial string replacements with delimiter and index

    master

    You can perform partial string replacements by splitting a field value and replacing a specific segment.

    Use the options object within source or targets to configure this:

    • delimiter: The string used to split the field.
    • index: The position in the split array to replace (default is 0).

    Behavioral Notes:

    • In a Source: If the index is out of bounds, Kustomize throws an error.
    • In a Target: If the index is less than 0, the value is prefixed. If the index is beyond the length of the split, the value is suffixed.
    # If source value is 'path/to/VALUE'
    options:
      delimiter: '/'
      index: 2
    # Resulting target value: 'path/to/NEW_VALUE'
  8. Use inline patches in kustomization files

    master

    Since version 3.2.0, Kustomize supports 'inline patches', allowing you to define patch content directly within the kustomization.yaml file as a single string using the |- YAML block scalar. This eliminates the need to manage separate patch files for simple modifications.

    There are three primary ways to apply patches in a kustomization.yaml file:

    1. patchesStrategicMerge: A list of patches parsed as Strategic Merge Patches.
    2. patchesJSON6902: A list of JSON Patches that must include an associated target. Each patch can only be applied to one target resource.
    3. patches: A flexible list of patches and their associated targets. Kustomize automatically detects whether the provided patch is a Strategic Merge Patch or a JSON Patch. This method allows a single patch to be applied to multiple objects.
  9. How globbing works in Kustomize

    master

    Kustomize does not support globbing (e.g., *.yaml) directly inside kustomization files during kustomize build. This prevents the configuration from depending on the local file system state in an undeclared way.

    Instead, you should use kustomize edit commands that accept globbed arguments. These commands expand the globs at edit time relative to your local file system and write the resulting explicit filenames into the kustomization file. This preserves the requirement that all resources are explicitly declared in version control.

  10. Use patchStrategicMerge for Kubernetes resources

    master

    A patchStrategicMerge (SMP) is a strategic-merge-style patch that uses an incomplete YAML specification of a Kubernetes resource. It includes TypeMeta fields (group, version, kind, name) to identify the target resource, followed by the specific fields you wish to change (e.g., an image tag).

    Key Behaviors:

    • Default Behavior: By default, an SMP replaces values. This is ideal for simple strings but may not be the intended behavior for lists.
    • Directives: You can change the default replacement behavior by adding a directive to the YAML patch. Supported directives are:
      • replace (the default)
      • delete
    • Custom Resources: For custom resources, SMPs are treated as JSON Merge Patches.
    • Flexibility: Any valid resource file can be used as an SMP; it will overwrite matching fields in a target resource with the same group/version/kind/name while leaving all other fields untouched.
  11. Kustomization file field change policy

    master

    Kustomize manages changes to the kustomization.yaml schema using the following rules:

    • Meaning: A field's semantic meaning cannot be changed.
    • Deprecation: A field may be marked as deprecated. This triggers a minor (semver) version bump in the Go API. The tool will provide a migration path via a non-fatal error message.
    • Removal: A field may be removed. This triggers a major (semver) version bump in the Go API. If an encounter with a removed field occurs, the tool will return a fatal error.