Skip to content

Commit 11210f8

Browse files
committed
Address prealloc lint issues
Issue: PGO-2844
1 parent 28b84a4 commit 11210f8

File tree

6 files changed

+34
-18
lines changed

6 files changed

+34
-18
lines changed

internal/controller/postgrescluster/cluster.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,12 @@ func (r *Reconciler) reconcileDataSource(ctx context.Context,
403403
var configs []string
404404
switch {
405405
case dataSource != nil:
406-
configs = []string{dataSource.ClusterName, dataSource.RepoName}
406+
configs = make([]string, 0, 2+len(dataSource.Options))
407+
configs = append(configs, dataSource.ClusterName, dataSource.RepoName)
407408
configs = append(configs, dataSource.Options...)
408409
case cloudDataSource != nil:
409-
configs = []string{cloudDataSource.Stanza, cloudDataSource.Repo.Name}
410+
configs = make([]string, 0, 2+len(cloudDataSource.Options))
411+
configs = append(configs, cloudDataSource.Stanza, cloudDataSource.Repo.Name)
410412
configs = append(configs, cloudDataSource.Options...)
411413
}
412414
configHash, err := hashFunc(configs)

internal/controller/postgrescluster/instance.go

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

953953
// grab all pods for the cluster using the observed instances
954-
pods := []corev1.Pod{}
954+
var podCount int
955+
for i := range observedInstances.forCluster {
956+
podCount += len(observedInstances.forCluster[i].Pods)
957+
}
958+
pods := make([]corev1.Pod, 0, podCount)
955959
for instanceIndex := range observedInstances.forCluster {
956960
for podIndex := range observedInstances.forCluster[instanceIndex].Pods {
957961
pods = append(pods, *observedInstances.forCluster[instanceIndex].Pods[podIndex])
@@ -1003,7 +1007,8 @@ func podsToKeep(instances []corev1.Pod, want map[string]int) []corev1.Pod {
10031007
return keep
10041008
}
10051009

1006-
keepPodList := []corev1.Pod{}
1010+
// preallocate with the total number of instance Pods
1011+
keepPodList := make([]corev1.Pod, 0, len(instances))
10071012
for name, num := range want {
10081013
list := []corev1.Pod{}
10091014
for _, instance := range instances {

internal/controller/standalone_pgadmin/configmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func generateClusterConfig(
199199
// which we can do by
200200
// a) sorting the ServerGroup name used as a key; and
201201
// b) sorting the clusters by name;
202-
keys := []string{}
202+
keys := make([]string, 0, len(clusters))
203203
for key := range clusters {
204204
keys = append(keys, key)
205205
}

internal/pgbouncer/config.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,20 @@ func podConfigFiles(
192192
// Start with an empty file at /etc/pgbouncer/pgbouncer.ini. This file can
193193
// be overridden by the user, but it must exist because our configuration
194194
// file refers to it.
195-
projections := []corev1.VolumeProjection{
196-
{
197-
ConfigMap: &corev1.ConfigMapProjection{
198-
LocalObjectReference: corev1.LocalObjectReference{
199-
Name: configmap.Name,
200-
},
201-
Items: []corev1.KeyToPath{{
202-
Key: emptyConfigMapKey,
203-
Path: emptyFileProjectionPath,
204-
}},
195+
// Preallocate: 3 (empty ini file + configmap projection + secret projection)
196+
// plus the number of specified files, len(config.Files)
197+
projections := make([]corev1.VolumeProjection, 0, 3+len(config.Files))
198+
projections = append(projections, corev1.VolumeProjection{
199+
ConfigMap: &corev1.ConfigMapProjection{
200+
LocalObjectReference: corev1.LocalObjectReference{
201+
Name: configmap.Name,
205202
},
203+
Items: []corev1.KeyToPath{{
204+
Key: emptyConfigMapKey,
205+
Path: emptyFileProjectionPath,
206+
}},
206207
},
207-
}
208+
})
208209

209210
// Add any specified projections. These may override the files above.
210211
// - 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
}

internal/util/volumes.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ func AddCloudLogVolumeToPod(podSpec *corev1.PodSpec, pvcName string) {
5555
}
5656

5757
func addVolumesAndMounts(pod *corev1.PodSpec, volumes []v1beta1.AdditionalVolume, namer func(string, bool) corev1.VolumeMount) []string {
58-
missingContainers := []string{}
58+
var containerRefCount int
59+
for _, v := range volumes {
60+
containerRefCount += len(v.Containers)
61+
}
62+
missingContainers := make([]string, 0, containerRefCount)
5963

6064
for _, spec := range volumes {
6165
// If it is an image volume, override readOnly to true

0 commit comments

Comments
 (0)