Skip to content

Commit 071f5d3

Browse files
Merge pull request #629 from stankevich/support-extra-flags-in-mysqld-exporter
Support extra flags for mysqld_exporter
2 parents dcadef4 + 9b78217 commit 071f5d3

File tree

15 files changed

+343
-30
lines changed

15 files changed

+343
-30
lines changed

deploy/crds/planetscale.com_vitessclusters.yaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,10 @@ spec:
20122012
type: object
20132013
mysqldExporter:
20142014
properties:
2015+
extraFlags:
2016+
additionalProperties:
2017+
type: string
2018+
type: object
20152019
resources:
20162020
properties:
20172021
claims:
@@ -2045,8 +2049,6 @@ spec:
20452049
x-kubernetes-int-or-string: true
20462050
type: object
20472051
type: object
2048-
required:
2049-
- resources
20502052
type: object
20512053
name:
20522054
default: ""
@@ -2453,6 +2455,10 @@ spec:
24532455
type: object
24542456
mysqldExporter:
24552457
properties:
2458+
extraFlags:
2459+
additionalProperties:
2460+
type: string
2461+
type: object
24562462
resources:
24572463
properties:
24582464
claims:
@@ -2486,8 +2492,6 @@ spec:
24862492
x-kubernetes-int-or-string: true
24872493
type: object
24882494
type: object
2489-
required:
2490-
- resources
24912495
type: object
24922496
name:
24932497
default: ""

deploy/crds/planetscale.com_vitesskeyspaces.yaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,10 @@ spec:
553553
type: object
554554
mysqldExporter:
555555
properties:
556+
extraFlags:
557+
additionalProperties:
558+
type: string
559+
type: object
556560
resources:
557561
properties:
558562
claims:
@@ -586,8 +590,6 @@ spec:
586590
x-kubernetes-int-or-string: true
587591
type: object
588592
type: object
589-
required:
590-
- resources
591593
type: object
592594
name:
593595
default: ""
@@ -994,6 +996,10 @@ spec:
994996
type: object
995997
mysqldExporter:
996998
properties:
999+
extraFlags:
1000+
additionalProperties:
1001+
type: string
1002+
type: object
9971003
resources:
9981004
properties:
9991005
claims:
@@ -1027,8 +1033,6 @@ spec:
10271033
x-kubernetes-int-or-string: true
10281034
type: object
10291035
type: object
1030-
required:
1031-
- resources
10321036
type: object
10331037
name:
10341038
default: ""

deploy/crds/planetscale.com_vitessshards.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,10 @@ spec:
536536
type: object
537537
mysqldExporter:
538538
properties:
539+
extraFlags:
540+
additionalProperties:
541+
type: string
542+
type: object
539543
resources:
540544
properties:
541545
claims:
@@ -569,8 +573,6 @@ spec:
569573
x-kubernetes-int-or-string: true
570574
type: object
571575
type: object
572-
required:
573-
- resources
574576
type: object
575577
name:
576578
default: ""

docs/api.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,22 @@ EtcdLockserverStatus
15261526
<tbody>
15271527
<tr>
15281528
<td>
1529+
<code>extraFlags</code><br>
1530+
<em>
1531+
map[string]string
1532+
</em>
1533+
</td>
1534+
<td>
1535+
<p>ExtraFlags can optionally be used to override default flags set by the
1536+
operator, or pass additional flags to mysqld_exporter. All entries must be
1537+
key-value string pairs of the form &ldquo;flag&rdquo;: &ldquo;value&rdquo;. The flag name should
1538+
not have any prefix (just &ldquo;flag&rdquo;, not &ldquo;-flag&rdquo;). To set a boolean flag,
1539+
set the string value to either &ldquo;true&rdquo; or &ldquo;false&rdquo;; the flag will be
1540+
automatically converted to the format expected by mysqld_exporter.</p>
1541+
</td>
1542+
</tr>
1543+
<tr>
1544+
<td>
15291545
<code>resources</code><br>
15301546
<em>
15311547
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.32/#resourcerequirements-v1-core">

docs/api/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,22 @@ <h3 id="planetscale.com/v2.MysqldExporterSpec">MysqldExporterSpec
15281528
<tbody>
15291529
<tr>
15301530
<td>
1531+
<code>extraFlags</code><br>
1532+
<em>
1533+
map[string]string
1534+
</em>
1535+
</td>
1536+
<td>
1537+
<p>ExtraFlags can optionally be used to override default flags set by the
1538+
operator, or pass additional flags to mysqld_exporter. All entries must be
1539+
key-value string pairs of the form &ldquo;flag&rdquo;: &ldquo;value&rdquo;. The flag name should
1540+
not have any prefix (just &ldquo;flag&rdquo;, not &ldquo;-flag&rdquo;). To set a boolean flag,
1541+
set the string value to either &ldquo;true&rdquo; or &ldquo;false&rdquo;; the flag will be
1542+
automatically converted to the format expected by mysqld_exporter.</p>
1543+
</td>
1544+
</tr>
1545+
<tr>
1546+
<td>
15311547
<code>resources</code><br>
15321548
<em>
15331549
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.32/#resourcerequirements-v1-core">

pkg/apis/planetscale/v2/vitessshard_types.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,16 @@ 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.
321-
Resources corev1.ResourceRequirements `json:"resources"`
329+
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
322330
}
323331

324332
// VitessTabletPoolType represents the tablet types for which it makes sense

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 2024 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+
}

0 commit comments

Comments
 (0)