Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support consul service meta data #1

Merged
merged 4 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# ^ added for shellcheck and file-type detection

# Watch & reload direnv on change
cd devshell || exit
watch_file devshell.toml

if [[ $(type -t use_flake) != function ]]; then
echo "ERROR: use_flake function missing."
echo "Please update direnv to v2.30.0 or later."
exit 1
fi
use flake
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ cover.html
# IDE files
.idea
*.iml

# Nix files
.direnv
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
FROM golang:1.18
FROM golang:1.19

ENV CONSUL_VERSION=1.11.4
ENV CONSUL_VERSION=1.13.3

RUN apt-get update \
&& apt-get install -y unzip \
&& go install honnef.co/go/tools/cmd/staticcheck@latest


RUN export CONSUL_CHECKSUM=5155f6a3b7ff14d3671b0516f6b7310530b509a2b882b95b4fdf25f4219342c8 \
RUN export CONSUL_CHECKSUM=5370b0b5bf765530e28cb80f90dcb47bd7d6ba78176c1ab2430f56e460ed279c \
&& export archive=consul_${CONSUL_VERSION}_linux_amd64.zip \
&& curl -Lso /tmp/${archive} https://releases.hashicorp.com/consul/${CONSUL_VERSION}/${archive} \
&& echo "${CONSUL_CHECKSUM} /tmp/${archive}" | sha256sum -c \
Expand Down
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/flynn/json5"
Expand Down Expand Up @@ -79,7 +79,7 @@ func RenderConfig(configFlag, renderFlag string) error {
fmt.Printf("%s", renderedConfig)
} else {
var err error
if err = ioutil.WriteFile(renderFlag, renderedConfig, 0644); err != nil {
if err = os.WriteFile(renderFlag, renderedConfig, 0644); err != nil {
return fmt.Errorf("could not write config file: %s", err)
}
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func loadConfigFile(configFlag string) ([]byte, error) {
if configFlag == "" {
return nil, errors.New("-config flag is required")
}
data, err := ioutil.ReadFile(configFlag)
data, err := os.ReadFile(configFlag)
if err != nil {
return nil, fmt.Errorf("could not read config file: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -33,6 +32,7 @@ func TestValidConfigJobs(t *testing.T) {
assert.Equal(job0.Port, 8080, "config for job0.Port")
assert.Equal(job0.Exec, "/bin/serviceA", "config for job0.Exec")
assert.Equal(job0.Tags, []string{"tag1", "tag2"}, "config for job0.Tags")
assert.Equal(job0.Meta, map[string]string{"keyA": "A"}, "config for job0.Meta")
assert.Equal(job0.Restarts, nil, "config for job1.Restarts")

job1 := cfg.Jobs[1]
Expand Down Expand Up @@ -184,8 +184,8 @@ func TestRenderConfigFileStdout(t *testing.T) {
temp.Close()
os.Stdout = old

renderedOut, _ := ioutil.ReadFile(fname)
renderedFile, _ := ioutil.ReadFile("testJSON.json")
renderedOut, _ := os.ReadFile(fname)
renderedFile, _ := os.ReadFile("testJSON.json")
if string(renderedOut) != string(renderedFile) {
t.Fatalf("expected the rendered file and stdout to be identical")
}
Expand Down
3 changes: 1 addition & 2 deletions config/logger/logging_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package logger

import (
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -84,7 +83,7 @@ func TestFileLogger(t *testing.T) {
// write a log message
logMsg := "this is a test"
logrus.Info(logMsg)
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
t.Errorf("Did not expect error: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion config/testdata/test.json5
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
interval: 19,
ttl: 30,
},
tags: ["tag1","tag2"]
tags: ["tag1","tag2"],
meta: {
keyA: "A",
}
},
{
name: "serviceB",
Expand Down
5 changes: 2 additions & 3 deletions control/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -56,7 +55,7 @@ func (pw PostHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// process. Returns empty response or HTTP422.
func (e Endpoints) PutEnviron(r *http.Request) (interface{}, int) {
var postEnv map[string]string
jsonBlob, err := ioutil.ReadAll(r.Body)
jsonBlob, err := io.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
return nil, http.StatusUnprocessableEntity
Expand Down Expand Up @@ -110,7 +109,7 @@ func (e Endpoints) PostDisableMaintenanceMode(r *http.Request) (interface{}, int
// Returns empty response or HTTP422.
func (e Endpoints) PostMetric(r *http.Request) (interface{}, int) {
var postMetrics map[string]interface{}
jsonBlob, err := ioutil.ReadAll(r.Body)
jsonBlob, err := io.ReadAll(r.Body)

defer r.Body.Close()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions control/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package control
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestPostHandler(t *testing.T) {
ph.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
status := resp.StatusCode
return status, string(body)
}
Expand Down
3 changes: 1 addition & 2 deletions core/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -152,7 +151,7 @@ func TestReloadConfig(t *testing.T) {
// write the configuration to a tempfile. caller is responsible
// for calling 'defer os.Remove(f.Name())' when done
func testCfgToTempFile(t *testing.T, text string) *os.File {
f, err := ioutil.TempFile(".", "test-")
f, err := os.CreateTemp(".", "test-")
if err != nil {
t.Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions devshell/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.direnv/
4 changes: 4 additions & 0 deletions devshell/devshell.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# https://numtide.github.io/devshell
[[commands]]
package = "devshell.cli"
help = "Per project developer environments"
92 changes: 92 additions & 0 deletions devshell/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions devshell/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
description = "virtual environments";

inputs.devshell.url = "github:numtide/devshell";
inputs.flake-utils.url = "github:numtide/flake-utils";

outputs = { self, flake-utils, devshell, nixpkgs }:
flake-utils.lib.eachDefaultSystem (system: {
devShell =
let pkgs = import nixpkgs {
inherit system;

overlays = [ devshell.overlay ];
};
in
pkgs.devshell.mkShell {
imports = [ (pkgs.devshell.importTOML ./devshell.toml) ];
devshell.packages = with pkgs; [
go
gnumake
];
};
});
}
39 changes: 39 additions & 0 deletions discovery/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestWithConsul(t *testing.T) {
t.Run("TestConsulReregister", testConsulReregister(testServer))
t.Run("TestConsulCheckForChanges", testConsulCheckForChanges(testServer))
t.Run("TestConsulEnableTagOverride", testConsulEnableTagOverride(testServer))
t.Run("testConsulTagsMeta", testConsulTagsMeta(testServer))
}

func testConsulTTLPass(testServer *TestServer) func(*testing.T) {
Expand Down Expand Up @@ -146,6 +147,39 @@ func testConsulReregister(testServer *TestServer) func(*testing.T) {
}
}

func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}

return false
}

func testConsulTagsMeta(testServer *TestServer) func(*testing.T) {
return func(t *testing.T) {
consul, _ := NewConsul(testServer.HTTPAddr)
name := "TestConsulReregister"
service := generateServiceDefinition(name, consul)
id := service.ID

service.SendHeartbeat() // force registration and 1st heartbeat
services, _ := consul.Agent().Services()
svc := services[id]
if !contains(svc.Tags, "a") || !contains(svc.Tags, "b") {
t.Fatalf("first tag must containt a & b but is %s", svc.Tags)
}
if svc.Meta["keyA"] != "A" {
t.Fatalf("first meta must containt keyA:A but is %s", svc.Meta["keyA"])
}
if svc.Meta["keyB"] != "B" {
t.Fatalf("first meta must containt keyB:B but is %s", svc.Meta["keyB"])
}

}
}

func testConsulCheckForChanges(testServer *TestServer) func(*testing.T) {
return func(t *testing.T) {
backend := "TestConsulCheckForChanges"
Expand Down Expand Up @@ -211,5 +245,10 @@ func generateServiceDefinition(serviceName string, consul *Consul) *ServiceDefin
TTL: 5,
Port: 9000,
Consul: consul,
Tags: []string{"a", "b"},
Meta: map[string]string{
"keyA": "A",
"keyB": "B",
},
}
}
2 changes: 2 additions & 0 deletions discovery/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ServiceDefinition struct {
Port int
TTL int
Tags []string
Meta map[string]string
InitialStatus string
IPAddress string
EnableTagOverride bool
Expand Down Expand Up @@ -93,6 +94,7 @@ func (service *ServiceDefinition) registerService(status string) error {
ID: service.ID,
Name: service.Name,
Tags: service.Tags,
Meta: service.Meta,
Port: service.Port,
Address: service.IPAddress,
EnableTagOverride: service.EnableTagOverride,
Expand Down
4 changes: 4 additions & 0 deletions docs/30-configuration/32-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ The following is a completed example of the JSON5 file configuration schema, wit
"app",
"prod"
],
meta: {
keyA: "A",
keyB: "B",
},
interfaces: [
"eth0",
"eth1[1]",
Expand Down
Loading