Skip to content
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

✨ Structured args in Testing #1541

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 54 additions & 8 deletions pkg/internal/testing/controlplane/apiserver.go
Expand Up @@ -44,6 +44,11 @@ type APIServer struct {
//
// If not specified, the minimal set of arguments to run the APIServer will
// be used.
//
// They will be loaded into the same argument set as Configure. Each flag
// will be Append-ed to the configured arguments just before launch.
//
// Deprecated: use Configure instead.
Args []string

// CertDir is a path to a directory containing whatever certificates the
Expand Down Expand Up @@ -72,20 +77,34 @@ type APIServer struct {
Err io.Writer

processState *process.State

// args contains the structured arguments to use for running the API server
// Lazily initialized by .Configure(), Defaulted eventually with .defaultArgs()
args *process.Arguments
}

// Configure returns Arguments that may be used to customize the
// flags used to launch the API server. A set of defaults will
// be applied underneath.
func (s *APIServer) Configure() *process.Arguments {
if s.args == nil {
s.args = process.EmptyArguments()
}
return s.args
}

// Start starts the apiserver, waits for it to come up, and returns an error,
// if occurred.
func (s *APIServer) Start() error {
if s.processState == nil {
if err := s.setState(); err != nil {
if err := s.setProcessState(); err != nil {
return err
}
}
return s.processState.Start(s.Out, s.Err)
}

func (s *APIServer) setState() error {
func (s *APIServer) setProcessState() error {
if s.EtcdURL == nil {
return fmt.Errorf("expected EtcdURL to be configured")
}
Expand Down Expand Up @@ -133,15 +152,42 @@ func (s *APIServer) setState() error {
return err
}

args := s.Args
if len(args) == 0 {
args = APIServerDefaultArgs
}

s.processState.Args, err = process.RenderTemplates(args, s)
s.processState.Args, err = process.TemplateAndArguments(s.Args, s.Configure(), process.TemplateDefaults{
Data: s,
Defaults: s.defaultArgs(),
// as per kubernetes-sigs/controller-runtime#641, we need this (we
// probably need other stuff too, but this is the only thing that was
// previously considered a "minimal default")
MinimalDefaults: map[string][]string{
"service-cluster-ip-range": []string{"10.0.0.0/24"},
},
})
return err
}

func (s *APIServer) defaultArgs() map[string][]string {
args := map[string][]string{
"advertise-address": []string{"127.0.0.1"},
"service-cluster-ip-range": []string{"10.0.0.0/24"},
"allow-privileged": []string{"true"},
// we're keeping this disabled because if enabled, default SA is
// missing which would force all tests to create one in normal
// apiserver operation this SA is created by controller, but that is
// not run in integration environment
"disable-admission-plugins": []string{"ServiceAccount"},
"cert-dir": []string{s.CertDir},
"secure-port": []string{strconv.Itoa(s.SecurePort)},
}
if s.EtcdURL != nil {
args["etcd-servers"] = []string{s.EtcdURL.String()}
}
if s.URL != nil {
args["insecure-port"] = []string{s.URL.Port()}
args["insecure-bind-address"] = []string{s.URL.Hostname()}
}
return args
}

func (s *APIServer) populateAPIServerCerts() error {
_, statErr := os.Stat(filepath.Join(s.CertDir, "apiserver.crt"))
if !os.IsNotExist(statErr) {
Expand Down
48 changes: 38 additions & 10 deletions pkg/internal/testing/controlplane/etcd.go
Expand Up @@ -36,6 +36,11 @@ type Etcd struct {
//
// If not specified, the minimal set of arguments to run the Etcd will be
// used.
//
// They will be loaded into the same argument set as Configure. Each flag
// will be Append-ed to the configured arguments just before launch.
//
// Deprecated: use Configure instead.
Args []string

// DataDir is a path to a directory in which etcd can store its state.
Expand All @@ -59,22 +64,24 @@ type Etcd struct {

// processState contains the actual details about this running process
processState *process.State

// args contains the structured arguments to use for running etcd.
// Lazily initialized by .Configure(), Defaulted eventually with .defaultArgs()
args *process.Arguments
}

// Start starts the etcd, waits for it to come up, and returns an error, if one
// occoured.
func (e *Etcd) Start() error {
if e.processState == nil {
if err := e.setState(); err != nil {
if err := e.setProcessState(); err != nil {
return err
}
}
return e.processState.Start(e.Out, e.Err)
}

func (e *Etcd) setState() error {
var err error

func (e *Etcd) setProcessState() error {
e.processState = &process.State{
Dir: e.DataDir,
Path: e.Path,
Expand Down Expand Up @@ -106,12 +113,11 @@ func (e *Etcd) setState() error {
e.StartTimeout = e.processState.StartTimeout
e.StopTimeout = e.processState.StopTimeout

args := e.Args
if len(args) == 0 {
args = EtcdDefaultArgs
}

e.processState.Args, err = process.RenderTemplates(args, e)
var err error
e.processState.Args, err = process.TemplateAndArguments(e.Args, e.Configure(), process.TemplateDefaults{
Data: e,
Defaults: e.defaultArgs(),
})
return err
}

Expand All @@ -121,6 +127,28 @@ func (e *Etcd) Stop() error {
return e.processState.Stop()
}

func (e *Etcd) defaultArgs() map[string][]string {
args := map[string][]string{
"listen-peer-urls": []string{"http://localhost:0"},
"data-dir": []string{e.DataDir},
}
if e.URL != nil {
args["advertise-client-urls"] = []string{e.URL.String()}
args["listen-client-urls"] = []string{e.URL.String()}
}
return args
}

// Configure returns Arguments that may be used to customize the
// flags used to launch etcd. A set of defaults will
// be applied underneath.
func (e *Etcd) Configure() *process.Arguments {
if e.args == nil {
e.args = process.EmptyArguments()
}
return e.args
}

// EtcdDefaultArgs exposes the default args for Etcd so that you
// can use those to append your own additional arguments.
var EtcdDefaultArgs = []string{
Expand Down