From 34bb9db758807a7cf577a865c488a07949867927 Mon Sep 17 00:00:00 2001 From: Edward Welch <edward.welch@grafana.com> Date: Tue, 14 May 2019 11:02:40 -0400 Subject: [PATCH] using an environment variable passed into an arg instead of reading the pod name from a file and the downardApi added a makefile --- .gitignore | 1 + Makefile | 25 +++++++++++++++ cmd/loki-canary/main.go | 18 +++-------- .../ksonnet/loki-canary/loki-canary.libsonnet | 31 ++++--------------- tools/image-tag | 9 ++++++ 5 files changed, 45 insertions(+), 39 deletions(-) create mode 100644 Makefile create mode 100755 tools/image-tag diff --git a/.gitignore b/.gitignore index e69de29b..eae84e7a 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 00000000..8e2476fb --- /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 442fcc39..dc609427 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 195df2aa..b5a6bf65 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 00000000..31f023da --- /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" -- GitLab