Skip to content

Commit b2d3704

Browse files
authored
Fix prefix with POD_NAME in --grpc-public-host arg (#296)
1 parent 1ca6c03 commit b2d3704

File tree

4 files changed

+66
-52
lines changed

4 files changed

+66
-52
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Fixed
2+
body: 'bug: regression with pod name in grpc-public-host arg'
3+
time: 2025-02-23T22:28:50.688471+08:00

.golangci.yml

+2-24
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ run:
3535
# output configuration options
3636
output:
3737
# colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number"
38-
format: colored-line-number
38+
formats:
39+
- format: colored-line-number
3940

4041
print-issued-lines: true
4142

@@ -61,13 +62,6 @@ linters-settings:
6162
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
6263
# default is false: such cases aren't reported by default.
6364
check-blank: false
64-
govet:
65-
# report about shadowed variables
66-
shadow: true
67-
fieldalignment: true
68-
golint:
69-
# minimal confidence for issues, default is 0.8
70-
min-confidence: 0.8
7165
gofmt:
7266
# simplify code: gofmt with `-s` option, true by default
7367
simplify: true
@@ -85,9 +79,6 @@ linters-settings:
8579
- G101
8680
- G115
8781
- G601 # no longer actual since 1.22
88-
fieldalignment:
89-
# print struct with more effective memory layout or not, false by default
90-
suggest-new: true
9182
misspell:
9283
# Correct spellings using locale preferences for US or UK.
9384
# Default is to use a neutral variety of English.
@@ -118,17 +109,7 @@ linters-settings:
118109
- name: empty-block
119110
- name: superfluous-else
120111
- name: unreachable-code
121-
unused:
122-
# treat code as a program (not a library) and report unused exported identifiers; default is false.
123-
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
124-
# if it's called for subdir of a project it can't find funcs usages. All text editor integrations
125-
# with golangci-lint call it on a directory with the changed file.
126-
check-exported: false
127112
unparam:
128-
# call graph construction algorithm (cha, rta). In general, use cha for libraries,
129-
# and rta for programs with main packages. Default is cha.
130-
algo: cha
131-
132113
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
133114
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
134115
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations
@@ -192,9 +173,6 @@ issues:
192173
# Default value for this option is true.
193174
exclude-use-default: true
194175

195-
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
196-
max-per-linter: 0
197-
198176
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
199177
max-same-issues: 0
200178

internal/controllers/database/controller_test.go

+57-28
Original file line numberDiff line numberDiff line change
@@ -308,40 +308,47 @@ var _ = Describe("Database controller medium tests", func() {
308308
Expect(args).To(ContainElements([]string{"--grpc-public-address-v4", "--grpc-public-target-name-override"}))
309309
})
310310

311+
checkContainerArg := func(expectedArgKey, expectedArgValue string) error {
312+
foundStatefulSet := appsv1.StatefulSet{}
313+
Eventually(func() error {
314+
return k8sClient.Get(ctx,
315+
types.NamespacedName{
316+
Name: testobjects.DatabaseName,
317+
Namespace: testobjects.YdbNamespace,
318+
},
319+
&foundStatefulSet,
320+
)
321+
}, test.Timeout, test.Interval).Should(Succeed())
322+
podContainerArgs := foundStatefulSet.Spec.Template.Spec.Containers[0].Args
323+
for idx, argKey := range podContainerArgs {
324+
if argKey == expectedArgKey {
325+
if podContainerArgs[idx+1] != expectedArgValue {
326+
return fmt.Errorf(
327+
"Found arg `%s` value %s does not match with expected: %s",
328+
expectedArgKey,
329+
podContainerArgs[idx+1],
330+
expectedArgValue,
331+
)
332+
}
333+
}
334+
}
335+
return nil
336+
}
337+
311338
It("Check externalPort GRPC Service field propagation", func() {
312339
By("Create test database")
313340
databaseSample = *testobjects.DefaultDatabase()
341+
databaseSample.Spec.Service.GRPC.ExternalHost = fmt.Sprintf("%s.%s", testobjects.YdbNamespace, "k8s.external.net")
314342
Expect(k8sClient.Create(ctx, &databaseSample)).Should(Succeed())
315343

316-
checkPublicPortArg := func(expectedGRPCPort string) error {
317-
foundStatefulSet := appsv1.StatefulSet{}
318-
Eventually(func() error {
319-
return k8sClient.Get(ctx,
320-
types.NamespacedName{
321-
Name: testobjects.DatabaseName,
322-
Namespace: testobjects.YdbNamespace,
323-
},
324-
&foundStatefulSet,
325-
)
326-
}, test.Timeout, test.Interval).Should(Succeed())
327-
podContainerArgs := foundStatefulSet.Spec.Template.Spec.Containers[0].Args
328-
for idx, argKey := range podContainerArgs {
329-
if argKey == "--grpc-public-port" {
330-
if podContainerArgs[idx+1] != expectedGRPCPort {
331-
return fmt.Errorf(
332-
"Found arg `--grpc-public-port` value %s does not match with expected: %s",
333-
podContainerArgs[idx+1],
334-
expectedGRPCPort,
335-
)
336-
}
337-
}
338-
}
339-
return nil
340-
}
341-
342344
By("Check that args `--grpc-public-host` and `--grpc-public-port` propagated to StatefulSet pods...")
343345
Eventually(
344-
checkPublicPortArg(fmt.Sprintf("%d", v1alpha1.GRPCPort)),
346+
checkContainerArg("--grpc-public-host", fmt.Sprintf("%s.%s", "$(POD_NAME)", databaseSample.Spec.Service.GRPC.ExternalHost)),
347+
test.Timeout,
348+
test.Interval).ShouldNot(HaveOccurred())
349+
350+
Eventually(
351+
checkContainerArg("--grpc-public-port", fmt.Sprintf("%d", v1alpha1.GRPCPort)),
345352
test.Timeout,
346353
test.Interval).ShouldNot(HaveOccurred())
347354

@@ -384,7 +391,29 @@ var _ = Describe("Database controller medium tests", func() {
384391

385392
By("Check that args `--grpc-public-port` was updated in StatefulSet...")
386393
Eventually(
387-
checkPublicPortArg(fmt.Sprintf("%d", externalPort)),
394+
checkContainerArg("--grpc-public-port", fmt.Sprintf("%d", externalPort)),
395+
test.Timeout,
396+
test.Interval).ShouldNot(HaveOccurred())
397+
})
398+
399+
It("Checking args propagation from annotation to StatefulSet", func() {
400+
By("Check that Database with annotations was created...")
401+
databaseSample = *testobjects.DefaultDatabase()
402+
databaseSample.Annotations = map[string]string{
403+
v1alpha1.AnnotationGRPCPublicHost: fmt.Sprintf("%s.%s", testobjects.YdbNamespace, "k8s.external.net"),
404+
v1alpha1.AnnotationGRPCPublicPort: fmt.Sprintf("%d", 30001),
405+
}
406+
Expect(k8sClient.Create(ctx, &databaseSample)).Should(Succeed())
407+
408+
By("Check that args `--grpc-public-host` propagated to StatefulSet pods...")
409+
Eventually(
410+
checkContainerArg("--grpc-public-host", fmt.Sprintf("%s.%s.%s", "$(POD_NAME)", testobjects.YdbNamespace, "k8s.external.net")),
411+
test.Timeout,
412+
test.Interval).ShouldNot(HaveOccurred())
413+
414+
By("Check that args `--grpc-public-port` propagated to StatefulSet pods...")
415+
Eventually(
416+
checkContainerArg("--grpc-public-port", fmt.Sprintf("%d", 30001)),
388417
test.Timeout,
389418
test.Interval).ShouldNot(HaveOccurred())
390419
})

internal/resources/database_statefulset.go

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"regexp"
7+
"strings"
78

89
appsv1 "k8s.io/api/apps/v1"
910
corev1 "k8s.io/api/core/v1"
@@ -657,6 +658,9 @@ func (b *DatabaseStatefulSetBuilder) buildContainerArgs() ([]string, []string) {
657658
if value, ok := b.ObjectMeta.Annotations[api.AnnotationGRPCPublicHost]; ok {
658659
publicHost = value
659660
}
661+
if !(strings.HasPrefix(publicHost, "$(POD_NAME)") || strings.HasPrefix(publicHost, "$(NODE_NAME)")) {
662+
publicHost = fmt.Sprintf("%s.%s", "$(POD_NAME)", publicHost)
663+
}
660664

661665
if b.Spec.Service.GRPC.IPDiscovery != nil && b.Spec.Service.GRPC.IPDiscovery.Enabled {
662666
targetNameOverride := b.Spec.Service.GRPC.IPDiscovery.TargetNameOverride

0 commit comments

Comments
 (0)