Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module 14 #18

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,6 @@ override.tf.json
# Ignore CLI configuration files
.terraformrc
terraform.rc

#k8s
secrets.yaml
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,35 @@ LOG_LEVEL is set as an environmental Variable and can be edited to the below val
* WARNING
* ERROR



# MiniKube Set up
To configure a local minikube deployment of the todo-app. Assuming Docker desktop and Minikube are set up locally.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the phrasing here, because it looks a bit grammatically incorrect


### secret.yaml base format
there is a secrets_.yaml file supplied with the code - enter required values as specified and rename to secrets.yaml

### Build base Docker Image
`docker build --target production --tag todo-app:minikube.1 .`

## Kubernetes/Minikube Deployment
Load docker image to minikube cache
`minikube image load todo-app:minikube.1`
Start the deployments of services and files
`kubectl apply -f secrets.yaml`
`kubectl apply -f deployment.yaml`
`kubectl apply -f service.yaml`

start port forwarding of local ports to container
`kubectl port-forward service/todo-app 5000:5000`

The todoapp web site should now be accessible on localhost:5000 running from local minikube cluster







Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of whitespace here



49 changes: 49 additions & 0 deletions deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-app
spec:
selector:
matchLabels:
app: todo-app
replicas: 1
template:
metadata:
labels:
app: todo-app
spec:
containers:
- name: todo-app
image: todo-app:minikube.1
env:
- name: CLIENT_ID
valueFrom:
secretKeyRef:
name: my-secrets
key: CLIENT_ID
- name: CLIENT_SECRET
valueFrom:
secretKeyRef:
name: secrets
key: CLIENT_SECRET
- name: SECRET_KEY
valueFrom:
secretKeyRef:
name: secrets
key: SECRET_KEY
- name: CONNECTION_STRING
valueFrom:
secretKeyRef:
name: secrets
key: CONNECTION_STRING
- name: LOGGLY_TOKEN
valueFrom:
secretKeyRef:
name: secrets
key: LOGGLY_TOKEN
- name: PORT
value: '5000'
imagePullPolicy: Never
ports:
- containerPort: 5000
13 changes: 13 additions & 0 deletions secrets_.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#unpopulated secrets.yaml file. Populate and rename to secrets.yaml before deploying to minikube

Comment on lines +1 to +2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: the name could follow the convention similar to .env.template, in which case this would be secrets.yaml.template, making it more obvious that this isn't a yaml file that you should use

apiVersion: v1
kind: Secret
metadata:
name: secrets
type: Opaque
data:
CLIENT_ID: <github_client_id_here>
CLIENT_SECRET: <github_client secret here>
SECRET_KEY: <secretkey here>
CONNECTION_STRING: <connection string here to a mongodb>
#LOGGLY_TOKEN: <loggly token> # optional for external logging to loggly

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I don't think this needs to be commented out. It's clear from the inline comment later that this is optional, but leaving it empty should do the trick as well

13 changes: 13 additions & 0 deletions service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# service.yaml
kind: Service
apiVersion: v1
metadata:
name: todo-app
spec:
type: NodePort
selector:
app: todo-app
ports:
- protocol: TCP
port: 5000
targetPort: 5000
8 changes: 6 additions & 2 deletions todo_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

logger = logging.getLogger(__name__)

github_oauth_uri = os.getenv('GITHUB_OAUTH_URL')
github_oauth_uri = 'https://github.com/login/oauth/authorize'
client_id = os.getenv('CLIENT_ID')
client_secret = os.getenv('CLIENT_SECRET')
redirect_uri = os.getenv('REDIRECT_URI')
redirect_uri = 'http://127.0.0.1:5000/login/callback'
token_url = 'https://github.com/login/oauth/access_token'

administrators = ['94004061'] # list of admin userIds. hardcoded for demo/testing purposes.
Expand Down Expand Up @@ -97,6 +97,10 @@ def make_complete(id):
complete_item(id)
app.logger.info(f'Task completed. TaskID {id} Userid: {current_user.id}')
return redirect('/')

@app.route ('/version')
def version():
return 'module-14 30.12.12.2'


@app.route ('/AccessDenied')
Expand Down
24 changes: 14 additions & 10 deletions todo_app/data/database_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,36 @@
from logging import Formatter
from loggly.handlers import HTTPSHandler




logger = logging.getLogger(__name__)
if os.getenv('LOGGLY_TOKEN') is not None:
handler = HTTPSHandler(f"https://logs-01.loggly.com/inputs/{os.getenv('LOGGLY_TOKEN')}/tag/todo-app")
handler.setFormatter(Formatter("[%(asctime)s] %(levelname)s in %(module)s: %(message)s"))
logger.addHandler(handler)

def get_items():
def get_collection():
client = pymongo.MongoClient(os.getenv('CONNECTION_STRING'))
database = client[os.getenv('DATABASE')]
collection = database[os.getenv('COLLECTION')]
database = client['todo_app']
collection = database['items']
return collection

def get_items():
collection=get_collection()
logger.debug(f'database name : {collection}')
items = []
database_items = collection.find()
for item in database_items:
items.append(Item.from_database(item))
logger.debug(f"{len(items)} individual items collected from {collection.count_documents({})} total database records:## Item count and record count should match ##")
return items

def add_item(title):
client = pymongo.MongoClient(os.getenv('CONNECTION_STRING'))
database = client[os.getenv('DATABASE')]
collection = database[os.getenv('COLLECTION')]
def add_item(title):
collection=get_collection()
collection.insert_one({'name':title,'status':'To Do','date_modified':datetime.now()})

def complete_item(_id):
client = pymongo.MongoClient(os.getenv('CONNECTION_STRING'))
database = client[os.getenv('DATABASE')]
collection = database[os.getenv('COLLECTION')]
collection=get_collection()
collection.update_one({'_id': ObjectId(_id)}, {"$set": {'status':'Done', 'date_modified':datetime.now()}})

4 changes: 2 additions & 2 deletions todo_app/test_pytests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def client():
load_dotenv(file_path, override=True)
with mongomock.patch(servers=(('fakemongo.com', 27017),)):
mongoclient = pymongo.MongoClient(os.getenv('CONNECTION_STRING'))
db = mongoclient[os.getenv('DATABASE')]
collection = db[os.getenv('COLLECTION')]
db = mongoclient['todo_app']
collection = db['items']
collection.insert_one({'name': 'test item #222', 'status': 'To Do', 'date_modified' : datetime.now()})
test_app = app.create_app()
test_app.config['LOGIN_DISABLED'] = True
Expand Down