token_fetcher.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package fake
  13. import (
  14. "context"
  15. authv1 "k8s.io/api/authentication/v1"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. k8sv1 "k8s.io/client-go/kubernetes/typed/core/v1"
  18. )
  19. func NewCreateTokenMock(token string) *MockK8sV1 {
  20. return &MockK8sV1{
  21. token: token,
  22. }
  23. }
  24. // Mock K8s client for creating tokens.
  25. type MockK8sV1 struct {
  26. k8sv1.CoreV1Interface
  27. token string
  28. }
  29. func (m *MockK8sV1) ServiceAccounts(namespace string) k8sv1.ServiceAccountInterface {
  30. return &MockK8sV1SA{v1mock: m}
  31. }
  32. // Mock the K8s service account client.
  33. type MockK8sV1SA struct {
  34. k8sv1.ServiceAccountInterface
  35. v1mock *MockK8sV1
  36. }
  37. func (ma *MockK8sV1SA) CreateToken(
  38. ctx context.Context,
  39. serviceAccountName string,
  40. tokenRequest *authv1.TokenRequest,
  41. opts metav1.CreateOptions,
  42. ) (*authv1.TokenRequest, error) {
  43. return &authv1.TokenRequest{
  44. Status: authv1.TokenRequestStatus{
  45. Token: ma.v1mock.token,
  46. },
  47. }, nil
  48. }