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 (
"or TRACE")
namespaceF = flag.String("namespace", api_v1.NamespaceAll,
"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 "+
"out-of-cluster runs")
kubeconfigF = flag.String("kubeconfig", "", "config path for the "+
......@@ -190,12 +185,7 @@ func main() {
"Polska ciągle spada w światowym rankingu wolności prasy: " +
"https://rsf.org/en/ranking Nie uciszajcie wolnych mediów!")
vController, err := varnish.NewVarnishController(log, *tmplDirF,
*monIntvlF)
if err != nil {
log.Fatal("Cannot initialize Varnish controller: ", err)
os.Exit(-1)
}
vController := varnish.NewVarnishController(log, *monIntvlF)
hController := haproxy.NewOffloaderController(log, *monIntvlF)
config, err := clientcmd.BuildConfigFromFlags(*masterURLF, *kubeconfigF)
......
......@@ -31,7 +31,6 @@ RUN adduser --disabled-password --gecos "viking controller" \
"${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/pkg/varnish/vcl/*.tmpl /
USER controller:controller
ENTRYPOINT ["/k8s-ingress"]
......@@ -37,7 +37,6 @@ import (
"fmt"
"io"
"net"
"os"
"reflect"
"regexp"
"strings"
......@@ -205,23 +204,10 @@ type Controller struct {
// NewVarnishController returns an instance of Controller.
//
// log: logger object initialized at startup
// tmplDir: directory containing templates for VCL generation
//
// If tmplDir is the empty string, use the environment variable
// TEMPLATE_DIR. If the env variable does not exist, use the current
// 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
}
func NewVarnishController(
log *logrus.Logger,
monIntvl time.Duration,
) *Controller {
initMetrics()
return &Controller{
svcs: make(map[string]*varnishSvc),
......@@ -229,7 +215,7 @@ func NewVarnishController(log *logrus.Logger, tmplDir string,
log: log,
monIntvl: monIntvl,
wg: new(sync.WaitGroup),
}, nil
}
}
// 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 selector;
......@@ -46,3 +78,9 @@ sub vcl_recv {
{{- end}}
return (hash);
}
`
const reqDispTmplName = "request-disposition"
var reqDispTmpl = template.Must(template.New(reqDispTmplName).Funcs(fMap).
Parse(reqDispTmplSrc))
......@@ -33,7 +33,6 @@ import (
"fmt"
"hash/fnv"
"math/big"
"path"
"regexp"
"strings"
"text/template"
......@@ -144,32 +143,12 @@ var fMap = template.FuncMap{
},
}
const (
reqDispTmplSrc = "recv_disposition.tmpl"
// maxSymLen is a workaround for Varnish issue #2880
// 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)
// maxSymLen is a workaround for Varnish issue #2880
// https://github.com/varnishcache/varnish-cache/issues/2880
// Will be unnecssary as of the March 2019 release
const maxSymLen = 46
reqDispTmpl, err = template.New(reqDispTmplSrc).
Funcs(fMap).ParseFiles(reqDispTmplPath)
if err != nil {
return err
}
return nil
}
var vclIllegal = regexp.MustCompile("[^[:word:]-]+")
func replIllegal(ill []byte) []byte {
repl := []byte("_")
......
......@@ -30,9 +30,7 @@ package vcl
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
......@@ -46,20 +44,6 @@ func cmpGold(got []byte, goldfile string) (bool, error) {
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{
Name: "tea-svc",
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