Skip to content

Commit

Permalink
Remove tests (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettcurtis authored May 18, 2024
1 parent 534e98d commit af4101f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 137 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ WORKDIR /app

COPY main.go .

RUN go mod init gke-info-go
RUN go mod init gke-info
RUN go mod tidy
RUN go build -o main .

Expand Down
56 changes: 40 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,83 @@
# GKE Info Go

[![Docker Build and Test](https://github.com/osinfra-io/gke-info-go/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/osinfra-io/gke-info-go/actions/workflows/build-and-test.yml)
[![Docker Build and Test](https://github.com/osinfra-io/gke-info-go/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/osinfra-io/gke-info-go/actions/workflows/build-and-test.yml) [![Docker Build and Push](https://github.com/osinfra-io/gke-info-go/actions/workflows/build-and-push.yml/badge.svg)](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
```
2 changes: 1 addition & 1 deletion go.mod
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

Expand Down
126 changes: 60 additions & 66 deletions main.go
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)
}
53 changes: 0 additions & 53 deletions main_test.go

This file was deleted.

0 comments on commit af4101f

Please sign in to comment.