Skip to content

Commit

Permalink
change vcio as opts
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalcaliskan committed Aug 31, 2021
1 parent e16126e commit 742cde0
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 50 deletions.
11 changes: 5 additions & 6 deletions cmd/varnish-cache-invalidator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
var (
router *mux.Router
logger *zap.Logger
vcio *options.VarnishCacheInvalidatorOptions
opts *options.VarnishCacheInvalidatorOptions
)

func init() {
logger = logging.GetLogger()
vcio = options.GetVarnishCacheInvalidatorOptions()
opts = options.GetVarnishCacheInvalidatorOptions()
router = mux.NewRouter()
bannerBytes, _ := ioutil.ReadFile("banner.txt")
banner.Init(os.Stdout, true, false, strings.NewReader(string(bannerBytes)))
Expand All @@ -37,12 +37,11 @@ func main() {
}
}()

// below check ensures that we will use our Varnish instances as Kubernetes pods
if vcio.TargetHosts == "" {
// below check ensures that if our Varnish instances inside kubernetes or not
if opts.InCluster {
go k8s.RunPodInformer()
} else {
// TODO: Check the case that Varnish instances are not running inside Kubernetes. Check them after standalone installation
splitted := strings.Split(vcio.TargetHosts, ",")
splitted := strings.Split(opts.TargetHosts, ",")
for _, v := range splitted {
k8s.VarnishInstances = append(k8s.VarnishInstances, &v)
}
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ module varnish-cache-invalidator
go 1.16

require (
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect
github.com/dimiro1/banner v1.1.0
github.com/google/gofuzz v1.2.0 // indirect
github.com/gorilla/mux v1.8.0
github.com/imdario/mergo v0.3.12 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/prometheus/client_golang v1.11.0
github.com/spf13/pflag v1.0.5
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.19.0
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
k8s.io/api v0.22.0
k8s.io/apimachinery v0.22.0
k8s.io/client-go v0.22.0
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e // indirect
k8s.io/api v0.22.1
k8s.io/apimachinery v0.22.1
k8s.io/client-go v0.22.1
)
23 changes: 13 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2 h1:tjT4Jp4gxECvsJcYpAMtW2I3YqzBTPuB67OejxXs86s=
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w=
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be h1:J5BL2kskAlV9ckgEsNQXscjIaLiOYiZ75d4e94E6dcQ=
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -179,8 +180,9 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA=
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
Expand Down Expand Up @@ -368,6 +370,7 @@ golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -383,8 +386,8 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e h1:XMgFehsDnnLGtjvjOfqWSUzt0alpTR1RSEuznObga2c=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
Expand Down Expand Up @@ -524,12 +527,12 @@ honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.22.0 h1:elCpMZ9UE8dLdYxr55E06TmSeji9I3KH494qH70/y+c=
k8s.io/api v0.22.0/go.mod h1:0AoXXqst47OI/L0oGKq9DG61dvGRPXs7X4/B7KyjBCU=
k8s.io/apimachinery v0.22.0 h1:CqH/BdNAzZl+sr3tc0D3VsK3u6ARVSo3GWyLmfIjbP0=
k8s.io/apimachinery v0.22.0/go.mod h1:O3oNtNadZdeOMxHFVxOreoznohCpy0z6mocxbZr7oJ0=
k8s.io/client-go v0.22.0 h1:sD6o9O6tCwUKCENw8v+HFsuAbq2jCu8cWC61/ydwA50=
k8s.io/client-go v0.22.0/go.mod h1:GUjIuXR5PiEv/RVK5OODUsm6eZk7wtSWZSaSJbpFdGg=
k8s.io/api v0.22.1 h1:ISu3tD/jRhYfSW8jI/Q1e+lRxkR7w9UwQEZ7FgslrwY=
k8s.io/api v0.22.1/go.mod h1:bh13rkTp3F1XEaLGykbyRD2QaTTzPm0e/BMd8ptFONY=
k8s.io/apimachinery v0.22.1 h1:DTARnyzmdHMz7bFWFDDm22AM4pLWTQECMpRTFu2d2OM=
k8s.io/apimachinery v0.22.1/go.mod h1:O3oNtNadZdeOMxHFVxOreoznohCpy0z6mocxbZr7oJ0=
k8s.io/client-go v0.22.1 h1:jW0ZSHi8wW260FvcXHkIa0NLxFBQszTlhiAVsU5mopw=
k8s.io/client-go v0.22.1/go.mod h1:BquC5A4UOo4qVDUtoc04/+Nxp1MeHcVc1HJm1KmG8kk=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
k8s.io/klog/v2 v2.9.0 h1:D7HV+n1V57XeZ0m6tdRkfknthUaM06VFbWldOFh8kzM=
Expand Down
20 changes: 10 additions & 10 deletions internal/k8s/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ var (
clientSet *kubernetes.Clientset
err error
logger *zap.Logger
vcio *options.VarnishCacheInvalidatorOptions
opts *options.VarnishCacheInvalidatorOptions
// VarnishInstances keeps pointer of varnish instances' ip:port information
VarnishInstances []*string
)

func init() {
logger = logging.GetLogger()
vcio = options.GetVarnishCacheInvalidatorOptions()
opts = options.GetVarnishCacheInvalidatorOptions()

logger.Info("initializing kube client", zap.String("masterUrl", vcio.MasterUrl),
zap.String("kubeConfigPath", vcio.KubeConfigPath), zap.Bool("inCluster", vcio.InCluster))
logger.Info("initializing kube client", zap.String("masterUrl", opts.MasterUrl),
zap.String("kubeConfigPath", opts.KubeConfigPath), zap.Bool("inCluster", opts.InCluster))

restConfig, err = getConfig(vcio.MasterUrl, vcio.KubeConfigPath, vcio.InCluster)
restConfig, err = getConfig(opts.MasterUrl, opts.KubeConfigPath, opts.InCluster)
if err != nil {
logger.Fatal("fatal error occurred while initializing kube client", zap.String("error", err.Error()))
}
Expand All @@ -47,8 +47,8 @@ func init() {
// RunPodInformer continuously watches kube-apiserver with shared informer for Pod resources, then does necessary updates
// on VarnishInstances slice on Add/Update/Delete conditions
func RunPodInformer() {
varnishLabelKey := strings.Split(vcio.VarnishLabel, "=")[0]
varnishLabelValue := strings.Split(vcio.VarnishLabel, "=")[1]
varnishLabelKey := strings.Split(opts.VarnishLabel, "=")[0]
varnishLabelValue := strings.Split(opts.VarnishLabel, "=")[1]

informerFactory := informers.NewSharedInformerFactory(clientSet, time.Second*30)
podInformer := informerFactory.Core().V1().Pods()
Expand All @@ -57,7 +57,7 @@ func RunPodInformer() {
pod := obj.(*v1.Pod)
labels := pod.GetLabels()
for key, value := range labels {
if key == varnishLabelKey && value == varnishLabelValue && pod.Namespace == vcio.VarnishNamespace {
if key == varnishLabelKey && value == varnishLabelValue && pod.Namespace == opts.VarnishNamespace {
if pod.Status.PodIP != "" {
podUrl := fmt.Sprintf("http://%s:%d", pod.Status.PodIP, pod.Spec.Containers[0].Ports[0].ContainerPort)
logger.Info("Adding pod url to the varnishPods slice", zap.String("podUrl", podUrl))
Expand All @@ -78,7 +78,7 @@ func RunPodInformer() {

for key, value := range labels {
if key == varnishLabelKey && value == varnishLabelValue && oldPod.ResourceVersion != newPod.ResourceVersion &&
oldPod.Namespace == vcio.VarnishNamespace {
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),
zap.String("namespace", newPod.Namespace), zap.String("ipAddress", newPod.Status.PodIP))
Expand All @@ -93,7 +93,7 @@ func RunPodInformer() {
pod := obj.(*v1.Pod)
labels := pod.GetLabels()
for key, value := range labels {
if key == varnishLabelKey && value == varnishLabelValue && pod.Namespace == vcio.VarnishNamespace {
if key == varnishLabelKey && value == varnishLabelValue && pod.Namespace == opts.VarnishNamespace {
logger.Info("Varnish pod is deleted, removing from varnishPods slice", zap.String("pod", pod.Name),
zap.String("namespace", pod.Namespace))
podUrl := fmt.Sprintf("http://%s:%d", pod.Status.PodIP, pod.Spec.Containers[0].Ports[0].ContainerPort)
Expand Down
3 changes: 3 additions & 0 deletions internal/k8s/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

// addVarnishPod add a pod string to varnishPods *[]*string
func addVarnishPod(varnishPods *[]*string, pod *string) {
_, found := findVarnishPod(*varnishPods, *pod)
if !found {
*varnishPods = append(*varnishPods, pod)
}
}

// findVarnishPod finds the specific pod string in varnishPods []*string
func findVarnishPod(varnishPods []*string, pod string) (int, bool) {
for i, item := range varnishPods {
if pod == *item {
Expand All @@ -22,6 +24,7 @@ func findVarnishPod(varnishPods []*string, pod string) (int, bool) {
return -1, false
}

// removeVarnishPod removes the Varnish pod string from varnishPods *[]*string
func removeVarnishPod(varnishPods *[]*string, index int) {
*varnishPods = append((*varnishPods)[:index], (*varnishPods)[index+1:]...)
}
Expand Down
12 changes: 6 additions & 6 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (

var (
logger *zap.Logger
vcio *options.VarnishCacheInvalidatorOptions
opts *options.VarnishCacheInvalidatorOptions
)

func init() {
logger = logging.GetLogger()
vcio = options.GetVarnishCacheInvalidatorOptions()
opts = options.GetVarnishCacheInvalidatorOptions()
}

// TODO: Generate custom metrics, check below:
Expand All @@ -37,11 +37,11 @@ func RunMetricsServer(router *mux.Router) {

metricServer := &http.Server{
Handler: router,
Addr: fmt.Sprintf(":%d", vcio.MetricsPort),
WriteTimeout: time.Duration(int32(vcio.WriteTimeoutSeconds)) * time.Second,
ReadTimeout: time.Duration(int32(vcio.ReadTimeoutSeconds)) * time.Second,
Addr: fmt.Sprintf(":%d", opts.MetricsPort),
WriteTimeout: time.Duration(int32(opts.WriteTimeoutSeconds)) * time.Second,
ReadTimeout: time.Duration(int32(opts.ReadTimeoutSeconds)) * time.Second,
}
router.Handle("/metrics", promhttp.Handler())
logger.Info("metric server is up and running", zap.Int("port", vcio.MetricsPort))
logger.Info("metric server is up and running", zap.Int("port", opts.MetricsPort))
panic(metricServer.ListenAndServe())
}
8 changes: 4 additions & 4 deletions internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func TestRunMetricsServer(t *testing.T) {
router := mux.NewRouter()
metricServer := &http.Server{
Handler: router,
Addr: fmt.Sprintf(":%d", vcio.MetricsPort),
WriteTimeout: time.Duration(int32(vcio.WriteTimeoutSeconds)) * time.Second,
ReadTimeout: time.Duration(int32(vcio.ReadTimeoutSeconds)) * time.Second,
Addr: fmt.Sprintf(":%d", opts.MetricsPort),
WriteTimeout: time.Duration(int32(opts.WriteTimeoutSeconds)) * time.Second,
ReadTimeout: time.Duration(int32(opts.ReadTimeoutSeconds)) * time.Second,
}
router.Handle("/metrics", promhttp.Handler())
logger.Info("metric server is up and running", zap.Int("port", vcio.MetricsPort))
logger.Info("metric server is up and running", zap.Int("port", opts.MetricsPort))
errChan <- metricServer.ListenAndServe()
}()

Expand Down
2 changes: 1 addition & 1 deletion internal/web/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func purgeHandler(w http.ResponseWriter, r *http.Request) {
for _, v := range k8s.VarnishInstances {
fullUrl := fmt.Sprintf("%s%s", *v, purgePath)
req, _ := http.NewRequest("PURGE", fullUrl, nil)
req.Host = vcio.PurgeDomain
req.Host = opts.PurgeDomain

logger.Info("Making PURGE request", zap.String("requestMethod", "PURGE"), zap.String("targetHost", *v))
res, err := client.Do(req)
Expand Down
9 changes: 6 additions & 3 deletions internal/web/utils.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package web

import (
"github.com/gorilla/mux"
"go.uber.org/zap"
"log"
"net/http"
"time"

"github.com/gorilla/mux"
"go.uber.org/zap"
)

// registerHandlers registers the handlers of the web server
func registerHandlers(router *mux.Router) {
router.HandleFunc("/ban", banHandler).Methods("BAN").Schemes("http").Name("ban")
router.HandleFunc("/purge", purgeHandler).Methods("PURGE").Schemes("http").Name("purge")
}

// initServer initializes the web server
func initServer(router *mux.Router, addr string, writeTimeout time.Duration, readTimeout time.Duration,
lgr *zap.Logger) *http.Server {

logger = lgr
registerHandlers(router)
return &http.Server{
Expand All @@ -26,6 +28,7 @@ func initServer(router *mux.Router, addr string, writeTimeout time.Duration, rea
}
}

// writeResponse helper function writes a http response body as string to http.ResponseWriter
func writeResponse(w http.ResponseWriter, response string) {
_, err := w.Write([]byte(response))
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions internal/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
var (
client *http.Client
logger *zap.Logger
vcio *options.VarnishCacheInvalidatorOptions
opts *options.VarnishCacheInvalidatorOptions
)

func init() {
logger = logging.GetLogger()
client = &http.Client{}
vcio = options.GetVarnishCacheInvalidatorOptions()
opts = options.GetVarnishCacheInvalidatorOptions()
}

// RunWebServer runs the web server which multiplexes client requests
Expand All @@ -32,10 +32,10 @@ func RunWebServer(router *mux.Router) {
}
}()

webServer := initServer(router, fmt.Sprintf(":%d", vcio.ServerPort),
time.Duration(int32(vcio.WriteTimeoutSeconds))*time.Second,
time.Duration(int32(vcio.ReadTimeoutSeconds))*time.Second, logger)
webServer := initServer(router, fmt.Sprintf(":%d", opts.ServerPort),
time.Duration(int32(opts.WriteTimeoutSeconds))*time.Second,
time.Duration(int32(opts.ReadTimeoutSeconds))*time.Second, logger)

logger.Info("web server is up and running", zap.Int("port", vcio.ServerPort))
logger.Info("web server is up and running", zap.Int("port", opts.ServerPort))
panic(webServer.ListenAndServe())
}

0 comments on commit 742cde0

Please sign in to comment.