Skip to content

Commit bc29ff4

Browse files
committed
Support extra flags for mysqld_exporter
1 parent 2900f70 commit bc29ff4

File tree

12 files changed

+299
-9
lines changed

12 files changed

+299
-9
lines changed

deploy/crds/planetscale.com_vitessclusters.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,10 @@ spec:
19921992
type: object
19931993
mysqldExporter:
19941994
properties:
1995+
extraFlags:
1996+
additionalProperties:
1997+
type: string
1998+
type: object
19951999
resources:
19962000
properties:
19972001
claims:
@@ -2433,6 +2437,10 @@ spec:
24332437
type: object
24342438
mysqldExporter:
24352439
properties:
2440+
extraFlags:
2441+
additionalProperties:
2442+
type: string
2443+
type: object
24362444
resources:
24372445
properties:
24382446
claims:

deploy/crds/planetscale.com_vitesskeyspaces.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ spec:
550550
type: object
551551
mysqldExporter:
552552
properties:
553+
extraFlags:
554+
additionalProperties:
555+
type: string
556+
type: object
553557
resources:
554558
properties:
555559
claims:
@@ -991,6 +995,10 @@ spec:
991995
type: object
992996
mysqldExporter:
993997
properties:
998+
extraFlags:
999+
additionalProperties:
1000+
type: string
1001+
type: object
9941002
resources:
9951003
properties:
9961004
claims:

deploy/crds/planetscale.com_vitessshards.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,10 @@ spec:
533533
type: object
534534
mysqldExporter:
535535
properties:
536+
extraFlags:
537+
additionalProperties:
538+
type: string
539+
type: object
536540
resources:
537541
properties:
538542
claims:

docs/api.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,22 @@ EtcdLockserverStatus
15361536
<tbody>
15371537
<tr>
15381538
<td>
1539+
<code>extraFlags</code><br>
1540+
<em>
1541+
map[string]string
1542+
</em>
1543+
</td>
1544+
<td>
1545+
<p>ExtraFlags can optionally be used to override default flags set by the
1546+
operator, or pass additional flags to mysqld_exporter. All entries must be
1547+
key-value string pairs of the form &ldquo;flag&rdquo;: &ldquo;value&rdquo;. The flag name should
1548+
not have any prefix (just &ldquo;flag&rdquo;, not &ldquo;-flag&rdquo;). To set a boolean flag,
1549+
set the string value to either &ldquo;true&rdquo; or &ldquo;false&rdquo;; the flag will be
1550+
automatically converted to the format expected by mysqld_exporter.</p>
1551+
</td>
1552+
</tr>
1553+
<tr>
1554+
<td>
15391555
<code>resources</code><br>
15401556
<em>
15411557
<a href="https://v1-18.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#resourcerequirements-v1-core">

docs/api/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,22 @@ <h3 id="planetscale.com/v2.MysqldExporterSpec">MysqldExporterSpec
15381538
<tbody>
15391539
<tr>
15401540
<td>
1541+
<code>extraFlags</code><br>
1542+
<em>
1543+
map[string]string
1544+
</em>
1545+
</td>
1546+
<td>
1547+
<p>ExtraFlags can optionally be used to override default flags set by the
1548+
operator, or pass additional flags to mysqld_exporter. All entries must be
1549+
key-value string pairs of the form &ldquo;flag&rdquo;: &ldquo;value&rdquo;. The flag name should
1550+
not have any prefix (just &ldquo;flag&rdquo;, not &ldquo;-flag&rdquo;). To set a boolean flag,
1551+
set the string value to either &ldquo;true&rdquo; or &ldquo;false&rdquo;; the flag will be
1552+
automatically converted to the format expected by mysqld_exporter.</p>
1553+
</td>
1554+
</tr>
1555+
<tr>
1556+
<td>
15411557
<code>resources</code><br>
15421558
<em>
15431559
<a href="https://v1-18.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#resourcerequirements-v1-core">

pkg/apis/planetscale/v2/vitessshard_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ type MysqldSpec struct {
317317

318318
// MysqldExporterSpec configures the local MySQL exporter within a tablet.
319319
type MysqldExporterSpec struct {
320+
// ExtraFlags can optionally be used to override default flags set by the
321+
// operator, or pass additional flags to mysqld_exporter. All entries must be
322+
// key-value string pairs of the form "flag": "value". The flag name should
323+
// not have any prefix (just "flag", not "-flag"). To set a boolean flag,
324+
// set the string value to either "true" or "false"; the flag will be
325+
// automatically converted to the format expected by mysqld_exporter.
326+
ExtraFlags map[string]string `json:"extraFlags,omitempty"`
327+
320328
// Resources specify the compute resources to allocate for just the MySQL Exporter.
321329
Resources corev1.ResourceRequirements `json:"resources"`
322330
}

pkg/apis/planetscale/v2/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/operator/vitess/flags.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package vitess
1919
import (
2020
"fmt"
2121
"sort"
22+
"strings"
2223
)
2324

2425
// Flags represents values for flags to be passed to Vitess binaries.
@@ -61,6 +62,44 @@ func (f Flags) FormatArgs() []string {
6162
return args
6263
}
6364

65+
// FormatArgsConvertBoolean returns the flags as a flattened list of
66+
// command-line args with boolean values formatted as `--flag` or `--no-flag`.
67+
// This format is used by some tools, like mysqld_exporter.
68+
// Method is based on FormatArgs().
69+
func (f Flags) FormatArgsConvertBoolean() []string {
70+
// Sort flag names so the ordering is deterministic,
71+
// which is important when diffing object specs.
72+
// This also makes it easier for humans to find things.
73+
keys := make([]string, 0, len(f))
74+
for key := range f {
75+
keys = append(keys, key)
76+
}
77+
sort.Strings(keys)
78+
79+
// Make formatted args list.
80+
args := make([]string, 0, len(f))
81+
for _, key := range keys {
82+
// These args are passed to the command as a string array,
83+
// so we don't need to worry about quotes or escaping.
84+
//
85+
// We use two dashes (--) even though the standard flag parser
86+
// accepts either one or two dashes, because some wrappers like
87+
// pflags require two dashes.
88+
//
89+
// All boolean values are formatted as `--flag` or `--no-flag`.
90+
value := f[key].(string)
91+
switch value := strings.ToLower(value); value {
92+
case "true":
93+
args = append(args, fmt.Sprintf("--%v", key))
94+
case "false":
95+
args = append(args, fmt.Sprintf("--no-%v", key))
96+
default:
97+
args = append(args, fmt.Sprintf("--%v=%v", key, value))
98+
}
99+
}
100+
return args
101+
}
102+
64103
// Merge sets the given flags, overwriting duplicates.
65104
func (f Flags) Merge(flags Flags) Flags {
66105
for key, value := range flags {

pkg/operator/vitess/flags_test.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
Copyright 2019 PlanetScale Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package vitess
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
// TestFormatArgs tests the FormatArgs method of the Flags type.
26+
func TestFormatArgs(t *testing.T) {
27+
tests := []struct {
28+
name string
29+
flags Flags
30+
want []string
31+
}{
32+
{
33+
name: "empty flags",
34+
flags: Flags{},
35+
want: []string{},
36+
},
37+
{
38+
name: "single flag",
39+
flags: Flags{
40+
"flag1": "value1",
41+
},
42+
want: []string{"--flag1=value1"},
43+
},
44+
{
45+
name: "multiple flags",
46+
flags: Flags{
47+
"flag2": "value2",
48+
"flag3": "value3",
49+
},
50+
want: []string{"--flag2=value2", "--flag3=value3"},
51+
},
52+
}
53+
54+
for _, tt := range tests {
55+
t.Run(tt.name, func(t *testing.T) {
56+
got := tt.flags.FormatArgs()
57+
assert.Equal(t, tt.want, got)
58+
})
59+
}
60+
}
61+
62+
// TestFormatArgsConvertBoolean tests the FormatArgsConvertBoolean method of the Flags type.
63+
func TestFormatArgsConvertBoolean(t *testing.T) {
64+
tests := []struct {
65+
name string
66+
flags Flags
67+
want []string
68+
}{
69+
{
70+
name: "empty flags",
71+
flags: Flags{},
72+
want: []string{},
73+
},
74+
{
75+
name: "boolean flag true",
76+
flags: Flags{
77+
"flag1": "true",
78+
},
79+
want: []string{"--flag1"},
80+
},
81+
{
82+
name: "boolean flag false",
83+
flags: Flags{
84+
"flag2": "false",
85+
},
86+
want: []string{"--no-flag2"},
87+
},
88+
{
89+
name: "non-boolean flag",
90+
flags: Flags{
91+
"flag3": "value3",
92+
},
93+
want: []string{"--flag3=value3"},
94+
},
95+
{
96+
name: "multiple flags",
97+
flags: Flags{
98+
"flag4": "true",
99+
"flag5": "false",
100+
"flag6": "value6",
101+
},
102+
want: []string{"--flag4", "--no-flag5", "--flag6=value6"},
103+
},
104+
}
105+
106+
for _, tt := range tests {
107+
t.Run(tt.name, func(t *testing.T) {
108+
got := tt.flags.FormatArgsConvertBoolean()
109+
assert.Equal(t, tt.want, got)
110+
})
111+
}
112+
}
113+
114+
// TestMerge tests the Merge method of the Flags type.
115+
func TestMerge(t *testing.T) {
116+
tests := []struct {
117+
name string
118+
flags Flags
119+
merge Flags
120+
result Flags
121+
}{
122+
{
123+
name: "merge empty flags",
124+
flags: Flags{
125+
"flag1": "value1",
126+
},
127+
merge: Flags{},
128+
result: Flags{
129+
"flag1": "value1",
130+
},
131+
},
132+
{
133+
name: "merge non-empty flags",
134+
flags: Flags{
135+
"flag1": "value1",
136+
},
137+
merge: Flags{
138+
"flag2": "value2",
139+
},
140+
result: Flags{
141+
"flag1": "value1",
142+
"flag2": "value2",
143+
},
144+
},
145+
{
146+
name: "merge duplicate flags",
147+
flags: Flags{
148+
"flag1": "value1",
149+
},
150+
merge: Flags{
151+
"flag1": "value2",
152+
},
153+
result: Flags{
154+
"flag1": "value2",
155+
},
156+
},
157+
}
158+
159+
for _, tt := range tests {
160+
t.Run(tt.name, func(t *testing.T) {
161+
got := tt.flags.Merge(tt.merge)
162+
assert.Equal(t, tt.result, got)
163+
})
164+
}
165+
}

pkg/operator/vttablet/flags.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ func init() {
9595
}
9696
})
9797

98+
// Base mysqld_exporter flags.
99+
mysqldExporterFlags.Add(func(s lazy.Spec) vitess.Flags {
100+
spec := s.(*Spec)
101+
return vitess.Flags{
102+
"config.my-cnf": spec.myCnfFilePath(),
103+
// The default for `collect.info_schema.tables.databases` is `*`,
104+
// which causes new time series to be created for each user table.
105+
// This in turn causes scaling issues in Prometheus memory usage.
106+
"collect.info_schema.tables.databases": "sys,_vt",
107+
}
108+
})
109+
98110
// Base vtbackup flags.
99111
vtbackupFlags.Add(func(s lazy.Spec) vitess.Flags {
100112
backupSpec := s.(*BackupSpec)

0 commit comments

Comments
 (0)