diff --git a/release/obd-kardinal.yaml b/release/obd-kardinal.yaml index 30fc303..937acee 100644 --- a/release/obd-kardinal.yaml +++ b/release/obd-kardinal.yaml @@ -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 @@ -327,4 +331,4 @@ spec: port: 8070 targetPort: 8070 protocol: TCP - appProtocol: HTTP + appProtocol: HTTP \ No newline at end of file diff --git a/src/cartservice/Dockerfile b/src/cartservice/Dockerfile index c887344..2e5c159 100644 --- a/src/cartservice/Dockerfile +++ b/src/cartservice/Dockerfile @@ -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/ @@ -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. diff --git a/src/cartservice/cartstore/db.go b/src/cartservice/cartstore/db.go index c32e0c3..0b9e53b 100644 --- a/src/cartservice/cartstore/db.go +++ b/src/cartservice/cartstore/db.go @@ -3,12 +3,14 @@ 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 { @@ -16,13 +18,19 @@ type Db struct { } 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 @@ -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") } @@ -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{ + 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) diff --git a/src/cartservice/main.go b/src/cartservice/main.go index dfa0c2f..0c11ad6 100644 --- a/src/cartservice/main.go +++ b/src/cartservice/main.go @@ -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 ( @@ -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) }