Commit fbb971c4 authored by Brad Davidson's avatar Brad Davidson Committed by Brad Davidson

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: 's avatarBrad Davidson <brad.davidson@rancher.com> (cherry picked from commit e6327652) Signed-off-by: 's avatarBrad Davidson <brad.davidson@rancher.com>
parent 4c2fd2e3
...@@ -141,9 +141,13 @@ func (c *Cluster) initClusterAndHTTPS(ctx context.Context) error { ...@@ -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
......
package config package config
import ( import (
"context"
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
...@@ -373,11 +374,17 @@ type ControlRuntime struct { ...@@ -373,11 +374,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,
......
package mock package mock
import ( import (
"context"
"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 ( ...@@ -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{}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment