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

pkg/server/api: Add /healthz for load-balancer health checks #239

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
13 changes: 7 additions & 6 deletions cmd/machine-config-server/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"

"github.com/golang/glog"
"github.com/openshift/machine-config-operator/pkg/server"
Expand Down Expand Up @@ -37,18 +38,18 @@ func runBootstrapCmd(cmd *cobra.Command, args []string) {
glog.Infof("Version: %+v", version.Version)

bs, err := server.NewBootstrapServer(bootstrapOpts.serverBaseDir, bootstrapOpts.serverKubeConfig)

if err != nil {
glog.Exitf("Machine Config Server exited with error: %v", err)
}

apiHandler := server.NewServerAPIHandler(bs)
secureServer := server.NewAPIServer(apiHandler, rootOpts.sport, false, rootOpts.cert, rootOpts.key)
insecureServer := server.NewAPIServer(apiHandler, rootOpts.isport, true, "", "")
secureServer := *bs
secureServer.Addr = fmt.Sprintf(":%d", rootOpts.sport)
insecureServer := *bs
insecureServer.Addr = fmt.Sprintf(":%d", rootOpts.isport)

stopCh := make(chan struct{})
go secureServer.Serve()
go insecureServer.Serve()
go secureServer.ListenAndServeTLS(rootOpts.cert, rootOpts.key)
go insecureServer.ListenAndServe()
<-stopCh
panic("not possible")
}
12 changes: 7 additions & 5 deletions cmd/machine-config-server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"

"github.com/golang/glog"
"github.com/openshift/machine-config-operator/pkg/server"
Expand Down Expand Up @@ -45,13 +46,14 @@ func runStartCmd(cmd *cobra.Command, args []string) {
glog.Exitf("Machine Config Server exited with error: %v", err)
}

apiHandler := server.NewServerAPIHandler(cs)
secureServer := server.NewAPIServer(apiHandler, rootOpts.sport, false, rootOpts.cert, rootOpts.key)
insecureServer := server.NewAPIServer(apiHandler, rootOpts.isport, true, "", "")
secureServer := *cs
secureServer.Addr = fmt.Sprintf(":%d", rootOpts.sport)
insecureServer := *cs
insecureServer.Addr = fmt.Sprintf(":%d", rootOpts.isport)

stopCh := make(chan struct{})
go secureServer.Serve()
go insecureServer.Serve()
go secureServer.ListenAndServeTLS(rootOpts.cert, rootOpts.key)
go insecureServer.ListenAndServe()
<-stopCh
panic("not possible")
}
98 changes: 42 additions & 56 deletions pkg/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"path"

ignv2_2types "github.com/coreos/ignition/config/v2_2/types"
"github.com/golang/glog"
)

Expand All @@ -17,69 +18,24 @@ type poolRequest struct {
machinePool string
}

// APIServer provides the HTTP(s) endpoint
// for providing the machine configs.
type APIServer struct {
handler *APIHandler
port int
insecure bool
cert string
key string
}

// NewAPIServer initializes a new API server
// that runs the Machine Config Server as a
// handler.
func NewAPIServer(a *APIHandler, p int, is bool, c, k string) *APIServer {
return &APIServer{
handler: a,
port: p,
insecure: is,
cert: c,
key: k,
}
}
type getConfig func(request poolRequest) (*ignv2_2types.Config, error)

// Serve launches the API Server.
func (a *APIServer) Serve() {
func newHandler(getConfig getConfig) http.Handler {
mux := http.NewServeMux()
mux.Handle(apiPathConfig, a.handler)

mcs := &http.Server{
Addr: fmt.Sprintf(":%v", a.port),
Handler: mux,
}

glog.Info("launching server")
if a.insecure {
// Serve a non TLS server.
if err := mcs.ListenAndServe(); err != http.ErrServerClosed {
glog.Exitf("Machine Config Server exited with error: %v", err)
}
} else {
if err := mcs.ListenAndServeTLS(a.cert, a.key); err != http.ErrServerClosed {
glog.Exitf("Machine Config Server exited with error: %v", err)
}
}
mux.Handle("/healthz", &healthHandler{})
mux.Handle(apiPathConfig, &configHandler{getConfig: getConfig})
mux.Handle("/", &defaultHandler{})
return mux
}

// APIHandler is the HTTP Handler for the
// Machine Config Server.
type APIHandler struct {
server Server
}

// NewServerAPIHandler initializes a new API handler
// for the Machine Config Server.
func NewServerAPIHandler(s Server) *APIHandler {
return &APIHandler{
server: s,
}
// configHandler is the HTTP Handler for machine configuration.
type configHandler struct {
getConfig getConfig
}

// ServeHTTP handles the requests for the machine config server
// API handler.
func (sh *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (h *configHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.Header().Set("Content-Length", "0")
w.WriteHeader(http.StatusMethodNotAllowed)
Expand All @@ -96,7 +52,7 @@ func (sh *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
machinePool: path.Base(r.URL.Path),
}

conf, err := sh.server.GetConfig(cr)
conf, err := h.getConfig(cr)
if err != nil {
w.Header().Set("Content-Length", "0")
w.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -129,3 +85,33 @@ func (sh *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
glog.Errorf("failed to write %v response: %v", cr, err)
}
}

type healthHandler struct{}

// ServeHTTP handles /healthz requests.
func (h *healthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "0")

if r.URL.Path == "/healthz" {
if r.Method == http.MethodGet || r.Method == http.MethodHead {
w.WriteHeader(http.StatusNoContent)
return
}

w.WriteHeader(http.StatusMethodNotAllowed)
return
}

w.WriteHeader(http.StatusNotFound)
return
}

// defaultHandler is the HTTP Handler for backstopping invalid requests.
type defaultHandler struct{}

// ServeHTTP handles invalid requests.
func (h *defaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "0")
w.WriteHeader(http.StatusNotFound)
return
}
109 changes: 95 additions & 14 deletions pkg/server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ type checkResponse func(t *testing.T, response *http.Response)
type scenario struct {
name string
request *http.Request
serverFunc func(poolRequest) (*ignv2_2types.Config, error)
getConfig getConfig
checkResponse checkResponse
}

func TestAPIHandler(t *testing.T) {
func TestHandler(t *testing.T) {
scenarios := []scenario{
{
name: "get non-config path that does not exist",
request: httptest.NewRequest(http.MethodGet, "http://testrequest/does-not-exist", nil),
serverFunc: func(poolRequest) (*ignv2_2types.Config, error) {
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return nil, nil
},
checkResponse: func(t *testing.T, response *http.Response) {
Expand All @@ -44,7 +44,7 @@ func TestAPIHandler(t *testing.T) {
{
name: "get config path that does not exist",
request: httptest.NewRequest(http.MethodGet, "http://testrequest/config/does-not-exist", nil),
serverFunc: func(poolRequest) (*ignv2_2types.Config, error) {
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return new(ignv2_2types.Config), fmt.Errorf("not acceptable")
},
checkResponse: func(t *testing.T, response *http.Response) {
Expand All @@ -56,7 +56,7 @@ func TestAPIHandler(t *testing.T) {
{
name: "get config path that exists",
request: httptest.NewRequest(http.MethodGet, "http://testrequest/config/master", nil),
serverFunc: func(poolRequest) (*ignv2_2types.Config, error) {
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return new(ignv2_2types.Config), nil
},
checkResponse: func(t *testing.T, response *http.Response) {
Expand All @@ -69,7 +69,7 @@ func TestAPIHandler(t *testing.T) {
{
name: "head config path that exists",
request: httptest.NewRequest(http.MethodHead, "http://testrequest/config/master", nil),
serverFunc: func(poolRequest) (*ignv2_2types.Config, error) {
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return new(ignv2_2types.Config), nil
},
checkResponse: func(t *testing.T, response *http.Response) {
Expand All @@ -79,23 +79,107 @@ func TestAPIHandler(t *testing.T) {
checkBodyLength(t, response, 0)
},
},
{
name: "get /healthz",
request: httptest.NewRequest(http.MethodGet, "http://testrequest/healthz", nil),
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return new(ignv2_2types.Config), nil
},
checkResponse: func(t *testing.T, response *http.Response) {
checkStatus(t, response, http.StatusNoContent)
checkContentLength(t, response, 0)
checkBodyLength(t, response, 0)
},
},
{
name: "head /healthz",
request: httptest.NewRequest(http.MethodHead, "http://testrequest/healthz", nil),
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return new(ignv2_2types.Config), nil
},
checkResponse: func(t *testing.T, response *http.Response) {
checkStatus(t, response, http.StatusNoContent)
checkContentLength(t, response, 0)
checkBodyLength(t, response, 0)
},
},
{
name: "get health path that does not exist",
request: httptest.NewRequest(http.MethodGet, "http://testrequest/healthz-does-not-exist", nil),
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return nil, nil
},
checkResponse: func(t *testing.T, response *http.Response) {
checkStatus(t, response, http.StatusNotFound)
checkContentLength(t, response, 0)
checkBodyLength(t, response, 0)
},
},
{
name: "get health child that does not exist",
request: httptest.NewRequest(http.MethodGet, "http://testrequest/healthz/does-not-exist", nil),
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return nil, nil
},
checkResponse: func(t *testing.T, response *http.Response) {
checkStatus(t, response, http.StatusNotFound)
checkContentLength(t, response, 0)
checkBodyLength(t, response, 0)
},
},
{
name: "get root",
request: httptest.NewRequest(http.MethodGet, "http://testrequest/", nil),
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return nil, nil
},
checkResponse: func(t *testing.T, response *http.Response) {
checkStatus(t, response, http.StatusNotFound)
checkContentLength(t, response, 0)
checkBodyLength(t, response, 0)
},
},
{
name: "post root",
request: httptest.NewRequest(http.MethodPost, "http://testrequest/", nil),
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return nil, nil
},
checkResponse: func(t *testing.T, response *http.Response) {
checkStatus(t, response, http.StatusNotFound)
checkContentLength(t, response, 0)
checkBodyLength(t, response, 0)
},
},
{
name: "post non-config path that does not exist",
request: httptest.NewRequest(http.MethodPost, "http://testrequest/post", nil),
serverFunc: func(poolRequest) (*ignv2_2types.Config, error) {
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return nil, nil
},
checkResponse: func(t *testing.T, response *http.Response) {
checkStatus(t, response, http.StatusMethodNotAllowed)
checkStatus(t, response, http.StatusNotFound)
checkContentLength(t, response, 0)
checkBodyLength(t, response, 0)
},
},
{
name: "post config path that exists",
request: httptest.NewRequest(http.MethodPost, "http://testrequest/config/master", nil),
serverFunc: func(poolRequest) (*ignv2_2types.Config, error) {
return new(ignv2_2types.Config), nil
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return nil, nil
},
checkResponse: func(t *testing.T, response *http.Response) {
checkStatus(t, response, http.StatusMethodNotAllowed)
checkContentLength(t, response, 0)
checkBodyLength(t, response, 0)
},
},
{
name: "post /healthz",
request: httptest.NewRequest(http.MethodPost, "http://testrequest/healthz", nil),
getConfig: func(poolRequest) (*ignv2_2types.Config, error) {
return nil, nil
},
checkResponse: func(t *testing.T, response *http.Response) {
checkStatus(t, response, http.StatusMethodNotAllowed)
Expand All @@ -108,10 +192,7 @@ func TestAPIHandler(t *testing.T) {
for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
w := httptest.NewRecorder()
ms := &mockServer{
GetConfigFn: scenario.serverFunc,
}
handler := NewServerAPIHandler(ms)
handler := newHandler(scenario.getConfig)
handler.ServeHTTP(w, scenario.request)

resp := w.Result()
Expand Down
Loading