Skip to content

Commit 78a2f9f

Browse files
committed
Testing: Remove legacy cruft from process
This removes some legacy cruft from the process abstraction, namely: - the legacy health check support (startup message), since our supported etcd versions support the /health endpoint - URL populatation, since we end up needing multiple ports for the api server - gexec usage, since we don't need to check wait messages any more and the equivalent "send SIGTERM" and "wait for process to exit" parts are pretty straightforward for our usecases.
1 parent 481f7b9 commit 78a2f9f

File tree

6 files changed

+378
-473
lines changed

6 files changed

+378
-473
lines changed

pkg/internal/testing/controlplane/apiserver.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"fmt"
55
"io"
66
"io/ioutil"
7+
"net"
78
"net/url"
89
"os"
910
"path/filepath"
11+
"strconv"
1012
"time"
1113

1214
"sigs.k8s.io/controller-runtime/pkg/internal/testing/addr"
@@ -69,38 +71,34 @@ type APIServer struct {
6971
Out io.Writer
7072
Err io.Writer
7173

72-
processState *process.ProcessState
74+
processState *process.State
7375
}
7476

7577
// Start starts the apiserver, waits for it to come up, and returns an error,
7678
// if occurred.
7779
func (s *APIServer) Start() error {
7880
if s.processState == nil {
79-
if err := s.setProcessState(); err != nil {
81+
if err := s.setState(); err != nil {
8082
return err
8183
}
8284
}
8385
return s.processState.Start(s.Out, s.Err)
8486
}
8587

86-
func (s *APIServer) setProcessState() error {
88+
func (s *APIServer) setState() error {
8789
if s.EtcdURL == nil {
8890
return fmt.Errorf("expected EtcdURL to be configured")
8991
}
9092

9193
var err error
9294

93-
s.processState = &process.ProcessState{}
94-
95-
s.processState.DefaultedProcessInput, err = process.DoDefaulting(
96-
"kube-apiserver",
97-
s.URL,
98-
s.CertDir,
99-
s.Path,
100-
s.StartTimeout,
101-
s.StopTimeout,
102-
)
103-
if err != nil {
95+
s.processState = &process.State{
96+
Dir: s.CertDir,
97+
Path: s.Path,
98+
StartTimeout: s.StartTimeout,
99+
StopTimeout: s.StopTimeout,
100+
}
101+
if err := s.processState.Init("kube-apiserver"); err != nil {
104102
return err
105103
}
106104

@@ -112,9 +110,20 @@ func (s *APIServer) setProcessState() error {
112110
}
113111
}
114112

115-
s.processState.HealthCheckEndpoint = "/healthz"
113+
if s.URL == nil {
114+
port, host, err := addr.Suggest("")
115+
if err != nil {
116+
return err
117+
}
118+
s.URL = &url.URL{
119+
Scheme: "http",
120+
Host: net.JoinHostPort(host, strconv.Itoa(port)),
121+
}
122+
}
123+
124+
s.processState.HealthCheck.URL = *s.URL
125+
s.processState.HealthCheck.Path = "/healthz"
116126

117-
s.URL = &s.processState.URL
118127
s.CertDir = s.processState.Dir
119128
s.Path = s.processState.Path
120129
s.StartTimeout = s.processState.StartTimeout

pkg/internal/testing/controlplane/etcd.go

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package controlplane
22

33
import (
44
"io"
5-
"time"
6-
5+
"net"
76
"net/url"
7+
"strconv"
8+
"time"
89

10+
"sigs.k8s.io/controller-runtime/pkg/internal/testing/addr"
911
"sigs.k8s.io/controller-runtime/pkg/internal/testing/process"
1012
)
1113

@@ -55,40 +57,50 @@ type Etcd struct {
5557
Out io.Writer
5658
Err io.Writer
5759

58-
processState *process.ProcessState
60+
// processState contains the actual details about this running process
61+
processState *process.State
5962
}
6063

6164
// Start starts the etcd, waits for it to come up, and returns an error, if one
6265
// occoured.
6366
func (e *Etcd) Start() error {
6467
if e.processState == nil {
65-
if err := e.setProcessState(); err != nil {
68+
if err := e.setState(); err != nil {
6669
return err
6770
}
6871
}
6972
return e.processState.Start(e.Out, e.Err)
7073
}
7174

72-
func (e *Etcd) setProcessState() error {
75+
func (e *Etcd) setState() error {
7376
var err error
7477

75-
e.processState = &process.ProcessState{}
76-
77-
e.processState.DefaultedProcessInput, err = process.DoDefaulting(
78-
"etcd",
79-
e.URL,
80-
e.DataDir,
81-
e.Path,
82-
e.StartTimeout,
83-
e.StopTimeout,
84-
)
85-
if err != nil {
78+
e.processState = &process.State{
79+
Dir: e.DataDir,
80+
Path: e.Path,
81+
StartTimeout: e.StartTimeout,
82+
StopTimeout: e.StopTimeout,
83+
}
84+
85+
if err := e.processState.Init("etcd"); err != nil {
8686
return err
8787
}
8888

89-
e.processState.StartMessage = getEtcdStartMessage(e.processState.URL)
89+
if e.URL == nil {
90+
port, host, err := addr.Suggest("")
91+
if err != nil {
92+
return err
93+
}
94+
e.URL = &url.URL{
95+
Scheme: "http",
96+
Host: net.JoinHostPort(host, strconv.Itoa(port)),
97+
}
98+
}
99+
100+
// can use /health as of etcd 3.3.0
101+
e.processState.HealthCheck.URL = *e.URL
102+
e.processState.HealthCheck.Path = "/health"
90103

91-
e.URL = &e.processState.URL
92104
e.DataDir = e.processState.Dir
93105
e.Path = e.processState.Path
94106
e.StartTimeout = e.processState.StartTimeout
@@ -117,24 +129,3 @@ var EtcdDefaultArgs = []string{
117129
"--listen-client-urls={{ if .URL }}{{ .URL.String }}{{ end }}",
118130
"--data-dir={{ .DataDir }}",
119131
}
120-
121-
// isSecureScheme returns false when the schema is insecure.
122-
func isSecureScheme(scheme string) bool {
123-
// https://github.com/coreos/etcd/blob/d9deeff49a080a88c982d328ad9d33f26d1ad7b6/pkg/transport/listener.go#L53
124-
if scheme == "https" || scheme == "unixs" {
125-
return true
126-
}
127-
return false
128-
}
129-
130-
// getEtcdStartMessage returns an start message to inform if the client is or not insecure.
131-
// It will return true when the URL informed has the scheme == "https" || scheme == "unixs"
132-
func getEtcdStartMessage(listenURL url.URL) string {
133-
if isSecureScheme(listenURL.Scheme) {
134-
// https://github.com/coreos/etcd/blob/a7f1fbe00ec216fcb3a1919397a103b41dca8413/embed/serve.go#L167
135-
return "serving client requests on "
136-
}
137-
138-
// https://github.com/coreos/etcd/blob/a7f1fbe00ec216fcb3a1919397a103b41dca8413/embed/serve.go#L124
139-
return "serving insecure client requests on "
140-
}

pkg/internal/testing/process/arguments_test.go

Lines changed: 0 additions & 95 deletions
This file was deleted.

0 commit comments

Comments
 (0)