forked from GoogleCloudPlatform/k8s-multicluster-ingress
-
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.
- This simplifies the sample app a great deal since it no longer uses nginx (confuses users), it has simpler deployment manifest - This new sampel app is a web server directly, written in Go, it renders the flag of the gce region in the response. - It is clean and understandable code as opposed to ConfigMap/nginx.conf hack we used to have. This will likely require a lot of README.md changes, so please hold off until the other PRs regarding tutorial updates are merged. Signed-off-by: Ahmet Alp Balkan <[email protected]>
- Loading branch information
Showing
8 changed files
with
190 additions
and
83 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
...s/zone-printer/ingress/https-ingress.yaml → ...s/zone-printer/ingress/ingress-https.yaml
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,14 +1,14 @@ | ||
apiVersion: extensions/v1beta1 | ||
kind: Ingress | ||
metadata: | ||
name: nginx | ||
name: zoneprinter | ||
annotations: | ||
kubernetes.io/ingress.global-static-ip-name: $ZP_KUBEMCI_IP | ||
kubernetes.io/ingress.class: gce-multi-cluster | ||
kubernetes.io/ingress.global-static-ip-name: $ZP_KUBEMCI_IP | ||
spec: | ||
tls: | ||
# This assumes tls-secret exists. | ||
- secretName: tls-secret | ||
backend: | ||
serviceName: nginx | ||
serviceName: zoneprinter | ||
servicePort: 80 |
6 changes: 3 additions & 3 deletions
6
examples/zone-printer/ingress/nginx.yaml → examples/zone-printer/ingress/ingress.yaml
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,11 +1,11 @@ | ||
apiVersion: extensions/v1beta1 | ||
kind: Ingress | ||
metadata: | ||
name: nginx | ||
name: zoneprinter | ||
annotations: | ||
kubernetes.io/ingress.global-static-ip-name: $ZP_KUBEMCI_IP | ||
kubernetes.io/ingress.class: gce-multi-cluster | ||
kubernetes.io/ingress.global-static-ip-name: $ZP_KUBEMCI_IP | ||
spec: | ||
backend: | ||
serviceName: nginx | ||
serviceName: zoneprinter | ||
servicePort: 80 |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
apiVersion: apps/v1beta1 | ||
kind: Deployment | ||
metadata: | ||
name: zoneprinter | ||
labels: | ||
app: zoneprinter | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: zoneprinter | ||
template: | ||
metadata: | ||
labels: | ||
app: zoneprinter | ||
spec: | ||
containers: | ||
- name: frontend | ||
image: zoneprinter:latest | ||
ports: | ||
- containerPort: 80 |
16 changes: 7 additions & 9 deletions
16
examples/zone-printer/app/nginx-svc.yaml → examples/zone-printer/manifests/service.yaml
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,16 +1,14 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: zoneprinter | ||
labels: | ||
app: nginx | ||
name: nginx | ||
app: zoneprinter | ||
spec: | ||
type: NodePort | ||
selector: | ||
app: zoneprinter | ||
ports: | ||
- port: 80 | ||
protocol: TCP | ||
targetPort: 80 | ||
name: http | ||
- name: http | ||
port: 80 | ||
nodePort: 30061 | ||
selector: | ||
app: nginx | ||
type: NodePort |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM golang:1.10-alpine as compiler | ||
RUN apk add --no-cache git | ||
RUN go get cloud.google.com/go/compute/metadata | ||
WORKDIR /go/src/zoneprinter | ||
COPY . . | ||
RUN go install | ||
|
||
FROM alpine | ||
COPY --from=compiler /go/bin/zoneprinter ./zoneprinter | ||
ENTRYPOINT ./zoneprinter | ||
EXPOSE 80 | ||
|
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 |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright 2018 Google Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
|
||
"cloud.google.com/go/compute/metadata" | ||
) | ||
|
||
var ( | ||
computeZone string | ||
) | ||
|
||
func main() { | ||
if !metadata.OnGCE() { | ||
log.Println("warn: not running on gce") | ||
} else { | ||
zone, err := metadata.Zone() | ||
if err != nil { | ||
log.Fatalf("failed to get compute zone: %+v", err) | ||
} | ||
computeZone = zone | ||
log.Printf("info: determined zone: %q", zone) | ||
} | ||
|
||
log.Println("starting to listen on port 80") | ||
http.HandleFunc("/", handle) | ||
err := http.ListenAndServe(":80", nil) | ||
log.Fatal(err) | ||
} | ||
|
||
func handle(w http.ResponseWriter, r *http.Request) { | ||
var srcIP string | ||
if ipHeader := r.Header.Get("X-Forwarded-For"); ipHeader != "" { | ||
srcIP = ipHeader | ||
} else { | ||
srcIP = r.RemoteAddr | ||
} | ||
log.Printf("received request method=%s path=%q src=%q", r.Method, r.URL.Path, srcIP) | ||
|
||
if computeZone == "" { | ||
fmt.Fprintf(w, `<!DOCTYPE html> | ||
<h1>Cannot determine the compute zone :(</h1> | ||
<p>Is it running on a Google Compute Engine instance?</p>`) | ||
return | ||
} | ||
|
||
region := computeZone[:strings.LastIndex(computeZone, "-")] | ||
dc, ok := datacenters[region] | ||
if !ok { | ||
fmt.Fprintf(w, `<!DOCTYPE html> | ||
<h4>Welcome from Google Cloud datacenters at:<h4> | ||
<h1>%s!</h1>`, computeZone) | ||
return | ||
} | ||
fmt.Fprintf(w, `<!DOCTYPE html> | ||
<h4>Welcome from Google Cloud datacenters at:</h4> | ||
<h1>%s</h1> | ||
<h3>You are now connected to "%s"</h3> | ||
<img src="%s" style="width: 640px; height: auto; border: 1px solid black"/>`, dc.location, computeZone, dc.flagURL) | ||
} | ||
|
||
var ( | ||
// datacenters adopted from https://cloud.google.com/compute/docs/regions-zones/ | ||
datacenters = map[string]struct { | ||
location string | ||
flagURL string // flag images must be public domain | ||
}{ | ||
"northamerica-northeast1": { | ||
location: "Montréal, Canada", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/commons/d/d9/Flag_of_Canada_%28Pantone%29.svg", | ||
}, | ||
"us-central1": { | ||
location: "Council Bluffs, Iowa, USA", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg", | ||
}, | ||
"us-west1": { | ||
location: "The Dalles, Oregon, USA", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg", | ||
}, | ||
"us-east4": { | ||
location: "Ashburn, Virginia, USA", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg", | ||
}, | ||
"us-east1": { | ||
location: "Moncks Corner, South Carolina, USA", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg", | ||
}, | ||
"southamerica-east1": { | ||
location: "São Paulo, Brazil", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/en/0/05/Flag_of_Brazil.svg", | ||
}, | ||
"europe-west1": { | ||
location: "St. Ghislain, Belgium", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/commons/6/65/Flag_of_Belgium.svg", | ||
}, | ||
"europe-west2": { | ||
location: "London, U.K.", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/en/a/ae/Flag_of_the_United_Kingdom.svg", | ||
}, | ||
"europe-west3": { | ||
location: "Frankfurt, Germany", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/en/b/ba/Flag_of_Germany.svg", | ||
}, | ||
"europe-west4": { | ||
location: "Eemshaven, Netherlands", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/commons/2/20/Flag_of_the_Netherlands.svg", | ||
}, | ||
"asia-south1": { | ||
location: "Mumbai, India", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/en/4/41/Flag_of_India.svg", | ||
}, | ||
"asia-southeast1": { | ||
location: "Jurong West, Singapore", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/commons/4/48/Flag_of_Singapore.svg", | ||
}, | ||
"asia-east1": { | ||
location: "Changhua County, Taiwan", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/commons/7/72/Flag_of_the_Republic_of_China.svg", | ||
}, | ||
"asia-northeast1": { | ||
location: "Tokyo, Japan", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/en/9/9e/Flag_of_Japan.svg", | ||
}, | ||
"australia-southeast1": { | ||
location: "Sydney, Australia", | ||
flagURL: "https://upload.wikimedia.org/wikipedia/en/b/b9/Flag_of_Australia.svg", | ||
}, | ||
} | ||
) |