Skip to content

Commit 753f81d

Browse files
authored
chore(lint): address findings from prealloc linter (cloudnative-pg#9680)
golangci-lint 2.8.0 enables the prealloc linter. Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
1 parent 76817ec commit 753f81d

File tree

12 files changed

+52
-35
lines changed

12 files changed

+52
-35
lines changed

internal/cmd/manager/instance/upgrade/execute/cmd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,14 @@ func prepareConfigurationFiles(ctx context.Context, cluster apiv1.Cluster, destD
446446
func (ui upgradeInfo) runPgUpgrade(
447447
newDataDir string,
448448
) error {
449-
args := []string{
449+
args := make([]string, 0, 9+len(ui.pgUpgradeArgs))
450+
args = append(args,
450451
"--link",
451452
"--username", "postgres",
452453
"--old-bindir", ui.oldBinDir,
453454
"--old-datadir", ui.pgData,
454455
"--new-datadir", newDataDir,
455-
}
456+
)
456457
args = append(args, ui.pgUpgradeArgs...)
457458

458459
// Run the pg_upgrade command

internal/cmd/plugin/logical/psql.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ func getSQLCommand(
6565
sqlCommand string,
6666
args ...string,
6767
) (*psql.Command, error) {
68-
psqlArgs := []string{
68+
psqlArgs := make([]string, 0, 5+len(args))
69+
psqlArgs = append(psqlArgs,
6970
connectionString,
7071
"-U",
7172
"postgres",
7273
"-c",
7374
sqlCommand,
74-
}
75+
)
7576
psqlArgs = append(psqlArgs, args...)
7677
psqlOptions := psql.CommandOptions{
7778
Replica: false,

pkg/management/postgres/instance.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,11 +1136,12 @@ func (instance *Instance) Rewind(ctx context.Context) error {
11361136
instance.LogPgControldata(ctx, "before pg_rewind")
11371137

11381138
primaryConnInfo := instance.GetPrimaryConnInfo()
1139-
options := []string{
1139+
options := make([]string, 0, 6)
1140+
options = append(options,
11401141
"-P",
11411142
"--source-server", primaryConnInfo,
11421143
"--target-pgdata", instance.PgData,
1143-
}
1144+
)
11441145

11451146
// make sure restore_command is set in override.conf
11461147
if _, err := configurePostgresOverrideConfFile(instance.PgData, primaryConnInfo, ""); err != nil {

pkg/management/postgres/logicalimport/database.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,23 @@ func (ds *databaseSnapshotter) exportDatabases(
9494
extraOptions []string,
9595
) error {
9696
contextLogger := log.FromContext(ctx)
97-
sectionsToExport := []string{}
97+
sections := ds.getSectionsToExecute()
98+
sectionsToExport := make([]string, 0, len(sections))
9899

99-
for _, section := range ds.getSectionsToExecute() {
100+
for _, section := range sections {
100101
sectionsToExport = append(sectionsToExport, fmt.Sprintf("--section=%s", section))
101102
}
102103

103104
for _, database := range databases {
104105
contextLogger.Info("exporting database", "databaseName", database)
105106
dsn := target.GetDsn(database)
106-
options := []string{
107+
options := make([]string, 0, 6+len(sectionsToExport)+len(extraOptions))
108+
options = append(options,
107109
"-Fd",
108110
"-f", generateFileNameForDatabase(database),
109111
"-d", dsn,
110112
"-v",
111-
}
113+
)
112114
options = append(options, sectionsToExport...)
113115
options = append(options, extraOptions...)
114116

@@ -212,8 +214,6 @@ func (ds *databaseSnapshotter) importDatabaseContent(
212214
"section", section,
213215
)
214216

215-
var options []string
216-
217217
alwaysPresentOptions := []string{
218218
"-U", "postgres",
219219
"--no-owner",
@@ -224,7 +224,9 @@ func (ds *databaseSnapshotter) importDatabaseContent(
224224
generateFileNameForDatabase(database),
225225
}
226226

227-
options = append(options, flags.forSection(section)...)
227+
sectionFlags := flags.forSection(section)
228+
options := make([]string, 0, len(sectionFlags)+len(alwaysPresentOptions))
229+
options = append(options, sectionFlags...)
228230
options = append(options, alwaysPresentOptions...)
229231

230232
contextLogger.Info("Running pg_restore",

pkg/reconciler/majorupgrade/reconciler_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ var _ = Describe("Major upgrade job status reconciliation", func() {
7575
buildReplicaPVC(3),
7676
}
7777

78-
objects := []runtime.Object{
78+
objects := make([]runtime.Object, 0, 2+len(pvcs))
79+
objects = append(objects,
7980
job,
8081
cluster,
81-
}
82+
)
8283
for i := range pvcs {
8384
objects = append(objects, &pvcs[i])
8485
}

pkg/specs/jobs.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,15 @@ func CreatePrimaryJobViaRestoreSnapshot(
207207

208208
// CreatePrimaryJobViaRecovery creates a new primary instance in a Pod, restoring from a Backup
209209
func CreatePrimaryJobViaRecovery(cluster apiv1.Cluster, nodeSerial int, backup *apiv1.Backup) *batchv1.Job {
210-
initCommand := []string{
210+
commonFlags := buildCommonInitJobFlags(cluster)
211+
initCommand := make([]string, 0, 3+len(commonFlags))
212+
initCommand = append(initCommand,
211213
"/controller/manager",
212214
"instance",
213215
"restore",
214-
}
216+
)
215217

216-
initCommand = append(initCommand, buildCommonInitJobFlags(cluster)...)
218+
initCommand = append(initCommand, commonFlags...)
217219

218220
job := CreatePrimaryJob(cluster, nodeSerial, jobRoleFullRecovery, initCommand)
219221

@@ -248,41 +250,47 @@ func addBarmanEndpointCAToJobFromCluster(cluster apiv1.Cluster, backup *apiv1.Ba
248250

249251
// CreatePrimaryJobViaPgBaseBackup creates a new primary instance in a Pod
250252
func CreatePrimaryJobViaPgBaseBackup(cluster apiv1.Cluster, nodeSerial int) *batchv1.Job {
251-
initCommand := []string{
253+
commonFlags := buildCommonInitJobFlags(cluster)
254+
initCommand := make([]string, 0, 3+len(commonFlags))
255+
initCommand = append(initCommand,
252256
"/controller/manager",
253257
"instance",
254258
"pgbasebackup",
255-
}
259+
)
256260

257-
initCommand = append(initCommand, buildCommonInitJobFlags(cluster)...)
261+
initCommand = append(initCommand, commonFlags...)
258262

259263
return CreatePrimaryJob(cluster, nodeSerial, jobRolePGBaseBackup, initCommand)
260264
}
261265

262266
// JoinReplicaInstance create a new PostgreSQL node, copying the contents from another Pod
263267
func JoinReplicaInstance(cluster apiv1.Cluster, nodeSerial int) *batchv1.Job {
264-
initCommand := []string{
268+
commonFlags := buildCommonInitJobFlags(cluster)
269+
initCommand := make([]string, 0, 5+len(commonFlags))
270+
initCommand = append(initCommand,
265271
"/controller/manager",
266272
"instance",
267273
"join",
268274
"--parent-node", cluster.GetServiceReadWriteName(),
269-
}
275+
)
270276

271-
initCommand = append(initCommand, buildCommonInitJobFlags(cluster)...)
277+
initCommand = append(initCommand, commonFlags...)
272278

273279
return CreatePrimaryJob(cluster, nodeSerial, jobRoleJoin, initCommand)
274280
}
275281

276282
// RestoreReplicaInstance creates a new PostgreSQL replica starting from a volume snapshot backup
277283
func RestoreReplicaInstance(cluster apiv1.Cluster, nodeSerial int) *batchv1.Job {
278-
initCommand := []string{
284+
commonFlags := buildCommonInitJobFlags(cluster)
285+
initCommand := make([]string, 0, 4+len(commonFlags))
286+
initCommand = append(initCommand,
279287
"/controller/manager",
280288
"instance",
281289
"restoresnapshot",
282290
"--immediate",
283-
}
291+
)
284292

285-
initCommand = append(initCommand, buildCommonInitJobFlags(cluster)...)
293+
initCommand = append(initCommand, commonFlags...)
286294

287295
job := CreatePrimaryJob(cluster, nodeSerial, jobRoleSnapshotRecovery, initCommand)
288296
return job

tests/e2e/asserts_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ func AssertSwitchoverWithHistory(
176176

177177
if !isReplica {
178178
By("confirming that the all postgres containers have *.history file after switchover", func() {
179-
pods = []string{}
180179
timeout := 120
181180

182181
// Gather pod names
183182
podList, err := clusterutils.ListPods(env.Ctx, env.Client, namespace, clusterName)
184183
Expect(len(podList.Items), err).To(BeEquivalentTo(oldPodListLength))
184+
pods = make([]string, 0, len(podList.Items))
185185
for _, p := range podList.Items {
186186
pods = append(pods, p.Name)
187187
}
@@ -2855,7 +2855,10 @@ func AssertClusterReplicationSlotsAligned(
28552855
podList, err := clusterutils.ListPods(env.Ctx, env.Client, namespace, clusterName)
28562856
Expect(err).ToNot(HaveOccurred())
28572857
Eventually(func(g Gomega) {
2858-
var lsnList []string
2858+
numPods := len(podList.Items)
2859+
// Capacity calculation: primary has (N-1) HA slots, each of the (N-1) replicas
2860+
// has (N-2) slots. Total LSNs: (N-1) + (N-1)*(N-2) = (N-1)^2
2861+
lsnList := make([]string, 0, (numPods-1)*(numPods-1))
28592862
for _, pod := range podList.Items {
28602863
out, err := replicationslot.GetReplicationSlotLsnsOnPod(
28612864
env.Ctx, env.Client, env.Interface, env.RestClientConfig,

tests/e2e/operator_ha_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ var _ = Describe("Operator High Availability", Serial,
122122
Eventually(func() []string {
123123
podList, err := podutils.List(env.Ctx, env.Client, operatorNamespace)
124124
Expect(err).ToNot(HaveOccurred())
125-
var podNames []string
125+
podNames := make([]string, 0, len(podList.Items))
126126
for _, podItem := range podList.Items {
127127
podNames = append(podNames, podItem.GetName())
128128
}

tests/e2e/tablespaces_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ func AssertClusterHasMountPointsAndVolumesForTablespaces(
947947
))
948948
}
949949

950-
var volumeNames []string
950+
volumeNames := make([]string, 0, len(pod.Spec.Volumes))
951951
var claimNames []string
952952
for _, vol := range pod.Spec.Volumes {
953953
volumeNames = append(volumeNames, vol.Name)

tests/e2e/upgrade_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,9 @@ var _ = Describe("Upgrade", Label(tests.LabelUpgrade, tests.LabelNoOpenshift), O
614614

615615
assertPGBouncerPodsAreReady(upgradeNamespace, pgBouncerSampleFile, 2)
616616

617-
var podUIDs []types.UID
618617
podList, err := clusterutils.ListPods(env.Ctx, env.Client, upgradeNamespace, clusterName1)
619618
Expect(err).ToNot(HaveOccurred())
619+
podUIDs := make([]types.UID, 0, len(podList.Items))
620620
for _, pod := range podList.Items {
621621
podUIDs = append(podUIDs, pod.GetUID())
622622
}

0 commit comments

Comments
 (0)