Skip to content

Instantly share code, notes, and snippets.

View slok's full-sized avatar
⚙️
Building simple solutions

Xabier Larrakoetxea Gallego slok

⚙️
Building simple solutions
View GitHub Profile
@slok
slok / handler-opentracing.go
Created May 23, 2018 13:06
kooper handler opentracing
hand := &handler.HandlerFunc{
AddFunc: func(ctx context.Context, obj runtime.Object) error {
// Get the parent span.
pSpan := opentracing.SpanFromContext(ctx)
// Create a new span.
span := tracer.StartSpan("AddFunc", opentracing.ChildOf(pSpan.Context()))
defer span.Finish()
// Do stuff...
// Initialize logger.
log := &log.Std{}
// Create the controller that will refresh every 30 seconds.
ctrl := controller.NewSequential(30*time.Second, hand, retr, log)
// Start our controller.
stopC := make(chan struct{})
if err := ctrl.Run(stopC); err != nil {
log.Errorf("error running controller: %s", err)
os.Exit(1)
}
// Our domain logic that will print every add/sync/update and delete event we .
hand := &handler.HandlerFunc{
AddFunc: func(obj runtime.Object) error {
pod := obj.(*corev1.Pod)
log.Infof("Pod Add event: %s/%s", pod.Namespace, pod.Name)
return nil
},
DeleteFunc: func(s string) error {
log.Infof("Pod Delete event: %s", s)
return nil
// Create our retriever so the controller knows how to get/listen for pod events.
retr := &retrieve.Resource{
Object: &corev1.Pod{},
ListerWatcher: &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return k8scli.CoreV1().Pods("").List(options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return k8scli.CoreV1().Pods("").Watch(options)
},
@slok
slok / amfirer.go
Last active November 11, 2021 06:10
Alertmanager alarm tester. Fire a custom alert to your alertmanager: https://github.com/prometheus/alertmanager
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"net/url"
#!/bin/bash
# Wait until consul instance on clust
INSTANCE_IP="127.0.0.1"
NEXT_WAIT_TIME=0
MAX_WAIT_TIME=35
check_consul_sync() {
# If we don't have leaders _last_log_index then get it
@slok
slok / mockserver.go
Created March 14, 2017 09:35
A simple HTTP server to mock responses
package main
import (
"flag"
"fmt"
"log"
"net/http"
)
func initFlags(addr *string, port *int, retCode *int, body *string) {
@slok
slok / trap.sh
Created September 15, 2016 16:23
onliner trap
trap 'echo captured;sleep 30;exit' SIGINT SIGTERM && while true; do sleep 1; done
@slok
slok / main.go
Created June 12, 2016 10:09
handler & handlerfunc golang pattern example
// Package main is an example of how handler pattern works in golang.
//
// At first you will need some sort of start point. To do this we create
// ExampleHandler interface, this interface has a trigger method that will
// execute the chain, in his case is RunExample, it accepts a writer, and a
// custom input object, as you see there is an out and an in parameter.
//
// We could work like this you can create multiple ExampleHandlers in a helper
// function and call them on in another. But this is not very handy and it smells
//
gvm install go1.6.2
gvm use go1.6.2 --default
gvm pkgset create slok
gvm pkgenv slok
# Add this lines
export GOPATH; GOPATH="/home/slok/projects/work/go:/home/slok/projects/personal/go:$GOPATH"
export PATH; PATH="/home/slok/projects/work/go/bin:/home/slok/projects/personal/go/bin:$PATH"
gvm pkgset use slok --default