Skip to content

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalcaliskan committed Dec 10, 2021
2 parents c72ab3c + ed9693f commit b29cb95
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion deployment/invalidator/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ spec:
- command: [ "./main" ]
args: [
"--varnishNamespace", "default",
"--varnishLabel", "'app=varnish'",
"--varnishLabel", "app=varnish",
"--inCluster=true"
]
image: 'docker.io/bilalcaliskan/varnish-cache-invalidator:latest'
Expand Down
17 changes: 6 additions & 11 deletions deployment/varnish/default.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,6 @@ sub vcl_deliver {
return (deliver);
}

sub vcl_purge {
# Only handle actual PURGE HTTP methods, everything else is discarded
if (req.method == "PURGE") {
# restart request
set req.http.X-Purge = "Yes";
return(restart);
}
}

sub vcl_synth {
if (resp.status == 720) {
# We use this special error status 720 to force redirects with 301 (permanent) redirects
Expand Down Expand Up @@ -201,7 +192,11 @@ sub vcl_recv {
# Normalize the query arguments
set req.url = std.querysort(req.url);

# Purge logic
if (req.method == "PURGE") {
if (req.http.purge-domain) {
set req.http.host = req.http.purge-domain;
}
return (purge);
}

Expand Down Expand Up @@ -237,8 +232,8 @@ sub vcl_backend_response {
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.

set beresp.ttl = 5m;
set beresp.grace = 30m;
#set beresp.ttl = 5m;
#set beresp.grace = 30m;

# Don't cache 50x responses
if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {
Expand Down
10 changes: 5 additions & 5 deletions internal/k8s/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ func RunPodInformer() {
if key == varnishLabelKey && value == varnishLabelValue && pod.Namespace == opts.VarnishNamespace {
if pod.Status.PodIP != "" {
podUrl := fmt.Sprintf(PodUrl, pod.Status.PodIP, pod.Spec.Containers[0].Ports[0].ContainerPort)
logger.Info("Adding pod url to the varnishPods slice", zap.String("podUrl", podUrl))
logger.Info("adding pod url to the varnishPods slice", zap.String("podUrl", podUrl))
addVarnishPod(&options.VarnishInstances, &podUrl)
} else {
logger.Warn("Varnish pod does not have an ip address yet, skipping add operation",
logger.Warn("varnish pod does not have an ip address yet, skipping add operation",
zap.String("pod", pod.Name), zap.String("namespace", pod.Namespace))
}
}
Expand All @@ -88,10 +88,10 @@ func RunPodInformer() {
if key == varnishLabelKey && value == varnishLabelValue && oldPod.ResourceVersion != newPod.ResourceVersion &&
oldPod.Namespace == opts.VarnishNamespace {
if oldPod.Status.PodIP == "" && newPod.Status.PodIP != "" {
logger.Info("Assigned an ip address to the pod, adding to varnishPods slice", zap.String("pod", newPod.Name),
logger.Info("assigned an ip address to the pod, adding to varnishPods slice", zap.String("pod", newPod.Name),
zap.String("namespace", newPod.Namespace), zap.String("ipAddress", newPod.Status.PodIP))
podUrl := fmt.Sprintf(PodUrl, newPod.Status.PodIP, newPod.Spec.Containers[0].Ports[0].ContainerPort)
logger.Info("Adding pod url to the varnishPods slice", zap.String("podUrl", podUrl))
logger.Info("adding pod url to the varnishPods slice", zap.String("podUrl", podUrl))
addVarnishPod(&options.VarnishInstances, &podUrl)
}
}
Expand All @@ -102,7 +102,7 @@ func RunPodInformer() {
labels := pod.GetLabels()
for key, value := range labels {
if key == varnishLabelKey && value == varnishLabelValue && pod.Namespace == opts.VarnishNamespace {
logger.Info("Varnish pod is deleted, removing from varnishPods slice", zap.String("pod", pod.Name),
logger.Info("varnish pod is deleted, removing from varnishPods slice", zap.String("pod", pod.Name),
zap.String("namespace", pod.Namespace))
podUrl := fmt.Sprintf(PodUrl, pod.Status.PodIP, pod.Spec.Containers[0].Ports[0].ContainerPort)
index, found := findVarnishPod(options.VarnishInstances, podUrl)
Expand Down
37 changes: 23 additions & 14 deletions internal/web/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,60 @@ import (
)

func purgeHandler(w http.ResponseWriter, r *http.Request) {
var successCount int
var response string
var (
successCount, failureCount int
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!",
logger.Error("unable to make a PURGE request to Varnish targets, header purge-path must be set!",
zap.String("requestMethod", "PURGE"))
http.Error(w, "Header purge-path must be set!", http.StatusBadRequest)
return
}

purgeDomain := r.Header.Get("purge-domain")
if purgeDomain == "" {
logger.Error("Unable to make a PURGE request to Varnish targets, header purge-domain must be set!")
logger.Error("unable to make a PURGE request to Varnish targets, header purge-domain must be set!")
http.Error(w, "Header purge-domain must be set!", http.StatusBadRequest)
return
}

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

logger.Info("Making PURGE request", zap.String("targetHost", *v))
logger.Info("making PURGE request", zap.String("url", fullUrl))
res, err := client.Do(req)
if err != nil {
logger.Error("An error occurred while making PURGE request", zap.String("targetHost", *v),
logger.Error("an error occurred while making PURGE request", zap.String("url", fullUrl),
zap.String("error", err.Error()))
failureCount++
}

if res != nil && res.StatusCode == http.StatusOK {
if res.StatusCode == http.StatusOK {
successCount++
}
}

if successCount == len(options.VarnishInstances) {
logger.Info("All PURGE requests succeeded on Varnish pods!", zap.Int("successCount", successCount))
logger.Info("all PURGE requests succeeded on Varnish pods!", zap.Int("successCount", successCount),
zap.Int("failureCount", failureCount))
httpResponse = fmt.Sprintf("All PURGE requests succeeded on Varnish pods!\nSucceeded request = %d\n"+
"Failed request = %d\n", successCount, failureCount)
w.WriteHeader(http.StatusOK)
} else {
logger.Warn("One or more Varnish PURGE requests failed", zap.Int("successCount", successCount),
logger.Warn("one or more Varnish PURGE requests failed", zap.Int("successCount", successCount),
zap.Int("failureCount", len(options.VarnishInstances)-successCount))
response = fmt.Sprintf("One or more Varnish PURGE requests failed, check the logs!\nSucceeded request = %d\n"+
"Failed request = %d", successCount, 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)
}

writeResponse(w, response)
writeResponse(w, httpResponse)
}

0 comments on commit b29cb95

Please sign in to comment.