Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Welcome defaultbackend to the ingress-gce repo #503

Merged
merged 4 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions cmd/404-server/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
approvers:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need a separate OWNERS for this. The top level OWNERS should suffice.

- bprashanth
- luxas
- mikedanese
- aledbf
- nicksardo
reviewers:
- bprashanth
- luxas
- mikedanese
- aledbf
- nicksardo
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)
}
}