changeset 15:6ed25bcaaf1f

add prefect and rebuild flow to k8s
author drewp@bigasterisk.com
date Fri, 19 Jul 2024 00:30:47 -0700
parents b9c2b7fedbcd
children 7a87ba2f00d9
files Dockerfile.server env k8s/deploy.yaml k8s/ingress.yaml k8s/service.yaml k8s/volumes.yaml scobot/index/access.py scobot/index/build_index_flow.py scobot/service/query.py
diffstat 9 files changed, 80 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/Dockerfile.server	Thu Jul 11 22:11:41 2024 -0700
+++ b/Dockerfile.server	Fri Jul 19 00:30:47 2024 -0700
@@ -1,6 +1,9 @@
 FROM reg:5000/base_basic
 
 WORKDIR /opt
+RUN useradd --uid 1020 --gid nogroup --create-home scobot
+RUN chown 1020 -R /opt /tmp/cache
+USER scobot
 
 COPY pyproject.toml pdm.lock ./
 RUN pdm sync
--- a/env	Thu Jul 11 22:11:41 2024 -0700
+++ b/env	Fri Jul 19 00:30:47 2024 -0700
@@ -1,3 +1,4 @@
 PREFECT_API_URL=http://dash:4200/api
-PREFECT_HOME=./prefect
-PREFECT_SERVER_API_HOST=0.0.0.0
\ No newline at end of file
+PREFECT_HOME=./data/prefect
+PREFECT_LOCAL_STORAGE_PATH=./data/prefect/storage
+PREFECT_SERVER_API_HOST=0.0.0.0
--- a/k8s/deploy.yaml	Thu Jul 11 22:11:41 2024 -0700
+++ b/k8s/deploy.yaml	Fri Jul 19 00:30:47 2024 -0700
@@ -26,8 +26,11 @@
             - vite
           ports:
             - containerPort: 8002
-        - name: server
+              name: web
+
+        - name: search-server
           image: reg:5000/sco_bot_server
+          securityContext: { runAsUser: 1020 }
           workingDir: /opt
           command:
             - pdm
@@ -41,9 +44,42 @@
             - scobot
           ports:
             - containerPort: 8001
+              name: server
           volumeMounts:
           - name: sco-bot-data
             mountPath: /opt/data
+
+        - name: prefect
+          image: reg:5000/sco_bot_server
+          securityContext: { runAsUser: 1020 }
+          workingDir: /opt
+          env:
+            - name: PREFECT_API_URL
+              value: https://sco-bot-prefect.bigasterisk.com/api
+          command:
+            - pdm
+            - run_prefect_server
+          ports:
+            - containerPort: 4200
+              name: prefect
+          volumeMounts:
+          - name: sco-bot-data
+            mountPath: /opt/data
+
+        - name: build-flow
+          image: reg:5000/sco_bot_server
+          securityContext: { runAsUser: 1020 }
+          workingDir: /opt
+          env:
+            - name: PREFECT_API_URL
+              value: http://localhost:4200/api
+          command:
+            - pdm
+            - run_build_flow
+          volumeMounts:
+          - name: sco-bot-data
+            mountPath: /opt/data
+
       affinity:
         nodeAffinity:
           requiredDuringSchedulingIgnoredDuringExecution:
--- a/k8s/ingress.yaml	Thu Jul 11 22:11:41 2024 -0700
+++ b/k8s/ingress.yaml	Fri Jul 19 00:30:47 2024 -0700
@@ -16,7 +16,36 @@
         paths:
           - pathType: Prefix
             path: /sco/
-            backend: { service: { name: sco-bot, port: { number: 80 } } }
+            backend: { service: { name: sco-bot, port: { name: web } } }
   tls:
     - hosts: [bigasterisk.com]
       secretName: bigasterisk.com-tls
+---
+apiVersion: networking.k8s.io/v1
+kind: Ingress
+metadata:
+  name: sco-bot-prefect
+  annotations:
+    cert-manager.io/cluster-issuer: letsencrypt-prod
+    ingress.pomerium.io/allow_public_unauthenticated_access: "false"
+    ingress.pomerium.io/pass_identity_headers: "true"
+    ingress.pomerium.io/preserve_host_header: "true"
+    ingress.pomerium.io/allow_websockets: "true"
+    ingress.pomerium.io/policy: |
+      allow:
+        or: 
+          - { email: { is: "drewpca@gmail.com" }}
+          - { email: { is: "besustainable@gmail.com" }}
+spec:
+  ingressClassName: pomerium
+  rules:
+    - host: "sco-bot-prefect.bigasterisk.com"
+      http:
+        paths:
+          - pathType: Prefix
+            # url sub path is broken of course, see https://github.com/PrefectHQ/prefect/issues/12096
+            path: /
+            backend: { service: { name: sco-bot, port: { name: prefect } } }
+  tls:
+    - hosts: [sco-bot-prefect.bigasterisk.com]
+      secretName: sco-bot-prefect.bigasterisk.com-tls
--- a/k8s/service.yaml	Thu Jul 11 22:11:41 2024 -0700
+++ b/k8s/service.yaml	Fri Jul 19 00:30:47 2024 -0700
@@ -4,6 +4,7 @@
   name: sco-bot
 spec:
   ports:
-    - { port: 80, targetPort: 8002 }
+    - { port: 80, name: web, targetPort: web }
+    - { port: 4200, name: prefect, targetPort: prefect }
   selector:
     app: sco-bot
--- a/k8s/volumes.yaml	Thu Jul 11 22:11:41 2024 -0700
+++ b/k8s/volumes.yaml	Fri Jul 19 00:30:47 2024 -0700
@@ -8,7 +8,7 @@
   storageClassName: manual
   hostPath: {path: "/my/serv/sco-bot/data"}
   capacity: {storage: 5Mi}
-  accessModes: ["ReadWriteOnce"]
+  accessModes: ["ReadWriteMany"]
   persistentVolumeReclaimPolicy: Retain
   claimRef: {namespace: default, name: sco-bot-data}
 ---
@@ -18,5 +18,5 @@
 spec:
   storageClassName: ""
   volumeName: "sco-bot-data"
-  accessModes: ["ReadWriteOnce"]
+  accessModes: ["ReadWriteMany"]
   resources: { requests: { storage: 5Mi } }
\ No newline at end of file
--- a/scobot/index/access.py	Thu Jul 11 22:11:41 2024 -0700
+++ b/scobot/index/access.py	Fri Jul 19 00:30:47 2024 -0700
@@ -12,7 +12,7 @@
 
     def __init__(self, indexDir: Path, delete_existing=True):
         if delete_existing:
-            shutil.rmtree(indexDir)
+            shutil.rmtree(indexDir, ignore_errors=True)
             indexDir.mkdir(parents=True, exist_ok=True)
             self.ix = create_in(indexDir, schema)
         else:
--- a/scobot/index/build_index_flow.py	Thu Jul 11 22:11:41 2024 -0700
+++ b/scobot/index/build_index_flow.py	Fri Jul 19 00:30:47 2024 -0700
@@ -91,7 +91,7 @@
     log = get_run_logger()
     scobot.index.access.log = log
 
-    index = SearchIndex(Path('data/index'))
+    index = SearchIndex(Path('data/build/index0'))
     for url in meetingListUrls():
         mtgs = cast(list[MeetingRow], getCityMutableJson(url))
         log.info(f'got {len(mtgs)=}')
--- a/scobot/service/query.py	Thu Jul 11 22:11:41 2024 -0700
+++ b/scobot/service/query.py	Fri Jul 19 00:30:47 2024 -0700
@@ -48,7 +48,7 @@
 
 @asynccontextmanager
 async def lifespan(app: FastAPI):
-    app.state.index = SearchIndexRO('data/index')
+    app.state.index = SearchIndexRO('data/build/index0')
     yield
 
 app = FastAPI(lifespan=lifespan)