Reputation: 85
I have the below code in a test function
import (
"k8s.io/client-go/kubernetes/fake"
)
// Mock Kubernetes client
k8sClient := fake.NewClientset()
// Mock service account
serviceAccountName := "test-service-account"
namespace := "test-namespace"
roleArn := "arn:aws:iam::123456789012:role/test-role"
tokenAudience := "sts.amazonaws.com"
// Create a mock service account object with annotations
mockSA := &corev1.ServiceAccount{
TypeMeta: metav1.TypeMeta{
Kind: "ServiceAccount",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: serviceAccountName,
Namespace: namespace,
Annotations: map[string]string{
roleARNAnnotation: roleArn,
audienceAnnotation: tokenAudience,
},
},
}
ctx := context.TODO()
// Create the mock service account
_, err := k8sClient.CoreV1().ServiceAccounts(namespace).Create(ctx, mockSA, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create mock service account: %v", err)
}
// Create the token for service account
token, err := k8sClient.CoreV1().ServiceAccounts(namespace).CreateToken(ctx, serviceAccountName, &authv1.TokenRequest{
Spec: authv1.TokenRequestSpec{
Audiences: []string{tokenAudience},
},
}, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create service account token: %v", err)
}
When I run this code, I get the error: failed to create service account token: serviceaccounts "" not found
. However, I can see that the serviceaccount is created properly by fake client. Not sure why the CreateToken call is refering to a blank serviceaccount. I tried figuring out the issue, but couldn't find the exact root cause.
Anybody experienced this issue before? Any help/suggestions would be appreciated.
Upvotes: 1
Views: 35