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