-
Notifications
You must be signed in to change notification settings - Fork 138
/
hello.go
98 lines (86 loc) · 2.61 KB
/
hello.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2019 Google LLC
//
// 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
//
// https://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"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
)
type Data struct {
Service string
Revision string
Project string
NetworkEgressError bool
ProjectFound bool
}
func main() {
tmpl := template.Must(template.ParseFiles("index.html"))
// Get project ID from metadata server
project := "???"
projectFound := false
client := &http.Client{}
req, _ := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/project/project-id", nil)
req.Header.Set("Metadata-Flavor", "Google")
res, err := client.Do(req)
if err == nil {
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
project = string(responseBody)
projectFound = true
}
// Check for network egress configuration (CR-GKE)
networkEgressError := false
if projectFound == false {
// Check to see if we can reach something off the cluster e.g. www.google.com
req, _ = http.NewRequest("HEAD", "https://www.google.com", nil)
res, err = client.Do(req)
if err == nil && res.StatusCode >= 200 && res.StatusCode <= 299 {
// egress worked successfully
log.Print("Verified that network egress is working as expected.")
} else {
log.Print("Network egress appears to be blocked. Unable to access https://www.google.com.")
networkEgressError = true
}
}
service := os.Getenv("K_SERVICE")
if service == "" {
service = "???"
}
revision := os.Getenv("K_REVISION")
if revision == "" {
revision = "???"
}
data := Data{
Service: service,
Revision: revision,
Project: project,
NetworkEgressError: networkEgressError,
ProjectFound: projectFound,
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl.Execute(w, data)
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Print("Hello from Cloud Run! The container started successfully and is listening for HTTP requests on $PORT.")
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}