Skip to content

Commit

Permalink
Added ServiceType dataclass, healthcheck is now more flexiable
Browse files Browse the repository at this point in the history
  • Loading branch information
idan-starkware committed Nov 6, 2024
1 parent 638c476 commit 5146f1c
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 41 deletions.
1 change: 1 addition & 0 deletions deployments/sequencer/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ verify_ssl = true
cdk8s = "~=2.66.2"
constructs = "~=10.2.70"
jsonschema = "~=4.23.0"
mypy = "*"

[requires]
python_version = "3.10"
57 changes: 56 additions & 1 deletion deployments/sequencer/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deployments/sequencer/config/sequencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self):
super().__init__(
schema=json.loads(open(os.path.join(CONFIG_DIR, 'default_config.json'), 'r').read()),
config=json.loads(open(os.path.join(CONFIG_DIR, 'presets', 'sepolia_testnet.json'), 'r').read())
)
)

def validate(self):
jsonschema.validate(self.config, schema=self.schema)
18 changes: 13 additions & 5 deletions deployments/sequencer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

from constructs import Construct # type: ignore
from cdk8s import App, Chart # type: ignore
from typing import Dict, Any
from typing import Dict, Any, Optional

from services.service import Service
from config.sequencer import Config, SequencerDevConfig
from services.objects import Probe, HealthCheck
from services.objects import HealthCheck, ServiceType, Probe
from services import defaults


Expand All @@ -18,7 +18,7 @@ class SystemStructure:
replicas: str = "2"
size: str = "small"
config: Config = SequencerDevConfig()
health_check = True
health_check: Optional[HealthCheck] = None

def __post_init__(self):
self.config.validate()
Expand Down Expand Up @@ -53,8 +53,16 @@ def __init__(
self.sequencer_node = Service(
self,
"sequencer-node",
image="",
container_port=8082
image="",
container_port=8082,
replicas=1,
config=system_structure.config.get(),
service_type=ServiceType.CLUSTER_IP,
health_check=HealthCheck(
startup_probe=Probe(port="http", path="/monitoring/NodeVersion", period_seconds=10, failure_threshold=10, timeout_seconds=5),
readiness_probe=Probe(port="http", path="/monitoring/ready", period_seconds=10, failure_threshold=5, timeout_seconds=5),
liveness_probe=Probe(port="http", path="/monitoring/alive", period_seconds=10, failure_threshold=5, timeout_seconds=5)
)
)


Expand Down
111 changes: 109 additions & 2 deletions deployments/sequencer/references/sequencer-system.k8s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ spec:
targetPort: 8080
selector:
app: sequencer-system-mempool
type: LoadBalancer
---
apiVersion: v1
kind: ConfigMap
Expand All @@ -36,9 +35,30 @@ spec:
spec:
containers:
- image: paulbouwer/hello-kubernetes:1.7
livenessProbe:
failureThreshold: 10
httpGet:
path: /
port: http
periodSeconds: 5
timeoutSeconds: 5
name: web
ports:
- containerPort: 8080
readinessProbe:
failureThreshold: 10
httpGet:
path: /
port: http
periodSeconds: 5
timeoutSeconds: 5
startupProbe:
failureThreshold: 10
httpGet:
path: /
port: http
periodSeconds: 5
timeoutSeconds: 5
volumes:
- configMap:
name: sequencer-system-mempool-config
Expand All @@ -55,7 +75,6 @@ spec:
targetPort: 2368
selector:
app: sequencer-system-batcher
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
Expand All @@ -74,6 +93,94 @@ spec:
spec:
containers:
- image: ghost
livenessProbe:
failureThreshold: 10
httpGet:
path: /
port: http
periodSeconds: 5
timeoutSeconds: 5
name: web
ports:
- containerPort: 2368
readinessProbe:
failureThreshold: 10
httpGet:
path: /
port: http
periodSeconds: 5
timeoutSeconds: 5
startupProbe:
failureThreshold: 10
httpGet:
path: /
port: http
periodSeconds: 5
timeoutSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
name: sequencer-system-sequencer-node-service
namespace: test-namespace
spec:
ports:
- port: 80
targetPort: 8082
selector:
app: sequencer-system-sequencer-node
type: ClusterIP
---
apiVersion: v1
kind: ConfigMap
metadata:
name: sequencer-system-sequencer-node-config
namespace: test-namespace
data:
config: '{"chain_id": "SN_SEPOLIA", "starknet_url": "https://alpha-sepolia.starknet.io/", "base_layer.starknet_contract_address": "0xe2bb56ee936fd6433dc0f6e7e3b8365c906aa057"}'
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sequencer-system-sequencer-node-deployment
namespace: test-namespace
spec:
replicas: 1
selector:
matchLabels:
app: sequencer-system-sequencer-node
template:
metadata:
labels:
app: sequencer-system-sequencer-node
spec:
containers:
- image: ""
livenessProbe:
failureThreshold: 5
httpGet:
path: /monitoring/alive
port: http
periodSeconds: 10
timeoutSeconds: 5
name: web
ports:
- containerPort: 8082
readinessProbe:
failureThreshold: 5
httpGet:
path: /monitoring/ready
port: http
periodSeconds: 10
timeoutSeconds: 5
startupProbe:
failureThreshold: 10
httpGet:
path: /monitoring/NodeVersion
port: http
periodSeconds: 10
timeoutSeconds: 5
volumes:
- configMap:
name: sequencer-system-sequencer-node-config
name: sequencer-system-sequencer-node-config
58 changes: 34 additions & 24 deletions deployments/sequencer/services/objects.py
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"
Loading

0 comments on commit 5146f1c

Please sign in to comment.