Skip to content

Assignment 4 #79

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

Open
wants to merge 1 commit 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
6 changes: 6 additions & 0 deletions assignment-4/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM golang:1.18-alpine
RUN mkdir /backend-app
COPY . /backend-app
WORKDIR /backend-app
RUN go build -o main .
CMD ["/backend-app/main"]
26 changes: 26 additions & 0 deletions assignment-4/backend-app/backend-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-app-deployment
labels:
app: backend-app
spec:
replicas: 3
selector:
matchLabels:
app: backend-app
template:
metadata:
labels:
app: backend-app
spec:
containers:
- name: backend-app
image: pratikpjain/app
env:
- name: SVC_HOST_NAME
value: "backend-app"
- name: SVC_HOST_PORT
value: "8080"
ports:
- containerPort: 80
11 changes: 11 additions & 0 deletions assignment-4/backend-app/backend-svc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: backendapp-service
spec:
selector:
app.kubernetes.io/name: backend-app
ports:
- protocol: TCP
port: 80
targetPort: 9376
27 changes: 27 additions & 0 deletions assignment-4/backend-app/postgres-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
spec:
selector:
matchLabels:
app: postgres
strategy:
type: Recreate
template:
metadata:
labels:
app: postgres
spec:
containers:
- image: postgres:alpine
name: postgres
env:
- name: POSTGRES_DB
value: store
- name: POSTGRES_USER
value: postgres
- name: POSTGRES_PASSWORD
value: root
ports:
- containerPort: 5433
10 changes: 10 additions & 0 deletions assignment-4/backend-app/postgres-svc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
ports:
- port: 5432
selector:
app: postgres
clusterIP: None
54 changes: 54 additions & 0 deletions assignment-4/db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package db

import (
"database/sql"
"time"

_ "github.com/jackc/pgconn"
_ "github.com/jackc/pgx/v4"
_ "github.com/jackc/pgx/v4/stdlib"
)

type DB struct {
SQL *sql.DB
}

var dbConn = &DB{}

const maxOpenDbConn = 64
const maxIdleDbConn = 64
const maxDbLifeTime = 5 * time.Minute

func ConnectDB(desc string) (*DB, error) {

db, err := NewDatabase(desc)

if err != nil {
panic(err)
}

db.SetMaxOpenConns(maxOpenDbConn)
db.SetMaxIdleConns(maxIdleDbConn)
db.SetConnMaxLifetime(maxDbLifeTime)

dbConn.SQL = db

return dbConn, nil
}

func NewDatabase(desc string) (*sql.DB, error) {
db, err := sql.Open("pgx", desc)
if err != nil {
return nil, err
}

if err = db.Ping(); err != nil {
return nil, err
}

return db, nil
}

func GetDB() *sql.DB {
return dbConn.SQL
}
32 changes: 32 additions & 0 deletions assignment-4/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: "3.8"

services:
db:
image: postgres:alpine
container_name: "postgres"
ports:
- "5433:5433"
environment:
- POSTGRES_DB=store
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
networks:
- fullstack
volumes:
- database_postgres:/var/lib/postgresql/data

web:
build: .
container_name: "backend-app"
command: ["go", "run", "main.go"]
ports:
- "8080:8080"
depends_on:
- db
networks:
- fullstack
volumes:
database_postgres:
networks:
fullstack:
driver: bridge
17 changes: 17 additions & 0 deletions assignment-4/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/practice

go 1.18

require (
github.com/gorilla/mux v1.8.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v4 v4.18.1 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/text v0.7.0 // indirect
)
192 changes: 192 additions & 0 deletions assignment-4/go.sum

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions assignment-4/helm-chart/.helmignore
Original file line number Diff line number Diff line change
@@ -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/
24 changes: 24 additions & 0 deletions assignment-4/helm-chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: backend-app
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-app-deployment
labels:
app: backend-app
spec:
replicas: 3
selector:
matchLabels:
app: backend-app
template:
metadata:
labels:
app: backend-app
spec:
containers:
- name: backend-app
image: pratikpjain/app
env:
- name: SVC_HOST_NAME
value: "backend-app"
- name: SVC_HOST_PORT
value: "8080"
ports:
- containerPort: 80
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: backendapp-service
spec:
selector:
app.kubernetes.io/name: backend-app
ports:
- protocol: TCP
port: 80
targetPort: 9376
10 changes: 10 additions & 0 deletions assignment-4/helm-chart/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-backendapp
spec:
defaultBackend:
service:
name: backendapp-service
port:
number: 8080
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
spec:
selector:
matchLabels:
app: postgres
strategy:
type: Recreate
template:
metadata:
labels:
app: postgres
spec:
containers:
- image: postgres:alpine
name: postgres
env:
- name: POSTGRES_DB
value: store
- name: POSTGRES_USER
value: postgres
- name: POSTGRES_PASSWORD
value: root
ports:
- containerPort: 5433
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
ports:
- port: 5432
selector:
app: postgres
clusterIP: None
7 changes: 7 additions & 0 deletions assignment-4/helm-chart/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Default values for helm-chart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

backend-app:
image: pratikjain/app
servicePort: 80
Loading