Replace *core.Factory with CoreFactory interface

Make this field an interface instead of pointer to allow mocking. Not sure why wrangler has a type that returns an interface instead of just making it an interface itself. Wrangler in general is hard to mock for testing.

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
(cherry picked from commit e6327652f0)
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
pull/11561/head
Brad Davidson 2024-12-17 23:23:54 +00:00 committed by Brad Davidson
parent 31ac700bc9
commit a3d768a7de
3 changed files with 43 additions and 4 deletions

View File

@ -141,9 +141,13 @@ func (c *Cluster) initClusterAndHTTPS(ctx context.Context) error {
func tlsStorage(ctx context.Context, dataDir string, runtime *config.ControlRuntime) dynamiclistener.TLSStorage { func tlsStorage(ctx context.Context, dataDir string, runtime *config.ControlRuntime) dynamiclistener.TLSStorage {
fileStorage := file.New(filepath.Join(dataDir, "tls/dynamic-cert.json")) fileStorage := file.New(filepath.Join(dataDir, "tls/dynamic-cert.json"))
cache := memory.NewBacked(fileStorage) cache := memory.NewBacked(fileStorage)
return kubernetes.New(ctx, func() *core.Factory { coreGetter := func() *core.Factory {
return runtime.Core if coreFactory, ok := runtime.Core.(*core.Factory); ok {
}, metav1.NamespaceSystem, version.Program+"-serving", cache) return coreFactory
}
return nil
}
return kubernetes.New(ctx, coreGetter, metav1.NamespaceSystem, version.Program+"-serving", cache)
} }
// wrapHandler wraps the dynamiclistener request handler, adding a User-Agent value to // wrapHandler wraps the dynamiclistener request handler, adding a User-Agent value to

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"context"
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
@ -372,11 +373,17 @@ type ControlRuntime struct {
K8s kubernetes.Interface K8s kubernetes.Interface
K3s *k3s.Factory K3s *k3s.Factory
Core *core.Factory Core CoreFactory
Event record.EventRecorder Event record.EventRecorder
EtcdConfig endpoint.ETCDConfig EtcdConfig endpoint.ETCDConfig
} }
type CoreFactory interface {
Core() core.Interface
Sync(ctx context.Context) error
Start(ctx context.Context, defaultThreadiness int) error
}
func NewRuntime(containerRuntimeReady <-chan struct{}) *ControlRuntime { func NewRuntime(containerRuntimeReady <-chan struct{}) *ControlRuntime {
return &ControlRuntime{ return &ControlRuntime{
ContainerRuntimeReady: containerRuntimeReady, ContainerRuntimeReady: containerRuntimeReady,

View File

@ -1,7 +1,10 @@
package mock package mock
import ( import (
"context"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/rancher/wrangler/v3/pkg/generated/controllers/core" "github.com/rancher/wrangler/v3/pkg/generated/controllers/core"
corev1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1" corev1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/v3/pkg/generic/fake" "github.com/rancher/wrangler/v3/pkg/generic/fake"
@ -16,6 +19,31 @@ import (
// Mocks so that we can call Runtime.Core.Core().V1() without a functioning apiserver // Mocks so that we can call Runtime.Core.Core().V1() without a functioning apiserver
// //
// explicit interface check for core factory mock
var _ config.CoreFactory = &CoreFactoryMock{}
type CoreFactoryMock struct {
CoreMock *CoreMock
}
func NewCoreFactory(c *gomock.Controller) *CoreFactoryMock {
return &CoreFactoryMock{
CoreMock: NewCore(c),
}
}
func (m *CoreFactoryMock) Core() core.Interface {
return m.CoreMock
}
func (m *CoreFactoryMock) Sync(ctx context.Context) error {
return nil
}
func (m *CoreFactoryMock) Start(ctx context.Context, defaultThreadiness int) error {
return nil
}
// explicit interface check for core mock // explicit interface check for core mock
var _ core.Interface = &CoreMock{} var _ core.Interface = &CoreMock{}