Simplify genericapiserver.Run()

This commit is contained in:
Dr. Stefan Schimanski 2016-09-20 16:52:36 +02:00
parent 8b33a9ed42
commit 3799ffa0a8

View File

@ -248,12 +248,9 @@ func (s *GenericAPIServer) Run(options *options.ServerRunOptions) {
s.InstallOpenAPI() s.InstallOpenAPI()
} }
secureLocation := ""
if options.SecurePort != 0 {
secureLocation = net.JoinHostPort(options.BindAddress.String(), strconv.Itoa(options.SecurePort))
}
secureStartedCh := make(chan struct{}) secureStartedCh := make(chan struct{})
if secureLocation != "" { if options.SecurePort != 0 {
secureLocation := net.JoinHostPort(options.BindAddress.String(), strconv.Itoa(options.SecurePort))
secureServer := &http.Server{ secureServer := &http.Server{
Addr: secureLocation, Addr: secureLocation,
Handler: s.Handler, Handler: s.Handler,
@ -301,10 +298,6 @@ func (s *GenericAPIServer) Run(options *options.ServerRunOptions) {
notifyStarted := sync.Once{} notifyStarted := sync.Once{}
for { for {
// err == systemd.SdNotifyNoSocket when not running on a systemd system
if err := systemd.SdNotify("READY=1\n"); err != nil && err != systemd.SdNotifyNoSocket {
glog.Errorf("Unable to send systemd daemon successful start message: %v\n", err)
}
if err := secureServer.ListenAndServeTLS(options.TLSCertFile, options.TLSPrivateKeyFile); err != nil { if err := secureServer.ListenAndServeTLS(options.TLSCertFile, options.TLSPrivateKeyFile); err != nil {
glog.Errorf("Unable to listen for secure (%v); will try again.", err) glog.Errorf("Unable to listen for secure (%v); will try again.", err)
} else { } else {
@ -316,20 +309,15 @@ func (s *GenericAPIServer) Run(options *options.ServerRunOptions) {
} }
}() }()
} else { } else {
// err == systemd.SdNotifyNoSocket when not running on a systemd system
if err := systemd.SdNotify("READY=1\n"); err != nil && err != systemd.SdNotifyNoSocket {
glog.Errorf("Unable to send systemd daemon successful start message: %v\n", err)
}
close(secureStartedCh) close(secureStartedCh)
} }
insecureLocation := net.JoinHostPort(options.InsecureBindAddress.String(), strconv.Itoa(options.InsecurePort)) insecureLocation := net.JoinHostPort(options.InsecureBindAddress.String(), strconv.Itoa(options.InsecurePort))
http := &http.Server{ insecureServer := &http.Server{
Addr: insecureLocation, Addr: insecureLocation,
Handler: s.InsecureHandler, Handler: s.InsecureHandler,
MaxHeaderBytes: 1 << 20, MaxHeaderBytes: 1 << 20,
} }
insecureStartedCh := make(chan struct{}) insecureStartedCh := make(chan struct{})
glog.Infof("Serving insecurely on %s", insecureLocation) glog.Infof("Serving insecurely on %s", insecureLocation)
go func() { go func() {
@ -337,7 +325,7 @@ func (s *GenericAPIServer) Run(options *options.ServerRunOptions) {
notifyStarted := sync.Once{} notifyStarted := sync.Once{}
for { for {
if err := http.ListenAndServe(); err != nil { if err := insecureServer.ListenAndServe(); err != nil {
glog.Errorf("Unable to listen for insecure (%v); will try again.", err) glog.Errorf("Unable to listen for insecure (%v); will try again.", err)
} else { } else {
notifyStarted.Do(func() { notifyStarted.Do(func() {
@ -352,6 +340,11 @@ func (s *GenericAPIServer) Run(options *options.ServerRunOptions) {
<-insecureStartedCh <-insecureStartedCh
s.RunPostStartHooks(PostStartHookContext{}) s.RunPostStartHooks(PostStartHookContext{})
// err == systemd.SdNotifyNoSocket when not running on a systemd system
if err := systemd.SdNotify("READY=1\n"); err != nil && err != systemd.SdNotifyNoSocket {
glog.Errorf("Unable to send systemd daemon successful start message: %v\n", err)
}
select {} select {}
} }