Code Monkey home page Code Monkey logo

cniep's Introduction


CNIEP is a replacement for default nginx backend controller to display customized error pages


Table of Contents


Description

cniep ( custom nginx ingress error pages ) is a web service coupled to nginx ingress controller designed to provide customized error pages.

cniep traps pages returning error responses and displays a customized error page.

To display custom error responses some ingress annotations are required.

cniep gets k8s deployments information which can be used to render custom error response pages.

More info about about custom error codes can be found here.


Installation

1 - Navigate to cniep kubernetes folder and customize manifests to fit your setup.

2 - Deploy cniep

Deployment command
# kubectl apply -f kubernetes

3 - Modify your nginx controller to use cniep as default backend.

4 - Assuming that cniep is installed in cniep k8s namespace, add default-backend-service arg in your ingress nginx controller deployment.

Ingress Deployment args
      ...
      containers:
      - args:
        - /nginx-ingress-controller
        - --default-backend-service=cniep/cniep
      ...

Enable cniep for your services

To enable cniep for your services, follow the next steps:

1 - Install cniep deployment in your k8s server (check installation section)

2 - Annotate your k8s services and ingresses.

Service annotations
apiVersion: v1
kind: Service
metadata:
  name: myservice
  annotations:
    cniep/deployment: myservice
    cniep/template: cniep
Ingress annotations

3 - Annotate your k8s ingress definition. i.e:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myservice
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/custom-http-errors: "503"

Supported Annotations

Annotation Description
cniep/template Defines the template used to render the error response pages
cniep/deployment Defines the deployment backend for the annotated service. If this annotation is not set, it will search for service selectors
cniep/customjsonresponse-xxx Renders a custom json response instead of a templated html page (status code specific). i.e. cniep/customjsonresponse-503: '{"a":"a"}'
cniep/customjsonresponse-global Renders a custom json response instead of a templated html page (all status codes)
cniep/s3-templatedir Syncs templates stored in an AWS S3 bucket. i.e. cniep/s3-templatedir: s3://mys3bucket/cniep2
cniep/customfield-xxxx Define custom variables to be used in error response pages. In the template section, there is more information about this
cniep/forceresponsecode-xxx Forces the HTTP status code returned by the request. i.e. cniep/forceresponsecode-503: '500'

How to template

cniep uses golang text/template package to render error pages.

Template folder structure
mytemplate (template folder)
 - index.html (html file)
 - style.css (css style file)
Template folder structure for specific status codes
mytemplate (template folder)
 - index-503.html (html file)
 - style-503.css (css style file)
 - index.html (html file)
 - style.css (css style file)
index.html
<!DOCTYPE  html>
<html>
<head>
<meta  http-equiv="Content-Type"  content="text/html; charset=utf-8"  />
<style>{{ template "styles" }}</style>
<title>Service Status</title>
<body>
<h1  style="text-align: center;">{{ .Code }} - {{ .Title }}  </h1>
</head>
</body>
</html>
style.css
{{ define "styles" }}
 .container {
   width: 100%;
   max-width: 100%;
 }
{{ end }}

Template variables:

Variable Description
{{.Code}} HTTP status code returned by the request
{{.Title}} HTTP status response message. standard codes
{{.Details.OriginalURI}} URI which originated the request
{{.Details.ContentType}} Request Content-type
{{.Details.Namespace}} Namespace where the backend Service is located
{{.Details.IngressName}} Name of the Ingress where the backend is defined
{{.Details.ServiceName}} Name of the Service backing the backend
{{.Details.DeploymentName}} Name of the Deployment backend
{{.Details.DesiredDeploymentReplicas}} Number of configured replicas in the backend deployment
{{.Details.CurrentDeploymentReplicas}} Number of current running replicas for the backend deployment
{{.Details.CustomFields.xxxx}} Custom fields defined in the service backing the backend (multiple custom fields are allowed)

Included templates:

[cniep] : This template renders an error page with information about the current deployment state.

[default] : Simple clean template.


Build from source:

1 - # make build-all

2 - # docker build . -t cniep:xxx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.