token_fetcher.go 1.6 KB

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