diff --git a/internal/controller/gardener_cluster_controller.go b/internal/controller/gardener_cluster_controller.go index f04dc6ad..112d7593 100644 --- a/internal/controller/gardener_cluster_controller.go +++ b/internal/controller/gardener_cluster_controller.go @@ -62,7 +62,7 @@ func NewGardenerClusterController(mgr ctrl.Manager, kubeconfigProvider Kubeconfi //go:generate mockery --name=KubeconfigProvider type KubeconfigProvider interface { - Fetch(shootName string) (string, error) + Fetch(ctx context.Context, shootName string) (string, error) } //+kubebuilder:rbac:groups=infrastructuremanager.kyma-project.io,resources=gardenerclusters,verbs=get;list;watch;create;update;patch;delete @@ -226,7 +226,7 @@ func (controller *GardenerClusterController) handleKubeconfig(ctx context.Contex return ksZero, err } - kubeconfig, err := controller.KubeconfigProvider.Fetch(cluster.Spec.Shoot.Name) + kubeconfig, err := controller.KubeconfigProvider.Fetch(ctx, cluster.Spec.Shoot.Name) if err != nil { cluster.UpdateConditionForErrorState(imv1.ConditionTypeKubeconfigManagement, imv1.ConditionReasonFailedToGetKubeconfig, metav1.ConditionTrue, err) return ksZero, err diff --git a/internal/controller/mocks/KubeconfigProvider.go b/internal/controller/mocks/KubeconfigProvider.go index b4efe0ee..5f04e7be 100644 --- a/internal/controller/mocks/KubeconfigProvider.go +++ b/internal/controller/mocks/KubeconfigProvider.go @@ -1,31 +1,35 @@ -// Code generated by mockery v2.33.2. DO NOT EDIT. +// Code generated by mockery v2.35.2. DO NOT EDIT. package mocks -import mock "github.com/stretchr/testify/mock" +import ( + context "context" + + mock "github.com/stretchr/testify/mock" +) // KubeconfigProvider is an autogenerated mock type for the KubeconfigProvider type type KubeconfigProvider struct { mock.Mock } -// Fetch provides a mock function with given fields: shootName -func (_m *KubeconfigProvider) Fetch(shootName string) (string, error) { - ret := _m.Called(shootName) +// Fetch provides a mock function with given fields: ctx, shootName +func (_m *KubeconfigProvider) Fetch(ctx context.Context, shootName string) (string, error) { + ret := _m.Called(ctx, shootName) var r0 string var r1 error - if rf, ok := ret.Get(0).(func(string) (string, error)); ok { - return rf(shootName) + if rf, ok := ret.Get(0).(func(context.Context, string) (string, error)); ok { + return rf(ctx, shootName) } - if rf, ok := ret.Get(0).(func(string) string); ok { - r0 = rf(shootName) + if rf, ok := ret.Get(0).(func(context.Context, string) string); ok { + r0 = rf(ctx, shootName) } else { r0 = ret.Get(0).(string) } - if rf, ok := ret.Get(1).(func(string) error); ok { - r1 = rf(shootName) + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, shootName) } else { r1 = ret.Error(1) } diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index 15c7c8b3..d4c08cf4 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -27,6 +27,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/pkg/errors" + . "github.com/stretchr/testify/mock" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" @@ -45,6 +46,7 @@ var ( testEnv *envtest.Environment //nolint:gochecknoglobals suiteCtx context.Context //nolint:gochecknoglobals cancelSuiteCtx context.CancelFunc //nolint:gochecknoglobals + anyContext = MatchedBy(func(ctx context.Context) bool { return true }) ) const TestKubeconfigValidityTime = 24 * time.Hour @@ -102,12 +104,12 @@ var _ = BeforeSuite(func() { }) func setupKubeconfigProviderMock(kpMock *mocks.KubeconfigProvider) { - kpMock.On("Fetch", "shootName1").Return("kubeconfig1", nil) - kpMock.On("Fetch", "shootName2").Return("kubeconfig2", nil) - kpMock.On("Fetch", "shootName3").Return("", errors.New("failed to get kubeconfig")) - kpMock.On("Fetch", "shootName6").Return("kubeconfig6", nil) - kpMock.On("Fetch", "shootName4").Return("kubeconfig4", nil) - kpMock.On("Fetch", "shootName5").Return("kubeconfig5", nil) + kpMock.On("Fetch", anyContext, "shootName1").Return("kubeconfig1", nil) + kpMock.On("Fetch", anyContext, "shootName2").Return("kubeconfig2", nil) + kpMock.On("Fetch", anyContext, "shootName3").Return("", errors.New("failed to get kubeconfig")) + kpMock.On("Fetch", anyContext, "shootName6").Return("kubeconfig6", nil) + kpMock.On("Fetch", anyContext, "shootName4").Return("kubeconfig4", nil) + kpMock.On("Fetch", anyContext, "shootName5").Return("kubeconfig5", nil) } var _ = AfterSuite(func() { diff --git a/internal/gardener/KubeConfigProvider.go b/internal/gardener/KubeConfigProvider.go index 83c81879..97920a79 100644 --- a/internal/gardener/KubeConfigProvider.go +++ b/internal/gardener/KubeConfigProvider.go @@ -38,8 +38,8 @@ func NewKubeconfigProvider( } } -func (kp KubeconfigProvider) Fetch(shootName string) (string, error) { - shoot, err := kp.shootClient.Get(context.Background(), shootName, v1.GetOptions{}) +func (kp KubeconfigProvider) Fetch(ctx context.Context, shootName string) (string, error) { + shoot, err := kp.shootClient.Get(ctx, shootName, v1.GetOptions{}) if err != nil { return "", errors.Wrap(err, "failed to get shoot") }