Skip to content

Commit 859c95b

Browse files
committed
fix: helm tests
1 parent 0b453f9 commit 859c95b

26 files changed

Lines changed: 158 additions & 4767 deletions

Dockerfile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
# Build stage for pgconfig binary
66
FROM golang:1.25-bookworm AS pgconfig-builder
77

8+
# Build arguments for version information
9+
ARG VERSION=dev
10+
ARG GIT_COMMIT=unknown
11+
ARG BUILD_DATE=unknown
12+
813
# Copy source code
914
WORKDIR /src/postgres
1015
COPY postgres/go.mod postgres/go.sum ./
@@ -13,10 +18,15 @@ RUN go mod download
1318

1419
COPY postgres/ /src/postgres
1520
COPY clicky /src/clicky
16-
# Build pgconfig binary with cache mounts
21+
# Build pgconfig binary with cache mounts and version info
1722
RUN --mount=type=cache,target=/root/.cache/go-build \
1823
--mount=type=cache,target=/go/pkg/mod \
19-
CGO_ENABLED=0 GOOS=linux go build -o postgres-cli ./cmd
24+
CGO_ENABLED=0 GOOS=linux go build \
25+
-ldflags "-s -w \
26+
-X 'main.Version=${VERSION}' \
27+
-X 'main.GitCommit=${GIT_COMMIT}' \
28+
-X 'main.BuildDate=$(date)'" \
29+
-o postgres-cli ./cmd
2030

2131
# Main stage
2232
FROM debian:bookworm-slim

Taskfile.build.yaml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ vars:
1212
IMAGE_BASE: '{{.IMAGE_BASE | default "flanksource/postgres"}}'
1313
IMAGE_TAG: '{{.IMAGE_TAG | default "latest"}}'
1414
TARGET_VERSIONS: [15, 16, 17]
15+
VERSION:
16+
sh: git describe --tags --always --dirty 2>/dev/null || echo "dev"
17+
GIT_COMMIT:
18+
sh: git rev-parse --short HEAD 2>/dev/null || echo "unknown"
19+
BUILD_DATE:
20+
sh: date -u '+%Y-%m-%dT%H:%M:%SZ'
1521

1622
tasks:
1723
build:
1824
desc: Build the flanksource/postgres Docker image
1925
cmds:
20-
- docker build -t {{.IMAGE_NAME}} .
26+
- >-
27+
docker build
28+
--build-arg VERSION={{.VERSION}}
29+
--build-arg GIT_COMMIT={{.GIT_COMMIT}}
30+
--build-arg BUILD_DATE={{.BUILD_DATE}}
31+
-t {{.IMAGE_NAME}}
32+
.
2133
sources:
2234
- Dockerfile
2335
- docker-entrypoint.sh
@@ -46,7 +58,15 @@ tasks:
4658
vars:
4759
VERSION: "{{.VERSION}}"
4860
cmds:
49-
- docker build --build-arg TARGET_VERSION={{.VERSION}} -t {{.REGISTRY}}/{{.IMAGE_BASE}}:to-{{.VERSION}} -t {{.REGISTRY}}/{{.IMAGE_BASE}}:to-{{.VERSION}}-{{.IMAGE_TAG}} .
61+
- >-
62+
docker build
63+
--build-arg TARGET_VERSION={{.VERSION}}
64+
--build-arg VERSION={{.VERSION}}
65+
--build-arg GIT_COMMIT={{.GIT_COMMIT}}
66+
--build-arg BUILD_DATE={{.BUILD_DATE}}
67+
-t {{.REGISTRY}}/{{.IMAGE_BASE}}:to-{{.VERSION}}
68+
-t {{.REGISTRY}}/{{.IMAGE_BASE}}:to-{{.VERSION}}-{{.IMAGE_TAG}}
69+
.
5070
sources:
5171
- Dockerfile
5272
- docker-entrypoint.sh

Taskfile.pgconfig.yaml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,32 @@ output:
99
vars:
1010
CLI_NAME: "postgres-cli"
1111
GOBIN: '{{.GOBIN | default "$(go env GOPATH)/bin"}}'
12+
VERSION:
13+
sh: git describe --tags --always --dirty 2>/dev/null || echo "dev"
14+
GIT_COMMIT:
15+
sh: git rev-parse --short HEAD 2>/dev/null || echo "unknown"
16+
BUILD_DATE:
17+
sh: date -u '+%Y-%m-%dT%H:%M:%SZ'
1218

1319
tasks:
1420
build:
1521
desc: Build the postgres-cli and install to GOBIN
16-
22+
vars:
23+
GOBIN_PATH:
24+
sh: go env GOPATH
25+
LDFLAGS: >-
26+
-s -w
27+
-X 'main.Version={{.VERSION}}'
28+
-X 'main.GitCommit={{.GIT_COMMIT}}'
29+
-X 'main.BuildDate={{.BUILD_DATE}}'
1730
cmds:
18-
- echo "Building postgres-cli..."
19-
- |
20-
GOBIN_PATH="$(go env GOPATH)/bin"
21-
echo "Installing to: $GOBIN_PATH/{{.CLI_NAME}}"
22-
go build -ldflags "-s -w" -o "$GOBIN_PATH/{{.CLI_NAME}}" ./cmd
23-
- echo "✅ Built and installed {{.CLI_NAME}} to $(go env GOPATH)/bin"
31+
- echo "Building {{.CLI_NAME}} {{.VERSION}} ({{.GIT_COMMIT}})..."
32+
- >-
33+
go build
34+
-ldflags "{{.LDFLAGS}}"
35+
-o "{{.GOBIN_PATH}}/bin/{{.CLI_NAME}}"
36+
./cmd
37+
- echo "✅ Built and installed {{.CLI_NAME}} to {{.GOBIN_PATH}}/bin"
2438
method: timestamp
2539

2640
install:

cmd/main.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"github.com/flanksource/postgres/pkg/utils"
1717
)
1818

19-
const version = "1.0.0"
20-
2119
var (
2220
postgres server.Postgres
2321

@@ -58,17 +56,16 @@ func getPostgresPassword() utils.SensitiveString {
5856
}
5957

6058
func main() {
59+
clicky.Infof(GetVersionInfo())
6160
rootCmd := &cobra.Command{
6261
Use: "postgres-cli",
6362
Short: "PostgreSQL Management CLI",
6463
Long: `A comprehensive CLI tool for managing PostgreSQL servers, generating configurations, and working with schemas.
6564
This unified tool combines PostgreSQL server management, configuration generation, and schema operations.`,
66-
Version: version,
65+
Version: Version,
6766
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
67+
6868
clicky.Flags.UseFlags()
69-
if err := postgres.Validate(); err != nil {
70-
return err
71-
}
7269

7370
// Load configuration if specified
7471
if configFile != "" {
@@ -89,6 +86,9 @@ This unified tool combines PostgreSQL server management, configuration generatio
8986
if postgres.Password.IsEmpty() {
9087
postgres.Password = getPostgresPassword()
9188
}
89+
if err := postgres.Validate(); err != nil {
90+
return err
91+
}
9292

9393
return nil
9494
},
@@ -241,8 +241,6 @@ func runAutoStart(cmd *cobra.Command, args []string) error {
241241

242242
// Step 4: Reset password if requested
243243
if autoResetPassword {
244-
clicky.Infof("🔑 Resetting postgres superuser password from POSTGRES_PASSWORD")
245-
246244
newPassword := os.Getenv("POSTGRES_PASSWORD")
247245
sensitivePassword := utils.NewSensitiveString(newPassword)
248246
if err := postgres.ResetPassword(sensitivePassword); err != nil {
@@ -283,8 +281,7 @@ func createVersionCommand() *cobra.Command {
283281
Use: "version",
284282
Short: "Show version information",
285283
Run: func(cmd *cobra.Command, args []string) {
286-
fmt.Printf("postgres-cli version %s\n", version)
287-
284+
fmt.Println(GetVersionInfo())
288285
},
289286
}
290287
}

cmd/server.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/spf13/cobra"
88

99
"github.com/flanksource/clicky"
10-
"github.com/flanksource/postgres/pkg/utils"
1110
)
1211

1312
// createServerCommands creates the server command group
@@ -224,21 +223,20 @@ func createResetPasswordCommand() *cobra.Command {
224223
Short: "Reset PostgreSQL password",
225224
Long: "Reset the PostgreSQL superuser password",
226225
RunE: func(cmd *cobra.Command, args []string) error {
227-
password, _ := cmd.Flags().GetString("password")
228-
if password == "" {
226+
if postgres.Password.IsEmpty() {
229227
return fmt.Errorf("password is required")
230228
}
231229

232-
sensitivePassword := utils.SensitiveString(password)
233-
if err := postgres.ResetPassword(sensitivePassword); err != nil {
230+
clicky.Infof("Resetting password for user %s", postgres.Username)
231+
232+
if err := postgres.ResetPassword(postgres.Password); err != nil {
234233
return fmt.Errorf("failed to reset password: %w", err)
235234
}
236235
fmt.Println("Password reset successfully")
237236
return nil
238237
},
239238
}
240-
resetPasswordCmd.Flags().StringP("password", "p", "", "New password (required)")
241-
resetPasswordCmd.MarkFlagRequired("password")
239+
242240
return resetPasswordCmd
243241
}
244242

cmd/version.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
var (
9+
// Version is the semantic version (set via ldflags)
10+
Version = "dev"
11+
// GitCommit is the git commit hash (set via ldflags)
12+
GitCommit = "unknown"
13+
// BuildDate is the build timestamp (set via ldflags)
14+
BuildDate = "unknown"
15+
)
16+
17+
// GetVersionInfo returns formatted version information
18+
func GetVersionInfo() string {
19+
return fmt.Sprintf(`postgres-cli version: %s
20+
Git commit: %s
21+
Build date: %s
22+
Go version: %s
23+
OS/Arch: %s/%s`, Version, GitCommit, BuildDate, runtime.Version(), runtime.GOOS, runtime.GOARCH)
24+
}

pkg/server/postgres.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ func NewPostgres(config *pkg.PostgresConf, dataDir string) *Postgres {
6767
}
6868

6969
func (p *Postgres) Validate() error {
70+
if !p.Password.IsEmpty() {
71+
clicky.RedactSecretValues(p.Password.Value())
72+
}
7073

7174
// Try to auto-detect directories
7275
detectedDirs, err := utils.DetectPostgreSQLDirs()
@@ -89,18 +92,7 @@ func (p *Postgres) Validate() error {
8992
return fmt.Errorf("postgres binary directory not set")
9093
}
9194

92-
if err := utils.CheckPGDATAPermissions(p.DataDir); err != nil {
93-
if permErr, ok := err.(*utils.PermissionError); ok {
94-
// Exit with code 126 (permission denied)
95-
fmt.Fprintf(os.Stderr, "\n❌ %s\n", permErr.Error())
96-
os.Exit(126)
97-
}
98-
if !os.IsNotExist(err) {
99-
return fmt.Errorf("permission check failed: %w", err)
100-
}
101-
}
102-
103-
return nil
95+
return utils.CheckPGDATAPermissions(p.DataDir)
10496
}
10597

10698
// Note: For embedded postgres functionality, use pkg/embedded.NewEmbeddedPostgres() instead
@@ -459,7 +451,7 @@ func (p *Postgres) bin(name string, args ...string) *exec.Process {
459451
}
460452
cmd.Timeout = 10 * time.Second
461453

462-
return cmd.Debug()
454+
return cmd
463455
}
464456

465457
func WrappedError(err error) exec.WrapperFunc {

pkg/utils/permissions.go

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -74,51 +74,23 @@ func CheckDirectoryPermissions(path string) (*PermissionCheckResult, error) {
7474
// Check if directory is writable
7575
result.Writable = info.Mode().Perm()&0200 != 0 || result.OwnerUID == currentUID
7676

77-
// Check for permission mismatches
78-
if result.OwnerUID != 999 || result.OwnerGID != 999 {
79-
result.ErrorMsg = fmt.Sprintf(
80-
"directory is owned by UID %d:GID %d, expected postgres user (999:999)",
81-
result.OwnerUID,
82-
result.OwnerGID,
83-
)
84-
85-
// Generate fix commands
77+
if !result.Readable || !result.Writable {
78+
result.ErrorMsg = fmt.Sprintf("directory permissions are too restrictive: readable=%t, writable=%t",
79+
result.Readable, result.Writable)
80+
81+
// Suggest fix commands
8682
if currentUID == 0 {
8783
result.FixCommands = []string{
8884
fmt.Sprintf("chown -R postgres:postgres %s", path),
89-
}
90-
} else if currentUID == result.OwnerUID {
91-
// User owns the directory but needs to run as root to fix
92-
result.FixCommands = []string{
93-
fmt.Sprintf("# Directory is owned by current user (UID %d), not postgres (999)", currentUID),
94-
fmt.Sprintf("# Option 1: Run container as root to fix permissions, then restart as postgres:"),
95-
fmt.Sprintf("docker run --user root -v <volume>:/var/lib/postgresql/data <image>"),
96-
fmt.Sprintf(""),
97-
fmt.Sprintf("# Option 2: Fix permissions on host (if using bind mount):"),
98-
fmt.Sprintf("sudo chown -R 999:999 %s", path),
99-
fmt.Sprintf(""),
100-
fmt.Sprintf("# Option 3: Use named volume (Docker handles permissions automatically):"),
101-
fmt.Sprintf("docker run -v pgdata:/var/lib/postgresql/data <image>"),
85+
fmt.Sprintf("chmod -R u+rw %s", path),
10286
}
10387
} else {
10488
result.FixCommands = []string{
105-
fmt.Sprintf("# Directory is owned by UID %d, not postgres (999) or current user (%d)", result.OwnerUID, currentUID),
106-
fmt.Sprintf("# Option 1: Run container explicitly as root to fix permissions:"),
107-
fmt.Sprintf("docker run --user root -v <volume>:/var/lib/postgresql/data <image>"),
108-
fmt.Sprintf(""),
109-
fmt.Sprintf("# Option 2: Fix permissions from host:"),
110-
fmt.Sprintf("docker run --rm --user root -v <volume>:/data alpine chown -R 999:999 /data"),
89+
fmt.Sprintf("# Run as root to fix permissions:"),
90+
fmt.Sprintf("sudo chown -R 999:999 %s", path),
91+
fmt.Sprintf("sudo chmod -R u+rw %s", path),
11192
}
11293
}
113-
} else if currentUID != 999 {
114-
result.ErrorMsg = fmt.Sprintf(
115-
"running as UID %d, but directory is owned by postgres (999). Container should run as postgres user",
116-
currentUID,
117-
)
118-
result.FixCommands = []string{
119-
"# Container is running as wrong user",
120-
"# Remove --user flag from docker run command to use default (postgres) user",
121-
}
12294
}
12395

12496
return result, nil

pkg/utils/sensitive.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (s SensitiveString) Equals(other SensitiveString) bool {
5959

6060
// NewSensitiveString creates a new SensitiveString from a regular string
6161
func NewSensitiveString(value string) SensitiveString {
62+
6263
return SensitiveString(value)
6364
}
6465

0 commit comments

Comments
 (0)