Skip to content

Example code


  1. Create a new file named in the directory system/generators/kubernetes/ and add the following content:

    import logging
    logger = logging.getLogger(__name__)
    from .common import KubernetesResource, kgenlib
  2. Register the generator classes with Kapitan using the @kgenlib.register_generator annotation. Here's an example:

    class GenSimpleFishGenerator(KubernetesResource):
      api_version = "fish/v1"
      kind = "Fish"
      def body(self):
        super().body()"Running {__name__} with id = {} and config = {self.config}")

    When Kapitan runs, it matches dictionary items with the specified path, then invokes your GenSimpleFishGenerator class.

  3. Execute Kapitan to see the generated output:

    ./kapitan compile -t tutorial

  4. Inspect the generated files:

    git status compiled

Enhancing the Resource

Utilize the self.config variable to enrich the fish object:

    ... = self.config.get("family", None)

This addition will populate the family attribute of the fish under the spec field.