Skip to content

Commit

Permalink
use reconciliation context in kubeconfig provider
Browse files Browse the repository at this point in the history
  • Loading branch information
m00g3n committed Nov 17, 2023
1 parent c8fa74a commit 3a6736c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions internal/controller/gardener_cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 15 additions & 11 deletions internal/controller/mocks/KubeconfigProvider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions internal/gardener/KubeConfigProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 3a6736c

Please sign in to comment.