Example code
GenSimpleFishGenerator
-
Create a new file named
fish.pyin the directorysystem/generators/kubernetes/and add the following content:import logging logger = logging.getLogger(__name__) from .common import KubernetesResource, kgenlib ... -
Register the generator classes with Kapitan using the
@kgenlib.register_generatorannotation. 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
GenSimpleFishGeneratorclass. -
Execute Kapitan to see the generated output:
./kapitan compile -t tutorial -
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.