Skip to content

Commit

Permalink
show the request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
rgl committed Mar 17, 2024
1 parent 8f109b7 commit df0e4e4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 44 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ Container that shows details about the environment its running on.

It will:

* Show its environment variables.
* Show its tokens, secrets, and configs (config maps).
* Show its pod name and namespace.
* Show the containers running inside its pod.
* Show its memory limits.
* Show its cgroups.
* Show the request method, url, and headers.
* Show the client and server address.
* Show the container environment variables.
* Show the container tokens, secrets, and configs (config maps).
* Show the container pod name and namespace.
* Show the containers running inside the container pod.
* Show the container memory limits.
* Show the container cgroups.
* Expose as a Kubernetes `LoadBalancer` `Service`.
* Note that this results in the creation of an [EC2 Classic Load Balancer (CLB)](https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/introduction.html).
* Use [Role and RoleBinding](https://kubernetes.io/docs/reference/access-authn-authz/rbac/).
Expand Down
102 changes: 64 additions & 38 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ table > tbody > tr:hover {
<tr><th>Uptime</th><td>{{.Uptime}}</td></tr>
</tbody>
</table>
<table>
<caption>Request Headers</caption>
<tbody>
{{- range .RequestHeaders}}
<tr>
<th>{{.Name}}</th>
<td>{{.Value}}</td>
</tr>
{{- end}}
</tbody>
</table>
<table>
<caption>Environment Variables</caption>
<tbody>
Expand Down Expand Up @@ -264,25 +275,26 @@ type nameValuePair struct {
}

type indexData struct {
Pid int
Uid int
Gid int
PodContainers string
Cgroup string
MemoryLimit string
MemoryUsage string
Request string
ClientAddress string
ServerAddress string
Hostname string
Os string
Architecture string
Runtime string
Uptime string
Environment []nameValuePair
Secrets []nameValuePair
Configs []nameValuePair
AzureDNSZones []nameValuePair
Pid int
Uid int
Gid int
PodContainers string
Cgroup string
MemoryLimit string
MemoryUsage string
Request string
RequestHeaders []nameValuePair
ClientAddress string
ServerAddress string
Hostname string
Os string
Architecture string
Runtime string
Uptime string
Environment []nameValuePair
Secrets []nameValuePair
Configs []nameValuePair
AzureDNSZones []nameValuePair
}

type nameValuePairs []nameValuePair
Expand All @@ -299,6 +311,17 @@ func logRequest(handler http.HandlerFunc) http.HandlerFunc {
}
}

func headersToNameValuePairs(headers http.Header) nameValuePairs {
var pairs nameValuePairs
for name, values := range headers {
for _, value := range values {
pairs = append(pairs, nameValuePair{name, value})
}
}
sort.Sort(nameValuePairs(pairs))
return pairs
}

func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

Expand Down Expand Up @@ -380,6 +403,8 @@ func main() {
}
}

requestHeaders := headersToNameValuePairs(r.Header)

podContainers, err := getPodContainers()
if err != nil {
panic(err)
Expand All @@ -388,25 +413,26 @@ func main() {
azureDNSZones := getAzureDNSZones()

err = indexTemplate.ExecuteTemplate(w, "Index", indexData{
Pid: os.Getpid(),
Uid: os.Getuid(),
Gid: os.Getgid(),
PodContainers: podContainers,
Cgroup: string(cgroup),
MemoryLimit: string(memoryLimit),
MemoryUsage: string(memoryUsage),
Request: fmt.Sprintf("%s %s%s", r.Method, r.Host, r.URL),
ClientAddress: r.RemoteAddr,
ServerAddress: r.Context().Value(http.LocalAddrContextKey).(net.Addr).String(),
Hostname: hostname,
Os: runtime.GOOS,
Architecture: runtime.GOARCH,
Runtime: runtime.Version(),
Uptime: uptime().String(),
Environment: environment,
Secrets: secrets,
Configs: configs,
AzureDNSZones: azureDNSZones,
Pid: os.Getpid(),
Uid: os.Getuid(),
Gid: os.Getgid(),
PodContainers: podContainers,
Cgroup: string(cgroup),
MemoryLimit: string(memoryLimit),
MemoryUsage: string(memoryUsage),
Request: fmt.Sprintf("%s %s%s", r.Method, r.Host, r.URL),
RequestHeaders: requestHeaders,
ClientAddress: r.RemoteAddr,
ServerAddress: r.Context().Value(http.LocalAddrContextKey).(net.Addr).String(),
Hostname: hostname,
Os: runtime.GOOS,
Architecture: runtime.GOARCH,
Runtime: runtime.Version(),
Uptime: uptime().String(),
Environment: environment,
Secrets: secrets,
Configs: configs,
AzureDNSZones: azureDNSZones,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down

0 comments on commit df0e4e4

Please sign in to comment.