Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove tests #4

Merged
merged 3 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 59 additions & 66 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,77 @@
package main

import (
"fmt"
"gopkg.in/DataDog/dd-trace-go.v1/profiler"
"io"
"log"
"net/http"
"os"
"fmt"
"gopkg.in/DataDog/dd-trace-go.v1/profiler"
"io"
"log"
"net/http"
"os"
)

func getClusterInfo(w http.ResponseWriter, _ *http.Request, client *http.Client) {
// Check if client is nil
if client == nil {
http.Error(w, "HTTP client is nil: cannot proceed with metadata retrieval", http.StatusInternalServerError)
return
}
func getClusterInfo(w http.ResponseWriter, r *http.Request) {
// GCP metadata URL to retrieve the GKE cluster name
metadataURL := "http://metadata.google.internal/computeMetadata/v1/instance/attributes/cluster-name"

// GCP metadata URL to retrieve the GKE cluster name
metadataURL := "http://metadata.google.internal/computeMetadata/v1/instance/attributes/cluster-name"
// Set the metadata request headers
req, err := http.NewRequest("GET", metadataURL, nil)
if err != nil {
http.Error(w, fmt.Sprintf("Error creating request: %v", err), http.StatusInternalServerError)
return
}
req.Header.Set("Metadata-Flavor", "Google")

// Set the metadata request headers
req, err := http.NewRequest("GET", metadataURL, nil)
if err != nil {
http.Error(w, fmt.Sprintf("Error creating request: %v", err), http.StatusInternalServerError)
return
}
req.Header.Set("Metadata-Flavor", "Google")
// Send the request to the metadata server
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
http.Error(w, fmt.Sprintf("Error retrieving metadata: %v", err), http.StatusInternalServerError)
return
}
defer resp.Body.Close()

// Send the request to the metadata server
resp, err := client.Do(req)
if err != nil {
http.Error(w, fmt.Sprintf("Error retrieving metadata: %v", err), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// Check the response status code
if resp.StatusCode != http.StatusOK {
http.Error(w, fmt.Sprintf("Error retrieving metadata, status code: %d", resp.StatusCode), http.StatusInternalServerError)
return
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
http.Error(w, fmt.Sprintf("Error retrieving metadata, status code: %d", resp.StatusCode), http.StatusInternalServerError)
return
}
// Set the Content-Type header to JSON
w.Header().Set("Content-Type", "application/json")

// Set the Content-Type header to JSON
w.Header().Set("Content-Type", "application/json")

// Copy the response from the metadata server to the output
_, err = io.Copy(w, resp.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Error writing response: %v", err), http.StatusInternalServerError)
return
}
// Copy the response from the metadata server to the output
_, err = io.Copy(w, resp.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Error writing response: %v", err), http.StatusInternalServerError)
return
}
}

func main() {
err := profiler.Start(
profiler.WithProfileTypes(
profiler.CPUProfile,
profiler.HeapProfile,
err := profiler.Start(
profiler.WithProfileTypes(
profiler.CPUProfile,
profiler.HeapProfile,

// The profiles below are disabled by
// default to keep overhead low, but
// can be enabled as needed.
// profiler.BlockProfile,
// profiler.MutexProfile,
// profiler.GoroutineProfile,
),
)
if err != nil {
log.Fatal(err)
}
defer profiler.Stop()
// The profiles below are disabled by
// default to keep overhead low, but
// can be enabled as needed.
// profiler.BlockProfile,
// profiler.MutexProfile,
// profiler.GoroutineProfile,
),
)
if err != nil {
log.Fatal(err)
}
defer profiler.Stop()

http.HandleFunc("/gke-info", func(w http.ResponseWriter, r *http.Request) {
getClusterInfo(w, r, nil)
})
http.HandleFunc("/gke-info", getClusterInfo)

port := "8080"
if envPort := os.Getenv("PORT"); envPort != "" {
port = envPort
}
port := "8080"
if envPort := os.Getenv("PORT"); envPort != "" {
port = envPort
}

http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
}
53 changes: 0 additions & 53 deletions main_test.go

This file was deleted.

Loading