Simplify and clean more

This commit is contained in:
Gareth Smith 2018-01-11 10:57:46 +00:00 committed by Hannes Hörl
parent 284bf217da
commit 12403c5eaf
4 changed files with 23 additions and 49 deletions

View File

@ -18,9 +18,9 @@ import (
// APIServer knows how to run a kubernetes apiserver.
type APIServer struct {
// Address is the address, a host and a port, the ApiServer should listen on for client connections.
// URL is the address, a host and a port, the ApiServer should listen on for client connections.
// If this is not specified, we default to a random free port on localhost.
Address *url.URL
URL *url.URL
// Path is the path to the apiserver binary. If this is left as the empty
// string, we will attempt to locate a binary, by checking for the
@ -49,14 +49,6 @@ type APIServer struct {
stdErr *gbytes.Buffer
}
// URL returns the URL APIServer is listening on. Clients can use this to connect to APIServer.
func (s *APIServer) URL() (string, error) {
if s.Address == nil {
return "", fmt.Errorf("APIServer's Address is not initialized or configured")
}
return s.Address.String(), nil
}
// Start starts the apiserver, waits for it to come up, and returns an error, if occoured.
func (s *APIServer) Start() error {
err := s.ensureInitialized()
@ -69,13 +61,7 @@ func (s *APIServer) Start() error {
return err
}
etcdURLString, err := s.Etcd.URL()
if err != nil {
if etcdStopErr := s.Etcd.Stop(); etcdStopErr != nil {
return fmt.Errorf("%s, %s", err.Error(), etcdStopErr.Error())
}
return err
}
etcdURLString := s.Etcd.URL.String()
args := []string{
"--authorization-mode=Node,RBAC",
@ -87,11 +73,11 @@ func (s *APIServer) Start() error {
"--storage-backend=etcd3",
fmt.Sprintf("--etcd-servers=%s", etcdURLString),
fmt.Sprintf("--cert-dir=%s", s.actualCertDir),
fmt.Sprintf("--insecure-port=%s", s.Address.Port()),
fmt.Sprintf("--insecure-bind-address=%s", s.Address.Hostname()),
fmt.Sprintf("--insecure-port=%s", s.URL.Port()),
fmt.Sprintf("--insecure-bind-address=%s", s.URL.Hostname()),
}
detectedStart := s.stdErr.Detect(fmt.Sprintf("Serving insecurely on %s", s.Address.Host))
detectedStart := s.stdErr.Detect(fmt.Sprintf("Serving insecurely on %s", s.URL.Host))
timedOut := time.After(s.StartTimeout)
command := exec.Command(s.Path, args...)
@ -112,13 +98,13 @@ func (s *APIServer) ensureInitialized() error {
if s.Path == "" {
s.Path = internal.BinPathFinder("kube-apiserver")
}
if s.Address == nil {
if s.URL == nil {
am := &internal.AddressManager{}
port, host, err := am.Initialize()
if err != nil {
return err
}
s.Address = &url.URL{
s.URL = &url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%d", host, port),
}

View File

@ -1,6 +1,10 @@
// Package test an integration test framework for k8s
package test
import (
"net/url"
)
// ControlPlane is a struct that knows how to start your test control plane.
//
// Right now, that means Etcd and your APIServer. This is likely to increase in future.
@ -8,8 +12,6 @@ type ControlPlane struct {
APIServer *APIServer
}
//go:generate counterfeiter . ControlPlaneProcess
// NewControlPlane will give you a ControlPlane struct that's properly wired together.
func NewControlPlane() *ControlPlane {
return &ControlPlane{
@ -27,7 +29,7 @@ func (f *ControlPlane) Stop() error {
return f.APIServer.Stop()
}
// APIServerURL returns the URL to the APIServer. Clients can use this URL to connect to the APIServer.
func (f *ControlPlane) APIServerURL() (string, error) {
return f.APIServer.URL()
// APIURL returns the URL you should connect to to talk to your API.
func (f *ControlPlane) APIURL() *url.URL {
return f.APIServer.URL
}

View File

@ -20,7 +20,7 @@ import (
// The documentation and examples for the Etcd's properties can be found in
// in the documentation for the `APIServer`, as both implement a `ControlPaneProcess`.
type Etcd struct {
Address *url.URL
URL *url.URL
Path string
DataDir string
actualDataDir string
@ -31,14 +31,6 @@ type Etcd struct {
stdErr *gbytes.Buffer
}
// URL returns the URL Etcd is listening on. Clients can use this to connect to Etcd.
func (e *Etcd) URL() (string, error) {
if e.Address == nil {
return "", fmt.Errorf("Etcd's Address not initialized or configured")
}
return e.Address.String(), nil
}
// Start starts the etcd, waits for it to come up, and returns an error, if occoured.
func (e *Etcd) Start() error {
err := e.ensureInitialized()
@ -49,13 +41,13 @@ func (e *Etcd) Start() error {
args := []string{
"--debug",
"--listen-peer-urls=http://localhost:0",
fmt.Sprintf("--advertise-client-urls=%s", e.Address),
fmt.Sprintf("--listen-client-urls=%s", e.Address),
fmt.Sprintf("--advertise-client-urls=%s", e.URL),
fmt.Sprintf("--listen-client-urls=%s", e.URL),
fmt.Sprintf("--data-dir=%s", e.actualDataDir),
}
detectedStart := e.stdErr.Detect(fmt.Sprintf(
"serving insecure client requests on %s", e.Address.Hostname()))
"serving insecure client requests on %s", e.URL.Hostname()))
timedOut := time.After(e.StartTimeout)
command := exec.Command(e.Path, args...)
@ -76,14 +68,14 @@ func (e *Etcd) ensureInitialized() error {
if e.Path == "" {
e.Path = internal.BinPathFinder("etcd")
}
if e.Address == nil {
if e.URL == nil {
am := &internal.AddressManager{}
port, host, err := am.Initialize()
if err != nil {
return err
}
e.Address = &url.URL{
e.URL = &url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%d", host, port),
}

View File

@ -23,14 +23,8 @@ var _ = Describe("The Testing Framework", func() {
Expect(err).NotTo(HaveOccurred(), "Expected controlPlane to start successfully")
var apiServerURL, etcdClientURL *url.URL
etcdUrlString, err := controlPlane.APIServer.Etcd.URL()
Expect(err).NotTo(HaveOccurred())
etcdClientURL, err = url.Parse(etcdUrlString)
Expect(err).NotTo(HaveOccurred())
urlString, err := controlPlane.APIServerURL()
Expect(err).NotTo(HaveOccurred())
apiServerURL, err = url.Parse(urlString)
Expect(err).NotTo(HaveOccurred())
etcdClientURL = controlPlane.APIServer.Etcd.URL
apiServerURL = controlPlane.APIURL()
isEtcdListeningForClients := isSomethingListeningOnPort(etcdClientURL.Host)
isAPIServerListening := isSomethingListeningOnPort(apiServerURL.Host)