|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START gke_quickstart_multitenant_backend] |
| 16 | +apiVersion: v1 |
| 17 | +kind: ConfigMap |
| 18 | +metadata: |
| 19 | + name: backend-configmap |
| 20 | + namespace: backend-team |
| 21 | + labels: |
| 22 | + app: backend |
| 23 | +data: |
| 24 | + go.mod: | |
| 25 | + module multitenant |
| 26 | +
|
| 27 | + go 1.22 |
| 28 | +
|
| 29 | + require github.com/go-sql-driver/mysql v1.8.1 |
| 30 | +
|
| 31 | + require filippo.io/edwards25519 v1.1.0 // indirect |
| 32 | +
|
| 33 | + go.sum: | |
| 34 | + filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= |
| 35 | + filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= |
| 36 | + github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= |
| 37 | + github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= |
| 38 | +
|
| 39 | + backend.go: | |
| 40 | + package main |
| 41 | +
|
| 42 | + import ( |
| 43 | + "database/sql" |
| 44 | + "fmt" |
| 45 | + "log" |
| 46 | + "math/rand" |
| 47 | + "net/http" |
| 48 | + "os" |
| 49 | +
|
| 50 | + _ "github.com/go-sql-driver/mysql" |
| 51 | + ) |
| 52 | +
|
| 53 | + func main() { |
| 54 | + mux := http.NewServeMux() |
| 55 | + mux.HandleFunc("/", frontend) |
| 56 | +
|
| 57 | + port := "8080" |
| 58 | +
|
| 59 | + log.Printf("Server listening on port %s", port) |
| 60 | + log.Fatal(http.ListenAndServe(":"+port, mux)) |
| 61 | + } |
| 62 | +
|
| 63 | + func frontend(w http.ResponseWriter, r *http.Request) { |
| 64 | + log.Printf("Serving request: %s", r.URL.Path) |
| 65 | +
|
| 66 | + host, _ := os.Hostname() |
| 67 | + fmt.Fprintf(w, "Backend!\n") |
| 68 | + fmt.Fprintf(w, "Hostname: %s\n", host) |
| 69 | +
|
| 70 | + // Open database using cloud-sql-proxy sidecar |
| 71 | + db, err := sql.Open("mysql", "multitenant-app@tcp/multitenant-app") |
| 72 | + if err != nil { |
| 73 | + fmt.Fprintf(w, "Error: %v\n", err) |
| 74 | + return |
| 75 | + } |
| 76 | +
|
| 77 | + // Create metadata Table if not exists |
| 78 | + _, err = db.Exec("CREATE TABLE IF NOT EXISTS metadata (metadata_key varchar(255) NOT NULL, metadata_value varchar(255) NOT NULL, PRIMARY KEY (metadata_key))") |
| 79 | + if err != nil { |
| 80 | + fmt.Fprintf(w, "Error: %v\n", err) |
| 81 | + return |
| 82 | + } |
| 83 | +
|
| 84 | + // Pick random primary color |
| 85 | + var color string |
| 86 | + randInt := rand.Intn(3) + 1 |
| 87 | + switch { |
| 88 | + case randInt == 1: |
| 89 | + color = "red" |
| 90 | + case randInt == 2: |
| 91 | + color = "green" |
| 92 | + case randInt == 3: |
| 93 | + color = "blue" |
| 94 | + } |
| 95 | +
|
| 96 | + // Set color in database |
| 97 | + _, err = db.Exec(fmt.Sprintf("REPLACE INTO metadata (metadata_key, metadata_value) VALUES ('color', '%s')", color)) |
| 98 | + if err != nil { |
| 99 | + fmt.Fprintf(w, "Error: %v\n", err) |
| 100 | + return |
| 101 | + } |
| 102 | +
|
| 103 | + fmt.Fprintf(w, "Set Color: %s\n", color) |
| 104 | + } |
| 105 | +
|
| 106 | +--- |
| 107 | +apiVersion: apps/v1 |
| 108 | +kind: Deployment |
| 109 | +metadata: |
| 110 | + name: backendweb |
| 111 | + namespace: backend-team |
| 112 | + labels: |
| 113 | + app: backend |
| 114 | +spec: |
| 115 | + selector: |
| 116 | + matchLabels: |
| 117 | + app: backend |
| 118 | + tier: web |
| 119 | + template: |
| 120 | + metadata: |
| 121 | + labels: |
| 122 | + app: backend |
| 123 | + tier: web |
| 124 | + spec: |
| 125 | + containers: |
| 126 | + - name: backend-container |
| 127 | + image: golang:1.22 |
| 128 | + command: ["go"] |
| 129 | + args: ["run", "."] |
| 130 | + workingDir: "/tmp/backend" |
| 131 | + volumeMounts: |
| 132 | + - name: backend-configmap |
| 133 | + mountPath: /tmp/backend/ |
| 134 | + readOnly: true |
| 135 | + - name: cloud-sql-proxy |
| 136 | + image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.11.4 |
| 137 | + args: |
| 138 | + - "--structured-logs" |
| 139 | + - "--port=3306" |
| 140 | + - "$(CONNECTION_NAME_KEY)" |
| 141 | + securityContext: |
| 142 | + runAsNonRoot: true |
| 143 | + env: |
| 144 | + - name: CONNECTION_NAME_KEY |
| 145 | + valueFrom: |
| 146 | + configMapKeyRef: |
| 147 | + name: database-configmap |
| 148 | + key: CONNECTION_NAME |
| 149 | + volumes: |
| 150 | + - name: backend-configmap |
| 151 | + configMap: { name: backend-configmap } |
| 152 | +--- |
| 153 | +apiVersion: v1 |
| 154 | +kind: Service |
| 155 | +metadata: |
| 156 | + name: backendweb |
| 157 | + namespace: backend-team |
| 158 | + labels: |
| 159 | + app: backend |
| 160 | + annotations: |
| 161 | + networking.gke.io/load-balancer-type: "Internal" # Remove to create an external loadbalancer |
| 162 | +spec: |
| 163 | + selector: |
| 164 | + app: backend |
| 165 | + tier: web |
| 166 | + ports: |
| 167 | + - port: 80 |
| 168 | + targetPort: 8080 |
| 169 | + type: LoadBalancer |
| 170 | +# [END gke_quickstart_multitenant_backend] |
0 commit comments