Skip to content
Snippets Groups Projects
Verified Commit 80a9389b authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Initial Commit

parents
Branches
No related tags found
No related merge requests found
Pipeline #425 failed
/.idea/
/vendor/
\ No newline at end of file
FROM golang:alpine as builder
RUN apk add --no-cache git
WORKDIR /src
COPY go.* ./
RUN go mod download
COPY *.go ./
RUN CGO_ENABLED=0 GOOS=linux go build .
FROM scratch
COPY mime.types /etc/mime.types
COPY assets /assets
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /src/custom-errors /custom-errors
ENTRYPOINT ["/custom-errors"]
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error {{.Code}} | kuschku.de</title>
<link rel="shortcut icon" href="https://kuschku.de/favicon.png">
<link rel="shortcut icon" href="https://kuschku.de/favicon.svg">
<meta name="generator" content="Human v1.0">
<meta name="referrer" content="origin">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="HandheldFriendly" content="True">
<meta name="theme-color" content="#0075BF">
<meta name="msapplication-navbutton-color" content="#0075BF">
<meta name="apple-mobile-web-app-status-bar-style" content="#0075BF">
<title>Error {{.Code}} | kuschku.de</title>
<style>
* {
margin: 0;
padding: 0
}
html, code {
font: 15px/22px arial, sans-serif
}
html {
background: #fff;
color: #222;
padding: 15px
}
body {
margin: 7% auto 0;
max-width: 390px;
min-height: 180px;
padding: 30px 0 15px
}
p {
margin: 11px 0 22px;
overflow: hidden
}
ins {
color: #777;
text-decoration: none
}
a {
text-decoration: none
}
a img {
width: 32px;
height: 32px;
vertical-align: bottom
}
a span {
color: #0075BF;
font-weight: bold;
font-size: 24px;
letter-spacing: -1px;
line-height: 24px;
margin-left: 4px
}
@media screen and (max-width: 772px) {
body {
margin-top: 0;
max-width: none
}
}
</style>
</head>
<body>
<p>
<a href="//kuschku.de/">
<img src="//kuschku.de/assets/images/logo.svg" aria-hidden="true" alt="Logo">
<span>kuschku.de</span>
</a>
</p>
<p>
<b>{{.Code}}.</b>
<ins>That’s an error.</ins>
</p>
<p>
The requested site <code>{{.Url}}</code> was not found on this server.<br>
<ins>That’s all we know.</ins>
</p>
</body>
</html>
\ No newline at end of file
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error {{.Code}} | kuschku.de</title>
<link rel="shortcut icon" href="https://kuschku.de/favicon.png">
<link rel="shortcut icon" href="https://kuschku.de/favicon.svg">
<meta name="generator" content="Human v1.0">
<meta name="referrer" content="origin">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="HandheldFriendly" content="True">
<meta name="theme-color" content="#0075BF">
<meta name="msapplication-navbutton-color" content="#0075BF">
<meta name="apple-mobile-web-app-status-bar-style" content="#0075BF">
<style>
* {
margin: 0;
padding: 0
}
html, code {
font: 15px/22px arial, sans-serif
}
html {
background: #fff;
color: #222;
padding: 15px
}
body {
margin: 7% auto 0;
max-width: 390px;
min-height: 180px;
padding: 30px 0 15px
}
p {
margin: 11px 0 22px;
overflow: hidden
}
ins {
color: #777;
text-decoration: none
}
a {
text-decoration: none
}
a img {
width: 32px;
height: 32px;
vertical-align: bottom
}
a span {
color: #0075BF;
font-weight: bold;
font-size: 24px;
letter-spacing: -1px;
line-height: 24px;
margin-left: 4px
}
@media screen and (max-width: 772px) {
body {
margin-top: 0;
max-width: none
}
}
</style>
</head>
<body>
<p>
<a href="//kuschku.de/">
<img src="//kuschku.de/assets/images/logo.svg" aria-hidden="true" alt="Logo">
<span>kuschku.de</span>
</a>
</p>
<p>
<b>{{.Code}}.</b>
<ins>That’s an error.</ins>
</p>
<p>
An error occured while trying to access <code>{{.Url}}</code>.<br>
<ins>That’s all we know.</ins>
</p>
</body>
</html>
\ No newline at end of file
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error {{.Code}} | kuschku.de</title>
<link rel="shortcut icon" href="https://kuschku.de/favicon.png">
<link rel="shortcut icon" href="https://kuschku.de/favicon.svg">
<meta name="generator" content="Human v1.0">
<meta name="referrer" content="origin">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="HandheldFriendly" content="True">
<meta name="theme-color" content="#0075BF">
<meta name="msapplication-navbutton-color" content="#0075BF">
<meta name="apple-mobile-web-app-status-bar-style" content="#0075BF">
<style>
* {
margin: 0;
padding: 0
}
html, code {
font: 15px/22px arial, sans-serif
}
html {
background: #fff;
color: #222;
padding: 15px
}
body {
margin: 7% auto 0;
max-width: 390px;
min-height: 180px;
padding: 30px 0 15px
}
p {
margin: 11px 0 22px;
overflow: hidden
}
ins {
color: #777;
text-decoration: none
}
a {
text-decoration: none
}
a img {
width: 32px;
height: 32px;
vertical-align: bottom
}
a span {
color: #0075BF;
font-weight: bold;
font-size: 24px;
letter-spacing: -1px;
line-height: 24px;
margin-left: 4px
}
@media screen and (max-width: 772px) {
body {
margin-top: 0;
max-width: none
}
}
</style>
</head>
<body>
<p>
<a href="//kuschku.de/">
<img src="//kuschku.de/assets/images/logo.svg" aria-hidden="true" alt="Logo">
<span>kuschku.de</span>
</a>
</p>
<p>
<b>{{.Code}}.</b>
<ins>That’s an error.</ins>
</p>
<p>
An error occured while trying to access <code>{{.Url}}</code>.<br>
<ins>That’s all we know.</ins>
</p>
</body>
</html>
\ No newline at end of file
build.sh 0 → 100755
#!/bin/sh
IMAGE=k8r.eu/justjanne/custom-errors
TAGS=$(git describe --always --tags HEAD)
docker build -t $IMAGE:$TAGS .
docker tag $IMAGE:$TAGS $IMAGE:latest
echo Successfully tagged $IMAGE:latest
docker push $IMAGE:$TAGS
docker push $IMAGE:latest
\ No newline at end of file
#!/bin/sh
IMAGE=k8r.eu/justjanne/custom-errors
TAGS=$(git describe --always --tags HEAD)
DEPLOYMENT=custom-errors
POD=custom-errors
NAMESPACE=ingress
kubectl -n $NAMESPACE set image deployment/$DEPLOYMENT $POD=$IMAGE:$TAGS
\ No newline at end of file
package: git.kuschku.de/justjanne/custom-errors
import:
- package: github.com/prometheus/client_golang
\ No newline at end of file
go.mod 0 → 100644
go.sum 0 → 100644
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/prometheus/client_golang v0.9.2 h1:awm861/B8OKDd2I/6o1dy3ra4BamzKhYOiGItCeZ740=
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 h1:idejC8f05m9MGOsuEi1ATq9shN03HrxNkD/luQvxCv8=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275 h1:PnBWHBf+6L0jOqq0gIVUe6Yk0/QMZ640k6NvkxcBf+8=
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a h1:9a8MnZMP0X2nLJdBg+pBmGgkJlSaKC2KaQmTCk1XDtE=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
main.go 0 → 100644
/*
Copyright 2017 The Kubernetes Authors.
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"
"html/template"
"log"
"net/url"
"path"
"mime"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
// FormatHeader name of the header used to extract the format
FormatHeader = "X-Format"
// CodeHeader name of the header used as source of the HTTP statu code to return
CodeHeader = "X-Code"
// ContentType name of the header that defines the format of the reply
ContentType = "Content-Type"
// OriginalURI name of the header with the original URL from NGINX
OriginalURI = "X-Original-URI"
// Namespace name of the header that contains information about the Ingress namespace
Namespace = "X-Namespace"
// IngressName name of the header that contains the matched Ingress
IngressName = "X-Ingress-Name"
// ServiceName name of the header that contains the matched Service in the Ingress
ServiceName = "X-Service-Name"
// ServicePort name of the header that contains the matched Service port in the Ingress
ServicePort = "X-Service-Port"
// ErrFilesPathVar is the name of the environment variable indicating
// the location on disk of files served by the handler.
ErrFilesPathVar = "ERROR_FILES_PATH"
)
type ErrorInfo struct {
Url string
Code int
}
func main() {
errFilesPath := "/assets"
if os.Getenv(ErrFilesPathVar) != "" {
errFilesPath = os.Getenv(ErrFilesPathVar)
}
http.HandleFunc("/", errorHandler(errFilesPath))
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
http.ListenAndServe(fmt.Sprintf(":8080"), nil)
}
func errorHandler(errorFilesPath string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
ext := "html"
if os.Getenv("DEBUG") != "" {
w.Header().Set(FormatHeader, r.Header.Get(FormatHeader))
w.Header().Set(CodeHeader, r.Header.Get(CodeHeader))
w.Header().Set(ContentType, r.Header.Get(ContentType))
w.Header().Set(OriginalURI, r.Header.Get(OriginalURI))
w.Header().Set(Namespace, r.Header.Get(Namespace))
w.Header().Set(IngressName, r.Header.Get(IngressName))
w.Header().Set(ServiceName, r.Header.Get(ServiceName))
w.Header().Set(ServicePort, r.Header.Get(ServicePort))
}
format := r.Header.Get(FormatHeader)
if format == "" {
format = "text/html"
log.Printf("format not specified. Using %v", format)
}
cext, err := mime.ExtensionsByType(format)
if err != nil {
log.Printf("unexpected error reading media type extension: %v. Using %v", err, ext)
} else if len(cext) == 0 {
log.Printf("couldn't get media type extension. Using %v", ext)
} else {
ext = cext[0]
}
w.Header().Set(ContentType, format)
errCode := r.Header.Get(CodeHeader)
originalURI, _ := url.Parse(r.Header.Get(OriginalURI))
var requestURI string
if originalURI != nil {
requestURI, err = url.PathUnescape(originalURI.RequestURI())
}
code, err := strconv.Atoi(errCode)
if err != nil {
code = 404
log.Printf("unexpected error reading return code: %v. Using %v", err, code)
}
w.WriteHeader(code)
if !strings.HasPrefix(ext, ".") {
ext = "." + ext
}
file := fmt.Sprintf("%v/%v%v", errorFilesPath, code, ext)
if _, err := os.Stat(file); os.IsNotExist(err) {
log.Printf("unexpected error opening file: %v", err)
scode := strconv.Itoa(code)
file = fmt.Sprintf("%v/%cxx%v", errorFilesPath, scode[0], ext)
if _, err := os.Stat(file); os.IsNotExist(err) {
log.Printf("unexpected error opening file: %v", err)
http.NotFound(w, r)
return
}
}
tmpl, err := template.New(path.Base(file)).ParseFiles(file)
if err != nil {
log.Printf("unexpected error executing template: %v", err)
http.NotFound(w, r)
return
}
log.Printf("serving custom error response for code %v and format %v from file %v", code, format, file)
err = tmpl.Execute(w, ErrorInfo{
Url: requestURI,
Code: code,
})
if err != nil {
log.Printf("unexpected error executing template: %v", err)
http.NotFound(w, r)
return
}
duration := time.Now().Sub(start).Seconds()
proto := strconv.Itoa(r.ProtoMajor)
proto = fmt.Sprintf("%s.%s", proto, strconv.Itoa(r.ProtoMinor))
requestCount.WithLabelValues(proto).Inc()
requestDuration.WithLabelValues(proto).Observe(duration)
}
}
/*
Copyright 2017 The Kubernetes Authors.
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.
*/
// Collect and display prometheus metrics
package main
import (
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "default_http_backend"
subsystem = "http"
)
var (
requestCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "request_count_total",
Help: "Counter of HTTP requests made.",
}, []string{"proto"})
requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "request_duration_milliseconds",
Help: "Histogram of the time (in milliseconds) each request took.",
Buckets: append([]float64{.001, .003}, prometheus.DefBuckets...),
}, []string{"proto"})
)
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment