Commit 8692cdf2 authored by Geoff Simmons's avatar Geoff Simmons

All VCL templates are internal, there are no external files.

The controller executable is the only artifact needed for its
container.

The templatedir CLI argument is removed.
parent 48594811
...@@ -65,11 +65,6 @@ var ( ...@@ -65,11 +65,6 @@ var (
"or TRACE") "or TRACE")
namespaceF = flag.String("namespace", api_v1.NamespaceAll, namespaceF = flag.String("namespace", api_v1.NamespaceAll,
"namespace in which to listen for resources (default all)") "namespace in which to listen for resources (default all)")
tmplDirF = flag.String("templatedir", "",
"directory of templates for VCL generation. Defaults to \n"+
"the TEMPLATE_DIR env variable, if set, or the \n"+
"current working directory when the ingress \n"+
"controller is invoked")
masterURLF = flag.String("masterurl", "", "cluster master URL, for "+ masterURLF = flag.String("masterurl", "", "cluster master URL, for "+
"out-of-cluster runs") "out-of-cluster runs")
kubeconfigF = flag.String("kubeconfig", "", "config path for the "+ kubeconfigF = flag.String("kubeconfig", "", "config path for the "+
...@@ -190,12 +185,7 @@ func main() { ...@@ -190,12 +185,7 @@ func main() {
"Polska ciągle spada w światowym rankingu wolności prasy: " + "Polska ciągle spada w światowym rankingu wolności prasy: " +
"https://rsf.org/en/ranking Nie uciszajcie wolnych mediów!") "https://rsf.org/en/ranking Nie uciszajcie wolnych mediów!")
vController, err := varnish.NewVarnishController(log, *tmplDirF, vController := varnish.NewVarnishController(log, *monIntvlF)
*monIntvlF)
if err != nil {
log.Fatal("Cannot initialize Varnish controller: ", err)
os.Exit(-1)
}
hController := haproxy.NewOffloaderController(log, *monIntvlF) hController := haproxy.NewOffloaderController(log, *monIntvlF)
config, err := clientcmd.BuildConfigFromFlags(*masterURLF, *kubeconfigF) config, err := clientcmd.BuildConfigFromFlags(*masterURLF, *kubeconfigF)
......
...@@ -31,7 +31,6 @@ RUN adduser --disabled-password --gecos "viking controller" \ ...@@ -31,7 +31,6 @@ RUN adduser --disabled-password --gecos "viking controller" \
"${USER}" "${USER}"
COPY --from=builder /go/src/code.uplex.de/uplex-varnish/k8s-ingress/k8s-ingress /k8s-ingress COPY --from=builder /go/src/code.uplex.de/uplex-varnish/k8s-ingress/k8s-ingress /k8s-ingress
COPY --from=builder /go/src/code.uplex.de/uplex-varnish/k8s-ingress/pkg/varnish/vcl/*.tmpl /
USER controller:controller USER controller:controller
ENTRYPOINT ["/k8s-ingress"] ENTRYPOINT ["/k8s-ingress"]
...@@ -37,7 +37,6 @@ import ( ...@@ -37,7 +37,6 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"os"
"reflect" "reflect"
"regexp" "regexp"
"strings" "strings"
...@@ -205,23 +204,10 @@ type Controller struct { ...@@ -205,23 +204,10 @@ type Controller struct {
// NewVarnishController returns an instance of Controller. // NewVarnishController returns an instance of Controller.
// //
// log: logger object initialized at startup // log: logger object initialized at startup
// tmplDir: directory containing templates for VCL generation func NewVarnishController(
// log *logrus.Logger,
// If tmplDir is the empty string, use the environment variable monIntvl time.Duration,
// TEMPLATE_DIR. If the env variable does not exist, use the current ) *Controller {
// working directory.
func NewVarnishController(log *logrus.Logger, tmplDir string,
monIntvl time.Duration) (*Controller, error) {
if tmplDir == "" {
tmplEnv, exists := os.LookupEnv("TEMPLATE_DIR")
if exists {
tmplDir = tmplEnv
}
}
if err := vcl.InitTemplates(tmplDir); err != nil {
return nil, err
}
initMetrics() initMetrics()
return &Controller{ return &Controller{
svcs: make(map[string]*varnishSvc), svcs: make(map[string]*varnishSvc),
...@@ -229,7 +215,7 @@ func NewVarnishController(log *logrus.Logger, tmplDir string, ...@@ -229,7 +215,7 @@ func NewVarnishController(log *logrus.Logger, tmplDir string,
log: log, log: log,
monIntvl: monIntvl, monIntvl: monIntvl,
wg: new(sync.WaitGroup), wg: new(sync.WaitGroup),
}, nil }
} }
// EvtGenerator sets the object that implements interface // EvtGenerator sets the object that implements interface
......
/*
* Copyright (c) 2020 UPLEX Nils Goroll Systemoptimierung
* All rights reserved
*
* Author: Geoffrey Simmons <geoffrey.simmons@uplex.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package vcl
import "text/template"
const reqDispTmplSrc = `
import re2; import re2;
import selector; import selector;
...@@ -46,3 +78,9 @@ sub vcl_recv { ...@@ -46,3 +78,9 @@ sub vcl_recv {
{{- end}} {{- end}}
return (hash); return (hash);
} }
`
const reqDispTmplName = "request-disposition"
var reqDispTmpl = template.Must(template.New(reqDispTmplName).Funcs(fMap).
Parse(reqDispTmplSrc))
...@@ -33,7 +33,6 @@ import ( ...@@ -33,7 +33,6 @@ import (
"fmt" "fmt"
"hash/fnv" "hash/fnv"
"math/big" "math/big"
"path"
"regexp" "regexp"
"strings" "strings"
"text/template" "text/template"
...@@ -144,32 +143,12 @@ var fMap = template.FuncMap{ ...@@ -144,32 +143,12 @@ var fMap = template.FuncMap{
}, },
} }
const ( // maxSymLen is a workaround for Varnish issue #2880
reqDispTmplSrc = "recv_disposition.tmpl" // https://github.com/varnishcache/varnish-cache/issues/2880
// Will be unnecssary as of the March 2019 release
// maxSymLen is a workaround for Varnish issue #2880 const maxSymLen = 46
// https://github.com/varnishcache/varnish-cache/issues/2880
// Will be unnecssary as of the March 2019 release
maxSymLen = 46
)
var (
reqDispTmpl *template.Template
vclIllegal = regexp.MustCompile("[^[:word:]-]+")
)
// InitTemplates initializes templates for VCL generation.
func InitTemplates(tmplDir string) error {
var err error
reqDispTmplPath := path.Join(tmplDir, reqDispTmplSrc)
reqDispTmpl, err = template.New(reqDispTmplSrc). var vclIllegal = regexp.MustCompile("[^[:word:]-]+")
Funcs(fMap).ParseFiles(reqDispTmplPath)
if err != nil {
return err
}
return nil
}
func replIllegal(ill []byte) []byte { func replIllegal(ill []byte) []byte {
repl := []byte("_") repl := []byte("_")
......
...@@ -30,9 +30,7 @@ package vcl ...@@ -30,9 +30,7 @@ package vcl
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
) )
...@@ -46,20 +44,6 @@ func cmpGold(got []byte, goldfile string) (bool, error) { ...@@ -46,20 +44,6 @@ func cmpGold(got []byte, goldfile string) (bool, error) {
return bytes.Equal(got, gold), nil return bytes.Equal(got, gold), nil
} }
func TestMain(m *testing.M) {
tmplDir := ""
tmplEnv, exists := os.LookupEnv("TEMPLATE_DIR")
if !exists {
tmplDir = tmplEnv
}
if err := InitTemplates(tmplDir); err != nil {
fmt.Printf("Cannot parse templates: %v\n", err)
os.Exit(-1)
}
code := m.Run()
os.Exit(code)
}
var teaSvc = Service{ var teaSvc = Service{
Name: "tea-svc", Name: "tea-svc",
Addresses: []Address{ Addresses: []Address{
......
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