Skip to content

Expose underlying server struct #3422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sdk/testing/server/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/cert"

"github.com/kcp-dev/kcp/pkg/server"
corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
)

Expand Down Expand Up @@ -98,6 +99,10 @@ func (s *externalKCPServer) KubeconfigPath() string {
return s.kubeconfigPath
}

func (s *externalKCPServer) Server() *server.Server {
return nil
}

func (s *externalKCPServer) RawConfig() (clientcmdapi.Config, error) {
return s.cfg.RawConfig()
}
Expand Down
29 changes: 19 additions & 10 deletions sdk/testing/server/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (

"github.com/kcp-dev/logicalcluster/v3"

"github.com/kcp-dev/kcp/pkg/server"
corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
kcpscheme "github.com/kcp-dev/kcp/sdk/client/clientset/versioned/scheme"
"github.com/kcp-dev/kcp/sdk/testing/env"
Expand All @@ -61,14 +62,14 @@ const kcpBinariesDirEnvDir = "KCP_BINARIES_DIR"
// RunInProcessFunc instantiates the kcp server in process for easier debugging.
// It is here to decouple the rest of the code from kcp core dependencies.
// Deprecated: Use ContextRunInProcessFunc instead.
var RunInProcessFunc func(t TestingT, dataDir string, args []string) (<-chan struct{}, error)
var RunInProcessFunc func(t TestingT, dataDir string, args []string) (*server.Server, <-chan struct{}, error)

type KcpRunner func(context.Context, TestingT, Config) (<-chan struct{}, error)
type KcpRunner func(context.Context, TestingT, Config) (*server.Server, <-chan struct{}, error)

// ContextRunInProcessFunc instantiates the kcp server in process for easier debugging.
// It is here to decouple the rest of the code from kcp core dependencies.
var ContextRunInProcessFunc KcpRunner = func(ctx context.Context, t TestingT, cfg Config) (<-chan struct{}, error) {
return nil, fmt.Errorf("not implemented")
var ContextRunInProcessFunc KcpRunner = func(ctx context.Context, t TestingT, cfg Config) (*server.Server, <-chan struct{}, error) {
return nil, nil, fmt.Errorf("not implemented")
}

// Fixture manages the lifecycle of a set of kcp servers.
Expand Down Expand Up @@ -168,6 +169,7 @@ type kcpServer struct {
cfg Config
lock *sync.Mutex
clientCfg clientcmd.ClientConfig
server *server.Server
cancel func()
shutdownComplete bool
}
Expand Down Expand Up @@ -273,7 +275,7 @@ func (c *kcpServer) Run(t TestingT) error {
// variant
runner = ContextRunInProcessFunc
} else {
runner = func(ctx context.Context, t TestingT, cfg Config) (<-chan struct{}, error) {
runner = func(ctx context.Context, t TestingT, cfg Config) (*server.Server, <-chan struct{}, error) {
t.Log("RunInProcessFunc is deprecated, please migrate to ContextRunInProcessFunc")
t.Log("RunInProcessFunc is deprecated, stopping the server will not work")
return RunInProcessFunc(t, cfg.DataDir, cfg.Args)
Expand All @@ -286,11 +288,12 @@ func (c *kcpServer) Run(t TestingT) error {

ctx, ctxCancel := context.WithCancel(context.Background())

shutdownComplete, err := runner(ctx, t, c.cfg)
s, shutdownComplete, err := runner(ctx, t, c.cfg)
if err != nil {
ctxCancel()
return err
}
c.server = s

c.cancel = func() {
t.Log("cleanup: canceling context")
Expand All @@ -309,6 +312,12 @@ func (c *kcpServer) Run(t TestingT) error {
return nil
}

func (c *kcpServer) Server() *server.Server {
c.lock.Lock()
defer c.lock.Unlock()
return c.server
}

func (c *kcpServer) Stop() {
if c.cancel == nil {
return
Expand All @@ -322,7 +331,7 @@ func (c *kcpServer) Stopped() bool {
return c.shutdownComplete
}

func runExternal(ctx context.Context, t TestingT, cfg Config) (<-chan struct{}, error) {
func runExternal(ctx context.Context, t TestingT, cfg Config) (*server.Server, <-chan struct{}, error) {
commandLine := append(StartKcpCommand("KCP"), cfg.Args...)

t.Logf("running: %v", strings.Join(commandLine, " "))
Expand All @@ -341,7 +350,7 @@ func runExternal(ctx context.Context, t TestingT, cfg Config) (<-chan struct{},

logFile, err := os.Create(filepath.Join(cfg.ArtifactDir, "kcp.log"))
if err != nil {
return nil, fmt.Errorf("could not create log file: %w", err)
return nil, nil, fmt.Errorf("could not create log file: %w", err)
}

// Closing the logfile is necessary so the cmd.Wait() call in the goroutine below can finish (it only finishes
Expand All @@ -368,7 +377,7 @@ func runExternal(ctx context.Context, t TestingT, cfg Config) (<-chan struct{},
if os.Getenv(kcpBinariesDirEnvDir) == "" && commandLine[0] == "kcp" {
t.Log("Consider setting KCP_BINARIES_DIR pointing to a directory with a kcp binary.")
}
return nil, fmt.Errorf("failed to start kcp: %w", err)
return nil, nil, fmt.Errorf("failed to start kcp: %w", err)
}

go func() {
Expand Down Expand Up @@ -397,7 +406,7 @@ func runExternal(ctx context.Context, t TestingT, cfg Config) (<-chan struct{},
}
}()

return shutdownComplete, nil
return nil, shutdownComplete, nil
}

// filterKcpLogs is a silly hack to get rid of the nonsense output that
Expand Down
4 changes: 4 additions & 0 deletions sdk/testing/server/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package server

import (
"github.com/kcp-dev/kcp/pkg/server"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
Expand All @@ -39,4 +40,7 @@ type RunningServer interface {
// Stopped returns true if the server has ran and stopped.
// Stopped is a noop for external servers.
Stopped() bool
// Server returns the underlying server struct.
// It may be nil. Nil does not indicate an error.
Server() *server.Server
}
18 changes: 9 additions & 9 deletions test/e2e/framework/inprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
func init() {
globalOptionsLock := &sync.Mutex{}

kcptestingserver.ContextRunInProcessFunc = func(ctx context.Context, t kcptestingserver.TestingT, cfg kcptestingserver.Config) (<-chan struct{}, error) {
kcptestingserver.ContextRunInProcessFunc = func(ctx context.Context, t kcptestingserver.TestingT, cfg kcptestingserver.Config) (*server.Server, <-chan struct{}, error) {
ctx, cancel := context.WithCancel(ctx)
t.Cleanup(cancel)

Expand All @@ -52,38 +52,38 @@ func init() {
all.AddFlagSet(fs)
}
if err := all.Parse(cfg.Args); err != nil {
return nil, err
return nil, nil, err
}

completed, err := serverOptions.Complete(ctx)
if err != nil {
return nil, err
return nil, nil, err
}
if errs := completed.Validate(); len(errs) > 0 {
return nil, utilerrors.NewAggregate(errs)
return nil, nil, utilerrors.NewAggregate(errs)
}

config, err := server.NewConfig(ctx, completed.Server)
if err != nil {
return nil, err
return nil, nil, err
}

completedConfig, err := config.Complete()
if err != nil {
return nil, err
return nil, nil, err
}

// the etcd server must be up before NewServer because storage decorators access it right away
if completedConfig.EmbeddedEtcd.Config != nil {
if err := embeddedetcd.NewServer(completedConfig.EmbeddedEtcd).Run(ctx); err != nil {
return nil, err
return nil, nil, err
}
}

stopCh := make(chan struct{})
s, err := server.NewServer(completedConfig)
if err != nil {
return nil, err
return nil, nil, err
}
go func() {
defer close(stopCh)
Expand All @@ -92,6 +92,6 @@ func init() {
}
}()

return stopCh, nil
return s, stopCh, nil
}
}
6 changes: 6 additions & 0 deletions test/integration/framework/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func TestServer(t *testing.T) {
server.Stop()
}

func TestServerServer(t *testing.T) {
t.Parallel()
server, _, _ := StartTestServer(t)
require.NotNil(t, server.Server().Apis)
}

func TestServerCreateConfigMap(t *testing.T) {
t.Parallel()
_, _, kubeClient := StartTestServer(t)
Expand Down