-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
534e98d
commit af4101f
Showing
5 changed files
with
102 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,83 @@ | ||
# GKE Info Go | ||
|
||
[](https://github.com/osinfra-io/gke-info-go/actions/workflows/build-and-test.yml) | ||
[](https://github.com/osinfra-io/gke-info-go/actions/workflows/build-and-test.yml) [](https://github.com/osinfra-io/gke-info-go/actions/workflows/build-and-push.yml) | ||
|
||
## Usage | ||
|
||
```yaml | ||
--- | ||
apiVersion: v1 | ||
kind: Namespace | ||
|
||
metadata: | ||
name: gke-info | ||
|
||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
|
||
metadata: | ||
name: gke-info-go | ||
namespace: gke-info | ||
|
||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: gke-info-go | ||
version: v1 | ||
|
||
template: | ||
metadata: | ||
labels: | ||
app: gke-info-go | ||
version: v1 | ||
|
||
spec: | ||
containers: | ||
- image: ghcr.io/osinfra-io/gke-info-go:latest | ||
imagePullPolicy: IfNotPresent | ||
name: gke-info-go | ||
ports: | ||
- containerPort: 8080 | ||
imagePullSecrets: | ||
- name: github-container-registry-key | ||
- image: ghcr.io/osinfra-io/gke-info-go:latest | ||
imagePullPolicy: Always | ||
name: gke-info-go | ||
|
||
--- | ||
ports: | ||
- containerPort: 8080 | ||
|
||
resources: | ||
limits: | ||
cpu: "50m" | ||
memory: "128Mi" | ||
requests: | ||
cpu: "25m" | ||
memory: "64Mi" | ||
|
||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
|
||
metadata: | ||
name: gke-info-go | ||
namespace: gke-info | ||
|
||
labels: | ||
app: gke-info-go | ||
|
||
spec: | ||
ports: | ||
- name: http | ||
port: 8080 | ||
targetPort: 8080 | ||
- name: http | ||
port: 8080 | ||
targetPort: 8080 | ||
|
||
selector: | ||
app: gke-info-go | ||
|
||
``` | ||
|
||
After deploying the above, you can check the status of the cluster by running: | ||
After deploying, you can get the information about the GKE cluster by running the following command: | ||
|
||
```bash | ||
kubectl port-forward --namespace gke-info $(kubectl get pod --namespace gke-info --selector="app=gke-info-go" --output jsonpath='{.items[0].metadata.name}') 8080:8080 | ||
``` | ||
|
||
Open your browser to <http://localhost:8080/cluster-name> and you should see the name of the cluster. | ||
Curl the endpoint: | ||
|
||
```bash | ||
curl http://localhost:8080/gke-info | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module gke-info-go | ||
module gke-info | ||
|
||
go 1.22.3 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,78 @@ | ||
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. | ||
|
||
// 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() | ||
// 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) | ||
} |
This file was deleted.
Oops, something went wrong.