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

feat: allow cartservice to accept POSTGRES env var #17

Merged
merged 7 commits into from
Aug 16, 2024
Merged
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: 5 additions & 1 deletion release/obd-kardinal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ spec:
timeoutSeconds: 5
failureThreshold: 3
env:
# if POSTGRES is set, uses this to connect
# otherwise uses environment variables below
- name: POSTGRES
value: ""
- name: PORT
value: "8090"
- name: DB_USERNAME
Expand Down Expand Up @@ -327,4 +331,4 @@ spec:
port: 8070
targetPort: 8070
protocol: TCP
appProtocol: HTTP
appProtocol: HTTP
20 changes: 14 additions & 6 deletions src/cartservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ FROM golang:1.21.9-alpine AS builder
ENV CGO_ENABLED=0 GOOS=linux
WORKDIR /go/src/hipstershop

# Install dependencies
RUN apk --update --no-cache add ca-certificates make protoc


# Build Go binary
COPY Makefile go.mod go.sum ./
RUN go env -w GOPROXY=https://goproxy.io,direct/
Expand All @@ -18,8 +14,20 @@ COPY . .
ARG SKAFFOLD_GO_GCFLAGS
RUN go build -gcflags="${SKAFFOLD_GO_GCFLAGS}" -o /go/src/hipstershop/cartservice .

# Deployment container
FROM scratch
# # Deployment container
FROM alpine:latest

# Install dependencies
RUN apk --update --no-cache add ca-certificates make protoc

# These tools are for debuggin the containres
RUN apk add --no-cache \
bash \
curl \
wget \
net-tools \
iputils \
postgresql-client

# Definition of this variable is used by 'skaffold debug' to identify a golang binary.
# Default behavior - a failure prints a stack trace for the current goroutine.
Expand Down
26 changes: 23 additions & 3 deletions src/cartservice/cartstore/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,34 @@ package cartstore
import (
"context"
"fmt"
"net"
"time"

cartservice_rest_types "github.com/kurtosis-tech/new-obd/src/cartservice/api/http_rest/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"time"
)

type Db struct {
db *gorm.DB
}

func NewDb(
uri string,
host string,
username string,
password string,
name string,
port string,
) (*Db, error) {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable", host, username, password, name, port)
var dsn string
if uri != "" {
dsn = uri
} else {
dsn = fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=require", host, username, password, name, port)
}
maxRetries := 5
initialBackoff := 1 * time.Second
backoffMultiplier := 2.0
Expand All @@ -32,6 +40,8 @@ func NewDb(
return nil, errors.Wrap(err, fmt.Sprintf("An error occurred opening the connection to the database with dsn %s", dsn))
}

logrus.Info("connected to database")

if err = db.AutoMigrate(&Item{}); err != nil {
return nil, errors.Wrap(err, "An error occurred migrating the database")
}
Expand All @@ -49,7 +59,17 @@ func retryConnect(dsn string, maxRetries int, initialBackoff time.Duration, back
backoff := initialBackoff

for i := 0; i < maxRetries; i++ {
// Attempt to execute the operation
// Need to change the resolver to resolve all addresses to use ipv4 instead of ipv6
net.DefaultResolver = &net.Resolver{
tedim52 marked this conversation as resolved.
Show resolved Hide resolved
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Millisecond * time.Duration(10000),
}
return d.DialContext(ctx, "tcp4", address)
},
}
logrus.Infof("Attempting to connecting to datbase with dsn: %v\n", dsn)
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
logrus.Debugf("An error occurred opening the connection to the database with dsn %s", dsn)
Expand Down
8 changes: 5 additions & 3 deletions src/cartservice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import (
"fmt"
"net"
"os"

cartservice_server_rest_server "github.com/kurtosis-tech/new-obd/src/cartservice/api/http_rest/server"
"github.com/kurtosis-tech/new-obd/src/cartservice/cartstore"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/sirupsen/logrus"
"net"
"os"
)

const (
Expand Down Expand Up @@ -36,13 +37,14 @@ func main() {
AllowHeaders: defaultCORSHeaders,
}))

uri := os.Getenv("POSTGRES")
dbHost := os.Getenv("DB_HOST")
dbUsername := os.Getenv("DB_USERNAME")
dbPassword := os.Getenv("DB_PASSWORD")
dbName := os.Getenv("DB_NAME")
dbPort := os.Getenv("DB_PORT")

db, err := cartstore.NewDb(dbHost, dbUsername, dbPassword, dbName, dbPort)
db, err := cartstore.NewDb(uri, dbHost, dbUsername, dbPassword, dbName, dbPort)
if err != nil {
logrus.Fatal(err)
}
Expand Down
Loading