Skip to content

Commit

Permalink
Configure Gateway TLS (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettcurtis authored Jul 13, 2024
1 parent 4b150e8 commit 82bc8b1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cmd/http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
// main initializes the HTTP server and sets up the routes.
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/metadata/", metadata.MetadataHandler)
mux.HandleFunc("/health", metadata.HealthCheckHandler)
mux.HandleFunc("/gke-info-go/metadata/", metadata.MetadataHandler)
mux.HandleFunc("/gke-info-go/health", metadata.HealthCheckHandler)

port := "8080"
if envPort := os.Getenv("PORT"); envPort != "" {
Expand Down
10 changes: 5 additions & 5 deletions cmd/http/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestMain(t *testing.T) {
// Set up a test server
mux := http.NewServeMux()
mux.HandleFunc("/metadata/", metadata.MetadataHandler)
mux.HandleFunc("/gke-info-go/metadata/", metadata.MetadataHandler)

ts := httptest.NewServer(mux)
defer ts.Close()
Expand All @@ -31,10 +31,10 @@ func TestMain(t *testing.T) {
expectedBody string
isJSON bool
}{
{ts.URL + "/metadata/cluster-name", http.StatusOK, `{"cluster-name":"test-cluster-name"}`, true},
{ts.URL + "/metadata/cluster-location", http.StatusOK, `{"cluster-location":"test-cluster-location"}`, true},
{ts.URL + "/metadata/instance-zone", http.StatusOK, `{"instance-zone":"us-central1-a"}`, true},
{ts.URL + "/metadata/unknown", http.StatusBadRequest, "Unknown metadata type\n", false},
{ts.URL + "/gke-info-go/metadata/cluster-name", http.StatusOK, `{"cluster-name":"test-cluster-name"}`, true},
{ts.URL + "/gke-info-go/metadata/cluster-location", http.StatusOK, `{"cluster-location":"test-cluster-location"}`, true},
{ts.URL + "/gke-info-go/metadata/instance-zone", http.StatusOK, `{"instance-zone":"us-central1-a"}`, true},
{ts.URL + "/gke-info-go/metadata/unknown", http.StatusBadRequest, "Unknown metadata type\n", false},
}

// Mock FetchMetadata for the tests
Expand Down
8 changes: 4 additions & 4 deletions deployments/regional/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ resource "kubernetes_deployment_v1" "gke_info_go" {

liveness_probe {
http_get {
path = "/health"
path = "/gke-info-go/health"
port = "8080"
}

Expand All @@ -243,7 +243,7 @@ resource "kubernetes_deployment_v1" "gke_info_go" {

readiness_probe {
http_get {
path = "/health"
path = "/gke-info-go/health"
port = "8080"
}

Expand Down Expand Up @@ -280,7 +280,7 @@ resource "kubernetes_service_v1" "gke_info_go" {
}

spec {
cluster_ip = "None"
type = "ClusterIP"
selector = {
app = "gke-info-go"
}
Expand All @@ -300,7 +300,7 @@ resource "kubernetes_service_v1" "gke_info_go_regional" {
}

spec {
cluster_ip = "None"
type = "ClusterIP"
selector = {
app = "gke-info-go"
}
Expand Down
8 changes: 4 additions & 4 deletions internal/metadata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
}
}

// MetadataHandler handles the /metadata/* endpoint and fetches the requested metadata.
// MetadataHandler handles the /gke-info-go/metadata/* endpoint and fetches the requested metadata.
func MetadataHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("Received request for %s", r.URL.Path)

pathParts := strings.Split(r.URL.Path, "/")
if len(pathParts) < 3 {
if len(pathParts) < 4 {
log.Printf("Invalid request: %s", r.URL.Path)
http.Error(w, "Invalid request: expected /metadata/{type}", http.StatusBadRequest)
http.Error(w, "Invalid request: expected /gke-info-go/metadata/{type}", http.StatusBadRequest)
return
}

metadataType := pathParts[2]
metadataType := pathParts[3]
var url string

switch metadataType {
Expand Down
8 changes: 4 additions & 4 deletions internal/metadata/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ func TestMetadataHandler(t *testing.T) {
expectedBody string
isJSON bool
}{
{"/metadata/cluster-name", http.StatusOK, `{"cluster-name":"test-cluster-name"}`, true},
{"/metadata/cluster-location", http.StatusOK, `{"cluster-location":"test-cluster-location"}`, true},
{"/metadata/instance-zone", http.StatusOK, `{"instance-zone":"us-central1-a"}`, true},
{"/metadata/unknown", http.StatusBadRequest, "Unknown metadata type", false},
{"/gke-info-go/metadata/cluster-name", http.StatusOK, `{"cluster-name":"test-cluster-name"}`, true},
{"/gke-info-go/metadata/cluster-location", http.StatusOK, `{"cluster-location":"test-cluster-location"}`, true},
{"/gke-info-go/metadata/instance-zone", http.StatusOK, `{"instance-zone":"us-central1-a"}`, true},
{"/gke-info-go/metadata/unknown", http.StatusBadRequest, "Unknown metadata type", false},
}

for _, test := range tests {
Expand Down

0 comments on commit 82bc8b1

Please sign in to comment.