-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ServiceType dataclass, healthcheck is now more flexiable
- Loading branch information
1 parent
638c476
commit 5146f1c
Showing
7 changed files
with
222 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,51 @@ | ||
import dataclasses | ||
|
||
from typing import Union, Optional | ||
from typing import Optional, Union | ||
from imports import k8s | ||
from enum import Enum | ||
|
||
|
||
@dataclasses.dataclass | ||
class Probe: | ||
port: Union[str, int] | ||
port: Union[int, str] | ||
path: str | ||
period_seconds: int | ||
failure_threshold: int | ||
timeout_seconds: int | ||
|
||
@dataclasses.dataclass | ||
class HealthCheck: | ||
startup_probe: Probe | ||
readiness_probe: Probe | ||
liveness_probe: Probe | ||
|
||
def __post_init__(self): | ||
self.get() | ||
assert not isinstance(self.port, (bool)), \ | ||
"Port must be of type int or str, not bool." | ||
|
||
def to_k8s_probe(self) -> k8s.Probe: | ||
k8s_port = ( | ||
k8s.IntOrString.from_string(self.port) | ||
if isinstance(self.port, str) | ||
else k8s.IntOrString.from_number(self.port) | ||
) | ||
k8s_http_get = k8s.HttpGetAction(port=k8s_port, path=self.path) | ||
return k8s.Probe( | ||
http_get=k8s_http_get, | ||
period_seconds=self.period_seconds, | ||
failure_threshold=self.failure_threshold, | ||
timeout_seconds=self.timeout_seconds, | ||
) | ||
|
||
def _create_port(self, port: Union[str, int]): | ||
return k8s.IntOrString.from_string(port) if isinstance(port, str) else k8s.IntOrString.from_number(port) | ||
|
||
def _create_http_get_action(self, probe: Probe): | ||
return k8s.HttpGetAction(port=self._create_port(probe.port), path=probe.path) | ||
@dataclasses.dataclass | ||
class HealthCheck: | ||
startup_probe: Optional[k8s.Probe] = None | ||
readiness_probe: Optional[k8s.Probe] = None | ||
liveness_probe: Optional[k8s.Probe] = None | ||
|
||
def __init__(self, startup_probe: Optional[Probe] = None, readiness_probe: Optional[Probe] = None, liveness_probe: Optional[Probe] = None): | ||
self.startup_probe = startup_probe.to_k8s_probe() if startup_probe is not None else None | ||
self.readiness_probe = readiness_probe.to_k8s_probe() if readiness_probe is not None else None | ||
self.liveness_probe = liveness_probe.to_k8s_probe() if liveness_probe is not None else None | ||
|
||
def _create_k8s_probe(self, probe: Probe): | ||
return k8s.Probe( | ||
http_get=self._create_http_get_action(probe), | ||
period_seconds=probe.period_seconds, | ||
failure_threshold=probe.failure_threshold, | ||
timeout_seconds=probe.timeout_seconds | ||
) | ||
|
||
def get(self): | ||
self.startup_probe = self._create_k8s_probe(self.startup_probe) | ||
self.readiness_probe = self._create_k8s_probe(self.readiness_probe) | ||
self.liveness_probe = self._create_k8s_probe(self.liveness_probe) | ||
@dataclasses.dataclass | ||
class ServiceType(Enum): | ||
CLUSTER_IP = "ClusterIP" | ||
LOAD_BALANCER = "LoadBalancer" | ||
NODE_PORT = "NodePort" |
Oops, something went wrong.