Decouple shared functions between controllers (#8829)
* Decouple shared functions between controllers * Apply suggestions from code review Co-authored-by: Jintao Zhang <tao12345666333@163.com> * Fix package names and fmt Co-authored-by: Jintao Zhang <tao12345666333@163.com>
This commit is contained in:
parent
8f9df544ea
commit
4c6a7ee158
27 changed files with 413 additions and 134 deletions
|
|
@ -21,34 +21,34 @@ import (
|
|||
"fmt"
|
||||
"math/rand" // #nosec
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
discovery "k8s.io/apimachinery/pkg/version"
|
||||
"k8s.io/apiserver/pkg/server/healthz"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
certutil "k8s.io/client-go/util/cert"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"k8s.io/ingress-nginx/internal/file"
|
||||
"k8s.io/ingress-nginx/internal/ingress/controller"
|
||||
"k8s.io/ingress-nginx/internal/ingress/metric"
|
||||
"k8s.io/ingress-nginx/internal/k8s"
|
||||
"k8s.io/ingress-nginx/internal/net/ssl"
|
||||
"k8s.io/ingress-nginx/internal/nginx"
|
||||
"k8s.io/ingress-nginx/pkg/util/file"
|
||||
"k8s.io/ingress-nginx/version"
|
||||
|
||||
ingressflags "k8s.io/ingress-nginx/pkg/flags"
|
||||
"k8s.io/ingress-nginx/pkg/metrics"
|
||||
"k8s.io/ingress-nginx/pkg/util/process"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
@ -58,7 +58,7 @@ func main() {
|
|||
|
||||
fmt.Println(version.String())
|
||||
|
||||
showVersion, conf, err := parseFlags()
|
||||
showVersion, conf, err := ingressflags.ParseFlags()
|
||||
if showVersion {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
|
@ -125,8 +125,8 @@ func main() {
|
|||
|
||||
reg := prometheus.NewRegistry()
|
||||
|
||||
reg.MustRegister(prometheus.NewGoCollector())
|
||||
reg.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{
|
||||
reg.MustRegister(collectors.NewGoCollector())
|
||||
reg.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{
|
||||
PidFn: func() (int, error) { return os.Getpid(), nil },
|
||||
ReportErrors: true,
|
||||
}))
|
||||
|
|
@ -143,14 +143,14 @@ func main() {
|
|||
mc.Start(conf.ValidationWebhook)
|
||||
|
||||
if conf.EnableProfiling {
|
||||
go registerProfiler()
|
||||
go metrics.RegisterProfiler("127.0.0.1", nginx.ProfilerPort)
|
||||
}
|
||||
|
||||
ngx := controller.NewNGINXController(conf, mc)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
registerHealthz(nginx.HealthPath, ngx, mux)
|
||||
registerMetrics(reg, mux)
|
||||
metrics.RegisterHealthz(nginx.HealthPath, mux, ngx)
|
||||
metrics.RegisterMetrics(reg, mux)
|
||||
|
||||
_, errExists := os.Stat("/chroot")
|
||||
if errExists == nil {
|
||||
|
|
@ -159,35 +159,14 @@ func main() {
|
|||
|
||||
}
|
||||
|
||||
go startHTTPServer(conf.HealthCheckHost, conf.ListenPorts.Health, mux)
|
||||
go metrics.StartHTTPServer(conf.HealthCheckHost, conf.ListenPorts.Health, mux)
|
||||
go ngx.Start()
|
||||
|
||||
handleSigterm(ngx, conf.PostShutdownGracePeriod, func(code int) {
|
||||
process.HandleSigterm(ngx, conf.PostShutdownGracePeriod, func(code int) {
|
||||
os.Exit(code)
|
||||
})
|
||||
}
|
||||
|
||||
type exiter func(code int)
|
||||
|
||||
func handleSigterm(ngx *controller.NGINXController, delay int, exit exiter) {
|
||||
signalChan := make(chan os.Signal, 1)
|
||||
signal.Notify(signalChan, syscall.SIGTERM)
|
||||
<-signalChan
|
||||
klog.InfoS("Received SIGTERM, shutting down")
|
||||
|
||||
exitCode := 0
|
||||
if err := ngx.Stop(); err != nil {
|
||||
klog.Warningf("Error during shutdown: %v", err)
|
||||
exitCode = 1
|
||||
}
|
||||
|
||||
klog.Infof("Handled quit, delaying controller exit for %d seconds", delay)
|
||||
time.Sleep(time.Duration(delay) * time.Second)
|
||||
|
||||
klog.InfoS("Exiting", "code", exitCode)
|
||||
exit(exitCode)
|
||||
}
|
||||
|
||||
// createApiserverClient creates a new Kubernetes REST client. apiserverHost is
|
||||
// the URL of the API server in the format protocol://address:port/pathPrefix,
|
||||
// kubeConfig is the location of a kubeconfig file. If defined, the kubeconfig
|
||||
|
|
@ -293,60 +272,6 @@ func handleFatalInitError(err error) {
|
|||
err)
|
||||
}
|
||||
|
||||
func registerHealthz(healthPath string, ic *controller.NGINXController, mux *http.ServeMux) {
|
||||
// expose health check endpoint (/healthz)
|
||||
healthz.InstallPathHandler(mux,
|
||||
healthPath,
|
||||
healthz.PingHealthz,
|
||||
ic,
|
||||
)
|
||||
}
|
||||
|
||||
func registerMetrics(reg *prometheus.Registry, mux *http.ServeMux) {
|
||||
mux.Handle(
|
||||
"/metrics",
|
||||
promhttp.InstrumentMetricHandler(
|
||||
reg,
|
||||
promhttp.HandlerFor(reg, promhttp.HandlerOpts{}),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func registerProfiler() {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/debug/pprof/", pprof.Index)
|
||||
mux.HandleFunc("/debug/pprof/heap", pprof.Index)
|
||||
mux.HandleFunc("/debug/pprof/mutex", pprof.Index)
|
||||
mux.HandleFunc("/debug/pprof/goroutine", pprof.Index)
|
||||
mux.HandleFunc("/debug/pprof/threadcreate", pprof.Index)
|
||||
mux.HandleFunc("/debug/pprof/block", pprof.Index)
|
||||
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
||||
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
||||
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
||||
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: fmt.Sprintf("127.0.0.1:%v", nginx.ProfilerPort),
|
||||
//G112 (CWE-400): Potential Slowloris Attack
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
Handler: mux,
|
||||
}
|
||||
klog.Fatal(server.ListenAndServe())
|
||||
}
|
||||
|
||||
func startHTTPServer(host string, port int, mux *http.ServeMux) {
|
||||
server := &http.Server{
|
||||
Addr: fmt.Sprintf("%s:%v", host, port),
|
||||
Handler: mux,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
WriteTimeout: 300 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
}
|
||||
klog.Fatal(server.ListenAndServe())
|
||||
}
|
||||
|
||||
func checkService(key string, kubeClient *kubernetes.Clientset) error {
|
||||
ns, name, err := k8s.ParseNameNS(key)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue