router

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2017 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrMsgTmplInvalidAnnotationName is the error message template for an invalid annotation name
	ErrMsgTmplInvalidAnnotationName = "invalid annotation name: %s %v"
	// ErrMsgTmplInvalidLabelSelector is the error message template for an invalid label selector
	ErrMsgTmplInvalidLabelSelector = "has an invalid label selector: %s $v\n"
	// ErrMsgTmplInvalidPort is the error message template for an invalid port
	ErrMsgTmplInvalidPort = "%s is an invalid port\n"
	// ErrMsgTmplInvalidServerReturnHTTPStatusCode is the error message template for invalid status code
	ErrMsgTmplInvalidServerReturnHTTPStatusCode = "%d is an invalid status code 100-999 for default server return"
	// ErrMsgTmplInvalidServerReturnURL is the error message for an invalid url used for default server
	ErrMsgTmplInvalidServerReturnURL = "%s is an invalid url for default server return %v"
	//ErrMsgTmplInvalidPath is the error message for an invalid path
	ErrMsgTmplInvalidPath = "%s is an invalid path"
	//ErrMsgTmplMissingSSLKey is the error message for missing ssl key
	ErrMsgTmplMissingSSLKey = "must provide a ssl key when ssl cert is provided"
	// ErrMsgTmplSSLCertMissing error for when ssl cert/key does not exist on disk
	ErrMsgTmplSSLCertMissing = "ssl cert does not exist %s"
)

Variables

This section is empty.

Functions

func ProcessEvent

func ProcessEvent(cache *Cache, resourceType WatchableResourceSet, event watch.Event) bool

ProcessEvent takes in a WatchableResourceSet and a k8s watch.Event and runs the logic to either add/remove/update the cache with the new resouce if needed. Returns true if cache has changed and nginx needs a restart.

  • If the Resource is delete from k8s the cache entry is removed and a restart is needed.
  • If the Resource is added to k8s it's added to the cache and a restart is needed.
  • If the Resouce is updated in k8s it's checked if the resouce is still watchable, if not it's removed from the cache. If it's still watchable and not equal to the cached object it's replaced and a restart is needed.

Types

type Cache

type Cache struct {
	Namespaces map[string]*Namespace
	Pods       map[string]*PodWithRoutes
	Secrets    map[string]*Secret
}

Cache is the structure containing the router API Keys and the routable pods cache and namespaces

func NewCache

func NewCache() *Cache

NewCache returns a create a Cache object and returns a pointer to the object

type Config

type Config struct {
	// The secret name used to store the API Key for the namespace
	APIKeySecret string
	// The secret data field name to store the API Key for the namespace
	APIKeySecretDataField string
	// The label selector used to identify routable namespaces and pods
	RoutableLabelSelector string
	// The name of the annotation used to find hosts to route on the namespace
	NamespaceHostsAnnotation string
	// The name of the label used to find org name of namespace
	NamespaceOrgLabel string
	// The name of the label used to find env name of namespace
	NamespaceEnvLabel string
	// Nginx Specific configurations
	Nginx NginxConfig
	// The name of the annotation used to find routing information
	PodsPathsAnnotation string
	// The name of the label used for applications name
	PodsAppNameLabel string
	// The name of the label used for applications revision
	PodsAppRevLabel string
}

Config is the structure containing the router configuration

func ConfigFromEnv

func ConfigFromEnv() (*Config, error)

ConfigFromEnv returns the configuration based on the environment variables and validates the values

type HealthCheck

type HealthCheck struct {
	HTTPCheck          bool
	Path               string
	Method             string
	TimeoutMs          int32
	IntervalMs         int32
	UnhealthyThreshold int32
	HealthyThreshold   int32
	Port               int32
}

HealthCheck of an Outgoing upstream server allows nginx to monitor pod health.

type HostOptions

type HostOptions struct {
	SSLOptions *SSLOptions `json:"ssl,omitempty"`
}

HostOptions contains any options for the host. Nothing right now.

type Incoming

type Incoming struct {
	Path string
}

Incoming describes the information required to route an incoming request

type Namespace

type Namespace struct {
	Name         string
	Hosts        map[string]HostOptions
	Organization string
	Environment  string
	// contains filtered or unexported fields
}

Namespace describes the information stored on the k8s namespace object for routing

func (Namespace) Hash

func (ns Namespace) Hash() uint64

Hash returns the stored version of all the annotations hashed using fnv

func (Namespace) ID

func (ns Namespace) ID() string

ID returns the namespace's name

type NamespaceWatchableSet

type NamespaceWatchableSet struct {
	Config     *Config
	KubeClient *kubernetes.Clientset
}

NamespaceWatchableSet implements WatchableResourceSet interface to provide access to k8s namespace resouces.

func (NamespaceWatchableSet) CacheAdd

func (s NamespaceWatchableSet) CacheAdd(cache *Cache, item WatchableResource)

CacheAdd adds Namespace to the cache's namespace bucket

func (NamespaceWatchableSet) CacheCompare

func (s NamespaceWatchableSet) CacheCompare(cache *Cache, newItem WatchableResource) bool

CacheCompare compares the given Namespace with the namespace in the cache, if equal returns true otherwise returns false. If cache value does not exist return false.

func (NamespaceWatchableSet) CacheRemove

func (s NamespaceWatchableSet) CacheRemove(cache *Cache, id string)

CacheRemove removes the Namespace using the id given from the Cache's Namespaces bucket

func (NamespaceWatchableSet) ConvertToModel

func (s NamespaceWatchableSet) ConvertToModel(in interface{}) WatchableResource

ConvertToModel takes in a k8s *api.Namespace as a blank interface and converts it to a Namespace as a WatchableResource

func (NamespaceWatchableSet) Get

Get returns a list of Namespace in the form of a WatchableResource interface and a k8s resource version. If any k8s client errors occur it is returned.

func (NamespaceWatchableSet) IDFromObject

func (s NamespaceWatchableSet) IDFromObject(in interface{}) string

IDFromObject returns the Namespaces' name from the *api.Namespace object

func (NamespaceWatchableSet) Watch

func (s NamespaceWatchableSet) Watch(resouceVersion string) (watch.Interface, error)

Watch returns a k8s watch.Interface that subscribes to any namespace changes

func (NamespaceWatchableSet) Watchable

func (s NamespaceWatchableSet) Watchable(in interface{}) bool

Watchable tests where the *api.Namespace has the routable label selector for the namespace to be watched.

type NginxConfig

type NginxConfig struct {
	// The header name used to identify the API Key
	APIKeyHeader string
	// Enable or disable nginx health checks for each pod
	EnableHealthChecks bool
	// Status path for nginx status endpoint on default server.
	StatusPath string
	// Max client request body size. nginx config: client_max_body_size. eg 10m
	MaxClientBodySize string
	// The port that nginx will listen on
	Port int
	// The port that nginx will listen for ssl connections
	SSLPort int
	// SSLCertificateDir path to store certificates
	SSLCertificateDir string
	// SSLCert the default ssl cert used for all request that don't match a valid ssl host
	SSLCert string
	// SSLKey the default ssl cert key used for all request that don't match a valid ssl host
	SSLKey string
	// SSLEnabled flag to enable/disable ssl support, set by ConfigFromEnv
	SSLEnabled bool
	// Default location return if request does not match any patjs, Defaults: 404
	DefaultLocationReturn string
	// RunInMockMode enables starting/stopping nginx if disabled. In mock mode starting/stopping is ignored.
	RunInMockMode bool
}

NginxConfig is the structure containing the nginx specific configuration

type OptionValue

type OptionValue struct {
	ValueFrom *ValueFrom `json:"valueFrom,omitempty"`
}

OptionValue value from ref

type Outgoing

type Outgoing struct {
	IP          string
	Port        string
	TargetPath  *string
	HealthCheck *HealthCheck
	Weight      *uint
}

Outgoing describes the information required to proxy to a backend

type PodWatchableSet

type PodWatchableSet struct {
	Config     *Config
	KubeClient *kubernetes.Clientset
}

PodWatchableSet implements WatchableResourceSet interface to provide access to k8s pod resouces.

func (PodWatchableSet) CacheAdd

func (s PodWatchableSet) CacheAdd(cache *Cache, item WatchableResource)

CacheAdd adds Namespace to the cache's namespace bucket

func (PodWatchableSet) CacheCompare

func (s PodWatchableSet) CacheCompare(cache *Cache, newItem WatchableResource) bool

CacheCompare compares the given Pod with the pod in the cache, if equal returns true otherwise returns false. If cache value does not exist return false.

func (PodWatchableSet) CacheRemove

func (s PodWatchableSet) CacheRemove(cache *Cache, id string)

CacheRemove removes the Namespace using the id given from the Cache's Namespaces bucket

func (PodWatchableSet) ConvertToModel

func (s PodWatchableSet) ConvertToModel(in interface{}) WatchableResource

ConvertToModel takes in a k8s *api.Pod as a blank interface and converts it to a Namespace as a WatchableResource

func (PodWatchableSet) Get

Get returns a list of Namespace in the form of a WatchableResource interface and a k8s resource version. If any k8s client errors occur it is returned.

func (PodWatchableSet) IDFromObject

func (s PodWatchableSet) IDFromObject(in interface{}) string

IDFromObject returns the Pods' name from the *api.Pod object

func (PodWatchableSet) Watch

func (s PodWatchableSet) Watch(resouceVersion string) (watch.Interface, error)

Watch returns a k8s watch.Interface that subscribes to any pod changes

func (PodWatchableSet) Watchable

func (s PodWatchableSet) Watchable(in interface{}) bool

Watchable tests where the *api.Pod has the routable label selector for the pod to be watched.

type PodWithRoutes

type PodWithRoutes struct {
	Name        string
	AppName     string
	AppRevision string
	Namespace   string
	Status      api.PodPhase
	Routes      []*Route
	// contains filtered or unexported fields
}

PodWithRoutes contains a pod and its routes

func (PodWithRoutes) Hash

func (pod PodWithRoutes) Hash() uint64

Hash returns the stored version of all the annotations hashed using fnv

func (PodWithRoutes) ID

func (pod PodWithRoutes) ID() string

ID returns the namespace's name

type Route

type Route struct {
	Incoming *Incoming
	Outgoing *Outgoing
}

Route describes the incoming route matching details and the outgoing proxy backend details

func GetRoutes

func GetRoutes(config *Config, pod *api.Pod) []*Route

GetRoutes returns an array of routes defined within the provided pod

type SSLOptions

type SSLOptions struct {
	Certificate      OptionValue  `json:"certificate"`
	Key              OptionValue  `json:"certificateKey"`
	ClientCertficate *OptionValue `json:"clientCertificate,omitempty"`
}

SSLOptions contains options for the host

type Secret

type Secret struct {
	Namespace  string
	RoutingKey *[]byte
	Fields     map[string][]byte // raw data values for each field in secret
	// contains filtered or unexported fields
}

Secret struct implements a WatchableResource interface and contains the Namespace name and Data inside the secret

func (Secret) Hash

func (s Secret) Hash() uint64

Hash returns a fnv hasve of the Secret Data

func (Secret) ID

func (s Secret) ID() string

ID returns the namespace name

type SecretRef

type SecretRef struct {
	Key string `json:"key"`
}

SecretRef ref to secret field

type SecretWatchableSet

type SecretWatchableSet struct {
	Config     *Config
	KubeClient *kubernetes.Clientset
}

SecretWatchableSet struct to implement the WatchableResourceSet interface. Has pointers to the config and kubernetes api.

func (SecretWatchableSet) CacheAdd

func (s SecretWatchableSet) CacheAdd(cache *Cache, item WatchableResource)

CacheAdd adds Secret to the caches Secret bucket

func (SecretWatchableSet) CacheCompare

func (s SecretWatchableSet) CacheCompare(cache *Cache, newItem WatchableResource) bool

CacheCompare compares the given Secret with the Secret in the cache, if equal returns true otherwise returns false. If cache value does not exist return false.

func (SecretWatchableSet) CacheRemove

func (s SecretWatchableSet) CacheRemove(cache *Cache, id string)

CacheRemove removes the Secret using the id given from the Cache's Secrets bucket

func (SecretWatchableSet) ConvertToModel

func (s SecretWatchableSet) ConvertToModel(in interface{}) WatchableResource

ConvertToModel converts an *api.Secret k8s secret to a WatchableResource

func (SecretWatchableSet) Get

Get returns a list of Secrets in form of WatchableResources interfaces and a k8s resource version. If any error occurs it is returned from k8s client.

func (SecretWatchableSet) IDFromObject

func (s SecretWatchableSet) IDFromObject(in interface{}) string

IDFromObject returns the Namespace name from the *api.Secret

func (SecretWatchableSet) Watch

func (s SecretWatchableSet) Watch(resouceVersion string) (watch.Interface, error)

Watch returns a k8s watch.Interface that subscribes secretes that change in any namespace

func (SecretWatchableSet) Watchable

func (s SecretWatchableSet) Watchable(in interface{}) bool

Watchable tests where the *api.Secret inputed has the Name of of the configured APIKeySecret

type ValueFrom

type ValueFrom struct {
	SecretKeyRef *SecretRef `json:"secretKeyRef,omitempty"`
}

ValueFrom specify reference to a secret

type WatchableResource

type WatchableResource interface {
	// ID used to store in cache
	ID() string
	Hash() uint64
}

WatchableResource interface that each watchable resource most implement. Id() as the cache key and a hash method for comparison

type WatchableResourceSet

type WatchableResourceSet interface {
	// Returns all current k8s resources converted to the appropriate model
	Get() ([]WatchableResource, string, error)
	// Returns a k8s watch.Interface subscribing to changes
	Watch(resouceVersion string) (watch.Interface, error)
	// Converts a k8s object into a WatchableResource per type
	ConvertToModel(interface{}) WatchableResource
	//Watchable takes in a k8s object as a generic interface and tests if the resouce should be watched by dispatcher using it's label selectors
	Watchable(interface{}) bool
	// Adds Resouce to Cache in the proper bucket based on it's type
	CacheAdd(*Cache, WatchableResource)
	// Removes a Resouce from Cache's bucket based on it's type
	CacheRemove(*Cache, string)
	// Takes a resource and compares it to the resource in the Cache based on it's id, returns true if equal. Returns false otherwise and if the cache object does not exist.
	CacheCompare(*Cache, WatchableResource) bool
	// Returns the cache id of the k8s object without having to convert it fully to the Dispatcher model
	IDFromObject(interface{}) string
}

WatchableResourceSet provides an interface to a k8s resource that is watchable by dispatcher. Implementation must provide Get, Watch methods and a way to convert k8s object to a dispatch WatchableResource model

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL