Skip to content

Commit 0a96693

Browse files
committed
REL_5_7 backpatch: Address prealloc lint issues
Does not include the update to internal/util/volumes.go due to differences in code. Issue: PGO-2844
1 parent 8ae47bd commit 0a96693

5 files changed

Lines changed: 29 additions & 17 deletions

File tree

internal/controller/postgrescluster/cluster.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,12 @@ func (r *Reconciler) reconcileDataSource(ctx context.Context,
366366
var configs []string
367367
switch {
368368
case dataSource != nil:
369-
configs = []string{dataSource.ClusterName, dataSource.RepoName}
369+
configs = make([]string, 0, 2+len(dataSource.Options))
370+
configs = append(configs, dataSource.ClusterName, dataSource.RepoName)
370371
configs = append(configs, dataSource.Options...)
371372
case cloudDataSource != nil:
372-
configs = []string{cloudDataSource.Stanza, cloudDataSource.Repo.Name}
373+
configs = make([]string, 0, 2+len(cloudDataSource.Options))
374+
configs = append(configs, cloudDataSource.Stanza, cloudDataSource.Repo.Name)
373375
configs = append(configs, cloudDataSource.Options...)
374376
}
375377
configHash, err := hashFunc(configs)

internal/controller/postgrescluster/instance.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,11 @@ func (r *Reconciler) scaleDownInstances(
985985
}
986986

987987
// grab all pods for the cluster using the observed instances
988-
pods := []corev1.Pod{}
988+
var podCount int
989+
for i := range observedInstances.forCluster {
990+
podCount += len(observedInstances.forCluster[i].Pods)
991+
}
992+
pods := make([]corev1.Pod, 0, podCount)
989993
for instanceIndex := range observedInstances.forCluster {
990994
for podIndex := range observedInstances.forCluster[instanceIndex].Pods {
991995
pods = append(pods, *observedInstances.forCluster[instanceIndex].Pods[podIndex])
@@ -1037,7 +1041,8 @@ func podsToKeep(instances []corev1.Pod, want map[string]int) []corev1.Pod {
10371041
return keep
10381042
}
10391043

1040-
keepPodList := []corev1.Pod{}
1044+
// preallocate with the total number of instance Pods
1045+
keepPodList := make([]corev1.Pod, 0, len(instances))
10411046
for name, num := range want {
10421047
list := []corev1.Pod{}
10431048
for _, instance := range instances {

internal/controller/standalone_pgadmin/configmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func generateClusterConfig(
140140
// which we can do by
141141
// a) sorting the ServerGroup name used as a key; and
142142
// b) sorting the clusters by name;
143-
keys := []string{}
143+
keys := make([]string, 0, len(clusters))
144144
for key := range clusters {
145145
keys = append(keys, key)
146146
}

internal/pgbouncer/config.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,20 @@ func podConfigFiles(
179179
// Start with an empty file at /etc/pgbouncer/pgbouncer.ini. This file can
180180
// be overridden by the user, but it must exist because our configuration
181181
// file refers to it.
182-
projections := []corev1.VolumeProjection{
183-
{
184-
ConfigMap: &corev1.ConfigMapProjection{
185-
LocalObjectReference: corev1.LocalObjectReference{
186-
Name: configmap.Name,
187-
},
188-
Items: []corev1.KeyToPath{{
189-
Key: emptyConfigMapKey,
190-
Path: emptyFileProjectionPath,
191-
}},
182+
// Preallocate: 3 (empty ini file + configmap projection + secret projection)
183+
// plus the number of specified files, len(config.Files)
184+
projections := make([]corev1.VolumeProjection, 0, 3+len(config.Files))
185+
projections = append(projections, corev1.VolumeProjection{
186+
ConfigMap: &corev1.ConfigMapProjection{
187+
LocalObjectReference: corev1.LocalObjectReference{
188+
Name: configmap.Name,
192189
},
190+
Items: []corev1.KeyToPath{{
191+
Key: emptyConfigMapKey,
192+
Path: emptyFileProjectionPath,
193+
}},
193194
},
194-
}
195+
})
195196

196197
// Add any specified projections. These may override the files above.
197198
// - https://docs.k8s.io/concepts/storage/volumes/#projected

internal/postgres/exec.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ func (exec Executor) ExecInDatabasesFromQuery(
7474
// database is passed via standard input while the database query is passed
7575
// as the first argument. Remaining arguments are passed through to `psql`.
7676
stdin := strings.NewReader(sql)
77-
args := []string{databases}
77+
78+
// Using make to preallocate args (using 1 (databases) + len(variables)) triggered
79+
// a CodeQL scanning error that a potentially large value might cause an overflow.
80+
// To address this, don't preallocate args and have the linter ignore this line.
81+
args := []string{databases} //nolint:prealloc
7882
for k, v := range variables {
7983
args = append(args, "--set="+k+"="+v)
8084
}

0 commit comments

Comments
 (0)