pbacsko commented on code in PR #915:
URL: https://github.com/apache/yunikorn-k8shim/pull/915#discussion_r1806368001
##########
test/e2e/framework/helpers/k8s/k8s_utils.go:
##########
@@ -1779,3 +1822,96 @@ func (k *KubeCtl) DeleteStorageClass(scName string)
error {
}
return nil
}
+
+func (k *KubeCtl) GetSecrets(namespace string) (*v1.SecretList, error) {
+ return k.clientSet.CoreV1().Secrets(namespace).List(context.TODO(),
metav1.ListOptions{})
+}
+
+func (k *KubeCtl) GetSecretValue(namespace, secretName, key string) (string,
error) {
+ var secret *v1.Secret
+ var err error
+
+ // Retry loop in case secret is not yet populated
+ for i := 0; i < 5; i++ {
+ secret, err =
k.clientSet.CoreV1().Secrets(namespace).Get(context.TODO(), secretName,
metav1.GetOptions{})
+ if err != nil {
+ return "", err
+ }
+
+ // Check if the secret contains data
+ if len(secret.Data) > 0 {
+ value, ok := secret.Data[key]
+ if !ok {
+ return "", fmt.Errorf("key %s not found in
secret %s", key, secretName)
+ }
+ return string(value), nil
+ }
+
+ // Wait before retrying
+ time.Sleep(2 * time.Second)
Review Comment:
You don't need to code the retry. We can take advantage of existing k8s
functions.
To be consistent with existing code, let's use the following:
```
func (k *KubeCtl) GetSecretValue(namespace, secretName, key string) (string,
error) {
secret, err := k.GetSecret(namespace, secretName)
if err != nil {
return "", err
}
// Check if the key exists in the secret
value, ok := secret.Data[key]
if !ok {
return "", fmt.Errorf("key %s not found in secret %s", key,
secretName)
}
return string(value), nil
}
func (k *KubeCtl) GetSecret(namespace, secretName string) (*v1.Secret,
error) {
secret, err :=
k.clientSet.CoreV1().Secrets(namespace).Get(context.TODO(), secretName,
metav1.GetOptions{})
if err != nil {
return nil, err
}
return secret, nil
}
func (k *KubeCtl) WaitForSecret(namespace, secretName string, timeout
time.Duration) error {
var cond wait.ConditionFunc
cond = func() (done bool, err error) {
secret, err := k.GetSecret(namespace, secretName)
if err != nil {
return false, err
}
if secret != nil {
return false, nil
}
return true, nil
}
return wait.PollUntilContextTimeout(context.TODO(), time.Second,
timeout, false, cond.WithContext())
}
```
First, you call `kClient.WaitForSecret()` to indicate that you're waiting
for a secret to appear over the API. Then call `kClient.GetSecretValue()`. By
having these 3 methods, all of them are usable indepdently of one another.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]