debug: /debug endpoint — DNS, TCP, S3 диагностика
This commit is contained in:
+49
-3
@@ -8,6 +8,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -96,13 +97,15 @@ func main() {
|
|||||||
|
|
||||||
var err error
|
var err error
|
||||||
useSSL := os.Getenv("S3_USE_SSL") != "false"
|
useSSL := os.Getenv("S3_USE_SSL") != "false"
|
||||||
|
log.Printf("S3 connecting: endpoint=%s bucket=%s useSSL=%v", endpoint, bucketName, useSSL)
|
||||||
s3Client, err = s3.New(endpoint, &s3.Options{
|
s3Client, err = s3.New(endpoint, &s3.Options{
|
||||||
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
||||||
Secure: useSSL,
|
Secure: useSSL,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalf("S3 init failed: %v", err)
|
||||||
}
|
}
|
||||||
|
log.Printf("S3 client created, testing BucketExists...")
|
||||||
|
|
||||||
// 2. HTTP Handlers
|
// 2. HTTP Handlers
|
||||||
http.HandleFunc("/.well-known/terraform.json", discoveryHandler)
|
http.HandleFunc("/.well-known/terraform.json", discoveryHandler)
|
||||||
@@ -113,6 +116,7 @@ func main() {
|
|||||||
http.Handle("/", http.HandlerFunc(rootHandler))
|
http.Handle("/", http.HandlerFunc(rootHandler))
|
||||||
http.HandleFunc("/healthz", healthzHandler)
|
http.HandleFunc("/healthz", healthzHandler)
|
||||||
http.HandleFunc("/readyz", readyzHandler)
|
http.HandleFunc("/readyz", readyzHandler)
|
||||||
|
http.HandleFunc("/debug", debugHandler)
|
||||||
|
|
||||||
port := os.Getenv("PORT")
|
port := os.Getenv("PORT")
|
||||||
if port == "" {
|
if port == "" {
|
||||||
@@ -141,6 +145,47 @@ func main() {
|
|||||||
log.Println("Server stopped")
|
log.Println("Server stopped")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func debugHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
|
|
||||||
|
fmt.Fprintf(w, "=== ENV ===\n")
|
||||||
|
for _, e := range os.Environ() {
|
||||||
|
if strings.Contains(e, "S3_") || strings.Contains(e, "REGISTRY") || strings.Contains(e, "PORT") {
|
||||||
|
fmt.Fprintln(w, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s3ep := os.Getenv("S3_ENDPOINT")
|
||||||
|
fmt.Fprintf(w, "\n=== DNS: %s ===\n", s3ep)
|
||||||
|
addrs, err := net.LookupHost(s3ep)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(w, "DNS ERROR: %v\n", err)
|
||||||
|
} else {
|
||||||
|
for _, a := range addrs {
|
||||||
|
fmt.Fprintf(w, " %s\n", a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(w, "\n=== TCP dial: %s:443 ===\n", s3ep)
|
||||||
|
conn, err := net.DialTimeout("tcp", s3ep+":443", 5*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(w, "TCP ERROR: %v\n", err)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(w, "TCP OK: %s -> %s\n", conn.LocalAddr(), conn.RemoteAddr())
|
||||||
|
conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(w, "\n=== S3 BucketExists ===\n")
|
||||||
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
_, err = s3Client.BucketExists(ctx, bucketName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(w, "S3 ERROR: %v\n", err)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(w, "S3 OK: bucket %s accessible\n", bucketName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path != "/" {
|
if r.URL.Path != "/" {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
@@ -415,9 +460,10 @@ func healthzHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
func readyzHandler(w http.ResponseWriter, r *http.Request) {
|
func readyzHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
_, err := s3Client.BucketExists(context.Background(), bucketName)
|
_, err := s3Client.BucketExists(context.Background(), bucketName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("readyz: S3 bucket check failed: %v", err)
|
msg := fmt.Sprintf("s3 unreachable: %v", err)
|
||||||
|
log.Printf("readyz: %s", msg)
|
||||||
w.WriteHeader(http.StatusServiceUnavailable)
|
w.WriteHeader(http.StatusServiceUnavailable)
|
||||||
w.Write([]byte("s3 unreachable"))
|
w.Write([]byte(msg))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|||||||
Reference in New Issue
Block a user