Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fake client gives error using CreateToken operation for ServiceAccounts #1396

Open
gdasson opened this issue Dec 23, 2024 · 0 comments
Open

Comments

@gdasson
Copy link

gdasson commented Dec 23, 2024

I have the below code in a test function

     // 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.

@gdasson gdasson changed the title fake client gives error using CreateToken operation fake client gives error using CreateToken operation for ServiceAccounts Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant