Skip to content

Commit

Permalink
Rewrite zone-printer app
Browse files Browse the repository at this point in the history
- 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
ahmetb committed Apr 4, 2018
1 parent b6bebd5 commit 6e313b1
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 83 deletions.
44 changes: 0 additions & 44 deletions examples/zone-printer/app/nginx-dep.yaml

This file was deleted.

24 changes: 0 additions & 24 deletions examples/zone-printer/app/zonefetch.yaml

This file was deleted.

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
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
20 changes: 20 additions & 0 deletions examples/zone-printer/manifests/deployment.yaml
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
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
12 changes: 12 additions & 0 deletions examples/zone-printer/src/Dockerfile
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

145 changes: 145 additions & 0 deletions examples/zone-printer/src/main.go
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 &quot;%s&quot;</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",
},
}
)

0 comments on commit 6e313b1

Please sign in to comment.