Skip to content

Commit 6f07d13

Browse files
authored
fix bug: missing error handler for arg --auth-token-file (#283)
* fix bug: missing error handler for arg --auth-token-file * fix golangci-lint
1 parent 97f0b97 commit 6f07d13

File tree

7 files changed

+119
-67
lines changed

7 files changed

+119
-67
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Added
2+
body: 'annotations overrides default secret name and key for arg --auth-token-file'
3+
time: 2025-01-24T14:18:10.344319+08:00
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Fixed
2+
body: 'bug: missing error handler for arg --auth-token-file'
3+
time: 2025-01-24T14:16:31.463111+08:00

api/v1alpha1/const.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,24 @@ const (
2828
DiskPathPrefix = "/dev/kikimr_ssd"
2929
DiskNumberMaxDigits = 2
3030
DiskFilePath = "/data"
31-
YdbAuthToken = "ydb-auth-token-file"
3231

33-
ConfigDir = "/opt/ydb/cfg"
34-
ConfigFileName = "config.yaml"
32+
AuthTokenSecretName = "ydb-auth-token-file"
33+
AuthTokenSecretKey = "ydb-auth-token-file"
34+
AuthTokenFileArg = "--auth-token-file"
3535

3636
DatabaseEncryptionKeySecretDir = "database_encryption"
3737
DatabaseEncryptionKeySecretFile = "key"
3838
DatabaseEncryptionKeyConfigFile = "key.txt"
3939

40+
ConfigDir = "/opt/ydb/cfg"
41+
ConfigFileName = "config.yaml"
42+
4043
BinariesDir = "/opt/ydb/bin"
4144
DaemonBinaryName = "ydbd"
4245

46+
AdditionalSecretsDir = "/opt/ydb/secrets"
47+
AdditionalVolumesDir = "/opt/ydb/volumes"
48+
4349
DefaultRootUsername = "root"
4450
DefaultRootPassword = ""
4551
DefaultDatabaseDomain = "Root"
@@ -60,6 +66,8 @@ const (
6066
AnnotationGRPCPublicHost = "ydb.tech/grpc-public-host"
6167
AnnotationNodeHost = "ydb.tech/node-host"
6268
AnnotationNodeDomain = "ydb.tech/node-domain"
69+
AnnotationAuthTokenSecretName = "ydb.tech/auth-token-secret-name"
70+
AnnotationAuthTokenSecretKey = "ydb.tech/auth-token-secret-key"
6371

6472
AnnotationValueTrue = "true"
6573

internal/controllers/storage/controller_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ var _ = Describe("Storage controller medium tests", func() {
6262
})
6363

6464
It("Checking field propagation to objects", func() {
65+
getStatefulSet := func(objName string) (appsv1.StatefulSet, error) {
66+
foundStatefulSets := appsv1.StatefulSetList{}
67+
err := k8sClient.List(ctx, &foundStatefulSets, client.InNamespace(
68+
testobjects.YdbNamespace,
69+
))
70+
if err != nil {
71+
return appsv1.StatefulSet{}, err
72+
}
73+
for _, statefulSet := range foundStatefulSets.Items {
74+
if statefulSet.Name == objName {
75+
return statefulSet, nil
76+
}
77+
}
78+
79+
return appsv1.StatefulSet{}, fmt.Errorf("Statefulset with name %s was not found", objName)
80+
}
81+
6582
storageSample := testobjects.DefaultStorage(filepath.Join("..", "..", "..", "tests", "data", "storage-mirror-3-dc-config.yaml"))
6683

6784
tmpFilesDir := "/tmp/mounted_volume"
@@ -227,5 +244,66 @@ var _ = Describe("Storage controller medium tests", func() {
227244
return len(foundStatefulSets.Items)
228245
}, test.Timeout, test.Interval).Should(Equal(1))
229246
})
247+
248+
By("check --auth-token-file arg in StatefulSet...", func() {
249+
By("create auth-token Secret with default name...")
250+
defaultAuthTokenSecret := &corev1.Secret{
251+
ObjectMeta: metav1.ObjectMeta{
252+
Name: v1alpha1.AuthTokenSecretName,
253+
Namespace: testobjects.YdbNamespace,
254+
},
255+
StringData: map[string]string{
256+
v1alpha1.AuthTokenSecretKey: "StaffApiUserToken: 'default-token'",
257+
},
258+
}
259+
Expect(k8sClient.Create(ctx, defaultAuthTokenSecret))
260+
261+
By("append auth-token Secret inside Storage manifest...")
262+
Eventually(func() error {
263+
foundStorage := v1alpha1.Storage{}
264+
Expect(k8sClient.Get(ctx, types.NamespacedName{
265+
Name: testobjects.StorageName,
266+
Namespace: testobjects.YdbNamespace,
267+
}, &foundStorage))
268+
foundStorage.Spec.Secrets = []*corev1.LocalObjectReference{
269+
{
270+
Name: v1alpha1.AuthTokenSecretName,
271+
},
272+
}
273+
return k8sClient.Update(ctx, &foundStorage)
274+
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
275+
276+
checkAuthTokenArgs := func() error {
277+
statefulSet, err := getStatefulSet(testobjects.StorageName)
278+
if err != nil {
279+
return err
280+
}
281+
podContainerArgs := statefulSet.Spec.Template.Spec.Containers[0].Args
282+
var argExist bool
283+
var currentArgValue string
284+
authTokenFileArgValue := fmt.Sprintf("%s/%s/%s",
285+
v1alpha1.AdditionalSecretsDir,
286+
v1alpha1.AuthTokenSecretName,
287+
v1alpha1.AuthTokenSecretKey,
288+
)
289+
for idx, arg := range podContainerArgs {
290+
if arg == v1alpha1.AuthTokenFileArg {
291+
argExist = true
292+
currentArgValue = podContainerArgs[idx+1]
293+
break
294+
}
295+
}
296+
if !argExist {
297+
return fmt.Errorf("arg `%s` did not found in StatefulSet podTemplate args: %v", v1alpha1.AuthTokenFileArg, podContainerArgs)
298+
}
299+
if authTokenFileArgValue != currentArgValue {
300+
return fmt.Errorf("current arg `%s` value `%s` did not match with expected: %s", v1alpha1.AuthTokenFileArg, currentArgValue, authTokenFileArgValue)
301+
}
302+
return nil
303+
}
304+
305+
By("check that --auth-token-file arg was added to Statefulset template...")
306+
Eventually(checkAuthTokenArgs, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
307+
})
230308
})
231309
})

internal/resources/database_statefulset.go

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package resources
22

33
import (
4-
"context"
54
"errors"
65
"fmt"
7-
"log"
86
"regexp"
97
"strconv"
108

@@ -619,30 +617,23 @@ func (b *DatabaseStatefulSetBuilder) buildContainerArgs() ([]string, []string) {
619617
)
620618
}
621619

620+
authTokenSecretName := api.AuthTokenSecretName
621+
authTokenSecretKey := api.AuthTokenSecretKey
622+
if value, ok := b.ObjectMeta.Annotations[api.AnnotationAuthTokenSecretName]; ok {
623+
authTokenSecretName = value
624+
}
625+
if value, ok := b.ObjectMeta.Annotations[api.AnnotationAuthTokenSecretKey]; ok {
626+
authTokenSecretKey = value
627+
}
622628
for _, secret := range b.Spec.Secrets {
623-
exist, err := CheckSecretKey(
624-
context.Background(),
625-
b.GetNamespace(),
626-
b.RestConfig,
627-
&corev1.SecretKeySelector{
628-
LocalObjectReference: corev1.LocalObjectReference{
629-
Name: secret.Name,
630-
},
631-
Key: api.YdbAuthToken,
632-
},
633-
)
634-
if err != nil {
635-
log.Default().Printf("Failed to inspect a secret %s: %s\n", secret.Name, err.Error())
636-
continue
637-
}
638-
if exist {
629+
if secret.Name == authTokenSecretName {
639630
args = append(args,
640-
"--auth-token-file",
631+
api.AuthTokenFileArg,
641632
fmt.Sprintf(
642633
"%s/%s/%s",
643634
wellKnownDirForAdditionalSecrets,
644-
secret.Name,
645-
api.YdbAuthToken,
635+
authTokenSecretName,
636+
authTokenSecretKey,
646637
),
647638
)
648639
}

internal/resources/secret.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,6 @@ import (
1313
"sigs.k8s.io/controller-runtime/pkg/client"
1414
)
1515

16-
func CheckSecretKey(
17-
ctx context.Context,
18-
namespace string,
19-
config *rest.Config,
20-
secretKeyRef *corev1.SecretKeySelector,
21-
) (bool, error) {
22-
clientset, err := kubernetes.NewForConfig(config)
23-
if err != nil {
24-
return false, fmt.Errorf("failed to create kubernetes clientset, error: %w", err)
25-
}
26-
27-
getCtx, cancel := context.WithTimeout(ctx, time.Second)
28-
defer cancel()
29-
secret, err := clientset.CoreV1().Secrets(namespace).Get(getCtx, secretKeyRef.Name, metav1.GetOptions{})
30-
if err != nil {
31-
return false, fmt.Errorf("failed to get secret %s, error: %w", secretKeyRef.Name, err)
32-
}
33-
34-
_, exist := secret.Data[secretKeyRef.Key]
35-
return exist, nil
36-
}
37-
3816
func GetSecretKey(
3917
ctx context.Context,
4018
namespace string,

internal/resources/storage_statefulset.go

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package resources
22

33
import (
4-
"context"
54
"errors"
65
"fmt"
7-
"log"
86
"strconv"
97

108
appsv1 "k8s.io/api/apps/v1"
@@ -521,30 +519,23 @@ func (b *StorageStatefulSetBuilder) buildContainerArgs() ([]string, []string) {
521519
)
522520
}
523521

522+
authTokenSecretName := api.AuthTokenSecretName
523+
authTokenSecretKey := api.AuthTokenSecretKey
524+
if value, ok := b.ObjectMeta.Annotations[api.AnnotationAuthTokenSecretName]; ok {
525+
authTokenSecretName = value
526+
}
527+
if value, ok := b.ObjectMeta.Annotations[api.AnnotationAuthTokenSecretKey]; ok {
528+
authTokenSecretKey = value
529+
}
524530
for _, secret := range b.Spec.Secrets {
525-
exist, err := CheckSecretKey(
526-
context.Background(),
527-
b.GetNamespace(),
528-
b.RestConfig,
529-
&corev1.SecretKeySelector{
530-
LocalObjectReference: corev1.LocalObjectReference{
531-
Name: secret.Name,
532-
},
533-
Key: api.YdbAuthToken,
534-
},
535-
)
536-
if err != nil {
537-
log.Default().Printf("Failed to inspect a secret %s: %s\n", secret.Name, err.Error())
538-
continue
539-
}
540-
if exist {
531+
if secret.Name == authTokenSecretName {
541532
args = append(args,
542-
"--auth-token-file",
533+
api.AuthTokenFileArg,
543534
fmt.Sprintf(
544535
"%s/%s/%s",
545536
wellKnownDirForAdditionalSecrets,
546-
secret.Name,
547-
api.YdbAuthToken,
537+
authTokenSecretName,
538+
authTokenSecretKey,
548539
),
549540
)
550541
}

0 commit comments

Comments
 (0)