Commit 1c189b14 authored by Geoff Simmons's avatar Geoff Simmons

CLI option 'class' sets the value for Ingress annotation ingress.class.

"varnish" remains as the default value. This makes it possible to
run more than one controller to manage different Varnish Services
(or just choose a different annotation value).

Ref #26
parent 91b538f1
...@@ -78,6 +78,9 @@ var ( ...@@ -78,6 +78,9 @@ var (
"Monitor deactivated when <= 0s") "Monitor deactivated when <= 0s")
metricsPortF = flag.Uint("metricsport", 8080, metricsPortF = flag.Uint("metricsport", 8080,
"port at which to listen for the /metrics endpoint") "port at which to listen for the /metrics endpoint")
ingressClassF = flag.String("class", "varnish", "value of the Ingress "+
"annotation kubernetes.io/ingress.class\nthe controller only "+
"considers Ingresses with this value for the\nannotation")
logFormat = logrus.TextFormatter{ logFormat = logrus.TextFormatter{
DisableColors: true, DisableColors: true,
FullTimestamp: true, FullTimestamp: true,
...@@ -136,6 +139,11 @@ func main() { ...@@ -136,6 +139,11 @@ func main() {
os.Exit(-1) os.Exit(-1)
} }
if *ingressClassF == "" {
log.Fatalf("class may not be empty")
os.Exit(-1)
}
if *metricsPortF > math.MaxUint16 { if *metricsPortF > math.MaxUint16 {
log.Fatalf("metricsport %d out of range (max %d)", log.Fatalf("metricsport %d out of range (max %d)",
*metricsPortF, math.MaxUint16) *metricsPortF, math.MaxUint16)
...@@ -143,6 +151,7 @@ func main() { ...@@ -143,6 +151,7 @@ func main() {
} }
log.Info("Starting Varnish Ingress controller version:", version) log.Info("Starting Varnish Ingress controller version:", version)
log.Info("Ingress class:", *ingressClassF)
vController, err := varnish.NewVarnishController(log, *tmplDirF, vController, err := varnish.NewVarnishController(log, *tmplDirF,
*monIntvlF) *monIntvlF)
...@@ -185,8 +194,9 @@ func main() { ...@@ -185,8 +194,9 @@ func main() {
// informers.WithNamespace(*namespaceF)) // informers.WithNamespace(*namespaceF))
} }
ingController, err := controller.NewIngressController(log, kubeClient, ingController, err := controller.NewIngressController(log,
vController, informerFactory, vcrInformerFactory) *ingressClassF, kubeClient, vController, informerFactory,
vcrInformerFactory)
if err != nil { if err != nil {
log.Fatalf("Could not initialize controller: %v") log.Fatalf("Could not initialize controller: %v")
os.Exit(-1) os.Exit(-1)
......
...@@ -115,6 +115,7 @@ type IngressController struct { ...@@ -115,6 +115,7 @@ type IngressController struct {
// NewIngressController creates a controller. // NewIngressController creates a controller.
// //
// log: logger initialized at startup // log: logger initialized at startup
// ingClass: value of the ingress.class Ingress annotation
// kubeClient: k8s client initialized at startup // kubeClient: k8s client initialized at startup
// vc: Varnish controller // vc: Varnish controller
// infFactory: SharedInformerFactory to create informers & listers for // infFactory: SharedInformerFactory to create informers & listers for
...@@ -122,6 +123,7 @@ type IngressController struct { ...@@ -122,6 +123,7 @@ type IngressController struct {
// vcrInfFactory: SharedInformerFactory for the project's own client APIs // vcrInfFactory: SharedInformerFactory for the project's own client APIs
func NewIngressController( func NewIngressController(
log *logrus.Logger, log *logrus.Logger,
ingClass string,
kubeClient kubernetes.Interface, kubeClient kubernetes.Interface,
vc *varnish.VarnishController, vc *varnish.VarnishController,
infFactory informers.SharedInformerFactory, infFactory informers.SharedInformerFactory,
...@@ -190,8 +192,8 @@ func NewIngressController( ...@@ -190,8 +192,8 @@ func NewIngressController(
Lister(), Lister(),
} }
ingc.nsQs = NewNamespaceQueues(ingc.log, ingc.vController, ingc.listers, ingc.nsQs = NewNamespaceQueues(ingc.log, ingClass, ingc.vController,
ingc.client, ingc.recorder) ingc.listers, ingc.client, ingc.recorder)
return &ingc, nil return &ingc, nil
} }
......
...@@ -65,7 +65,7 @@ func (worker *NamespaceWorker) syncEndp(key string) error { ...@@ -65,7 +65,7 @@ func (worker *NamespaceWorker) syncEndp(key string) error {
worker.log.Debugf("Update ingresses for endpoints %s", key) worker.log.Debugf("Update ingresses for endpoints %s", key)
for _, ing := range ings { for _, ing := range ings {
if !isVarnishIngress(ing) { if !worker.isVarnishIngress(ing) {
worker.log.Debugf("Ingress %s/%s: not Varnish", worker.log.Debugf("Ingress %s/%s: not Varnish",
ing.Namespace, ing.Name) ing.Namespace, ing.Name)
continue continue
......
...@@ -875,10 +875,11 @@ func (worker *NamespaceWorker) addOrUpdateIng(ing *extensions.Ingress) error { ...@@ -875,10 +875,11 @@ func (worker *NamespaceWorker) addOrUpdateIng(ing *extensions.Ingress) error {
return nil return nil
} }
// We only handle Ingresses with the class annotation "varnish". // We only handle Ingresses with the class annotation with the value
func isVarnishIngress(ing *extensions.Ingress) bool { // given as the "class" flag (default "varnish").
func (worker *NamespaceWorker) isVarnishIngress(ing *extensions.Ingress) bool {
class, exists := ing.Annotations[ingressClassKey] class, exists := ing.Annotations[ingressClassKey]
return exists && class == "varnish" return exists && class == worker.ingClass
} }
func (worker *NamespaceWorker) syncIng(key string) error { func (worker *NamespaceWorker) syncIng(key string) error {
...@@ -889,10 +890,10 @@ func (worker *NamespaceWorker) syncIng(key string) error { ...@@ -889,10 +890,10 @@ func (worker *NamespaceWorker) syncIng(key string) error {
return err return err
} }
if !isVarnishIngress(ing) { if !worker.isVarnishIngress(ing) {
worker.log.Infof("Ignoring Ingress %s/%s, Annotation '%v' "+ worker.log.Infof("Ignoring Ingress %s/%s, Annotation '%v' "+
"absent or is not 'varnish'", ing.Namespace, ing.Name, "absent or is not '%s'", ing.Namespace, ing.Name,
ingressClassKey) ingressClassKey, worker.ingClass)
syncCounters.WithLabelValues(worker.namespace, "Ingress", syncCounters.WithLabelValues(worker.namespace, "Ingress",
"Ignore").Inc() "Ignore").Inc()
return nil return nil
......
...@@ -97,7 +97,7 @@ func (worker *NamespaceWorker) enqueueIngressForService( ...@@ -97,7 +97,7 @@ func (worker *NamespaceWorker) enqueueIngressForService(
return err return err
} }
for _, ing := range ings { for _, ing := range ings {
if !isVarnishIngress(ing) { if !worker.isVarnishIngress(ing) {
continue continue
} }
worker.queue.Add(&SyncObj{Type: Update, Obj: ing}) worker.queue.Add(&SyncObj{Type: Update, Obj: ing})
......
...@@ -59,7 +59,7 @@ func (worker *NamespaceWorker) enqueueIngsForVcfg( ...@@ -59,7 +59,7 @@ func (worker *NamespaceWorker) enqueueIngsForVcfg(
return err return err
} }
for _, ing := range ings { for _, ing := range ings {
if !isVarnishIngress(ing) { if !worker.isVarnishIngress(ing) {
continue continue
} }
vSvc, err := worker.getVarnishSvcForIng(ing) vSvc, err := worker.getVarnishSvcForIng(ing)
......
...@@ -63,6 +63,7 @@ const ( ...@@ -63,6 +63,7 @@ const (
// namespaces are synced in parallel. // namespaces are synced in parallel.
type NamespaceWorker struct { type NamespaceWorker struct {
namespace string namespace string
ingClass string
log *logrus.Logger log *logrus.Logger
vController *varnish.VarnishController vController *varnish.VarnishController
queue workqueue.RateLimitingInterface queue workqueue.RateLimitingInterface
...@@ -271,6 +272,7 @@ func (worker *NamespaceWorker) work() { ...@@ -271,6 +272,7 @@ func (worker *NamespaceWorker) work() {
// responsible for each namespace. // responsible for each namespace.
type NamespaceQueues struct { type NamespaceQueues struct {
Queue workqueue.RateLimitingInterface Queue workqueue.RateLimitingInterface
ingClass string
log *logrus.Logger log *logrus.Logger
vController *varnish.VarnishController vController *varnish.VarnishController
workers map[string]*NamespaceWorker workers map[string]*NamespaceWorker
...@@ -282,12 +284,14 @@ type NamespaceQueues struct { ...@@ -282,12 +284,14 @@ type NamespaceQueues struct {
// NewNamespaceQueues creates a NamespaceQueues object. // NewNamespaceQueues creates a NamespaceQueues object.
// //
// log: logger initialized at startup // log: logger initialized at startup
// ingClass: value of the ingress.class Ingress annotation
// vController: Varnish controller initialied at startup // vController: Varnish controller initialied at startup
// listers: client-go/lister instance for each resource type // listers: client-go/lister instance for each resource type
// client: k8s API client initialized at startup // client: k8s API client initialized at startup
// recorder: Event broadcaster initialized at startup // recorder: Event broadcaster initialized at startup
func NewNamespaceQueues( func NewNamespaceQueues(
log *logrus.Logger, log *logrus.Logger,
ingClass string,
vController *varnish.VarnishController, vController *varnish.VarnishController,
listers *Listers, listers *Listers,
client kubernetes.Interface, client kubernetes.Interface,
...@@ -298,6 +302,7 @@ func NewNamespaceQueues( ...@@ -298,6 +302,7 @@ func NewNamespaceQueues(
return &NamespaceQueues{ return &NamespaceQueues{
Queue: q, Queue: q,
log: log, log: log,
ingClass: ingClass,
vController: vController, vController: vController,
workers: make(map[string]*NamespaceWorker), workers: make(map[string]*NamespaceWorker),
listers: listers, listers: listers,
...@@ -338,6 +343,7 @@ func (qs *NamespaceQueues) next() { ...@@ -338,6 +343,7 @@ func (qs *NamespaceQueues) next() {
workqueue.DefaultControllerRateLimiter(), ns) workqueue.DefaultControllerRateLimiter(), ns)
worker = &NamespaceWorker{ worker = &NamespaceWorker{
namespace: ns, namespace: ns,
ingClass: qs.ingClass,
log: qs.log, log: qs.log,
vController: qs.vController, vController: qs.vController,
queue: q, queue: q,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment