token_fetcher.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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() *MockK8sV1 {
  20. return &MockK8sV1{}
  21. }
  22. // Mock K8s client for creating tokens.
  23. type MockK8sV1 struct {
  24. k8sv1.CoreV1Interface
  25. token string
  26. err error
  27. }
  28. func (m *MockK8sV1) WithToken(token string) *MockK8sV1 {
  29. m.token = token
  30. return m
  31. }
  32. func (m *MockK8sV1) WithError(err error) *MockK8sV1 {
  33. m.err = err
  34. return m
  35. }
  36. func (m *MockK8sV1) ServiceAccounts(namespace string) k8sv1.ServiceAccountInterface {
  37. return &MockK8sV1SA{v1mock: m}
  38. }
  39. // Mock the K8s service account client.
  40. type MockK8sV1SA struct {
  41. k8sv1.ServiceAccountInterface
  42. v1mock *MockK8sV1
  43. }
  44. func (ma *MockK8sV1SA) CreateToken(
  45. ctx context.Context,
  46. serviceAccountName string,
  47. tokenRequest *authv1.TokenRequest,
  48. opts metav1.CreateOptions,
  49. ) (*authv1.TokenRequest, error) {
  50. if ma.v1mock.err != nil {
  51. return nil, ma.v1mock.err
  52. }
  53. return &authv1.TokenRequest{
  54. Status: authv1.TokenRequestStatus{
  55. Token: ma.v1mock.token,
  56. },
  57. }, nil
  58. }