Merge pull request #65830 from sttts/sttts-apiserver-readwrite-port
Automatic merge from submit-queue (batch tested with PRs 65830, 65780, 65961). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. apiserver: get rid of ReadWritePort in config Executing removal TODO by making the read write port logic explicit, and not hidden deep in the secure serving code. Preparation for https://github.com/kubernetes/kubernetes/pull/65832 Kubernetes-commit: f6bbf1f6f8ab856b95baea56ab624672b09662fd
This commit is contained in:
commit
55cfb76302
File diff suppressed because it is too large
Load Diff
|
|
@ -181,9 +181,6 @@ type Config struct {
|
||||||
// values below here are targets for removal
|
// values below here are targets for removal
|
||||||
//===========================================================================
|
//===========================================================================
|
||||||
|
|
||||||
// The port on PublicAddress where a read-write server will be installed.
|
|
||||||
// Defaults to 6443 if not set.
|
|
||||||
ReadWritePort int
|
|
||||||
// PublicAddress is the IP address where members of the cluster (kubelet,
|
// PublicAddress is the IP address where members of the cluster (kubelet,
|
||||||
// kube-proxy, services, etc.) can reach the GenericAPIServer.
|
// kube-proxy, services, etc.) can reach the GenericAPIServer.
|
||||||
// If nil or 0.0.0.0, the host's default interface will be used.
|
// If nil or 0.0.0.0, the host's default interface will be used.
|
||||||
|
|
@ -250,7 +247,6 @@ type AuthorizationInfo struct {
|
||||||
func NewConfig(codecs serializer.CodecFactory) *Config {
|
func NewConfig(codecs serializer.CodecFactory) *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
Serializer: codecs,
|
Serializer: codecs,
|
||||||
ReadWritePort: 443,
|
|
||||||
BuildHandlerChainFunc: DefaultBuildHandlerChain,
|
BuildHandlerChainFunc: DefaultBuildHandlerChain,
|
||||||
HandlerChainWaitGroup: new(utilwaitgroup.SafeWaitGroup),
|
HandlerChainWaitGroup: new(utilwaitgroup.SafeWaitGroup),
|
||||||
LegacyAPIGroupPrefixes: sets.NewString(DefaultLegacyAPIPrefix),
|
LegacyAPIGroupPrefixes: sets.NewString(DefaultLegacyAPIPrefix),
|
||||||
|
|
@ -354,16 +350,21 @@ type CompletedConfig struct {
|
||||||
// Complete fills in any fields not set that are required to have valid data and can be derived
|
// Complete fills in any fields not set that are required to have valid data and can be derived
|
||||||
// from other fields. If you're going to `ApplyOptions`, do that first. It's mutating the receiver.
|
// from other fields. If you're going to `ApplyOptions`, do that first. It's mutating the receiver.
|
||||||
func (c *Config) Complete(informers informers.SharedInformerFactory) CompletedConfig {
|
func (c *Config) Complete(informers informers.SharedInformerFactory) CompletedConfig {
|
||||||
host := c.ExternalAddress
|
if len(c.ExternalAddress) == 0 && c.PublicAddress != nil {
|
||||||
if host == "" && c.PublicAddress != nil {
|
c.ExternalAddress = c.PublicAddress.String()
|
||||||
host = c.PublicAddress.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if there is no port, and we have a ReadWritePort, use that
|
// if there is no port, and we listen on one securely, use that one
|
||||||
if _, _, err := net.SplitHostPort(host); err != nil && c.ReadWritePort != 0 {
|
if _, _, err := net.SplitHostPort(c.ExternalAddress); err != nil {
|
||||||
host = net.JoinHostPort(host, strconv.Itoa(c.ReadWritePort))
|
if c.SecureServing == nil {
|
||||||
|
glog.Fatalf("cannot derive external address port without listening on a secure port.")
|
||||||
|
}
|
||||||
|
_, port, err := c.SecureServing.HostPort()
|
||||||
|
if err != nil {
|
||||||
|
glog.Fatalf("cannot derive external address from the secure port: %v", err)
|
||||||
|
}
|
||||||
|
c.ExternalAddress = net.JoinHostPort(c.ExternalAddress, strconv.Itoa(port))
|
||||||
}
|
}
|
||||||
c.ExternalAddress = host
|
|
||||||
|
|
||||||
if c.OpenAPIConfig != nil && c.OpenAPIConfig.SecurityDefinitions != nil {
|
if c.OpenAPIConfig != nil && c.OpenAPIConfig.SecurityDefinitions != nil {
|
||||||
// Setup OpenAPI security: all APIs will have the same authentication for now.
|
// Setup OpenAPI security: all APIs will have the same authentication for now.
|
||||||
|
|
@ -615,3 +616,19 @@ func NewRequestInfoResolver(c *Config) *apirequest.RequestInfoFactory {
|
||||||
GrouplessAPIPrefixes: legacyAPIPrefixes,
|
GrouplessAPIPrefixes: legacyAPIPrefixes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SecureServingInfo) HostPort() (string, int, error) {
|
||||||
|
if s == nil || s.Listener == nil {
|
||||||
|
return "", 0, fmt.Errorf("no listener found")
|
||||||
|
}
|
||||||
|
addr := s.Listener.Addr().String()
|
||||||
|
host, portStr, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
return "", 0, fmt.Errorf("failed to get port from listener address %q: %v", addr, err)
|
||||||
|
}
|
||||||
|
port, err := strconv.Atoi(portStr)
|
||||||
|
if err != nil {
|
||||||
|
return "", 0, fmt.Errorf("invalid non-numeric port %q", portStr)
|
||||||
|
}
|
||||||
|
return host, port, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ import (
|
||||||
|
|
||||||
func TestNewWithDelegate(t *testing.T) {
|
func TestNewWithDelegate(t *testing.T) {
|
||||||
delegateConfig := NewConfig(codecs)
|
delegateConfig := NewConfig(codecs)
|
||||||
|
delegateConfig.ExternalAddress = "192.168.10.4:443"
|
||||||
delegateConfig.PublicAddress = net.ParseIP("192.168.10.4")
|
delegateConfig.PublicAddress = net.ParseIP("192.168.10.4")
|
||||||
delegateConfig.LegacyAPIGroupPrefixes = sets.NewString("/api")
|
delegateConfig.LegacyAPIGroupPrefixes = sets.NewString("/api")
|
||||||
delegateConfig.LoopbackClientConfig = &rest.Config{}
|
delegateConfig.LoopbackClientConfig = &rest.Config{}
|
||||||
|
|
@ -64,6 +65,7 @@ func TestNewWithDelegate(t *testing.T) {
|
||||||
delegateServer.PrepareRun()
|
delegateServer.PrepareRun()
|
||||||
|
|
||||||
wrappingConfig := NewConfig(codecs)
|
wrappingConfig := NewConfig(codecs)
|
||||||
|
wrappingConfig.ExternalAddress = "192.168.10.4:443"
|
||||||
wrappingConfig.PublicAddress = net.ParseIP("192.168.10.4")
|
wrappingConfig.PublicAddress = net.ParseIP("192.168.10.4")
|
||||||
wrappingConfig.LegacyAPIGroupPrefixes = sets.NewString("/api")
|
wrappingConfig.LegacyAPIGroupPrefixes = sets.NewString("/api")
|
||||||
wrappingConfig.LoopbackClientConfig = &rest.Config{}
|
wrappingConfig.LoopbackClientConfig = &rest.Config{}
|
||||||
|
|
|
||||||
|
|
@ -125,6 +125,7 @@ func testGetOpenAPIDefinitions(_ kubeopenapi.ReferenceCallback) map[string]kubeo
|
||||||
// setUp is a convience function for setting up for (most) tests.
|
// setUp is a convience function for setting up for (most) tests.
|
||||||
func setUp(t *testing.T) (Config, *assert.Assertions) {
|
func setUp(t *testing.T) (Config, *assert.Assertions) {
|
||||||
config := NewConfig(codecs)
|
config := NewConfig(codecs)
|
||||||
|
config.ExternalAddress = "192.168.10.4:443"
|
||||||
config.PublicAddress = net.ParseIP("192.168.10.4")
|
config.PublicAddress = net.ParseIP("192.168.10.4")
|
||||||
config.LegacyAPIGroupPrefixes = sets.NewString("/api")
|
config.LegacyAPIGroupPrefixes = sets.NewString("/api")
|
||||||
config.LoopbackClientConfig = &restclient.Config{}
|
config.LoopbackClientConfig = &restclient.Config{}
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,8 @@ type SecureServingOptions struct {
|
||||||
// BindNetwork is the type of network to bind to - defaults to "tcp", accepts "tcp",
|
// BindNetwork is the type of network to bind to - defaults to "tcp", accepts "tcp",
|
||||||
// "tcp4", and "tcp6".
|
// "tcp4", and "tcp6".
|
||||||
BindNetwork string
|
BindNetwork string
|
||||||
|
// Required set to true means that BindPort cannot be zero.
|
||||||
|
Required bool
|
||||||
|
|
||||||
// Listener is the secure server network listener.
|
// Listener is the secure server network listener.
|
||||||
// either Listener or BindAddress/BindPort/BindNetwork is set,
|
// either Listener or BindAddress/BindPort/BindNetwork is set,
|
||||||
|
|
@ -102,7 +104,9 @@ func (s *SecureServingOptions) Validate() []error {
|
||||||
|
|
||||||
errors := []error{}
|
errors := []error{}
|
||||||
|
|
||||||
if s.BindPort < 0 || s.BindPort > 65535 {
|
if s.Required && s.BindPort < 1 || s.BindPort > 65535 {
|
||||||
|
errors = append(errors, fmt.Errorf("--secure-port %v must be between 1 and 65535, inclusive. It cannot turned off with 0", s.BindPort))
|
||||||
|
} else if s.BindPort < 0 || s.BindPort > 65535 {
|
||||||
errors = append(errors, fmt.Errorf("--secure-port %v must be between 0 and 65535, inclusive. 0 for turning off secure port", s.BindPort))
|
errors = append(errors, fmt.Errorf("--secure-port %v must be between 0 and 65535, inclusive. 0 for turning off secure port", s.BindPort))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,9 +122,14 @@ func (s *SecureServingOptions) AddFlags(fs *pflag.FlagSet) {
|
||||||
"The IP address on which to listen for the --secure-port port. The "+
|
"The IP address on which to listen for the --secure-port port. The "+
|
||||||
"associated interface(s) must be reachable by the rest of the cluster, and by CLI/web "+
|
"associated interface(s) must be reachable by the rest of the cluster, and by CLI/web "+
|
||||||
"clients. If blank, all interfaces will be used (0.0.0.0 for all IPv4 interfaces and :: for all IPv6 interfaces).")
|
"clients. If blank, all interfaces will be used (0.0.0.0 for all IPv4 interfaces and :: for all IPv6 interfaces).")
|
||||||
fs.IntVar(&s.BindPort, "secure-port", s.BindPort, ""+
|
|
||||||
"The port on which to serve HTTPS with authentication and authorization. If 0, "+
|
desc := "The port on which to serve HTTPS with authentication and authorization."
|
||||||
"don't serve HTTPS at all.")
|
if s.Required {
|
||||||
|
desc += "It cannot switched off with 0."
|
||||||
|
} else {
|
||||||
|
desc += "If 0, don't serve HTTPS at all."
|
||||||
|
}
|
||||||
|
fs.IntVar(&s.BindPort, "secure-port", s.BindPort, desc)
|
||||||
|
|
||||||
fs.StringVar(&s.ServerCert.CertDirectory, "cert-dir", s.ServerCert.CertDirectory, ""+
|
fs.StringVar(&s.ServerCert.CertDirectory, "cert-dir", s.ServerCert.CertDirectory, ""+
|
||||||
"The directory where the TLS certs are located. "+
|
"The directory where the TLS certs are located. "+
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,6 @@ func (s *SecureServingOptionsWithLoopback) ApplyTo(c *server.Config) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
c.ReadWritePort = s.BindPort
|
|
||||||
|
|
||||||
// create self-signed cert+key with the fake server.LoopbackClientServerNameOverride and
|
// create self-signed cert+key with the fake server.LoopbackClientServerNameOverride and
|
||||||
// let the server return it when the loopback client connects.
|
// let the server return it when the loopback client connects.
|
||||||
certPem, keyPem, err := certutil.GenerateSelfSignedCertKey(server.LoopbackClientServerNameOverride, nil, nil)
|
certPem, keyPem, err := certutil.GenerateSelfSignedCertKey(server.LoopbackClientServerNameOverride, nil, nil)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue