outposts: update kubernetes controller to use pk as identifier instead of name

This commit is contained in:
Jens Langhammer 2020-10-19 17:39:12 +02:00
parent d8c522233e
commit 774b9c8a61
5 changed files with 24 additions and 25 deletions

View file

@ -1,5 +1,5 @@
"""Kubernetes Deployment Reconciler""" """Kubernetes Deployment Reconciler"""
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Dict
from kubernetes.client import ( from kubernetes.client import (
AppsV1Api, AppsV1Api,
@ -19,7 +19,6 @@ from kubernetes.client import (
from passbook import __version__ from passbook import __version__
from passbook.outposts.controllers.k8s.base import ( from passbook.outposts.controllers.k8s.base import (
KubernetesObjectReconciler, KubernetesObjectReconciler,
NeedsRecreate,
NeedsUpdate, NeedsUpdate,
) )
from passbook.outposts.models import Outpost from passbook.outposts.models import Outpost
@ -42,11 +41,9 @@ class DeploymentReconciler(KubernetesObjectReconciler[V1Deployment]):
@property @property
def name(self) -> str: def name(self) -> str:
return f"passbook-outpost-{self.outpost.name}" return f"passbook-outpost-{self.controller.outpost.uuid.hex}"
def reconcile(self, current: V1Deployment, reference: V1Deployment): def reconcile(self, current: V1Deployment, reference: V1Deployment):
if current.spec.selector.match_labels != reference.spec.selector.match_labels:
raise NeedsRecreate()
if current.spec.replicas != reference.spec.replicas: if current.spec.replicas != reference.spec.replicas:
raise NeedsUpdate() raise NeedsUpdate()
if ( if (
@ -55,6 +52,14 @@ class DeploymentReconciler(KubernetesObjectReconciler[V1Deployment]):
): ):
raise NeedsUpdate() raise NeedsUpdate()
def get_pod_meta(self) -> Dict[str, str]:
"""Get common object metadata"""
return {
"app.kubernetes.io/name": "passbook-outpost",
"app.kubernetes.io/managed-by": "passbook.beryju.org",
"passbook.beryju.org/outpost-uuid": self.controller.outpost.uuid.hex,
}
def get_reference_object(self) -> V1Deployment: def get_reference_object(self) -> V1Deployment:
"""Get deployment object for outpost""" """Get deployment object for outpost"""
# Generate V1ContainerPort objects # Generate V1ContainerPort objects
@ -62,13 +67,14 @@ class DeploymentReconciler(KubernetesObjectReconciler[V1Deployment]):
for port_name, port in self.controller.deployment_ports.items(): for port_name, port in self.controller.deployment_ports.items():
container_ports.append(V1ContainerPort(container_port=port, name=port_name)) container_ports.append(V1ContainerPort(container_port=port, name=port_name))
meta = self.get_object_meta(name=self.name) meta = self.get_object_meta(name=self.name)
secret_name = f"passbook-outpost-{self.controller.outpost.uuid.hex}-api"
return V1Deployment( return V1Deployment(
metadata=meta, metadata=meta,
spec=V1DeploymentSpec( spec=V1DeploymentSpec(
replicas=self.outpost.config.kubernetes_replicas, replicas=self.outpost.config.kubernetes_replicas,
selector=V1LabelSelector(match_labels=meta.labels), selector=V1LabelSelector(match_labels=self.get_pod_meta()),
template=V1PodTemplateSpec( template=V1PodTemplateSpec(
metadata=V1ObjectMeta(labels=meta.labels), metadata=V1ObjectMeta(labels=self.get_pod_meta()),
spec=V1PodSpec( spec=V1PodSpec(
containers=[ containers=[
V1Container( V1Container(
@ -80,7 +86,7 @@ class DeploymentReconciler(KubernetesObjectReconciler[V1Deployment]):
name="PASSBOOK_HOST", name="PASSBOOK_HOST",
value_from=V1EnvVarSource( value_from=V1EnvVarSource(
secret_key_ref=V1SecretKeySelector( secret_key_ref=V1SecretKeySelector(
name=f"passbook-outpost-{self.outpost.name}-api", name=secret_name,
key="passbook_host", key="passbook_host",
) )
), ),
@ -89,7 +95,7 @@ class DeploymentReconciler(KubernetesObjectReconciler[V1Deployment]):
name="PASSBOOK_TOKEN", name="PASSBOOK_TOKEN",
value_from=V1EnvVarSource( value_from=V1EnvVarSource(
secret_key_ref=V1SecretKeySelector( secret_key_ref=V1SecretKeySelector(
name=f"passbook-outpost-{self.outpost.name}-api", name=secret_name,
key="token", key="token",
) )
), ),
@ -98,7 +104,7 @@ class DeploymentReconciler(KubernetesObjectReconciler[V1Deployment]):
name="PASSBOOK_INSECURE", name="PASSBOOK_INSECURE",
value_from=V1EnvVarSource( value_from=V1EnvVarSource(
secret_key_ref=V1SecretKeySelector( secret_key_ref=V1SecretKeySelector(
name=f"passbook-outpost-{self.outpost.name}-api", name=secret_name,
key="passbook_host_insecure", key="passbook_host_insecure",
) )
), ),
@ -120,9 +126,7 @@ class DeploymentReconciler(KubernetesObjectReconciler[V1Deployment]):
) )
def retrieve(self) -> V1Deployment: def retrieve(self) -> V1Deployment:
return self.api.read_namespaced_deployment( return self.api.read_namespaced_deployment(self.name, self.namespace)
f"passbook-outpost-{self.outpost.name}", self.namespace
)
def update(self, current: V1Deployment, reference: V1Deployment): def update(self, current: V1Deployment, reference: V1Deployment):
return self.api.patch_namespaced_deployment( return self.api.patch_namespaced_deployment(

View file

@ -27,7 +27,7 @@ class SecretReconciler(KubernetesObjectReconciler[V1Secret]):
@property @property
def name(self) -> str: def name(self) -> str:
return f"passbook-outpost-{self.controller.outpost.name}-api" return f"passbook-outpost-{self.controller.outpost.uuid.hex}-api"
def reconcile(self, current: V1Secret, reference: V1Secret): def reconcile(self, current: V1Secret, reference: V1Secret):
for key in reference.data.keys(): for key in reference.data.keys():
@ -59,9 +59,7 @@ class SecretReconciler(KubernetesObjectReconciler[V1Secret]):
) )
def retrieve(self) -> V1Secret: def retrieve(self) -> V1Secret:
return self.api.read_namespaced_secret( return self.api.read_namespaced_secret(self.name, self.namespace)
f"passbook-outpost-{self.controller.outpost.name}-api", self.namespace
)
def update(self, current: V1Secret, reference: V1Secret): def update(self, current: V1Secret, reference: V1Secret):
return self.api.patch_namespaced_secret( return self.api.patch_namespaced_secret(

View file

@ -21,7 +21,7 @@ class ServiceReconciler(KubernetesObjectReconciler[V1Service]):
@property @property
def name(self) -> str: def name(self) -> str:
return f"passbook-outpost-{self.controller.outpost.name}" return f"passbook-outpost-{self.controller.outpost.uuid.hex}"
def reconcile(self, current: V1Service, reference: V1Service): def reconcile(self, current: V1Service, reference: V1Service):
if len(current.spec.ports) != len(reference.spec.ports): if len(current.spec.ports) != len(reference.spec.ports):
@ -50,9 +50,7 @@ class ServiceReconciler(KubernetesObjectReconciler[V1Service]):
) )
def retrieve(self) -> V1Service: def retrieve(self) -> V1Service:
return self.api.read_namespaced_service( return self.api.read_namespaced_service(self.name, self.namespace)
f"passbook-outpost-{self.controller.outpost.name}", self.namespace
)
def update(self, current: V1Service, reference: V1Service): def update(self, current: V1Service, reference: V1Service):
return self.api.patch_namespaced_service( return self.api.patch_namespaced_service(

View file

@ -34,7 +34,7 @@ class IngressReconciler(KubernetesObjectReconciler[NetworkingV1beta1Ingress]):
@property @property
def name(self) -> str: def name(self) -> str:
return f"passbook-outpost-{self.controller.outpost.name}" return f"passbook-outpost-{self.controller.outpost.uuid.hex}"
def reconcile( def reconcile(
self, current: NetworkingV1beta1Ingress, reference: NetworkingV1beta1Ingress self, current: NetworkingV1beta1Ingress, reference: NetworkingV1beta1Ingress
@ -117,9 +117,7 @@ class IngressReconciler(KubernetesObjectReconciler[NetworkingV1beta1Ingress]):
) )
def retrieve(self) -> NetworkingV1beta1Ingress: def retrieve(self) -> NetworkingV1beta1Ingress:
return self.api.read_namespaced_ingress( return self.api.read_namespaced_ingress(self.name, self.namespace)
f"passbook-outpost-{self.controller.outpost.name}", self.namespace
)
def update( def update(
self, current: NetworkingV1beta1Ingress, reference: NetworkingV1beta1Ingress self, current: NetworkingV1beta1Ingress, reference: NetworkingV1beta1Ingress

View file

@ -404,6 +404,7 @@ _LOGGING_HANDLER_MAP = {
"websockets": "WARNING", "websockets": "WARNING",
"daphne": "WARNING", "daphne": "WARNING",
"dbbackup": "ERROR", "dbbackup": "ERROR",
"kubernetes": "INFO",
} }
for handler_name, level in _LOGGING_HANDLER_MAP.items(): for handler_name, level in _LOGGING_HANDLER_MAP.items():
# pyright: reportGeneralTypeIssues=false # pyright: reportGeneralTypeIssues=false