Skip to content

Commit

Permalink
Import fixes for generators (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
ademariag authored May 1, 2023
1 parent ab0e61e commit 370049f
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 109 deletions.
2 changes: 1 addition & 1 deletion compiled/tesoro/manifests/tesoro-bundle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ metadata:
labels:
app.kapicorp.dev/component: tesoro
name: tesoro-metrics
name: tesoro-metrics
name: tesoro
namespace: tesoro
spec:
endpoints:
Expand Down
2 changes: 1 addition & 1 deletion compiled/vault/manifests/vault-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
app.kubernetes.io/component: go
app.kubernetes.io/version: 1.7.3
name: vault-internal
name: vault-internal
name: vault
namespace: vault
spec:
clusterIP: None
Expand Down
60 changes: 42 additions & 18 deletions components/generators/kubernetes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from typing import Any

from kapitan.inputs.helm import HelmChart
from kapitan.inputs.kadet import (
BaseModel,
BaseObj,
Expand All @@ -10,14 +11,15 @@
load_from_search_paths,
)

kgenlib = load_from_search_paths("generators")

from .common import KubernetesResource, ResourceType
from .networking import NetworkPolicy
from .rbac import ClusterRole, ClusterRoleBinding, Role, RoleBinding
from .storage import ConfigMap, Secret

logger = logging.getLogger(__name__)

kgenlib = load_from_search_paths("generators")

inv = inventory(lazy=True)

Expand Down Expand Up @@ -189,6 +191,17 @@ def add_volumes_for_object(self, object):
)


@kgenlib.register_generator(path="generators.kubernetes.service_accounts")
class ServiceAccountGenerator(kgenlib.BaseStore):
def body(self):
config = self.config
name = config.get("name", self.name)
sa = ServiceAccount(name=name, config=config)
sa.add_annotations(config.annotations)
sa.add_labels(config.labels)
self.add(sa)


class ServiceAccount(KubernetesResource):
resource_type = ResourceType(
kind="ServiceAccount", api_version="v1", id="service_account"
Expand Down Expand Up @@ -952,21 +965,6 @@ def body(self):
}


def generate_docs(input_params):
obj = BaseObj()
template = input_params.get("template_path", None)
if template:
for name, component in get_components():
obj.root["{}-readme.md".format(name)] = kgenlib.render_jinja(
template,
{
"service_component": component.to_dict(),
"inventory": inv.parameters.to_dict(),
},
)
return obj


@kgenlib.register_generator(path="ingresses")
class IngressComponent(kgenlib.BaseStore):
name: str
Expand Down Expand Up @@ -1147,9 +1145,35 @@ def body(self):
self.add(backend_config)


class MyHelmChart(HelmChart):
def new(self):
for obj in self.load_chart():
if obj:
self.root[
f"{obj['metadata']['name'].lower()}-{obj['kind'].lower().replace(':','-')}"
] = BaseObj.from_dict(obj)


@kgenlib.register_generator(path="charts")
class HelmChartGenerator(kgenlib.BaseStore):
name: str
config: Any

def body(self):
helm_config = self.config.to_dict()
chart_name = self.config.helm_params.name

rendered_chart = MyHelmChart(**helm_config)

for helm_resource in rendered_chart.root.values():
resource = KubernetesResource.from_baseobj(helm_resource)
resource.add_label("app.kapicorp.dev/component", chart_name)
self.add(resource)


def main(input_params):
kgenlib.BaseGenerator.inventory = inventory
store = kgenlib.BaseGenerator.generate()
generator = kgenlib.BaseGenerator(inventory=inv)
store = generator.generate()
store.process_mutations(input_params.get("mutations", {}))

return store.dump()
38 changes: 34 additions & 4 deletions components/generators/kubernetes/common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from kapitan.inputs.kadet import BaseModel, load_from_search_paths
import logging

from kapitan.inputs.kadet import BaseModel, BaseObj, load_from_search_paths

kgenlib = load_from_search_paths("generators")

logger = logging.getLogger(__name__)


class ResourceType(BaseModel):
kind: str
Expand All @@ -12,26 +16,46 @@ class ResourceType(BaseModel):
class KubernetesResource(kgenlib.BaseContent):
resource_type: ResourceType
name: str
namespace: str = None
config: dict = None
api_version: str = None
kind: str = None
rendered_name: str = None
id: str = None

@classmethod
def from_baseobj(cls, baseobj: BaseObj):
"""Return a BaseContent initialised with baseobj."""

kind = baseobj.root.kind
api_version = baseobj.root.apiVersion
id = kind.lower()

resource_type = ResourceType(kind=kind, api_version=api_version, id=id)
resource = cls(resource_type=resource_type, name=baseobj.root.metadata.name)
resource.root = baseobj.root
return resource

@property
def component_name(self):
return self.root.metadata.labels.get("app.kapicorp.dev/component", self.name)
return self.get_label("app.kapicorp.dev/component") or self.name

def new(self):
self.kind = self.resource_type.kind
self.api_version = self.resource_type.api_version
self.id = self.resource_type.id
self.namespace = self.config.get("namespace", None)
if self.config:
if not self.namespace:
self.namespace = self.config.get("namespace", None)

if not self.rendered_name:
self.rendered_name = self.config.get("rendered_name", self.name)

def body(self):
self.root.apiVersion = self.api_version
self.root.kind = self.kind
self.name = self.name
self.rendered_name = self.config.get("rendered_name", self.name)

self.root.metadata.namespace = self.namespace
self.root.metadata.name = self.rendered_name
self.add_label("name", self.name)
Expand All @@ -43,9 +67,15 @@ def add_labels(self, labels: dict):
for key, value in labels.items():
self.add_label(key, value)

def get_label(self, key: str):
return self.root.metadata.labels.get(key, None)

def add_annotation(self, key: str, value: str):
self.root.metadata.annotations[key] = value

def get_annotation(self, key: str):
return self.root.metadata.annotations.get(key, None)

def add_annotations(self, annotations: dict):
for key, value in annotations.items():
self.add_annotation(key, value)
Expand Down
Loading

0 comments on commit 370049f

Please sign in to comment.