mirror of https://github.com/k3s-io/k3s
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
parent
31ac700bc9
commit
a3d768a7de
|
@ -141,9 +141,13 @@ func (c *Cluster) initClusterAndHTTPS(ctx context.Context) error {
|
|||
func tlsStorage(ctx context.Context, dataDir string, runtime *config.ControlRuntime) dynamiclistener.TLSStorage {
|
||||
fileStorage := file.New(filepath.Join(dataDir, "tls/dynamic-cert.json"))
|
||||
cache := memory.NewBacked(fileStorage)
|
||||
return kubernetes.New(ctx, func() *core.Factory {
|
||||
return runtime.Core
|
||||
}, metav1.NamespaceSystem, version.Program+"-serving", cache)
|
||||
coreGetter := func() *core.Factory {
|
||||
if coreFactory, ok := runtime.Core.(*core.Factory); ok {
|
||||
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
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
|
@ -372,11 +373,17 @@ type ControlRuntime struct {
|
|||
|
||||
K8s kubernetes.Interface
|
||||
K3s *k3s.Factory
|
||||
Core *core.Factory
|
||||
Core CoreFactory
|
||||
Event record.EventRecorder
|
||||
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 {
|
||||
return &ControlRuntime{
|
||||
ContainerRuntimeReady: containerRuntimeReady,
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package mock
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/k3s-io/k3s/pkg/daemons/config"
|
||||
"github.com/rancher/wrangler/v3/pkg/generated/controllers/core"
|
||||
corev1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1"
|
||||
"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
|
||||
//
|
||||
|
||||
// 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
|
||||
var _ core.Interface = &CoreMock{}
|
||||
|
||||
|
|
Loading…
Reference in New Issue