fix(app): use CBR and correct sa
This commit is contained in:
11
README.md
11
README.md
@@ -62,8 +62,8 @@ docker push <your-ecr-repo>:latest
|
|||||||
Apply RBAC and deployment configurations:
|
Apply RBAC and deployment configurations:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
kubectl apply -f app/example/k8s/rbac.yaml
|
kubectl apply -f examples/k8s/rbac.yaml
|
||||||
kubectl apply -f app/example/k8s/deployment.yaml
|
kubectl apply -f examples/k8s/deployment.yaml
|
||||||
```
|
```
|
||||||
|
|
||||||
### Access the Service
|
### Access the Service
|
||||||
@@ -80,6 +80,13 @@ Test the API:
|
|||||||
curl http://<EXTERNAL-IP>/k8s/data
|
curl http://<EXTERNAL-IP>/k8s/data
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Kubernetes Integration
|
||||||
|
|
||||||
|
The `/k8s/data` endpoint retrieves information about:
|
||||||
|
|
||||||
|
- Nodes: CPU, memory, and allocatable pods.
|
||||||
|
- Namespaces: List of all namespaces in the cluster.
|
||||||
|
|
||||||
## Endpoints
|
## Endpoints
|
||||||
|
|
||||||
| Method | Endpoint | Description |
|
| Method | Endpoint | Description |
|
||||||
|
|||||||
@@ -45,26 +45,32 @@ def display_virtual_machines():
|
|||||||
|
|
||||||
def display_kubernetes_nodes():
|
def display_kubernetes_nodes():
|
||||||
console = Console()
|
console = Console()
|
||||||
|
config.load_incluster_config()
|
||||||
|
v1 = client.CoreV1Api()
|
||||||
|
|
||||||
|
# Nodes table
|
||||||
table = Table(title="Kubernetes Nodes")
|
table = Table(title="Kubernetes Nodes")
|
||||||
table.add_column("ID", justify="right")
|
|
||||||
table.add_column("Cluster Name")
|
|
||||||
table.add_column("Node Name")
|
table.add_column("Node Name")
|
||||||
table.add_column("CPU", justify="right")
|
table.add_column("CPU", justify="right")
|
||||||
table.add_column("Memory")
|
table.add_column("Memory", justify="right")
|
||||||
table.add_column("Storage")
|
table.add_column("Pods Allocatable", justify="right")
|
||||||
table.add_column("Type")
|
|
||||||
table.add_column("Namespaces")
|
|
||||||
|
|
||||||
nodes = fetch_all("kubernetes_nodes")
|
nodes = v1.list_node()
|
||||||
for node in nodes:
|
for node in nodes.items:
|
||||||
table.add_row(
|
table.add_row(
|
||||||
str(node[0]), node[1], node[2],
|
node.metadata.name,
|
||||||
str(node[3]), node[4], node[5],
|
node.status.capacity.get("cpu"),
|
||||||
node[6], node[7]
|
node.status.capacity.get("memory"),
|
||||||
|
node.status.allocatable.get("pods")
|
||||||
)
|
)
|
||||||
|
|
||||||
console.print(table)
|
console.print(table)
|
||||||
|
|
||||||
|
# Namespaces
|
||||||
|
console.print("\n[bold]Namespaces:[/bold]")
|
||||||
|
namespaces = [ns.metadata.name for ns in v1.list_namespace().items]
|
||||||
|
console.print(", ".join(namespaces))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
display_metal_nodes()
|
display_metal_nodes()
|
||||||
display_virtual_machines()
|
display_virtual_machines()
|
||||||
|
display_kubernetes_nodes()
|
||||||
|
|||||||
@@ -1,19 +1,38 @@
|
|||||||
# Export data in YAML or JSON format
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from database import fetch_all
|
from database import fetch_all
|
||||||
import yaml
|
import yaml
|
||||||
import json
|
import json
|
||||||
|
from kubernetes import client, config
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
def fetch_k8s_data():
|
||||||
|
config.load_incluster_config()
|
||||||
|
v1 = client.CoreV1Api()
|
||||||
|
# Nodes
|
||||||
|
nodes = v1.list_node()
|
||||||
|
node_data = [{
|
||||||
|
"node_name": node.metadata.name,
|
||||||
|
"cpu": node.status.capacity.get("cpu"),
|
||||||
|
"memory": node.status.capacity.get("memory"),
|
||||||
|
"pods_allocatable": node.status.allocatable.get("pods")
|
||||||
|
} for node in nodes.items]
|
||||||
|
|
||||||
|
# Namespaces
|
||||||
|
namespaces = [ns.metadata.name for ns in v1.list_namespace().items]
|
||||||
|
|
||||||
|
return {"nodes": node_data, "namespaces": namespaces}
|
||||||
|
|
||||||
@router.get("/export")
|
@router.get("/export")
|
||||||
def export_data(format: str = "yaml"):
|
def export_data(format: str = "yaml"):
|
||||||
|
# Fetch database and Kubernetes data
|
||||||
data = {
|
data = {
|
||||||
"metal_nodes": fetch_all("metal_nodes"),
|
"metal_nodes": fetch_all("metal_nodes"),
|
||||||
"virtual_machines": fetch_all("virtual_machines"),
|
"virtual_machines": fetch_all("virtual_machines"),
|
||||||
"kubernetes_nodes": fetch_all("kubernetes_nodes"),
|
"kubernetes": fetch_k8s_data(),
|
||||||
}
|
}
|
||||||
# Return data in the requested format
|
|
||||||
|
# Return in the requested format
|
||||||
if format.lower() == "yaml":
|
if format.lower() == "yaml":
|
||||||
return yaml.safe_dump(data)
|
return yaml.safe_dump(data)
|
||||||
return json.dumps(data, indent=2)
|
return json.dumps(data, indent=2)
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ spec:
|
|||||||
labels:
|
labels:
|
||||||
app: metalcheck
|
app: metalcheck
|
||||||
spec:
|
spec:
|
||||||
|
serviceAccountName: metalcheck-sa
|
||||||
containers:
|
containers:
|
||||||
- name: backend
|
- name: backend
|
||||||
image: <your-ecr-repo>:latest
|
image: <your-ecr-repo>:latest
|
||||||
@@ -29,6 +30,7 @@ apiVersion: v1
|
|||||||
kind: Service
|
kind: Service
|
||||||
metadata:
|
metadata:
|
||||||
name: metalcheck-backend
|
name: metalcheck-backend
|
||||||
|
namespace: metalcheck
|
||||||
spec:
|
spec:
|
||||||
selector:
|
selector:
|
||||||
app: metalcheck
|
app: metalcheck
|
||||||
|
|||||||
@@ -5,25 +5,23 @@ metadata:
|
|||||||
namespace: metalcheck
|
namespace: metalcheck
|
||||||
---
|
---
|
||||||
apiVersion: rbac.authorization.k8s.io/v1
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
kind: Role
|
kind: ClusterRole
|
||||||
metadata:
|
metadata:
|
||||||
namespace: metalcheck
|
name: metalcheck-clusterrole
|
||||||
name: metalcheck-role
|
|
||||||
rules:
|
rules:
|
||||||
- apiGroups: [""]
|
- apiGroups: [""]
|
||||||
resources: ["pods", "nodes", "namespaces"]
|
resources: ["pods", "nodes", "namespaces"]
|
||||||
verbs: ["get", "list", "watch"]
|
verbs: ["get", "list", "watch"]
|
||||||
---
|
---
|
||||||
apiVersion: rbac.authorization.k8s.io/v1
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
kind: RoleBinding
|
kind: ClusterRoleBinding
|
||||||
metadata:
|
metadata:
|
||||||
name: metalcheck-rolebinding
|
name: metalcheck-clusterrolebinding
|
||||||
namespace: metalcheck
|
|
||||||
subjects:
|
subjects:
|
||||||
- kind: ServiceAccount
|
- kind: ServiceAccount
|
||||||
name: metalcheck-sa
|
name: metalcheck-sa
|
||||||
namespace: metalcheck
|
namespace: metalcheck
|
||||||
roleRef:
|
roleRef:
|
||||||
kind: Role
|
kind: ClusterRole
|
||||||
name: metalcheck-role
|
name: metalcheck-clusterrole
|
||||||
apiGroup: rbac.authorization.k8s.io
|
apiGroup: rbac.authorization.k8s.io
|
||||||
|
|||||||
Reference in New Issue
Block a user