diff --git a/.github/workflows/build-and-push.yaml b/.github/workflows/build-and-push.yaml index 5e54c8d0..3223d417 100644 --- a/.github/workflows/build-and-push.yaml +++ b/.github/workflows/build-and-push.yaml @@ -3,7 +3,7 @@ name: Build and push images on: push: branches: - - develop + - k8s-manifest jobs: docker-build: diff --git a/api/Dockerfile b/api/Dockerfile index fbda8fb9..fa690ab6 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -8,7 +8,7 @@ RUN pip install --no-cache-dir --upgrade -r /requirements.txt COPY ./api/main.py main.py EXPOSE 8080 -CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"] +# CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"] # If running behind a proxy like Nginx or Traefik add --proxy-headers -# CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--proxy-headers"] +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080", "--proxy-headers"] diff --git a/api/main.py b/api/main.py index ea325f51..2a147b33 100644 --- a/api/main.py +++ b/api/main.py @@ -10,12 +10,15 @@ # Set base URLs based on the environment if env == "prod": - REAL_FAKE_SENSORS_BASE_URL = "http://real_fake_sensors:8081" - SENSATIONAL_SENSORS_BASE_URL = "http://sensational_sensors:8082" + REAL_FAKE_SENSORS_BASE_URL = "http://real-fake-service:8081" + SENSATIONAL_SENSORS_BASE_URL = "http://sensational-sensor-service:8082" else: REAL_FAKE_SENSORS_BASE_URL = "http://localhost:8081" SENSATIONAL_SENSORS_BASE_URL = "http://localhost:8082" +REAL_FAKE_SENSORS_BASE_URL = "http://real-fake-service:8081" +SENSATIONAL_SENSORS_BASE_URL = "http://sensational-sensor-service:8082" + app = FastAPI() # whitelist @@ -27,9 +30,11 @@ "http://0.0.0.0:8082", ] +origin = ["*"] + app.add_middleware( CORSMiddleware, - allow_origins=origins, + allow_origins=origin, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], @@ -38,7 +43,6 @@ class SensorType(Enum): LIQUID_BIN_LEVEL = "liquid bin level" - SOLID_BIN_LEVEL = "solid bin level" class BasicSensor(BaseModel): @@ -105,7 +109,7 @@ class SensationalSensor(BaseModel): def sensational_sensor_to_simple_sensor(sensor: SensationalSensor) -> BasicSensor: return BasicSensor( id=sensor["id"], - sensor_type=SensorType.SOLID_BIN_LEVEL, + sensor_type=SensorType.LIQUID_BIN_LEVEL, fill_level=sensor["fill_level"] if sensor["fill_level"] else None, sim=sensor["sim"], lat=sensor["lat"], @@ -131,12 +135,12 @@ def get_sensors(): real_fake_sensors = requests.get(REAL_FAKE_SENSORS_BASE_URL + "/sensors").json() rfs_as_simple_sensor_list = rfs_list_to_bs_list(real_fake_sensors["sensors"]) - sensational_sensors = requests.get(SENSATIONAL_SENSORS_BASE_URL + "/sensors").json() - ss_as_simple_sensor_list = sensational_sensor_list_to_simple_sensor_list( - sensational_sensors["sensors"] - ) + # sensational_sensors = requests.get(SENSATIONAL_SENSORS_BASE_URL + "/sensors").json() + # ss_as_simple_sensor_list = sensational_sensor_list_to_simple_sensor_list( + # sensational_sensors["sensors"] + # ) - all_sensors_list = rfs_as_simple_sensor_list + ss_as_simple_sensor_list + all_sensors_list = rfs_as_simple_sensor_list #+ ss_as_simple_sensor_list return {"total": len(all_sensors_list), "sensors": all_sensors_list} diff --git a/app/pages/index.tsx b/app/pages/index.tsx index 52ba74d9..5281307c 100644 --- a/app/pages/index.tsx +++ b/app/pages/index.tsx @@ -21,7 +21,7 @@ export interface Sensor { address_line2: string; } -const API_URL = process.env.NEXT_PUBLIC_API_HOST || "http://localhost:8080"; +const API_URL = process.env.NEXT_PUBLIC_API_HOST || "http://real.iot.local"; const Home: NextPage = () => { const Map = useMemo( diff --git a/deploy/manifest/api.yaml b/deploy/manifest/api.yaml new file mode 100644 index 00000000..a92d982b --- /dev/null +++ b/deploy/manifest/api.yaml @@ -0,0 +1,42 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: api-app + labels: + app: iotapp +spec: + replicas: 1 + selector: + matchLabels: + app: api-app + template: + metadata: + labels: + app: api-app + spec: + imagePullSecrets: + - name: ghcr-secret + containers: + - name: api-app + image: ghcr.io/button-inc/iot-system-prototype/api:latest + ports: + - containerPort: 8080 + resources: + requests: + memory: "16Mi" + cpu: "40m" + limits: + memory: "128Mi" + cpu: "100m" +--- +apiVersion: v1 +kind: Service +metadata: + name: api-app-service +spec: + selector: + app: api-app + ports: + - protocol: TCP + port: 8080 + targetPort: 8080 diff --git a/deploy/manifest/app.yaml b/deploy/manifest/app.yaml new file mode 100644 index 00000000..c88c3603 --- /dev/null +++ b/deploy/manifest/app.yaml @@ -0,0 +1,42 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: custom-app + labels: + app: iotapp +spec: + replicas: 1 + selector: + matchLabels: + app: custom-app + template: + metadata: + labels: + app: custom-app + spec: + imagePullSecrets: + - name: ghcr-secret + containers: + - name: custom-app + image: ghcr.io/button-inc/iot-system-prototype/app:latest + ports: + - containerPort: 3000 + resources: + requests: + memory: "16Mi" + cpu: "40m" + limits: + memory: "128Mi" + cpu: "100m" +--- +apiVersion: v1 +kind: Service +metadata: + name: custom-app-service +spec: + selector: + app: custom-app + ports: + - protocol: TCP + port: 3000 + targetPort: 3000 diff --git a/deploy/manifest/iot-system-ingress.yaml b/deploy/manifest/iot-system-ingress.yaml new file mode 100644 index 00000000..f1be2f9f --- /dev/null +++ b/deploy/manifest/iot-system-ingress.yaml @@ -0,0 +1,23 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: iot-ingress +spec: + rules: + - host: iot.local + http: + paths: + - path: /sensors + pathType: Prefix + backend: + service: + name: api-app-service + port: + number: 8080 + - path: / + pathType: Prefix + backend: + service: + name: custom-app-service + port: + number: 3000 diff --git a/deploy/manifest/iot-system/.helmignore b/deploy/manifest/iot-system/.helmignore new file mode 100644 index 00000000..0e8a0eb3 --- /dev/null +++ b/deploy/manifest/iot-system/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/deploy/manifest/iot-system/Chart.yaml b/deploy/manifest/iot-system/Chart.yaml new file mode 100644 index 00000000..8d13db9b --- /dev/null +++ b/deploy/manifest/iot-system/Chart.yaml @@ -0,0 +1,13 @@ +apiVersion: v2 +name: iot-system +description: A Helm chart for leveraging IoT sensor data infrastructure in the cloud. +keywords: + - iot + - sensors + - helm + +type: application + +version: 1.0.0 + +appVersion: "1.0.0" diff --git a/deploy/manifest/iot-system/templates/api.yaml b/deploy/manifest/iot-system/templates/api.yaml new file mode 100644 index 00000000..57e69d9c --- /dev/null +++ b/deploy/manifest/iot-system/templates/api.yaml @@ -0,0 +1,49 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ .Values.deployApi.name }} + namespace: {{ .Values.deployApi.namespace }} + labels: + app: iotapp +spec: + replicas: {{ .Values.deployApi.replicas }} + selector: + matchLabels: + app: {{ .Values.deployApi.name }} + template: + metadata: + labels: + app: {{ .Values.deployApi.name }} + spec: + imagePullSecrets: + - name: ghcr-secret + containers: + - name: {{ .Values.deployApi.name }} + image: "{{ .Values.deployApi.image.name }}:{{ .Values.deployApi.image.tag }}" + ports: + - containerPort: {{ .Values.deployApi.port }} + env: + - name: ENV + valueFrom: + configMapKeyRef: + name: iot-configmap + key: environment + resources: + requests: + memory: {{ .Values.deployApi.resources.requests.memory }} + cpu: {{ .Values.deployApi.resources.requests.cpu }} + limits: + memory: {{ .Values.deployApi.resources.limits.memory }} + cpu: {{ .Values.deployApi.resources.limits.cpu }} +--- +apiVersion: v1 +kind: Service +metadata: + name: {{ .Values.serviceApi.name }} +spec: + selector: + app: {{ .Values.deployApi.name }} + ports: + - protocol: TCP + port: {{ .Values.serviceApi.port }} + targetPort: {{ .Values.serviceApi.targetPort }} diff --git a/deploy/manifest/iot-system/templates/app.yaml b/deploy/manifest/iot-system/templates/app.yaml new file mode 100644 index 00000000..81841733 --- /dev/null +++ b/deploy/manifest/iot-system/templates/app.yaml @@ -0,0 +1,59 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ .Values.deployApp.name }} + namespace: {{ .Values.deployApp.namespace }} + labels: + app: iotapp +spec: + replicas: {{ .Values.deployApp.replicas }} + selector: + matchLabels: + app: {{ .Values.deployApp.name }} + template: + metadata: + labels: + app: {{ .Values.deployApp.name }} + spec: + imagePullSecrets: + - name: ghcr-secret + containers: + - name: {{ .Values.deployApp.name }} + image: "{{ .Values.deployApp.image.name }}:{{ .Values.deployApp.image.tag }}" + ports: + - containerPort: {{ .Values.deployApp.port }} + env: + - name: API + valueFrom: + configMapKeyRef: + name: iot-configmap + key: api + - name: APP_URL + valueFrom: + configMapKeyRef: + name: iot-configmap + key: next_public_api_host + - name: API_PORT + valueFrom: + configMapKeyRef: + name: iot-configmap + key: api_port + resources: + requests: + memory: {{ .Values.deployApp.resources.requests.memory }} + cpu: {{ .Values.deployApp.resources.requests.cpu }} + limits: + memory: {{ .Values.deployApp.resources.limits.memory }} + cpu: {{ .Values.deployApp.resources.limits.cpu }} +--- +apiVersion: v1 +kind: Service +metadata: + name: {{ .Values.serviceApp.name }} +spec: + selector: + app: {{ .Values.deployApp.name }} + ports: + - protocol: TCP + port: {{ .Values.serviceApp.port }} + targetPort: {{ .Values.serviceApp.targetPort }} diff --git a/deploy/manifest/iot-system/templates/iot-configmap.yaml b/deploy/manifest/iot-system/templates/iot-configmap.yaml new file mode 100644 index 00000000..0c08deaa --- /dev/null +++ b/deploy/manifest/iot-system/templates/iot-configmap.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: iot-configmap +data: + api: iot.local + api_port: "8080" + environment: prod + next_public_api_host: http://real.iot.local/sensors diff --git a/deploy/manifest/iot-system/templates/iot-system-ingress.yaml b/deploy/manifest/iot-system/templates/iot-system-ingress.yaml new file mode 100644 index 00000000..f1be2f9f --- /dev/null +++ b/deploy/manifest/iot-system/templates/iot-system-ingress.yaml @@ -0,0 +1,23 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: iot-ingress +spec: + rules: + - host: iot.local + http: + paths: + - path: /sensors + pathType: Prefix + backend: + service: + name: api-app-service + port: + number: 8080 + - path: / + pathType: Prefix + backend: + service: + name: custom-app-service + port: + number: 3000 diff --git a/deploy/manifest/iot-system/templates/real-fake.yaml b/deploy/manifest/iot-system/templates/real-fake.yaml new file mode 100644 index 00000000..173a634a --- /dev/null +++ b/deploy/manifest/iot-system/templates/real-fake.yaml @@ -0,0 +1,43 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ .Values.deployReal.name }} + namespace: {{ .Values.deployReal.namespace }} + labels: + app: iotapp +spec: + replicas: {{ .Values.deployReal.replicas }} + selector: + matchLabels: + app: {{ .Values.deployReal.name }} + template: + metadata: + labels: + app: {{ .Values.deployReal.name }} + spec: + imagePullSecrets: + - name: ghcr-secret + containers: + - name: {{ .Values.deployReal.name }} + image: "{{ .Values.deployReal.image.name }}:{{ .Values.deployReal.image.tag }}" + ports: + - containerPort: {{ .Values.deployReal.port }} + resources: + requests: + memory: {{ .Values.deployReal.resources.requests.memory }} + cpu: {{ .Values.deployReal.resources.requests.cpu }} + limits: + memory: {{ .Values.deployReal.resources.limits.memory }} + cpu: {{ .Values.deployReal.resources.limits.cpu }} +--- +apiVersion: v1 +kind: Service +metadata: + name: {{ .Values.serviceReal.name }} +spec: + selector: + app: {{ .Values.deployReal.name }} + ports: + - protocol: TCP + port: {{ .Values.serviceReal.port }} + targetPort: {{ .Values.serviceReal.targetPort }} diff --git a/deploy/manifest/iot-system/templates/sensational-sensors.yaml b/deploy/manifest/iot-system/templates/sensational-sensors.yaml new file mode 100644 index 00000000..a02aedf6 --- /dev/null +++ b/deploy/manifest/iot-system/templates/sensational-sensors.yaml @@ -0,0 +1,43 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ .Values.deploySensational.name }} + namespace: {{ .Values.deploySensational.namespace }} + labels: + app: iotapp +spec: + replicas: {{ .Values.deploySensational.replicas }} + selector: + matchLabels: + app: {{ .Values.deploySensational.name }} + template: + metadata: + labels: + app: {{ .Values.deploySensational.name }} + spec: + imagePullSecrets: + - name: ghcr-secret + containers: + - name: {{ .Values.deploySensational.name }} + image: "{{ .Values.deploySensational.image.name }}:{{ .Values.deploySensational.image.tag }}" + ports: + - containerPort: {{ .Values.deploySensational.port }} + resources: + requests: + memory: {{ .Values.deploySensational.resources.requests.memory }} + cpu: {{ .Values.deploySensational.resources.requests.cpu }} + limits: + memory: {{ .Values.deploySensational.resources.limits.memory }} + cpu: {{ .Values.deploySensational.resources.limits.cpu }} +--- +apiVersion: v1 +kind: Service +metadata: + name: {{ .Values.serviceSensational.name }} +spec: + selector: + app: {{ .Values.deploySensational.name }} + ports: + - protocol: TCP + port: {{ .Values.serviceSensational.port }} + targetPort: {{ .Values.serviceSensational.targetPort }} diff --git a/deploy/manifest/iot-system/value.yaml b/deploy/manifest/iot-system/value.yaml new file mode 100644 index 00000000..4144ebd8 --- /dev/null +++ b/deploy/manifest/iot-system/value.yaml @@ -0,0 +1,81 @@ +deployApi: + name: api-app + namespace: default + port: 8080 + replicas: 1 + image: + name: ghcr.io/button-inc/iot-system-prototype/api + tag: latest + resources: + requests: + memory: "16Mi" + cpu: "40m" + limits: + memory: "128Mi" + cpu: "100m" +serviceApi: + name: api-app-service + port: 8080 + targetPort: 8080 + +deployApp: + name: custom-app + namespace: default + port: 3000 + replicas: 1 + image: + name: ghcr.io/button-inc/iot-system-prototype/app + tag: latest + env: + nextapihost: http://real.iot.local/sensors + resources: + requests: + memory: "14Mi" + cpu: "40m" + limits: + memory: "128Mi" + cpu: "100m" +serviceApp: + name: custom-app-service + port: 3000 + targetPort: 3000 + +deployReal: + name: real-fake + namespace: default + port: 8081 + replicas: 1 + image: + name: ghcr.io/button-inc/iot-system-prototype/real_fake_sensors + tag: latest + resources: + requests: + memory: "16Mi" + cpu: "40m" + limits: + memory: "128Mi" + cpu: "100m" +serviceReal: + name: real-fake-service + port: 8081 + targetPort: 8081 + +deploySensational: + name: sensational-sensors + namespace: default + port: 8082 + replicas: 1 + image: + name: ghcr.io/button-inc/iot-system-prototype/sensational_sensors + tag: latest + resources: + requests: + memory: "16Mi" + cpu: "40m" + limits: + memory: "128Mi" + cpu: "100m" +serviceSensational: + name: sensational-sensor-service + port: 8082 + targetPort: 8082 diff --git a/deploy/manifest/real-fake.yaml b/deploy/manifest/real-fake.yaml new file mode 100644 index 00000000..3cd518b1 --- /dev/null +++ b/deploy/manifest/real-fake.yaml @@ -0,0 +1,42 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: real-fake + labels: + app: iotapp +spec: + replicas: 1 + selector: + matchLabels: + app: real-fake + template: + metadata: + labels: + app: real-fake + spec: + imagePullSecrets: + - name: ghcr-secret + containers: + - name: real-fake + image: ghcr.io/button-inc/iot-system-prototype/real_fake_sensors:latest + ports: + - containerPort: 8081 + resources: + requests: + memory: "16Mi" + cpu: "40m" + limits: + memory: "128Mi" + cpu: "100m" +--- +apiVersion: v1 +kind: Service +metadata: + name: real-fake-service +spec: + selector: + app: real-fake + ports: + - protocol: TCP + port: 8081 + targetPort: 8081 diff --git a/deploy/manifest/sensational-sensors.yaml b/deploy/manifest/sensational-sensors.yaml new file mode 100644 index 00000000..406131d4 --- /dev/null +++ b/deploy/manifest/sensational-sensors.yaml @@ -0,0 +1,42 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: sensational-sensors + labels: + app: iotapp +spec: + replicas: 1 + selector: + matchLabels: + app: sensational-sensors + template: + metadata: + labels: + app: sensational-sensors + spec: + imagePullSecrets: + - name: ghcr-secret + containers: + - name: sensational-sensors + image: ghcr.io/button-inc/iot-system-prototype/sensational_sensors:latest + ports: + - containerPort: 8082 + resources: + requests: + memory: "16Mi" + cpu: "40m" + limits: + memory: "128Mi" + cpu: "100m" +--- +apiVersion: v1 +kind: Service +metadata: + name: sensational-sensor-service +spec: + selector: + app: sensational-sensors + ports: + - protocol: TCP + port: 8082 + targetPort: 8082 diff --git a/real_fake_sensors/Dockerfile b/real_fake_sensors/Dockerfile index 376ef2a0..2e6ca114 100644 --- a/real_fake_sensors/Dockerfile +++ b/real_fake_sensors/Dockerfile @@ -8,7 +8,8 @@ RUN pip install --no-cache-dir --upgrade -r /requirements.txt COPY ./real_fake_sensors/main.py main.py EXPOSE 8081 -CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8081"] +# CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8081"] # If running behind a proxy like Nginx or Traefik add --proxy-headers -# CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--proxy-headers"] +# CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80", "--proxy-headers"] +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8081", "--proxy-headers"] diff --git a/real_fake_sensors/main.py b/real_fake_sensors/main.py index 8bab97fb..6b028d71 100644 --- a/real_fake_sensors/main.py +++ b/real_fake_sensors/main.py @@ -8,10 +8,11 @@ # whitelist origins = ["http://localhost:8080", "api:8080"] +origin = ["*"] app.add_middleware( CORSMiddleware, - allow_origins=origins, + allow_origins=origin, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], diff --git a/sensational_sensors/Dockerfile b/sensational_sensors/Dockerfile index 903c0778..260bc26c 100644 --- a/sensational_sensors/Dockerfile +++ b/sensational_sensors/Dockerfile @@ -11,4 +11,4 @@ EXPOSE 8082 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8082"] # If running behind a proxy like Nginx or Traefik add --proxy-headers -# CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--proxy-headers"] +# CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80", "--proxy-headers"] diff --git a/sensational_sensors/main.py b/sensational_sensors/main.py index 635e8bb2..a92057d9 100644 --- a/sensational_sensors/main.py +++ b/sensational_sensors/main.py @@ -11,10 +11,11 @@ "http://localhost:8080", "http://0.0.0.0:8081", ] +origin = ["*"] app.add_middleware( CORSMiddleware, - allow_origins=origins, + allow_origins=origin, allow_credentials=True, allow_methods=["*"], allow_headers=["*"],