Skip to content

Commit

Permalink
fix inCluster logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalcaliskan committed Jan 16, 2022
1 parent de66e36 commit 92b6bd6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ instances at the same time to manage the cache properly. If you are using Varnis

varnish-cache-invalidator can be used for standalone Varnish instances or Varnish pods inside a Kubernetes cluster.

**Standalone mode**
**Standalone mode**
In that mode, varnish-cache-invalidator multiplexes requests on static standalone Varnish instances which are provided
with **--targetHosts** flag. This flag gets comma seperated list of hosts.

Expand All @@ -23,7 +23,7 @@ Please check all the command line arguments on **Configuration** section. Requir
--targetHosts
```

**Kubernetes mode**
**Kubernetes mode**
In that mode, varnish-cache-invalidator discovers kube-apiserver for running [Varnish](https://github.com/varnishcache/varnish-cache) pods inside
Kubernetes and multiplexes **PURGE** requests on them at the same time to manage the cache properly.

Expand Down Expand Up @@ -75,7 +75,7 @@ $ ./varnish-cache-invalidator --inCluster=false --targetHosts 10.0.0.100:6081,10
```

## Configuration
Varnish-cache-invalidator can be customized with several command line arguments. You can check [sample in-Kubernetes deployment
Varnish-cache-invalidator can be customized with several command line arguments. You can check [sample in-Kubernetes deployment
file](deployment/invalidator/deployment.yaml) for how it goes. Here is the list of arguments you can pass:

```
Expand All @@ -84,7 +84,7 @@ file](deployment/invalidator/deployment.yaml) for how it goes. Here is the list
--varnishNamespace string VarnishNamespace is the namespace of the target Varnish pods, defaults to default namespace
--varnishLabel string VarnishLabel is the label to select proper Varnish pods, defaults to app=varnish
--targetHosts string TargetHosts used when our Varnish instances(comma seperated) are not running in Kubernetes as
a pod, required for standalone Varnish instances, defaults to ''
a pod, required for standalone Varnish instances, defaults to 'http://127.0.0.1:6081'
--serverPort int ServerPort is the web server port of the varnish-cache-invalidator, defaults to 3000
--metricsPort int MetricsPort is the port of the metrics server, defaults to 3001
--writeTimeoutSeconds int WriteTimeoutSeconds is the write timeout of the both web server and metrics server, defaults to 10
Expand Down
43 changes: 19 additions & 24 deletions cmd/varnish-cache-invalidator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import (
)

var (
logger *zap.Logger
opts *options.VarnishCacheInvalidatorOptions
logger *zap.Logger
opts *options.VarnishCacheInvalidatorOptions
restConfig *rest.Config
clientSet *kubernetes.Clientset
err error
)

func init() {
Expand All @@ -30,32 +33,24 @@ func init() {

func main() {
defer func() {
if err := logger.Sync(); err != nil {
if err = logger.Sync(); err != nil {
panic(err)
}
}()

logger.Info("initializing kube client")

var (
restConfig *rest.Config
clientSet *kubernetes.Clientset
err error
)

if restConfig, err = k8s.GetConfig(); err != nil {
logger.Fatal("fatal error occurred while initializing kube client", zap.String("error", err.Error()))
}

if clientSet, err = k8s.GetClientSet(restConfig); err != nil {
logger.Fatal("fatal error occurred while getting client set", zap.String("error", err.Error()))
}

logger = logger.With(zap.Bool("inCluster", opts.InCluster), zap.String("masterIp", restConfig.Host),
zap.String("varnishLabel", opts.VarnishLabel), zap.String("varnishNamespace", opts.VarnishNamespace))

// below check ensures that if our Varnish instances inside kubernetes or not
if opts.InCluster {
logger.Info("initializing kube client")
if restConfig, err = k8s.GetConfig(); err != nil {
logger.Fatal("fatal error occurred while initializing kube client", zap.String("error", err.Error()))
}

if clientSet, err = k8s.GetClientSet(restConfig); err != nil {
logger.Fatal("fatal error occurred while getting client set", zap.String("error", err.Error()))
}

logger = logger.With(zap.Bool("inCluster", opts.InCluster), zap.String("masterIp", restConfig.Host),
zap.String("varnishLabel", opts.VarnishLabel), zap.String("varnishNamespace", opts.VarnishNamespace))
logger.Info("will use kubernetes pod instances, running pod informer to fetch pods")
go k8s.RunPodInformer(clientSet)
} else {
Expand All @@ -67,12 +62,12 @@ func main() {
}

go func() {
if err := metrics.RunMetricsServer(); err != nil {
if err = metrics.RunMetricsServer(); err != nil {
logger.Fatal("fatal error occured while spinning metrics server", zap.Error(err))
}
}()

if err := web.RunWebServer(); err != nil {
if err = web.RunWebServer(); err != nil {
logger.Fatal("fatal error occured while spinning web server", zap.Error(err))
}
}
5 changes: 2 additions & 3 deletions internal/web/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func purgeHandler(w http.ResponseWriter, r *http.Request) {
httpResponse string
)

logger = logger.With(zap.String("requestMethod", "PURGE"))
purgePath := r.Header.Get("purge-path")
if purgePath == "" {
logger.Error("unable to make a PURGE request to Varnish targets, header purge-path must be set!")
Expand All @@ -31,8 +30,8 @@ func purgeHandler(w http.ResponseWriter, r *http.Request) {

for _, v := range options.VarnishInstances {
fullUrl := fmt.Sprintf("%s%s", *v, purgePath)
// fullUrl := fmt.Sprintf("http://192.168.49.2:30654%s", purgePath)
req, _ := http.NewRequest("PURGE", fullUrl, nil)
// fullUrl := fmt.Sprintf("http://192.168.49.2:30654%s", purgePath)
// req.Host = "nginx.default.svc"
req.Host = purgeDomain

Expand Down Expand Up @@ -60,7 +59,7 @@ func purgeHandler(w http.ResponseWriter, r *http.Request) {
zap.Int("failureCount", len(options.VarnishInstances)-successCount))
httpResponse = fmt.Sprintf("One or more Varnish PURGE requests failed, check the logs!\nSucceeded request = %d\n"+
"Failed request = %d\n", successCount, failureCount)
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusInternalServerError)
}

writeResponse(w, httpResponse)
Expand Down

0 comments on commit 92b6bd6

Please sign in to comment.