Merge pull request #303 from thockin/4-printf-vs-logs

Clarify logging vs printf for fatal messages
This commit is contained in:
Kubernetes Prow Robot 2020-11-10 09:30:40 -08:00 committed by GitHub
commit 073b007ebd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 47 additions and 37 deletions

View File

@ -264,6 +264,10 @@ func main() {
os.Exit(127) os.Exit(127)
} }
//
// Parse and verify flags. Errors here are fatal.
//
pflag.Parse() pflag.Parse()
flag.CommandLine.Parse(nil) // Otherwise glog complains flag.CommandLine.Parse(nil) // Otherwise glog complains
setGlogFlags() setGlogFlags()
@ -362,11 +366,6 @@ func main() {
} }
} }
if _, err := exec.LookPath(*flGitCmd); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: git executable %q not found: %v\n", *flGitCmd, err)
os.Exit(1)
}
if *flSSH { if *flSSH {
if *flUsername != "" { if *flUsername != "" {
fmt.Fprintf(os.Stderr, "ERROR: only one of --ssh and --username may be specified\n") fmt.Fprintf(os.Stderr, "ERROR: only one of --ssh and --username may be specified\n")
@ -398,19 +397,30 @@ func main() {
} }
} }
// From here on, output goes through logging.
log.V(0).Info("starting up", "pid", os.Getpid(), "args", os.Args)
if _, err := exec.LookPath(*flGitCmd); err != nil {
log.Error(err, "ERROR: git executable not found", "git", *flGitCmd)
os.Exit(1)
}
if err := os.MkdirAll(*flRoot, 0700); err != nil { if err := os.MkdirAll(*flRoot, 0700); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't make root dir: %v", err) log.Error(err, "ERROR: can't make root dir", "path", *flRoot)
os.Exit(1) os.Exit(1)
} }
absRoot, err := normalizePath(*flRoot) absRoot, err := normalizePath(*flRoot)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't normalize root path: %v", err) log.Error(err, "ERROR: can't normalize root path", "path", *flRoot)
os.Exit(1) os.Exit(1)
} }
if absRoot != *flRoot {
log.V(0).Info("normalized root path", "path", *flRoot, "result", absRoot)
}
if *flAddUser { if *flAddUser {
if err := addUser(); err != nil { if err := addUser(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't write to /etc/passwd: %v\n", err) log.Error(err, "ERROR: can't add user")
os.Exit(1) os.Exit(1)
} }
} }
@ -421,21 +431,21 @@ func main() {
if *flUsername != "" && *flPassword != "" { if *flUsername != "" && *flPassword != "" {
if err := setupGitAuth(ctx, *flUsername, *flPassword, *flRepo); err != nil { if err := setupGitAuth(ctx, *flUsername, *flPassword, *flRepo); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't create .netrc file: %v\n", err) log.Error(err, "ERROR: can't set up git auth")
os.Exit(1) os.Exit(1)
} }
} }
if *flSSH { if *flSSH {
if err := setupGitSSH(*flSSHKnownHosts); err != nil { if err := setupGitSSH(*flSSHKnownHosts); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't configure SSH: %v\n", err) log.Error(err, "ERROR: can't set up git SSH")
os.Exit(1) os.Exit(1)
} }
} }
if *flCookieFile { if *flCookieFile {
if err := setupGitCookieFile(ctx); err != nil { if err := setupGitCookieFile(ctx); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't set git cookie file: %v\n", err) log.Error(err, "ERROR: can't set up git cookie file")
os.Exit(1) os.Exit(1)
} }
} }
@ -443,7 +453,7 @@ func main() {
if *flAskPassURL != "" { if *flAskPassURL != "" {
if err := callGitAskPassURL(ctx, *flAskPassURL); err != nil { if err := callGitAskPassURL(ctx, *flAskPassURL); err != nil {
askpassCount.WithLabelValues(metricKeyError).Inc() askpassCount.WithLabelValues(metricKeyError).Inc()
fmt.Fprintf(os.Stderr, "ERROR: failed to call ASKPASS callback URL: %v\n", err) log.Error(err, "ERROR: failed to call ASKPASS callback URL", "url", *flAskPassURL)
os.Exit(1) os.Exit(1)
} }
askpassCount.WithLabelValues(metricKeySuccess).Inc() askpassCount.WithLabelValues(metricKeySuccess).Inc()
@ -455,11 +465,10 @@ func main() {
if *flHTTPBind != "" { if *flHTTPBind != "" {
ln, err := net.Listen("tcp", *flHTTPBind) ln, err := net.Listen("tcp", *flHTTPBind)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: unable to bind HTTP endpoint: %v\n", err) log.Error(err, "ERROR: failed to bind HTTP endpoint", "endpoint", *flHTTPBind)
os.Exit(1) os.Exit(1)
} }
mux := http.NewServeMux() mux := http.NewServeMux()
go func() {
if *flHTTPMetrics { if *flHTTPMetrics {
mux.Handle("/metrics", promhttp.Handler()) mux.Handle("/metrics", promhttp.Handler())
} }
@ -480,13 +489,14 @@ func main() {
} }
// Otherwise success // Otherwise success
}) })
http.Serve(ln, mux) log.V(0).Info("serving HTTP", "endpoint", *flHTTPBind)
go func() {
err := http.Serve(ln, mux)
log.Error(err, "HTTP server terminated")
os.Exit(1)
}() }()
} }
// From here on, output goes through logging.
log.V(0).Info("starting up", "pid", os.Getpid(), "args", os.Args)
// Startup webhooks goroutine // Startup webhooks goroutine
var webhook *Webhook var webhook *Webhook
if *flWebhookURL != "" { if *flWebhookURL != "" {