Skip to content

Example code

GenSimpleFishGenerator

  1. Create a new file named fish.py 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:

    @kgenlib.register_generator(
        path="kapicorp.simple_fish_generator",
    )
    class GenSimpleFishGenerator(KubernetesResource):
      api_version = "fish/v1"
      kind = "Fish"
    
      def body(self):
        super().body()
        logger.info(f"Running {__name__} with id = {self.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:

    ...
    @kgenlib.register_generator(
        path="kapicorp.simple_fish_generator",
    )
    ...
    self.root.spec.family = self.config.get("family", None)

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