diff --git a/pkg/client/operations/healthz_parameters.go b/pkg/client/operations/healthz_parameters.go new file mode 100644 index 0000000..4e7020d --- /dev/null +++ b/pkg/client/operations/healthz_parameters.go @@ -0,0 +1,128 @@ +// Copyright 2018 Bloomberg Finance L.P. +// +// 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. + +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "net/http" + "time" + + "golang.org/x/net/context" + + "github.com/go-openapi/errors" + "github.com/go-openapi/runtime" + cr "github.com/go-openapi/runtime/client" + + strfmt "github.com/go-openapi/strfmt" +) + +// NewHealthzParams creates a new HealthzParams object +// with the default values initialized. +func NewHealthzParams() *HealthzParams { + + return &HealthzParams{ + + timeout: cr.DefaultTimeout, + } +} + +// NewHealthzParamsWithTimeout creates a new HealthzParams object +// with the default values initialized, and the ability to set a timeout on a request +func NewHealthzParamsWithTimeout(timeout time.Duration) *HealthzParams { + + return &HealthzParams{ + + timeout: timeout, + } +} + +// NewHealthzParamsWithContext creates a new HealthzParams object +// with the default values initialized, and the ability to set a context for a request +func NewHealthzParamsWithContext(ctx context.Context) *HealthzParams { + + return &HealthzParams{ + + Context: ctx, + } +} + +// NewHealthzParamsWithHTTPClient creates a new HealthzParams object +// with the default values initialized, and the ability to set a custom HTTPClient for a request +func NewHealthzParamsWithHTTPClient(client *http.Client) *HealthzParams { + + return &HealthzParams{ + HTTPClient: client, + } +} + +/*HealthzParams contains all the parameters to send to the API endpoint +for the healthz operation typically these are written to a http.Request +*/ +type HealthzParams struct { + timeout time.Duration + Context context.Context + HTTPClient *http.Client +} + +// WithTimeout adds the timeout to the healthz params +func (o *HealthzParams) WithTimeout(timeout time.Duration) *HealthzParams { + o.SetTimeout(timeout) + return o +} + +// SetTimeout adds the timeout to the healthz params +func (o *HealthzParams) SetTimeout(timeout time.Duration) { + o.timeout = timeout +} + +// WithContext adds the context to the healthz params +func (o *HealthzParams) WithContext(ctx context.Context) *HealthzParams { + o.SetContext(ctx) + return o +} + +// SetContext adds the context to the healthz params +func (o *HealthzParams) SetContext(ctx context.Context) { + o.Context = ctx +} + +// WithHTTPClient adds the HTTPClient to the healthz params +func (o *HealthzParams) WithHTTPClient(client *http.Client) *HealthzParams { + o.SetHTTPClient(client) + return o +} + +// SetHTTPClient adds the HTTPClient to the healthz params +func (o *HealthzParams) SetHTTPClient(client *http.Client) { + o.HTTPClient = client +} + +// WriteToRequest writes these params to a swagger request +func (o *HealthzParams) WriteToRequest(r runtime.ClientRequest, reg strfmt.Registry) error { + + if err := r.SetTimeout(o.timeout); err != nil { + return err + } + var res []error + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} diff --git a/pkg/client/operations/healthz_responses.go b/pkg/client/operations/healthz_responses.go new file mode 100644 index 0000000..c2728de --- /dev/null +++ b/pkg/client/operations/healthz_responses.go @@ -0,0 +1,117 @@ +// Copyright 2018 Bloomberg Finance L.P. +// +// 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. + +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "fmt" + "io" + + "github.com/go-openapi/runtime" + + strfmt "github.com/go-openapi/strfmt" + + models "github.com/bloomberg/goldpinger/pkg/models" +) + +// HealthzReader is a Reader for the Healthz structure. +type HealthzReader struct { + formats strfmt.Registry +} + +// ReadResponse reads a server response into the received o. +func (o *HealthzReader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) { + switch response.Code() { + + case 200: + result := NewHealthzOK() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return result, nil + + case 503: + result := NewHealthzServiceUnavailable() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return nil, result + + default: + return nil, runtime.NewAPIError("unknown error", response, response.Code()) + } +} + +// NewHealthzOK creates a HealthzOK with default headers values +func NewHealthzOK() *HealthzOK { + return &HealthzOK{} +} + +/*HealthzOK handles this case with default header values. + +Health check report +*/ +type HealthzOK struct { + Payload *models.HealthCheckResults +} + +func (o *HealthzOK) Error() string { + return fmt.Sprintf("[GET /healthz][%d] healthzOK %+v", 200, o.Payload) +} + +func (o *HealthzOK) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.HealthCheckResults) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} + +// NewHealthzServiceUnavailable creates a HealthzServiceUnavailable with default headers values +func NewHealthzServiceUnavailable() *HealthzServiceUnavailable { + return &HealthzServiceUnavailable{} +} + +/*HealthzServiceUnavailable handles this case with default header values. + +Unhealthy service +*/ +type HealthzServiceUnavailable struct { + Payload *models.HealthCheckResults +} + +func (o *HealthzServiceUnavailable) Error() string { + return fmt.Sprintf("[GET /healthz][%d] healthzServiceUnavailable %+v", 503, o.Payload) +} + +func (o *HealthzServiceUnavailable) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.HealthCheckResults) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} diff --git a/pkg/client/operations/operations_client.go b/pkg/client/operations/operations_client.go index 12b63e1..63cdd4e 100644 --- a/pkg/client/operations/operations_client.go +++ b/pkg/client/operations/operations_client.go @@ -94,6 +94,34 @@ func (a *Client) CheckServicePods(params *CheckServicePodsParams) (*CheckService } +/* +Healthz The healthcheck endpoint provides detailed information about the health of a web service. If each of the components required by the service are healthy, then the service is considered healthy and will return a 200 OK response. If any of the components needed by the service are unhealthy, then a 503 Service Unavailable response will be provided. +*/ +func (a *Client) Healthz(params *HealthzParams) (*HealthzOK, error) { + // TODO: Validate the params before sending + if params == nil { + params = NewHealthzParams() + } + + result, err := a.transport.Submit(&runtime.ClientOperation{ + ID: "healthz", + Method: "GET", + PathPattern: "/healthz", + ProducesMediaTypes: []string{"application/json"}, + ConsumesMediaTypes: []string{""}, + Schemes: []string{"http"}, + Params: params, + Reader: &HealthzReader{formats: a.formats}, + Context: params.Context, + Client: params.HTTPClient, + }) + if err != nil { + return nil, err + } + return result.(*HealthzOK), nil + +} + /* Ping return query stats */ diff --git a/pkg/goldpinger/client.go b/pkg/goldpinger/client.go index c2d0aa3..46cf20b 100644 --- a/pkg/goldpinger/client.go +++ b/pkg/goldpinger/client.go @@ -17,6 +17,7 @@ package goldpinger import ( "fmt" "sync" + "time" apiclient "github.com/bloomberg/goldpinger/pkg/client" "github.com/bloomberg/goldpinger/pkg/models" @@ -162,6 +163,17 @@ func CheckAllPods(pods map[string]string) *models.CheckAllResults { return &result } +func HealthCheck() *models.HealthCheckResults { + ok := true + start := time.Now() + result := models.HealthCheckResults{ + OK: &ok, + DurationNs: time.Since(start).Nanoseconds(), + GeneratedAt: strfmt.DateTime(start), + } + return &result +} + func getClient(hostIP string) *apiclient.Goldpinger { host := fmt.Sprintf("%s:%d", hostIP, GoldpingerConfig.Port) transport := httptransport.New(host, "", nil) diff --git a/pkg/models/health_check_results.go b/pkg/models/health_check_results.go new file mode 100644 index 0000000..80fcd16 --- /dev/null +++ b/pkg/models/health_check_results.go @@ -0,0 +1,88 @@ +// Copyright 2018 Bloomberg Finance L.P. +// +// 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. + +// Code generated by go-swagger; DO NOT EDIT. + +package models + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + strfmt "github.com/go-openapi/strfmt" + + "github.com/go-openapi/errors" + "github.com/go-openapi/swag" + "github.com/go-openapi/validate" +) + +// HealthCheckResults health check results +// swagger:model HealthCheckResults +type HealthCheckResults struct { + + // o k + OK *bool `json:"OK,omitempty"` + + // duration ns + DurationNs int64 `json:"duration-ns,omitempty"` + + // generated at + // Format: date-time + GeneratedAt strfmt.DateTime `json:"generated-at,omitempty"` +} + +// Validate validates this health check results +func (m *HealthCheckResults) Validate(formats strfmt.Registry) error { + var res []error + + if err := m.validateGeneratedAt(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (m *HealthCheckResults) validateGeneratedAt(formats strfmt.Registry) error { + + if swag.IsZero(m.GeneratedAt) { // not required + return nil + } + + if err := validate.FormatOf("generated-at", "body", "date-time", m.GeneratedAt.String(), formats); err != nil { + return err + } + + return nil +} + +// MarshalBinary interface implementation +func (m *HealthCheckResults) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *HealthCheckResults) UnmarshalBinary(b []byte) error { + var res HealthCheckResults + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +} diff --git a/pkg/restapi/configure_goldpinger.go b/pkg/restapi/configure_goldpinger.go index 29ecd8c..7da276f 100644 --- a/pkg/restapi/configure_goldpinger.go +++ b/pkg/restapi/configure_goldpinger.go @@ -70,6 +70,17 @@ func configureAPI(api *operations.GoldpingerAPI) http.Handler { return operations.NewCheckAllPodsOK().WithPayload(goldpinger.CheckNeighboursNeighbours()) }) + api.HealthzHandler = operations.HealthzHandlerFunc( + func(params operations.HealthzParams) middleware.Responder { + goldpinger.CountCall("received", "healthz") + healthResult := goldpinger.HealthCheck() + if *healthResult.OK { + return operations.NewHealthzServiceUnavailable().WithPayload(healthResult) + } else { + return operations.NewHealthzOK().WithPayload(healthResult) + } + }) + api.ServerShutdown = func() {} return setupGlobalMiddleware(api.Serve(setupMiddlewares)) diff --git a/pkg/restapi/embedded_spec.go b/pkg/restapi/embedded_spec.go index f087030..a889040 100644 --- a/pkg/restapi/embedded_spec.go +++ b/pkg/restapi/embedded_spec.go @@ -72,6 +72,29 @@ func init() { } } }, + "/healthz": { + "get": { + "description": "The healthcheck endpoint provides detailed information about the health of a web service. If each of the components required by the service are healthy, then the service is considered healthy and will return a 200 OK response. If any of the components needed by the service are unhealthy, then a 503 Service Unavailable response will be provided.", + "produces": [ + "application/json" + ], + "operationId": "healthz", + "responses": { + "200": { + "description": "Health check report", + "schema": { + "$ref": "#/definitions/HealthCheckResults" + } + }, + "503": { + "description": "Unhealthy service", + "schema": { + "$ref": "#/definitions/HealthCheckResults" + } + } + } + } + }, "/ping": { "get": { "description": "return query stats", @@ -172,6 +195,23 @@ func init() { "$ref": "#/definitions/PodResult" } }, + "HealthCheckResults": { + "type": "object", + "properties": { + "OK": { + "type": "boolean", + "default": false + }, + "duration-ns": { + "type": "integer", + "format": "int64" + }, + "generated-at": { + "type": "string", + "format": "date-time" + } + } + }, "PingResults": { "type": "object", "properties": { @@ -250,6 +290,29 @@ func init() { } } }, + "/healthz": { + "get": { + "description": "The healthcheck endpoint provides detailed information about the health of a web service. If each of the components required by the service are healthy, then the service is considered healthy and will return a 200 OK response. If any of the components needed by the service are unhealthy, then a 503 Service Unavailable response will be provided.", + "produces": [ + "application/json" + ], + "operationId": "healthz", + "responses": { + "200": { + "description": "Health check report", + "schema": { + "$ref": "#/definitions/HealthCheckResults" + } + }, + "503": { + "description": "Unhealthy service", + "schema": { + "$ref": "#/definitions/HealthCheckResults" + } + } + } + } + }, "/ping": { "get": { "description": "return query stats", @@ -350,6 +413,23 @@ func init() { "$ref": "#/definitions/PodResult" } }, + "HealthCheckResults": { + "type": "object", + "properties": { + "OK": { + "type": "boolean", + "default": false + }, + "duration-ns": { + "type": "integer", + "format": "int64" + }, + "generated-at": { + "type": "string", + "format": "date-time" + } + } + }, "PingResults": { "type": "object", "properties": { diff --git a/pkg/restapi/operations/goldpinger_api.go b/pkg/restapi/operations/goldpinger_api.go index 5c5cdfd..0e027e1 100644 --- a/pkg/restapi/operations/goldpinger_api.go +++ b/pkg/restapi/operations/goldpinger_api.go @@ -57,6 +57,9 @@ func NewGoldpingerAPI(spec *loads.Document) *GoldpingerAPI { CheckServicePodsHandler: CheckServicePodsHandlerFunc(func(params CheckServicePodsParams) middleware.Responder { return middleware.NotImplemented("operation CheckServicePods has not yet been implemented") }), + HealthzHandler: HealthzHandlerFunc(func(params HealthzParams) middleware.Responder { + return middleware.NotImplemented("operation Healthz has not yet been implemented") + }), PingHandler: PingHandlerFunc(func(params PingParams) middleware.Responder { return middleware.NotImplemented("operation Ping has not yet been implemented") }), @@ -95,6 +98,8 @@ type GoldpingerAPI struct { CheckAllPodsHandler CheckAllPodsHandler // CheckServicePodsHandler sets the operation handler for the check service pods operation CheckServicePodsHandler CheckServicePodsHandler + // HealthzHandler sets the operation handler for the healthz operation + HealthzHandler HealthzHandler // PingHandler sets the operation handler for the ping operation PingHandler PingHandler @@ -168,6 +173,10 @@ func (o *GoldpingerAPI) Validate() error { unregistered = append(unregistered, "CheckServicePodsHandler") } + if o.HealthzHandler == nil { + unregistered = append(unregistered, "HealthzHandler") + } + if o.PingHandler == nil { unregistered = append(unregistered, "PingHandler") } @@ -280,6 +289,11 @@ func (o *GoldpingerAPI) initHandlerCache() { } o.handlers["GET"]["/check"] = NewCheckServicePods(o.context, o.CheckServicePodsHandler) + if o.handlers["GET"] == nil { + o.handlers["GET"] = make(map[string]http.Handler) + } + o.handlers["GET"]["/healthz"] = NewHealthz(o.context, o.HealthzHandler) + if o.handlers["GET"] == nil { o.handlers["GET"] = make(map[string]http.Handler) } diff --git a/pkg/restapi/operations/healthz.go b/pkg/restapi/operations/healthz.go new file mode 100644 index 0000000..dcea87d --- /dev/null +++ b/pkg/restapi/operations/healthz.go @@ -0,0 +1,72 @@ +// Copyright 2018 Bloomberg Finance L.P. +// +// 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. + +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the generate command + +import ( + "net/http" + + middleware "github.com/go-openapi/runtime/middleware" +) + +// HealthzHandlerFunc turns a function with the right signature into a healthz handler +type HealthzHandlerFunc func(HealthzParams) middleware.Responder + +// Handle executing the request and returning a response +func (fn HealthzHandlerFunc) Handle(params HealthzParams) middleware.Responder { + return fn(params) +} + +// HealthzHandler interface for that can handle valid healthz params +type HealthzHandler interface { + Handle(HealthzParams) middleware.Responder +} + +// NewHealthz creates a new http.Handler for the healthz operation +func NewHealthz(ctx *middleware.Context, handler HealthzHandler) *Healthz { + return &Healthz{Context: ctx, Handler: handler} +} + +/*Healthz swagger:route GET /healthz healthz + +The healthcheck endpoint provides detailed information about the health of a web service. If each of the components required by the service are healthy, then the service is considered healthy and will return a 200 OK response. If any of the components needed by the service are unhealthy, then a 503 Service Unavailable response will be provided. + +*/ +type Healthz struct { + Context *middleware.Context + Handler HealthzHandler +} + +func (o *Healthz) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + route, rCtx, _ := o.Context.RouteInfo(r) + if rCtx != nil { + r = rCtx + } + var Params = NewHealthzParams() + + if err := o.Context.BindValidRequest(r, route, &Params); err != nil { // bind params + o.Context.Respond(rw, r, route.Produces, route, err) + return + } + + res := o.Handler.Handle(Params) // actually handle the request + + o.Context.Respond(rw, r, route.Produces, route, res) + +} diff --git a/pkg/restapi/operations/healthz_parameters.go b/pkg/restapi/operations/healthz_parameters.go new file mode 100644 index 0000000..69c7acf --- /dev/null +++ b/pkg/restapi/operations/healthz_parameters.go @@ -0,0 +1,59 @@ +// Copyright 2018 Bloomberg Finance L.P. +// +// 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. + +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "net/http" + + "github.com/go-openapi/errors" + "github.com/go-openapi/runtime/middleware" +) + +// NewHealthzParams creates a new HealthzParams object +// no default values defined in spec. +func NewHealthzParams() HealthzParams { + + return HealthzParams{} +} + +// HealthzParams contains all the bound params for the healthz operation +// typically these are obtained from a http.Request +// +// swagger:parameters healthz +type HealthzParams struct { + + // HTTP Request Object + HTTPRequest *http.Request `json:"-"` +} + +// BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface +// for simple values it will use straight method calls. +// +// To ensure default values, the struct must have been initialized with NewHealthzParams() beforehand. +func (o *HealthzParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error { + var res []error + + o.HTTPRequest = r + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} diff --git a/pkg/restapi/operations/healthz_responses.go b/pkg/restapi/operations/healthz_responses.go new file mode 100644 index 0000000..fcb7e71 --- /dev/null +++ b/pkg/restapi/operations/healthz_responses.go @@ -0,0 +1,116 @@ +// Copyright 2018 Bloomberg Finance L.P. +// +// 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. + +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "net/http" + + "github.com/go-openapi/runtime" + + models "github.com/bloomberg/goldpinger/pkg/models" +) + +// HealthzOKCode is the HTTP code returned for type HealthzOK +const HealthzOKCode int = 200 + +/*HealthzOK Health check report + +swagger:response healthzOK +*/ +type HealthzOK struct { + + /* + In: Body + */ + Payload *models.HealthCheckResults `json:"body,omitempty"` +} + +// NewHealthzOK creates HealthzOK with default headers values +func NewHealthzOK() *HealthzOK { + + return &HealthzOK{} +} + +// WithPayload adds the payload to the healthz o k response +func (o *HealthzOK) WithPayload(payload *models.HealthCheckResults) *HealthzOK { + o.Payload = payload + return o +} + +// SetPayload sets the payload to the healthz o k response +func (o *HealthzOK) SetPayload(payload *models.HealthCheckResults) { + o.Payload = payload +} + +// WriteResponse to the client +func (o *HealthzOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.WriteHeader(200) + if o.Payload != nil { + payload := o.Payload + if err := producer.Produce(rw, payload); err != nil { + panic(err) // let the recovery middleware deal with this + } + } +} + +// HealthzServiceUnavailableCode is the HTTP code returned for type HealthzServiceUnavailable +const HealthzServiceUnavailableCode int = 503 + +/*HealthzServiceUnavailable Unhealthy service + +swagger:response healthzServiceUnavailable +*/ +type HealthzServiceUnavailable struct { + + /* + In: Body + */ + Payload *models.HealthCheckResults `json:"body,omitempty"` +} + +// NewHealthzServiceUnavailable creates HealthzServiceUnavailable with default headers values +func NewHealthzServiceUnavailable() *HealthzServiceUnavailable { + + return &HealthzServiceUnavailable{} +} + +// WithPayload adds the payload to the healthz service unavailable response +func (o *HealthzServiceUnavailable) WithPayload(payload *models.HealthCheckResults) *HealthzServiceUnavailable { + o.Payload = payload + return o +} + +// SetPayload sets the payload to the healthz service unavailable response +func (o *HealthzServiceUnavailable) SetPayload(payload *models.HealthCheckResults) { + o.Payload = payload +} + +// WriteResponse to the client +func (o *HealthzServiceUnavailable) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.WriteHeader(503) + if o.Payload != nil { + payload := o.Payload + if err := producer.Produce(rw, payload); err != nil { + panic(err) // let the recovery middleware deal with this + } + } +} diff --git a/pkg/restapi/operations/healthz_urlbuilder.go b/pkg/restapi/operations/healthz_urlbuilder.go new file mode 100644 index 0000000..1302b7d --- /dev/null +++ b/pkg/restapi/operations/healthz_urlbuilder.go @@ -0,0 +1,98 @@ +// Copyright 2018 Bloomberg Finance L.P. +// +// 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. + +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the generate command + +import ( + "errors" + "net/url" + golangswaggerpaths "path" +) + +// HealthzURL generates an URL for the healthz operation +type HealthzURL struct { + _basePath string +} + +// WithBasePath sets the base path for this url builder, only required when it's different from the +// base path specified in the swagger spec. +// When the value of the base path is an empty string +func (o *HealthzURL) WithBasePath(bp string) *HealthzURL { + o.SetBasePath(bp) + return o +} + +// SetBasePath sets the base path for this url builder, only required when it's different from the +// base path specified in the swagger spec. +// When the value of the base path is an empty string +func (o *HealthzURL) SetBasePath(bp string) { + o._basePath = bp +} + +// Build a url path and query string +func (o *HealthzURL) Build() (*url.URL, error) { + var result url.URL + + var _path = "/healthz" + + _basePath := o._basePath + result.Path = golangswaggerpaths.Join(_basePath, _path) + + return &result, nil +} + +// Must is a helper function to panic when the url builder returns an error +func (o *HealthzURL) Must(u *url.URL, err error) *url.URL { + if err != nil { + panic(err) + } + if u == nil { + panic("url can't be nil") + } + return u +} + +// String returns the string representation of the path with query string +func (o *HealthzURL) String() string { + return o.Must(o.Build()).String() +} + +// BuildFull builds a full url with scheme, host, path and query string +func (o *HealthzURL) BuildFull(scheme, host string) (*url.URL, error) { + if scheme == "" { + return nil, errors.New("scheme is required for a full url on HealthzURL") + } + if host == "" { + return nil, errors.New("host is required for a full url on HealthzURL") + } + + base, err := o.Build() + if err != nil { + return nil, err + } + + base.Scheme = scheme + base.Host = host + return base, nil +} + +// StringFull returns the string representation of a complete url +func (o *HealthzURL) StringFull(scheme, host string) string { + return o.Must(o.BuildFull(scheme, host)).String() +} diff --git a/swagger.yml b/swagger.yml index 3fe690e..6a957b0 100644 --- a/swagger.yml +++ b/swagger.yml @@ -83,6 +83,18 @@ definitions: type: object additionalProperties: $ref: '#/definitions/CheckAllPodResult' + HealthCheckResults: + type: object + properties: + OK: + type: boolean + default: false + generated-at: + type: string + format: date-time + duration-ns: + type: integer + format: int64 paths: /ping: get: @@ -120,3 +132,23 @@ paths: description: Success, return response schema: $ref: '#/definitions/CheckAllResults' + /healthz: + get: + description: The healthcheck endpoint provides detailed information about + the health of a web service. If each of the components required + by the service are healthy, then the service is considered healthy + and will return a 200 OK response. If any of the components needed + by the service are unhealthy, then a 503 Service Unavailable + response will be provided. + produces: + - application/json + operationId: healthz + responses: + 200: + description: Health check report + schema: + $ref: '#/definitions/HealthCheckResults' + 503: + description: Unhealthy service + schema: + $ref: '#/definitions/HealthCheckResults'