Skip to content

Commit

Permalink
lab 12 part2
Browse files Browse the repository at this point in the history
  • Loading branch information
smasIner committed Nov 16, 2024
1 parent acc7c83 commit 4b78144
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 11 deletions.
5 changes: 4 additions & 1 deletion app_python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,8 @@ Sets up Docker service.
Builds a Docker image tagged as DOCKER_USERNAME/app_python:latest. - utilising GitHub Secrets
Pushes the Docker image to Docker Hub using secrets for authentication.

All steps are chained with needs:
## Compose:

docker-compose build

docker-compose up
31 changes: 30 additions & 1 deletion app_python/app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
from flask import Flask
from datetime import datetime
import pytz
import os

app = Flask(__name__)

# In-memory storage for metric data
view_metric = {}
buy_metric = {}

# File to store the visit counter
VISIT_FILE = 'visits/visits.txt'

# Initialize the counter from the file (if it exists)
counter = 0
if os.path.exists(VISIT_FILE):
with open(VISIT_FILE, 'r') as file:
counter = int(file.read().strip())


# Function to get Moscow time in seconds since epoch
def get_moscow_time_seconds():
Expand All @@ -16,11 +26,20 @@ def get_moscow_time_seconds():
return int(moscow_time.timestamp())


# Update the visit counter and save it to the file
def update_visit_count():
global counter
counter += 1
with open(VISIT_FILE, 'w') as file:
file.write(str(counter))


# Root route to display Moscow time
@app.route('/')
def show_time():
moscow_time = datetime.fromtimestamp(get_moscow_time_seconds())
formatted_time = moscow_time.strftime('%Y-%m-%d %H:%M:%S')
update_visit_count() # Increment visit counter on each access
return f"<h1>Moscow Time: {formatted_time}</h1>"


Expand All @@ -41,5 +60,15 @@ def metrics():
return metrics_output, 200, {'Content-Type': 'text/plain'}


# Visits route to display the visit count
@app.route('/visits')
def visits():
return f'Number of accessed times: {counter}'


if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)
# Ensure the visits directory exists
if not os.path.exists('visits'):
os.makedirs('visits')

app.run(debug=True, host='0.0.0.0', port=8080)
9 changes: 9 additions & 0 deletions app_python/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3'

services:
app_python:
build: .
ports:
- "8080:8080"
volumes:
- ./visits:/app/visits
1 change: 1 addition & 0 deletions app_python/visits/visits.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6
8 changes: 8 additions & 0 deletions k8s/12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Part 2
```bash
(venv) smasiner@smasIners-MacBook-Pro k8s % kubectl get po
NAME READY STATUS RESTARTS AGE
helmapp-7dfd8bd68-lp6m2 1/1 Running 0 113s
(venv) smasiner@smasIners-MacBook-Pro k8s % kubectl exec helmapp-7dfd8bd68-lp6m2 -- cat /config.json
{"abra" : "kadabra"}%
```
1 change: 1 addition & 0 deletions k8s/helmapp/files/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"abra" : "kadabra"}
7 changes: 7 additions & 0 deletions k8s/helmapp/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: config-configmap
data:
config.json: |-
{{ .Files.Get "files/config.json" | indent 4}}
3 changes: 3 additions & 0 deletions k8s/helmapp/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ spec:
volumeMounts:
{{- toYaml . | nindent 12 }}
{{- end }}
envFrom:
- configMapRef:
name: env-configmap
env:
- name: MY_PASS
valueFrom:
Expand Down
6 changes: 6 additions & 0 deletions k8s/helmapp/templates/envconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: env-configmap
data:
MY_SECRET: "a"
7 changes: 0 additions & 7 deletions k8s/helmapp/templates/patch-inject-secrets.yaml

This file was deleted.

14 changes: 12 additions & 2 deletions k8s/helmapp/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,24 @@ autoscaling:
# targetMemoryUtilizationPercentage: 80

# Additional volumes on the output Deployment definition.
volumes: []
volumes:
- name: config
configMap:
name: config-configmap
# - name: foo
# secret:
# secretName: mysecret
# optional: false

mySecret: "abraabra"

# Additional volumeMounts on the output Deployment definition.
volumeMounts: []
volumeMounts:
- name: config
mountPath: "/config.json"
subPath: config.json
readOnly: true

# - name: foo
# mountPath: "/etc/foo"
# readOnly: true
Expand Down

0 comments on commit 4b78144

Please sign in to comment.