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 4, 2021
2 parents 6fa3989 + 2a98dc1 commit a2e5a10
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
10 changes: 5 additions & 5 deletions internal/k8s/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"k8s.io/client-go/tools/cache"
)

const PodUrl = "http://%s:%d"

var (
restConfig *rest.Config
clientSet *kubernetes.Clientset
Expand Down Expand Up @@ -67,7 +69,7 @@ func RunPodInformer() {
for key, value := range labels {
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)
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))
addVarnishPod(&options.VarnishInstances, &podUrl)
} else {
Expand All @@ -82,15 +84,13 @@ func RunPodInformer() {
newPod := newObj.(*v1.Pod)
labels := oldPod.GetLabels()

// TODO: Handle all the cases

for key, value := range labels {
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),
zap.String("namespace", newPod.Namespace), zap.String("ipAddress", newPod.Status.PodIP))
podUrl := fmt.Sprintf("http://%s:%d", newPod.Status.PodIP, newPod.Spec.Containers[0].Ports[0].ContainerPort)
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))
addVarnishPod(&options.VarnishInstances, &podUrl)
}
Expand All @@ -104,7 +104,7 @@ func RunPodInformer() {
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)
podUrl := fmt.Sprintf(PodUrl, pod.Status.PodIP, pod.Spec.Containers[0].Ports[0].ContainerPort)
index, found := findVarnishPod(options.VarnishInstances, podUrl)
if found {
removeVarnishPod(&options.VarnishInstances, index)
Expand Down
5 changes: 1 addition & 4 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ func init() {
opts = options.GetVarnishCacheInvalidatorOptions()
}

// TODO: Generate custom metrics, check below:
// https://prometheus.io/docs/guides/go-application/
// https://www.robustperception.io/prometheus-middleware-for-gorilla-mux

// RunMetricsServer provides an endpoint, exports prometheus metrics using prometheus client golang
func RunMetricsServer(router *mux.Router) {
defer func() {
Expand All @@ -41,6 +37,7 @@ func RunMetricsServer(router *mux.Router) {
WriteTimeout: time.Duration(int32(opts.WriteTimeoutSeconds)) * time.Second,
ReadTimeout: time.Duration(int32(opts.ReadTimeoutSeconds)) * time.Second,
}

router.Handle("/metrics", promhttp.Handler())
logger.Info("starting metrics server", zap.Int("port", opts.MetricsPort))
panic(metricServer.ListenAndServe())
Expand Down

0 comments on commit a2e5a10

Please sign in to comment.