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

nomad provider #296

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 3 additions & 0 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ require (
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/cronexpr v1.1.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.4.0 // indirect
Expand All @@ -61,6 +63,7 @@ require (
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/nomad/api v0.0.0-20240530092428-bf11e39ac86f // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/hashicorp/vault/api v1.9.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/consul/api v1.19.1 h1:GLeK1WD4VIRvt4wRhQKHFudztEkRb8pDs+uRiJgNwes=
github.com/hashicorp/consul/api v1.19.1/go.mod h1:jAt316eYgWGNLJtxkMQrcqRpuDE/kFJdqkEFwRXFv8U=
github.com/hashicorp/consul/sdk v0.13.1 h1:EygWVWWMczTzXGpO93awkHFzfUka6hLYJ0qhETd+6lY=
github.com/hashicorp/cronexpr v1.1.2 h1:wG/ZYIKT+RT3QkOdgYc+xsKWVRgnxJ1OJtjjy84fJ9A=
github.com/hashicorp/cronexpr v1.1.2/go.mod h1:P4wA0KBl9C5q2hABiMO7cp6jcIg96CDh1Efb3g1PWA4=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -141,6 +145,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.4/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/YAJqrc=
github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR/prTM=
github.com/hashicorp/memberlist v0.5.0/go.mod h1:yvyXLpo0QaGE59Y7hDTsTzDD25JYBZ4mHgHUZ8lrOI0=
github.com/hashicorp/nomad/api v0.0.0-20240530092428-bf11e39ac86f h1:6KRhsavCBIW8BLy2ISi1I12ftdiZwNBaieL9sK9DLjs=
github.com/hashicorp/nomad/api v0.0.0-20240530092428-bf11e39ac86f/go.mod h1:svtxn6QnrQ69P23VvIWMR34tg3vmwLz4UdUzm1dSCgE=
github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY=
github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4=
github.com/hashicorp/vault/api v1.9.0 h1:ab7dI6W8DuCY7yCU8blo0UCYl2oHre/dloCmzMWg9w8=
Expand Down
25 changes: 25 additions & 0 deletions examples/read-nomad/httpecho.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
job "httpecho" {
datacenters = ["dc1"]

group "echo" {
count = 5
network {
port "http" {}
}

task "server" {
driver = "docker"

config {
image = "hashicorp/http-echo:latest"

ports = ["http"]

args = [
"-listen", ":${NOMAD_PORT_http}",
"-text", "Hello and welcome to ${NOMAD_IP_http} running on port ${NOMAD_PORT_http}",
]
}
}
}
}
282 changes: 282 additions & 0 deletions examples/read-nomad/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
package main

import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"strconv"

"github.com/hashicorp/nomad/api"
"github.com/knadh/koanf/providers/nomad/v2"

Check failure on line 12 in examples/read-nomad/main.go

View workflow job for this annotation

GitHub Actions / Go 1.18 Tests

no required module provides package github.com/knadh/koanf/providers/nomad/v2; to add it:

Check failure on line 12 in examples/read-nomad/main.go

View workflow job for this annotation

GitHub Actions / Go 1.19 Tests

no required module provides package github.com/knadh/koanf/providers/nomad/v2; to add it:

Check failure on line 12 in examples/read-nomad/main.go

View workflow job for this annotation

GitHub Actions / Go 1.20 Tests

no required module provides package github.com/knadh/koanf/providers/nomad/v2; to add it:

Check failure on line 12 in examples/read-nomad/main.go

View workflow job for this annotation

GitHub Actions / Go 1.21 Tests

no required module provides package github.com/knadh/koanf/providers/nomad/v2; to add it:
"github.com/knadh/koanf/v2"
)

// Example and test
// Requirements:
//
// - Docker installed
// - Docker daemon running
// - Nomad installed
// - Nomad running
// (sudo nomad agent -dev)
// - Nomad echo project running
// (nomad run httpecho.hcl)
// - curl
func main() {
fmt.Printf("Checking...\n")
var k = koanf.New(".")
var k2 = koanf.New(".")
var k3 = koanf.New(".")

// allocs
out, err := exec.Command("curl", "http://localhost:4646/v1/allocations").Output()
if err != nil {
log.Fatalf("error retreiving allocs from curl: %v\n", err)
}

var aListStub = []api.AllocationListStub{}
if err = json.Unmarshal(out, &aListStub); err != nil {
log.Fatalf("error unmarshalling alloc list: %s\n", err)
}

nAllocs := len(aListStub)
allocsHTTPData := make([]api.Allocation, nAllocs)

for i := 0; i < nAllocs; i++ {
cmdPath := fmt.Sprintf("http://localhost:4646/v1/allocation/%s", aListStub[i].ID)
out, err := exec.Command("curl", cmdPath).Output()
if err != nil {
log.Fatalf("error retrieving alloc with ID = %s: %s\n", aListStub[i].ID, err)
}

json.Unmarshal(out, &allocsHTTPData[i])
}

nmdAllocs, err := nomad.Provider(nil, "allocs")
if err != nil {
log.Fatalf("error creating provider: %v\n", err)
}

if err = k.Load(nmdAllocs, nil); err != nil {
log.Fatalf("error loading allocs: %v\n", err)
}

// allocs: checking
for i1 := 0; i1 < nAllocs; i1++ {
ID := allocsHTTPData[i1].ID

var key string
var flagFound bool = false

// finding alloc with this ID
for i2 := 0; i2 < nAllocs; i2++ {
key = "echo" + strconv.Itoa(i2)
if k.String(key+".ID") == ID {
flagFound = true
break
}
}

if !flagFound {
log.Fatalf("Alloc ID not found: test failed\n")
}

// comparing all data
if k.String(key+".Namespace") != allocsHTTPData[i1].Namespace {
fmt.Printf("Alloc Namespace: test failed\n")
os.Exit(1)
}

if k.String(key+".EvalID") != allocsHTTPData[i1].EvalID {
fmt.Printf("Alloc EvalID: test failed\n")
os.Exit(1)
}

if k.String(key+".Name") != allocsHTTPData[i1].Name {
fmt.Printf("Alloc Name: test failed\n")
os.Exit(1)
}

if k.String(key+".NodeID") != allocsHTTPData[i1].NodeID {
fmt.Printf("Alloc NodeID: test failed\n")
os.Exit(1)
}

if k.String(key+".NodeName") != allocsHTTPData[i1].NodeName {
fmt.Printf("Alloc NodeName: test failed\n")
os.Exit(1)
}

if k.String(key+".JobID") != allocsHTTPData[i1].JobID {
fmt.Printf("Alloc JobID: test failed\n")
os.Exit(1)
}

if k.String(key+".TaskGroup") != allocsHTTPData[i1].TaskGroup {
fmt.Printf("Alloc TaskGroup: test failed\n")
os.Exit(1)
}

if k.String(key+".DesiredStatus") != allocsHTTPData[i1].DesiredStatus {
fmt.Printf("Alloc DesiredStatus: test failed\n")
os.Exit(1)
}

if k.String(key+".ClientStatus") != allocsHTTPData[i1].ClientStatus {
fmt.Printf("Alloc ClientStatus: test failed\n")
os.Exit(1)
}

if k.String(key+".DeploymentID") != allocsHTTPData[i1].DeploymentID {
fmt.Printf("Alloc DeploymentID: test failed\n")
os.Exit(1)
}

if k.String(key+".DeploymentID") == allocsHTTPData[i1].ID {
fmt.Printf("Alloc DeploymentID = ID: test failed\n")
os.Exit(1)
}

// network comparison
for k1 := 0; k1 < len(allocsHTTPData[i1].Resources.Networks); k1++ {
ipSearch := allocsHTTPData[i1].Resources.Networks[k1].IP

// ipSearch in koanf
// koanf key
var keyNetwork string
for k2 := 0; k2 < len(allocsHTTPData[i1].Resources.Networks); k2++ {
keyNetwork = key + ".Network" + strconv.Itoa(k1) + ".IP"
if ipSearch == k.String(keyNetwork) {
break
}
}

// port search
for k2 := 0; k2 < len(allocsHTTPData[i1].Resources.Networks[k1].DynamicPorts); k2++ {
flagPort := false
for k3 := 0; k3 <= k2; k3++ {
keyPort := key + ".Network" + strconv.Itoa(k1) + ".Port" + strconv.Itoa(k3)
if k.Int(keyPort) == allocsHTTPData[i1].Resources.Networks[k1].DynamicPorts[k2].Value {
flagPort = true
break
}
}

if flagPort {
flagPort = false
} else {
fmt.Printf(
"Alloc ports: test failed, port %d isn't found\n",
allocsHTTPData[i1].Resources.Networks[k1].DynamicPorts[k2].Value)
os.Exit(1)
}
}
}
}

fmt.Printf("Allocations: test passed\n")

// raft
out, err = exec.Command("curl", "http://localhost:4646/v1/operator/raft/configuration").Output()
if err != nil {
log.Fatalf("error retreiving raft configuration from curl: %v\n", err)
}

raftCfg := api.RaftConfiguration{}
if err = json.Unmarshal(out, &raftCfg); err != nil {
log.Fatalf("error unmarshalling raft configuration: %s\n", err)
}

nmdRaft, err := nomad.Provider(nil, "raft")
if err != nil {
log.Fatalf("error creating provider: %v\n", err)
}

if err = k2.Load(nmdRaft, nil); err != nil {
log.Fatalf("error loading allocs: %v\n", err)
}

nServers := len(raftCfg.Servers)

// raft: checking
for i1 := 0; i1 < nServers; i1++ {
ID := raftCfg.Servers[i1].ID

var key string
var flagFound bool = false

// finding alloc with this ID
for i2 := 0; i2 < nServers; i2++ {
key = "server" + strconv.Itoa(i2)
if k2.String(key+".ID") == ID {
flagFound = true
break
}
}

if !flagFound {
log.Fatalf("Raft server ID not found: test failed")
}

if k2.String(key+".Node") != raftCfg.Servers[i1].Node {
log.Fatalf("Raft server node: test failed")
}

if k2.String(key+".Address") != raftCfg.Servers[i1].Address {
log.Fatalf("Raft server address: test failed")
}

if k2.Bool(key+".Leader") != raftCfg.Servers[i1].Leader {
log.Fatalf("Raft server leader: test failed")
}

if k2.Bool(key+".Voter") != raftCfg.Servers[i1].Voter {
log.Fatalf("Raft server voter: test failed")
}

if k2.String(key+".RaftProtocol") != raftCfg.Servers[i1].RaftProtocol {
log.Fatalf("Raft server protocol: test failed")
}
}

fmt.Printf("Raft configuration: test passed\n")

// vars
_, err = exec.Command(
"curl",
"--header",
"Content-Type: application/json",
"--request",
"POST",
"--data",
`{"Path": "databases/sql", "Items": {"mysql": "75cp21", "postgresql": "52pg24"}}`,
"http://localhost:4646/v1/var/databases/sql",
).Output()
if err != nil {
log.Fatalf("Couldn't create variables: %v\n", err)
}

nmdVars, err := nomad.Provider(nil, "vars")
if err != nil {
log.Fatalf("error creating provider: %v\n", err)
}

if err = k3.Load(nmdVars, nil); err != nil {
log.Fatalf("error loading vars: %v\n", err)
}

// vars: checking
if k3.String("databases/sql.mysql") != "75cp21" {
log.Fatalf("Vars: test failed\n")
}

if k3.String("databases/sql.postgresql") != "52pg24" {
log.Fatalf("Vars: test failed\n")
}

fmt.Printf("Vars: test passed\n")

fmt.Printf("ALL TESTS PASSED\n")
}
8 changes: 3 additions & 5 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,30 +125,28 @@ github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cncf/udpa/go v0.0.0-20220112060539-c52dc94e7fbe/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI=
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/envoyproxy/go-control-plane v0.11.1-0.20230524094728-9239064ad72f/go.mod h1:sfYdkwUW4BA3PbKjySwjJy+O4Pu0h62rlqCMHNk+K+Q=
github.com/envoyproxy/protoc-gen-validate v0.10.1/go.mod h1:DRjgyB0I43LtJapqN6NiRwroiAU2PaFuvk/vjgh61ss=
github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/golang/glog v1.1.0/go.mod h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/sdk v0.13.1/go.mod h1:SW/mM4LbKfqmMvcFu8v+eiQQ7oitXEFeiBe9StxERb0=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/oauth2 v0.7.0/go.mod h1:hPLQkd9LyjfXTiRohC/41GhcFqxisoUQ99sCUOHO9x4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down
Loading
Loading