Skip to content

Commit 619981a

Browse files
committed
Remove reconcilePGAdminVersion, adjust tests and add clarifying comments
1 parent 3757a8e commit 619981a

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
@@ -85,11 +85,21 @@ func (r *PGAdminReconciler) reconcilePGAdminUsers(ctx context.Context, pgadmin *
8585
if pgadmin.Status.MajorVersion == 0 || pgadmin.Status.MinorVersion == "" ||
8686
pgadmin.Status.ImageSHA != pgAdminImageSha {
8787

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

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

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

189180
var olderThan9_3 bool
190-
versionFloat, err := strconv.ParseFloat(pgadmin.Status.MinorVersion, 32)
181+
versionFloat, err := strconv.ParseFloat(pgadmin.Status.MinorVersion, 64)
191182
if err != nil {
192183
return err
193184
}
@@ -198,6 +189,8 @@ cd $PGADMIN_DIR
198189
intentUsers := []pgAdminUserForJson{}
199190
for _, user := range pgadmin.Spec.Users {
200191
var stdin, stdout, stderr bytes.Buffer
192+
// starting in pgAdmin 9.3, custom roles are supported and a new flag is used
193+
// - https://github.com/pgadmin-org/pgadmin4/pull/8631
201194
typeFlag := "--role User"
202195
if olderThan9_3 {
203196
typeFlag = "--nonadmin"
@@ -257,10 +250,13 @@ cd $PGADMIN_DIR
257250
log.Error(err, "PodExec failed: ")
258251
intentUsers = append(intentUsers, existingUser)
259252
continue
253+
260254
} else if strings.Contains(strings.TrimSpace(stderr.String()), "UserWarning: pkg_resources is deprecated as an API") {
255+
// Started seeing this error with pgAdmin 9.7 when using Python 3.11.
256+
// Issue appears to resolve with Python 3.13.
261257
log.Info(stderr.String())
262258
} else if strings.TrimSpace(stderr.String()) != "" {
263-
log.Error(errors.New(stderr.String()), fmt.Sprintf("pgAdmin setup.py error for %s: ",
259+
log.Error(errors.New(stderr.String()), fmt.Sprintf("pgAdmin setup.py update-user error for %s: ",
264260
intentUser.Username))
265261
intentUsers = append(intentUsers, existingUser)
266262
continue
@@ -294,9 +290,11 @@ cd $PGADMIN_DIR
294290
continue
295291
}
296292
if strings.Contains(strings.TrimSpace(stderr.String()), "UserWarning: pkg_resources is deprecated as an API") {
293+
// Started seeing this error with pgAdmin 9.7 when using Python 3.11.
294+
// Issue appears to resolve with Python 3.13.
297295
log.Info(stderr.String())
298296
} else if strings.TrimSpace(stderr.String()) != "" {
299-
log.Error(errors.New(stderr.String()), fmt.Sprintf("pgAdmin setup.py error for %s: ",
297+
log.Error(errors.New(stderr.String()), fmt.Sprintf("pgAdmin setup.py add-user error for %s: ",
300298
intentUser.Username))
301299
continue
302300
}

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.Client = 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)