Skip to content

Commit 8888136

Browse files
committed
fix grpc-public-host arg prefix
1 parent 182eed8 commit 8888136

File tree

2 files changed

+62
-28
lines changed

2 files changed

+62
-28
lines changed

internal/controllers/database/controller_test.go

Lines changed: 58 additions & 28 deletions
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,8 +391,31 @@ 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
})
420+
391421
})

internal/resources/database_statefulset.go

Lines changed: 4 additions & 0 deletions
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)