sigs.k8s.io/controller-runtime@v0.18.2/pkg/manager/server.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package manager 18 19 import ( 20 "context" 21 "errors" 22 "net" 23 "net/http" 24 "time" 25 26 crlog "sigs.k8s.io/controller-runtime/pkg/log" 27 ) 28 29 var ( 30 _ Runnable = (*Server)(nil) 31 _ LeaderElectionRunnable = (*Server)(nil) 32 ) 33 34 // Server is a general purpose HTTP server Runnable for a manager. 35 // It is used to serve some internal handlers for health probes and profiling, 36 // but it can also be used to run custom servers. 37 type Server struct { 38 // Name is an optional string that describes the purpose of the server. It is used in logs to distinguish 39 // among multiple servers. 40 Name string 41 42 // Server is the HTTP server to run. It is required. 43 Server *http.Server 44 45 // Listener is an optional listener to use. If not set, the server start a listener using the server.Addr. 46 // Using a listener is useful when the port reservation needs to happen in advance of this runnable starting. 47 Listener net.Listener 48 49 // OnlyServeWhenLeader is an optional bool that indicates that the server should only be started when the manager is the leader. 50 OnlyServeWhenLeader bool 51 52 // ShutdownTimeout is an optional duration that indicates how long to wait for the server to shutdown gracefully. If not set, 53 // the server will wait indefinitely for all connections to close. 54 ShutdownTimeout *time.Duration 55 } 56 57 // Start starts the server. It will block until the server is stopped or an error occurs. 58 func (s *Server) Start(ctx context.Context) error { 59 log := crlog.FromContext(ctx) 60 if s.Name != "" { 61 log = log.WithValues("name", s.Name) 62 } 63 log = log.WithValues("addr", s.addr()) 64 65 serverShutdown := make(chan struct{}) 66 go func() { 67 <-ctx.Done() 68 log.Info("shutting down server") 69 70 shutdownCtx := context.Background() 71 if s.ShutdownTimeout != nil { 72 var shutdownCancel context.CancelFunc 73 shutdownCtx, shutdownCancel = context.WithTimeout(context.Background(), *s.ShutdownTimeout) 74 defer shutdownCancel() 75 } 76 77 if err := s.Server.Shutdown(shutdownCtx); err != nil { 78 log.Error(err, "error shutting down server") 79 } 80 close(serverShutdown) 81 }() 82 83 log.Info("starting server") 84 if err := s.serve(); err != nil && !errors.Is(err, http.ErrServerClosed) { 85 return err 86 } 87 88 <-serverShutdown 89 return nil 90 } 91 92 // NeedLeaderElection returns true if the server should only be started when the manager is the leader. 93 func (s *Server) NeedLeaderElection() bool { 94 return s.OnlyServeWhenLeader 95 } 96 97 func (s *Server) addr() string { 98 if s.Listener != nil { 99 return s.Listener.Addr().String() 100 } 101 return s.Server.Addr 102 } 103 104 func (s *Server) serve() error { 105 if s.Listener != nil { 106 return s.Server.Serve(s.Listener) 107 } 108 return s.Server.ListenAndServe() 109 }