diff --git a/.gitignore b/.gitignore index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..eae84e7a0ce956aa3d76f9082d27432b77c22f68 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +loki-canary \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..8e2476fb34de3c1a80ff386362328860d7cc27f1 --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +.PHONY: all build test clean build-image push-image +.DEFAULT_GOAL := all + +IMAGE_PREFIX ?= grafana +IMAGE_TAG := $(shell ./tools/image-tag) + +all: test build-image + +build: + go build -o loki-canary -v cmd/loki-canary/main.go + +test: + go test -v ./... + +clean: + rm -f ./loki-canary + go clean ./... + +build-image: + docker build -t $(IMAGE_PREFIX)/loki-canary . + docker tag $(IMAGE_PREFIX)/loki-canary $(IMAGE_PREFIX)/loki-canary:$(IMAGE_TAG) + +push-image: + docker push $(IMAGE_PREFIX)/loki-canary:$(IMAGE_TAG) + docker push $(IMAGE_PREFIX)/loki-canary:latest \ No newline at end of file diff --git a/cmd/loki-canary/main.go b/cmd/loki-canary/main.go index 442fcc396a75701cfda0db0eaa44320859b0f124..dc609427faafde8128dc7a1f9045f6afdfb5ef5e 100644 --- a/cmd/loki-canary/main.go +++ b/cmd/loki-canary/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "io/ioutil" "net/http" "net/url" "os" @@ -22,7 +21,6 @@ func main() { lName := flag.String("labelname", "name", "The label name for this instance of loki-canary to use in the log selector") lVal := flag.String("labelvalue", "loki-canary", "The unique label value for this instance of loki-canary to use in the log selector") - usePodName := flag.Bool("usepod", false, "If true, loki-canary will read the pod name from /etc/loki-canary/pod_name as the unique label value") port := flag.Int("port", 3500, "Port which loki-canary should expose metrics") addr := flag.String("addr", "", "The Loki server URL:Port, e.g. loki:3100") tls := flag.Bool("tls", false, "Does the loki connection use TLS?") @@ -34,17 +32,9 @@ func main() { wait := flag.Duration("wait", 60*time.Second, "Duration to wait for log entries before reporting them lost") flag.Parse() - val := *lVal - if *usePodName { - data, err := ioutil.ReadFile("/etc/loki-canary/name") - if err != nil { - panic(err) - } - val = string(data) - } - if *addr == "" { - panic("Must specify a Loki address with -addr") + _, _ = fmt.Fprintf(os.Stderr, "Must specify a Loki address with -addr\n") + os.Exit(1) } var ui *url.Userinfo @@ -62,10 +52,10 @@ func main() { Host: *addr, User: ui, Path: "/api/prom/tail", - RawQuery: "query=" + url.QueryEscape(fmt.Sprintf("{stream=\"stdout\",%v=\"%v\"}", *lName, val)), + RawQuery: "query=" + url.QueryEscape(fmt.Sprintf("{stream=\"stdout\",%v=\"%v\"}", *lName, *lVal)), } - _, _ = fmt.Fprintf(os.Stderr, "Connecting to loki at %v, querying for label '%v' with value '%v'\n", u.String(), *lName, val) + _, _ = fmt.Fprintf(os.Stderr, "Connecting to loki at %v, querying for label '%v' with value '%v'\n", u.String(), *lName, *lVal) c := comparator.NewComparator(os.Stderr, *wait, 1*time.Second) w := writer.NewWriter(os.Stdout, c, *interval, *size) diff --git a/production/ksonnet/loki-canary/loki-canary.libsonnet b/production/ksonnet/loki-canary/loki-canary.libsonnet index 195df2aaa6e26d3c5ddfa2f5507bdee15f6aa756..b5a6bf65e44d1f43c30b5a105403bc951456637c 100644 --- a/production/ksonnet/loki-canary/loki-canary.libsonnet +++ b/production/ksonnet/loki-canary/loki-canary.libsonnet @@ -3,10 +3,12 @@ local k = import 'ksonnet-util/kausal.libsonnet'; k { local container = $.core.v1.container, - loki_canary_args:: {}, + loki_canary_args:: { + labelvalue: "$(POD_NAME)", + }, _images+:: { - loki_canary: 'loki-canary:latest', + loki_canary: 'grafana/loki-canary:latest', }, loki_canary_container:: @@ -15,32 +17,11 @@ k { container.withArgsMixin($.util.mapToFlags($.loki_canary_args)) + container.withEnv([ container.envType.fromFieldPath('HOSTNAME', 'spec.nodeName'), + container.envType.fromFieldPath('POD_NAME', 'metadata.name'), ]), local daemonSet = $.extensions.v1beta1.daemonSet, - local downwardApiMount(name, path, volumeMountMixin={}) = - local container = $.core.v1.container, - deployment = $.extensions.v1beta1.deployment, - volumeMount = $.core.v1.volumeMount, - volume = $.core.v1.volume, - addMount(c) = c + container.withVolumeMountsMixin( - volumeMount.new(name, path) + - volumeMountMixin, - ); - - deployment.mapContainers(addMount) + - deployment.mixin.spec.template.spec.withVolumesMixin([ - volume.withName(name) + - volume.mixin.downwardApi.withItems([ - { - path: "name", - fieldRef: { fieldPath: "metadata.name" }, - }, - ]), - ]), - loki_canary_daemonset: - daemonSet.new('loki-canary', [$.loki_canary_container]) + - downwardApiMount('pod-name', '/etc/loki-canary'), + daemonSet.new('loki-canary', [$.loki_canary_container]), } \ No newline at end of file diff --git a/tools/image-tag b/tools/image-tag new file mode 100755 index 0000000000000000000000000000000000000000..31f023dac0e82264b00b70918daf55d5431de2ed --- /dev/null +++ b/tools/image-tag @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +WORKING_SUFFIX=$(if git status --porcelain | grep -qE '^(?:[^?][^ ]|[^ ][^?])\s'; then echo "-WIP"; else echo ""; fi) +BRANCH_PREFIX=$(git rev-parse --abbrev-ref HEAD) +echo "${BRANCH_PREFIX//\//-}-$(git rev-parse --short HEAD)$WORKING_SUFFIX"