Skip to content

Commit 08699d0

Browse files
committed
Move permanent flags into ExporterStartCommand
Issue: PGO-635
1 parent 32839c0 commit 08699d0

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

internal/controller/postgrescluster/pgmonitor.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,7 @@ func addPGMonitorExporterToInstancePodSpec(
269269
Image: config.PGExporterContainerImage(cluster),
270270
ImagePullPolicy: cluster.Spec.ImagePullPolicy,
271271
Resources: cluster.Spec.Monitoring.PGMonitor.Exporter.Resources,
272-
Command: pgmonitor.ExporterStartCommand([]string{
273-
pgmonitor.ExporterExtendQueryPathFlag, pgmonitor.ExporterWebListenAddressFlag,
274-
}),
272+
Command: pgmonitor.ExporterStartCommand(),
275273
Env: []corev1.EnvVar{
276274
{Name: "DATA_SOURCE_URI", Value: fmt.Sprintf("%s:%d/%s", pgmonitor.ExporterHost, *cluster.Spec.Port, pgmonitor.ExporterDB)},
277275
{Name: "DATA_SOURCE_USER", Value: pgmonitor.MonitoringUser},
@@ -400,9 +398,7 @@ func configureExporterTLS(cluster *v1beta1.PostgresCluster, template *corev1.Pod
400398
}}
401399

402400
exporterContainer.VolumeMounts = append(exporterContainer.VolumeMounts, mounts...)
403-
exporterContainer.Command = pgmonitor.ExporterStartCommand([]string{
404-
pgmonitor.ExporterExtendQueryPathFlag, pgmonitor.ExporterWebListenAddressFlag, pgmonitor.ExporterWebConfigFileFlag,
405-
})
401+
exporterContainer.Command = pgmonitor.ExporterStartCommand(pgmonitor.ExporterWebConfigFileFlag)
406402
}
407403
}
408404

internal/pgmonitor/exporter.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ const (
4343

4444
// postgres_exporter command flags
4545
var (
46-
ExporterExtendQueryPathFlag = "--extend.query-path=/tmp/queries.yml"
47-
ExporterWebListenAddressFlag = fmt.Sprintf("--web.listen-address=:%d", ExporterPort)
48-
ExporterWebConfigFileFlag = "--web.config.file=/web-config/web-config.yml"
46+
ExporterWebConfigFileFlag = "--web.config.file=/web-config/web-config.yml"
4947
)
5048

5149
// Defaults for certain values used in queries.yml
@@ -120,26 +118,31 @@ func GenerateDefaultExporterQueries(ctx context.Context, cluster *v1beta1.Postgr
120118
// ExporterStartCommand generates an entrypoint that will create a master queries file and
121119
// start the postgres_exporter. It will repeat those steps if it notices a change in
122120
// the source queries files.
123-
func ExporterStartCommand(commandFlags []string) []string {
121+
func ExporterStartCommand(commandFlags ...string) []string {
124122
script := strings.Join([]string{
125123
// Older images do not have the command on the PATH.
126124
`PATH="$PATH:$(echo /opt/cpm/bin/postgres_exporter-*)"`,
127125

128126
// Set up temporary file to hold postgres_exporter process id
129127
`POSTGRES_EXPORTER_PIDFILE=/tmp/postgres_exporter.pid`,
130128

129+
`postgres_exporter_flags=(`,
130+
`'--extend.query-path=/tmp/queries.yml'`,
131+
fmt.Sprintf(`'--web.listen-address=:%d'`, ExporterPort),
132+
`"$@")`,
133+
131134
// declare function that will combine custom queries file and default
132135
// queries and start the postgres_exporter
133136
`start_postgres_exporter() {`,
134-
`cat /conf/* > /tmp/queries.yml`,
135-
`echo "Starting postgres_exporter with the following flags..."`,
136-
`echo "$@"`,
137-
`postgres_exporter "$@" &`,
138-
`echo $! > $POSTGRES_EXPORTER_PIDFILE`,
137+
` cat /conf/* > /tmp/queries.yml`,
138+
` echo "Starting postgres_exporter with the following flags..."`,
139+
` echo "${postgres_exporter_flags[@]}"`,
140+
` postgres_exporter "${postgres_exporter_flags[@]}" &`,
141+
` echo $! > $POSTGRES_EXPORTER_PIDFILE`,
139142
`}`,
140143

141144
// run function to combine queries files and start postgres_exporter
142-
`start_postgres_exporter "$@"`,
145+
`start_postgres_exporter`,
143146

144147
// Create a file descriptor with a no-op process that will not get
145148
// cleaned up
@@ -153,7 +156,7 @@ func ExporterStartCommand(commandFlags []string) []string {
153156
// something must have changed, so kill the postgres_exporter and rerun
154157
// the function to combine queries files and start postgres_exporter
155158
` if ([ "/conf" -nt "/proc/self/fd/${fd}" ] || [ "/opt/crunchy/password" -nt "/proc/self/fd/${fd}" ]) \`,
156-
` && kill $(head -1 ${POSTGRES_EXPORTER_PIDFILE?}) && start_postgres_exporter "$@";`,
159+
` && kill $(head -1 ${POSTGRES_EXPORTER_PIDFILE?}) && start_postgres_exporter;`,
157160
` then`,
158161

159162
// When something changes we want to get rid of the old file descriptor, get a fresh one

internal/pgmonitor/exporter_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import (
2424
"testing"
2525

2626
"gotest.tools/v3/assert"
27+
"sigs.k8s.io/yaml"
2728

29+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
2830
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2931
)
3032

@@ -48,14 +50,25 @@ func TestGenerateDefaultExporterQueries(t *testing.T) {
4850
}
4951

5052
func TestExporterStartCommand(t *testing.T) {
51-
t.Run("OneFlag", func(t *testing.T) {
52-
commandSlice := ExporterStartCommand([]string{"--testFlag"})
53-
assert.DeepEqual(t, commandSlice[:3], []string{"bash", "-ceu", "--"})
54-
assert.DeepEqual(t, commandSlice[4:], []string{"postgres_exporter_watcher", "--testFlag"})
53+
t.Run("NoInput", func(t *testing.T) {
54+
command := ExporterStartCommand()
55+
assert.DeepEqual(t, command[:3], []string{"bash", "-ceu", "--"})
56+
assert.Assert(t, len(command) > 3)
57+
script := command[3]
58+
59+
assert.Assert(t, cmp.Contains(script, "'--extend.query-path=/tmp/queries.yml'"))
60+
assert.Assert(t, cmp.Contains(script, "'--web.listen-address=:9187'"))
61+
62+
t.Run("PrettyYAML", func(t *testing.T) {
63+
b, err := yaml.Marshal(script)
64+
assert.NilError(t, err)
65+
assert.Assert(t, strings.HasPrefix(string(b), `|`),
66+
"expected literal block scalar, got:\n%s", b)
67+
})
5568
})
5669

5770
t.Run("MultipleFlags", func(t *testing.T) {
58-
commandSlice := ExporterStartCommand([]string{"--firstTestFlag", "--secondTestFlag"})
71+
commandSlice := ExporterStartCommand("--firstTestFlag", "--secondTestFlag")
5972
assert.DeepEqual(t, commandSlice[:3], []string{"bash", "-ceu", "--"})
6073
assert.DeepEqual(t, commandSlice[4:], []string{"postgres_exporter_watcher", "--firstTestFlag", "--secondTestFlag"})
6174
})

0 commit comments

Comments
 (0)