Skip to content

Commit

Permalink
Merge pull request #503 from jonpulsifer/hello-404-server
Browse files Browse the repository at this point in the history
 Welcome defaultbackend to the ingress-gce repo
  • Loading branch information
k8s-ci-robot committed Nov 14, 2018
2 parents 411b66d + 06d7cd3 commit a101b02
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Dockerfile.404-server
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2015 The Kubernetes Authors. All rights reserved.
#
# 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.

FROM scratch

ADD bin/ARG_ARCH/ARG_BIN /ARG_BIN

# nobody:nobody
USER 65534:65534

ENTRYPOINT ["/ARG_BIN"]
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ PKG := k8s.io/ingress-gce
# List of binaries to build. You must have a matching Dockerfile.BINARY
# for each BINARY.
CONTAINER_BINARIES ?= \
404-server \
e2e-test \
echo \
fuzzer \
Expand Down
6 changes: 6 additions & 0 deletions cmd/404-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 404-server (default backend)

404-server is a simple webserver that satisfies the ingress, which means it has to do two things:

1. Serves a 404 page at `/`
2. Serves a 200 at `/healthz`
97 changes: 97 additions & 0 deletions cmd/404-server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
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.
*/

// A webserver that only serves a 404 page. Used as a default backend.
package main

import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)

func main() {
// command line arguments
port := flag.Int("port", 8080, "Port number to serve default backend 404 page.")
timeout := flag.Duration("timeout", 5*time.Second, "Time in seconds to wait before forcefully terminating the server.")

flag.Parse()

notFound := newHTTPServer(fmt.Sprintf(":%d", *port), notFound())

// start the main http server
go func() {
err := notFound.ListenAndServe()
if err != http.ErrServerClosed {
fmt.Fprintf(os.Stderr, "could not start http server: %s\n", err)
os.Exit(1)
}
}()

waitShutdown(notFound, *timeout)
}

type server struct {
mux *http.ServeMux
}

func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}

func newHTTPServer(addr string, handler http.Handler) *http.Server {
return &http.Server{
Addr: addr,
Handler: handler,
ReadTimeout: 10 * time.Second,
ReadHeaderTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 10 * time.Second,
}
}

func notFound(options ...func(*server)) *server {
s := &server{mux: http.NewServeMux()}
// TODO: this handler exists only to avoid breaking existing deployments
s.mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "ok")
})
s.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "default backend - 404")
})
return s
}

func waitShutdown(s *http.Server, timeout time.Duration) {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop

fmt.Fprintf(os.Stdout, "stopping http server...\n")
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

if err := s.Shutdown(ctx); err != nil {
fmt.Fprintf(os.Stderr, "could not gracefully shutdown http server: %s\n", err)
}
}

0 comments on commit a101b02

Please sign in to comment.