Skip to content

Commit e395e2a

Browse files
committed
Remove reconcilePGAdminVersion, adjust tests and add clarifying comments
1 parent d4dd1b8 commit e395e2a

File tree

2 files changed

+44
-58
lines changed

2 files changed

+44
-58
lines changed

internal/controller/standalone_pgadmin/users.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,21 @@ func (r *PGAdminReconciler) reconcilePGAdminUsers(ctx context.Context, pgadmin *
8686
if pgadmin.Status.MajorVersion == 0 || pgadmin.Status.MinorVersion == "" ||
8787
pgadmin.Status.ImageSHA != pgAdminImageSha {
8888

89-
pgadminMinorVersion, err := r.reconcilePGAdminVersion(ctx, podExecutor)
90-
if err != nil {
89+
// exec into the pgAdmin pod and retrieve the pgAdmin minor version
90+
script := fmt.Sprintf(`
91+
PGADMIN_DIR=%s
92+
cd $PGADMIN_DIR && python3 -c "import config; print(config.APP_VERSION)"
93+
`, pgAdminDir)
94+
95+
var stdin, stdout, stderr bytes.Buffer
96+
97+
if err := podExecutor(ctx, &stdin, &stdout, &stderr,
98+
[]string{"bash", "-ceu", "--", script}...); err != nil {
9199
return err
92100
}
93101

102+
pgadminMinorVersion := strings.TrimSpace(stdout.String())
103+
94104
// ensure minor version is valid before storing in status
95105
parsedMinorVersion, err := strconv.ParseFloat(pgadminMinorVersion, 64)
96106
if err != nil {
@@ -124,25 +134,6 @@ func (r *PGAdminReconciler) reconcilePGAdminUsers(ctx context.Context, pgadmin *
124134
return r.writePGAdminUsers(ctx, pgadmin, podExecutor)
125135
}
126136

127-
// reconcilePGAdminVersion execs into the pgAdmin pod and retrieves the pgAdmin minor version
128-
func (r *PGAdminReconciler) reconcilePGAdminVersion(ctx context.Context, exec Executor) (string, error) {
129-
script := fmt.Sprintf(`
130-
PGADMIN_DIR=%s
131-
cd $PGADMIN_DIR && python3 -c "import config; print(config.APP_VERSION)"
132-
`, pgAdminDir)
133-
134-
var stdin, stdout, stderr bytes.Buffer
135-
136-
err := exec(ctx, &stdin, &stdout, &stderr,
137-
[]string{"bash", "-ceu", "--", script}...)
138-
139-
if err != nil {
140-
return "", err
141-
}
142-
143-
return strings.TrimSpace(stdout.String()), nil
144-
}
145-
146137
// writePGAdminUsers takes the users in the pgAdmin spec and writes (adds or updates) their data
147138
// to both pgAdmin and the users.json file that is stored in the pgAdmin secret. If a user is
148139
// removed from the spec, its data is removed from users.json, but it is not deleted from pgAdmin.
@@ -188,7 +179,7 @@ cd $PGADMIN_DIR
188179
}
189180

190181
var olderThan9_3 bool
191-
versionFloat, err := strconv.ParseFloat(pgadmin.Status.MinorVersion, 32)
182+
versionFloat, err := strconv.ParseFloat(pgadmin.Status.MinorVersion, 64)
192183
if err != nil {
193184
return err
194185
}
@@ -199,6 +190,8 @@ cd $PGADMIN_DIR
199190
intentUsers := []pgAdminUserForJson{}
200191
for _, user := range pgadmin.Spec.Users {
201192
var stdin, stdout, stderr bytes.Buffer
193+
// starting in pgAdmin 9.3, custom roles are supported and a new flag is used
194+
// - https://github.com/pgadmin-org/pgadmin4/pull/8631
202195
typeFlag := "--role User"
203196
if olderThan9_3 {
204197
typeFlag = "--nonadmin"
@@ -258,10 +251,13 @@ cd $PGADMIN_DIR
258251
log.Error(err, "PodExec failed: ")
259252
intentUsers = append(intentUsers, existingUser)
260253
continue
254+
261255
} else if strings.Contains(strings.TrimSpace(stderr.String()), "UserWarning: pkg_resources is deprecated as an API") {
256+
// Started seeing this error with pgAdmin 9.7 when using Python 3.11.
257+
// Issue appears to resolve with Python 3.13.
262258
log.Info(stderr.String())
263259
} else if strings.TrimSpace(stderr.String()) != "" {
264-
log.Error(errors.New(stderr.String()), fmt.Sprintf("pgAdmin setup.py error for %s: ",
260+
log.Error(errors.New(stderr.String()), fmt.Sprintf("pgAdmin setup.py update-user error for %s: ",
265261
intentUser.Username))
266262
intentUsers = append(intentUsers, existingUser)
267263
continue
@@ -295,9 +291,11 @@ cd $PGADMIN_DIR
295291
continue
296292
}
297293
if strings.Contains(strings.TrimSpace(stderr.String()), "UserWarning: pkg_resources is deprecated as an API") {
294+
// Started seeing this error with pgAdmin 9.7 when using Python 3.11.
295+
// Issue appears to resolve with Python 3.13.
298296
log.Info(stderr.String())
299297
} else if strings.TrimSpace(stderr.String()) != "" {
300-
log.Error(errors.New(stderr.String()), fmt.Sprintf("pgAdmin setup.py error for %s: ",
298+
log.Error(errors.New(stderr.String()), fmt.Sprintf("pgAdmin setup.py add-user error for %s: ",
301299
intentUser.Username))
302300
continue
303301
}

internal/controller/standalone_pgadmin/users_test.go

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -195,52 +195,40 @@ func TestReconcilePGAdminUsers(t *testing.T) {
195195
assert.Equal(t, pgadmin.Status.MinorVersion, "")
196196
assert.Equal(t, pgadmin.Status.ImageSHA, "")
197197
})
198-
}
199198

200-
func TestReconcilePGAdminVersion(t *testing.T) {
201-
ctx := context.Background()
202-
pod := corev1.Pod{}
203-
pod.Namespace = "test-namespace"
204-
pod.Name = "pgadmin-123-0"
205-
reconciler := &PGAdminReconciler{}
199+
t.Run("PodExecError", func(t *testing.T) {
200+
pgadmin := pgadmin.DeepCopy()
201+
pod := pod.DeepCopy()
206202

207-
podExecutor := func(
208-
ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string,
209-
) error {
210-
return reconciler.PodExec(ctx, pod.Namespace, pod.Name, "pgadmin", stdin, stdout, stderr, command...)
211-
}
203+
pod.DeletionTimestamp = nil
204+
pod.Status.ContainerStatuses =
205+
[]corev1.ContainerStatus{{Name: naming.ContainerPGAdmin}}
206+
pod.Status.ContainerStatuses[0].State.Running =
207+
new(corev1.ContainerStateRunning)
208+
pod.Status.ContainerStatuses[0].ImageID = "fakeSHA"
212209

213-
t.Run("SuccessfulRetrieval", func(t *testing.T) {
214-
reconciler.PodExec = func(
210+
r := new(PGAdminReconciler)
211+
r.Reader = fake.NewClientBuilder().WithObjects(pod).Build()
212+
213+
calls := 0
214+
r.PodExec = func(
215215
ctx context.Context, namespace, pod, container string,
216216
stdin io.Reader, stdout, stderr io.Writer, command ...string,
217217
) error {
218+
calls++
219+
218220
assert.Equal(t, pod, "pgadmin-123-0")
219-
assert.Equal(t, namespace, "test-namespace")
221+
assert.Equal(t, namespace, pgadmin.Namespace)
220222
assert.Equal(t, container, naming.ContainerPGAdmin)
221223

222-
// Simulate a v9.3 version of pgAdmin by setting stdout to "9.3"
223-
// for podexec call in reconcilePGAdminVersion
224-
_, _ = stdout.Write([]byte("9.3"))
225-
return nil
226-
}
227-
228-
version, err := reconciler.reconcilePGAdminVersion(ctx, podExecutor)
229-
assert.NilError(t, err)
230-
assert.Equal(t, version, "9.3")
231-
})
232-
233-
t.Run("PodExecError", func(t *testing.T) {
234-
reconciler.PodExec = func(
235-
ctx context.Context, namespace, pod, container string,
236-
stdin io.Reader, stdout, stderr io.Writer, command ...string,
237-
) error {
238224
return errors.New("PodExecError")
239225
}
240226

241-
version, err := reconciler.reconcilePGAdminVersion(ctx, podExecutor)
242-
assert.Check(t, err != nil)
243-
assert.Equal(t, version, "")
227+
assert.Error(t, r.reconcilePGAdminUsers(ctx, pgadmin), "PodExecError")
228+
assert.Equal(t, calls, 1, "PodExec should be called once")
229+
assert.Equal(t, pgadmin.Status.MajorVersion, 0)
230+
assert.Equal(t, pgadmin.Status.MinorVersion, "")
231+
assert.Equal(t, pgadmin.Status.ImageSHA, "")
244232
})
245233
}
246234

0 commit comments

Comments
 (0)