Moving to kube, refactoring code, using env variables only, adding metrics exporter

This commit is contained in:
2023-07-19 16:47:23 +02:00
parent b64d359c27
commit 7ffefb1159
9 changed files with 342 additions and 192 deletions

View File

@@ -2,87 +2,63 @@
package varnish
import (
"context"
prometheus2 "http-broadcaster/Prometheus"
"log"
"net/http"
"os"
"strings"
vault "http-broadcaster/Vault"
"time"
)
var (
varnishList = InitializeVarnishList()
status = "200 Purged"
// VarnishList contains the list of varnish servers.
VarnishList []string
status = 200
)
// InitializeVarnishList sets varnishList variable according to the LIST_METHOD env var
func InitializeVarnishList() []string {
switch method := os.Getenv("LIST_METHOD"); method {
case "vault":
return GetVarnishListFromVault()
case "file":
return GetVarnishListFromFile()
default:
panic("LIST_METHOD empty, no provided method to retrieve varnish list")
}
}
// GetVarnishListFromVault builds a list of varnish servers from Vault.
func GetVarnishListFromVault() []string {
var value []string
client := vault.InitVaultConnection()
secret, err := client.KVv2("app").Get(context.Background(), "http-broadcaster/stg/envVars")
if err != nil {
log.Fatal("unable to read secret: %w", err)
return value
}
// selecting list key from retrieved secret
list, ok := secret.Data["varnish_list"].(string)
if !ok {
log.Fatal("value type assertion failed: %T %#v", secret.Data["varnish_list"], secret.Data["varnish_list"])
return value
}
value = strings.Split(string(list), ",")
return value
}
// GetVarnishListFromFile reads the list of varnish servers from a file on disk.
func GetVarnishListFromFile() []string {
Data, err := os.ReadFile("./varnish")
if err != nil {
log.Fatal(err)
}
sliceData := strings.Split(string(Data), ",")
func InitializeVarnishList(l string) []string {
data := os.Getenv("VARNISH_SERVERS")
sliceData := strings.Split(string(data), ",")
return sliceData
}
// SendToVarnish send to all varnish servers define in varnishList the request with the PURGE or BAN method
// and the X-Cache-Tags header if necessary.
func SendToVarnish(method string, url string, tag string) string {
status = "200 Purged"
func SendToVarnish(method string, url string, tag map[string]string) int {
status = 200
// Take url to ban as argument.
// Loop over the list of Varnish servers and send PURGE request to each.
// Update status variable to check if servers have successfully purge url.
for i := 0; i < len(varnishList); i++ {
client := &http.Client{}
domain := strings.Trim(varnishList[i], "\r\n")
for i := 0; i < len(VarnishList); i++ {
client := &http.Client{
Timeout: 10 * time.Second,
}
// sanitize varnish server host.
domain := strings.Trim(VarnishList[i], "\r\n")
req, err := http.NewRequest(method, domain+url, nil)
if err != nil {
log.Fatal("Create new request : %s", err)
}
if tag != "" {
req.Header.Add("X-Cache-Tags", tag)
// If X-Cache-Tags header is not empty with pass it to varnish.
if tag["X-Cache-Tags"] != "" {
req.Header.Add("X-Cache-Tags", tag["X-Cache-Tags"])
}
if tag["ApiPlatform-Ban-Regex"] != "" {
req.Header.Add("ApiPlatform-Ban-Regex", tag["ApiPlatform-Ban-Regex"])
}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Send new request : ", err)
log.Println("Send new request : ", err)
}
if prometheus2.MetricsEnabled {
prometheus2.IncrementBackendCounterVec(method)
}
if resp.StatusCode != 200 {
status = "405 Not Allowed"
status = 405
}
resp.Body.Close()
}
return status
}