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

✨ Support for using both an external control plane and automatic webhooks together #1265

Merged
merged 1 commit into from
Dec 14, 2020
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
2 changes: 1 addition & 1 deletion pkg/builder/builder_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var _ = BeforeSuite(func(done Done) {
// Prevent the metrics listener being created
metrics.DefaultBindAddress = "0"

webhook.DefaultPort, _, err = addr.Suggest()
webhook.DefaultPort, _, err = addr.Suggest("")
Expect(err).NotTo(HaveOccurred())

close(done)
Expand Down
24 changes: 17 additions & 7 deletions pkg/envtest/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type WebhookInstallOptions struct {
// CAData is the CA that can be used to trust the serving certificates in LocalServingCertDir.
LocalServingCAData []byte

// LocalServingHostExternalName is the hostname to use to reach the webhook server.
LocalServingHostExternalName string

// MaxTime is the max time to wait
MaxTime time.Duration

Expand Down Expand Up @@ -137,13 +140,19 @@ func modifyWebhook(webhook map[string]interface{}, caData []byte, hostPort strin
}

func (o *WebhookInstallOptions) generateHostPort() (string, error) {
port, host, err := addr.Suggest()
if err != nil {
return "", fmt.Errorf("unable to grab random port for serving webhooks on: %v", err)
if o.LocalServingPort == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any case where it is sensible to have the port be 0 and use and external name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like to actually set it to 0 in the OS? I don't think so, the reason for this check is the reverse, allowing passing in a fixed port if you are willing to take ownership of any collisions (ex. running just one suite in a CI job) while leaving the default still be "pick me a random port" like the OS would do, but without reuse collisions.

port, host, err := addr.Suggest(o.LocalServingHost)
if err != nil {
return "", fmt.Errorf("unable to grab random port for serving webhooks on: %v", err)
}
o.LocalServingPort = port
o.LocalServingHost = host
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about reusing this field instead? It would be generated IF not provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean re: adding the new struct field? LocalServingHost has to be a name or IP for the webhook server bind to, in most cases I would expect it to be set to 0.0.0.0 when doing this style of test. The ExternalName field is what ends up written into the webhook definition and needs to be the externally visible name for the remote kube-apiserver to reach us on.

}
host := o.LocalServingHostExternalName
if host == "" {
host = o.LocalServingHost
}
o.LocalServingPort = port
o.LocalServingHost = host
return net.JoinHostPort(host, fmt.Sprintf("%d", port)), nil
return net.JoinHostPort(host, fmt.Sprintf("%d", o.LocalServingPort)), nil
}

// PrepWithoutInstalling does the setup parts of Install (populating host-port,
Expand Down Expand Up @@ -266,7 +275,8 @@ func (o *WebhookInstallOptions) setupCA() ([]byte, error) {
return nil, fmt.Errorf("unable to set up webhook CA: %v", err)
}

hookCert, err := hookCA.NewServingCert()
names := []string{"localhost", o.LocalServingHost, o.LocalServingHostExternalName}
hookCert, err := hookCA.NewServingCert(names...)
if err != nil {
return nil, fmt.Errorf("unable to set up webhook serving certs: %v", err)
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/internal/testing/integration/addr/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ var cache = &portCache{
ports: make(map[int]time.Time),
}

func suggest() (port int, resolvedHost string, err error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
func suggest(listenHost string) (port int, resolvedHost string, err error) {
if listenHost == "" {
listenHost = "localhost"
}
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(listenHost, "0"))
if err != nil {
return
}
Expand All @@ -59,9 +62,9 @@ func suggest() (port int, resolvedHost string, err error) {
// a tuple consisting of a free port and the hostname resolved to its IP.
// It makes sure that new port allocated does not conflict with old ports
// allocated within 1 minute.
func Suggest() (port int, resolvedHost string, err error) {
func Suggest(listenHost string) (port int, resolvedHost string, err error) {
for i := 0; i < portConflictRetry; i++ {
port, resolvedHost, err = suggest()
port, resolvedHost, err = suggest(listenHost)
if err != nil {
return
}
Expand Down
34 changes: 33 additions & 1 deletion pkg/internal/testing/integration/addr/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

var _ = Describe("SuggestAddress", func() {
It("returns a free port and an address to bind to", func() {
port, host, err := addr.Suggest()
port, host, err := addr.Suggest("")

Expect(err).NotTo(HaveOccurred())
Expect(host).To(Or(Equal("127.0.0.1"), Equal("::1")))
Expand All @@ -26,4 +26,36 @@ var _ = Describe("SuggestAddress", func() {
}()
Expect(err).NotTo(HaveOccurred())
})

It("supports an explicit listenHost", func() {
port, host, err := addr.Suggest("localhost")

Expect(err).NotTo(HaveOccurred())
Expect(host).To(Or(Equal("127.0.0.1"), Equal("::1")))
Expect(port).NotTo(Equal(0))

addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(host, strconv.Itoa(port)))
Expect(err).NotTo(HaveOccurred())
l, err := net.ListenTCP("tcp", addr)
defer func() {
Expect(l.Close()).To(Succeed())
}()
Expect(err).NotTo(HaveOccurred())
})

It("supports a 0.0.0.0 listenHost", func() {
port, host, err := addr.Suggest("0.0.0.0")

Expect(err).NotTo(HaveOccurred())
Expect(host).To(Equal("0.0.0.0"))
Expect(port).NotTo(Equal(0))

addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(host, strconv.Itoa(port)))
Expect(err).NotTo(HaveOccurred())
l, err := net.ListenTCP("tcp", addr)
defer func() {
Expect(l.Close()).To(Succeed())
}()
Expect(err).NotTo(HaveOccurred())
})
})
2 changes: 1 addition & 1 deletion pkg/internal/testing/integration/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (s *APIServer) setProcessState() error {

// Defaulting the secure port
if s.SecurePort == 0 {
s.SecurePort, _, err = addr.Suggest()
s.SecurePort, _, err = addr.Suggest("")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/testing/integration/internal/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func DoDefaulting(
}

if listenURL == nil {
port, host, err := addr.Suggest()
port, host, err := addr.Suggest("")
if err != nil {
return DefaultedProcessInput{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/testing/integration/internal/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var _ = Describe("Start method", func() {
processState.HealthCheckEndpoint = healthURLPath
processState.StartTimeout = 500 * time.Millisecond

port, host, err := addr.Suggest()
port, host, err := addr.Suggest("")
Expect(err).NotTo(HaveOccurred())

processState.URL = url.URL{
Expand Down
44 changes: 40 additions & 4 deletions pkg/internal/testing/integration/internal/tinyca.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,51 @@ func (c *TinyCA) makeCert(cfg certutil.Config) (CertPair, error) {
}, nil
}

// NewServingCert returns a new CertPair for a serving HTTPS on localhost.
func (c *TinyCA) NewServingCert() (CertPair, error) {
// NewServingCert returns a new CertPair for a serving HTTPS on localhost (or other specified names).
func (c *TinyCA) NewServingCert(names ...string) (CertPair, error) {
if len(names) == 0 {
names = []string{"localhost"}
}
dnsNames, ips, err := resolveNames(names)
if err != nil {
return CertPair{}, err
}

return c.makeCert(certutil.Config{
CommonName: "localhost",
Organization: []string{c.orgName},
AltNames: certutil.AltNames{
DNSNames: []string{"localhost"},
IPs: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback},
DNSNames: dnsNames,
IPs: ips,
},
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
})
}

func resolveNames(names []string) ([]string, []net.IP, error) {
dnsNames := []string{}
ips := []net.IP{}
for _, name := range names {
if name == "" {
continue
}
ip := net.ParseIP(name)
if ip == nil {
dnsNames = append(dnsNames, name)
// Also resolve to IPs.
nameIPs, err := net.LookupHost(name)
if err != nil {
return nil, nil, err
}
for _, nameIP := range nameIPs {
ip = net.ParseIP(nameIP)
if ip != nil {
ips = append(ips, ip)
}
}
} else {
ips = append(ips, ip)
}
}
return dnsNames, ips, nil
}