diff --git a/README.md b/README.md index bf106c2..fb2f5f7 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. @@ -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: ``` @@ -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 diff --git a/cmd/varnish-cache-invalidator/main.go b/cmd/varnish-cache-invalidator/main.go index fd7adaa..f0e5031 100644 --- a/cmd/varnish-cache-invalidator/main.go +++ b/cmd/varnish-cache-invalidator/main.go @@ -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() { @@ -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 { @@ -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)) } } diff --git a/internal/web/handlers.go b/internal/web/handlers.go index 30bcf16..361ea6a 100644 --- a/internal/web/handlers.go +++ b/internal/web/handlers.go @@ -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!") @@ -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 @@ -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)