Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow starting without service endpoints #260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/kube-httpcache/internal/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type KubeHTTPProxyFlags struct {
VCLTemplate string
VCLTemplatePoll bool
WorkingDir string
FrontendInitTimeout time.Duration
BackendInitTimeout time.Duration
}
Readiness struct {
Enable bool
Expand Down Expand Up @@ -107,6 +109,10 @@ func (f *KubeHTTPProxyFlags) Parse() error {
flag.StringVar(&f.Varnish.AdditionalParameters, "varnish-additional-parameters", "", "Additional Varnish start parameters (-p, seperated by comma), like 'ban_dups=on,cli_timeout=30'")
flag.BoolVar(&f.Varnish.VCLTemplatePoll, "varnish-vcl-template-poll", false, "poll for file changes instead of using inotify (useful on some network filesystems)")
flag.StringVar(&f.Varnish.WorkingDir, "varnish-working-dir", "", "varnish working directory (-n)")
flag.DurationVar(&f.Varnish.FrontendInitTimeout, "varnish-frontend-init-timeout", time.Duration(0),
"timeout for initial frontend configuration to be discovered. The default 0s means wait indefinitely. When timeout is reached, the controller will continue with an empty list of front-ends")
flag.DurationVar(&f.Varnish.BackendInitTimeout, "varnish-backend-init-timeout", time.Duration(0),
"timeout for initial backend configuration to be discovered. The default 0s means wait indefinitely. When timeout is reached, the controller will continue with an empty list of back-ends")

// present for BC only; no effect until #36 [1] has resolved
// [1]: https://github.com/mittwald/kube-httpcache/issues/36
Expand Down
2 changes: 2 additions & 0 deletions cmd/kube-httpcache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func main() {
templateUpdates,
varnishSignaller,
opts.Varnish.VCLTemplate,
opts.Varnish.FrontendInitTimeout,
opts.Varnish.BackendInitTimeout,
)
if err != nil {
panic(err)
Expand Down
32 changes: 30 additions & 2 deletions pkg/controller/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,53 @@ import (
"os"
"os/exec"
"strings"
"time"

"github.com/golang/glog"
"github.com/mittwald/kube-httpcache/pkg/watcher"
)

func (v *VarnishController) waitForUpdate(updatesChan chan *watcher.EndpointConfig, timeout time.Duration) (*watcher.EndpointConfig, error) {
var cancel context.CancelFunc
var ctx context.Context

ctx = context.Background()

if timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}

select {
case u := <-updatesChan:
return u, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}

func (v *VarnishController) Run(ctx context.Context) error {
glog.Infof("waiting for initial configuration before starting Varnish")

v.frontend = watcher.NewEndpointConfig()
if v.frontendUpdates != nil {
v.frontend = <-v.frontendUpdates
if frontend, err := v.waitForUpdate(v.frontendUpdates, v.frontendInitTimeout); err == nil {
v.frontend = frontend
} else {
glog.Info("timeout while waiting for initial frontend configuration: %s", err.Error())
}
if v.varnishSignaller != nil {
v.varnishSignaller.SetEndpoints(v.frontend)
}
}

v.backend = watcher.NewEndpointConfig()
if v.backendUpdates != nil {
v.backend = <-v.backendUpdates
if backend, err := v.waitForUpdate(v.backendUpdates, v.backendInitTimeout); err == nil {
v.backend = backend
} else {
glog.Info("timeout while waiting for initial backend configuration: %s", err.Error())
}
}

target, err := os.Create(v.configFile)
Expand Down
29 changes: 18 additions & 11 deletions pkg/controller/types.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package controller

import (
"github.com/golang/glog"
"io"
"os"
"strings"
"text/template"
"time"

"github.com/golang/glog"

"github.com/mittwald/kube-httpcache/pkg/signaller"
"github.com/mittwald/kube-httpcache/pkg/watcher"
Expand All @@ -32,16 +34,17 @@ type VarnishController struct {

vclTemplate *template.Template
// md5 hash of unparsed template
vclTemplateHash string
vclTemplateUpdates chan []byte
frontendUpdates chan *watcher.EndpointConfig
frontend *watcher.EndpointConfig
backendUpdates chan *watcher.EndpointConfig
backend *watcher.EndpointConfig
varnishSignaller *signaller.Signaller
configFile string
localAdminAddr string
currentVCLName string
vclTemplateHash string
vclTemplateUpdates chan []byte
frontendUpdates chan *watcher.EndpointConfig
frontendInitTimeout time.Duration
backendInitTimeout time.Duration
frontend *watcher.EndpointConfig
backendUpdates chan *watcher.EndpointConfig
backend *watcher.EndpointConfig
varnishSignaller *signaller.Signaller
configFile string
currentVCLName string
}

func NewVarnishController(
Expand All @@ -59,6 +62,8 @@ func NewVarnishController(
templateUpdates chan []byte,
varnishSignaller *signaller.Signaller,
vclTemplateFile string,
frontendInitTimeout time.Duration,
backendInitTimeout time.Duration,
) (*VarnishController, error) {
contents, err := os.ReadFile(vclTemplateFile)
if err != nil {
Expand All @@ -80,6 +85,8 @@ func NewVarnishController(
backendUpdates: backendUpdates,
varnishSignaller: varnishSignaller,
configFile: "/tmp/vcl",
frontendInitTimeout: frontendInitTimeout,
backendInitTimeout: backendInitTimeout,
}
err = v.setTemplate(contents)
if err != nil {
Expand Down