Skip to content

Commit

Permalink
feat: add nix deployment flavor
Browse files Browse the repository at this point in the history
Change-Id: I58c94b3f911ae02b57af9ea3e3ff3a7b09687dc7
  • Loading branch information
fionera committed Jan 22, 2024
1 parent 300481f commit 21861fe
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 153 deletions.
54 changes: 54 additions & 0 deletions deploy/base/clickhouse/clickhouse.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package clickhouse

import (
schema "github.com/monogon-dev/NetMeta/deploy/single-node/schema"
"netmeta.monogon.tech/xml"
)

// A stripped down version of the #SamplerConfig found in deploy/single-node/config.cue
#SamplerConfig: [string]: {
device: string
samplingRate: int
anonymizeAddresses: bool
description: string
interface: [string]: {
id: int
description: string
}
vlan: [string]: {
id: int
description: string
}
host: [string]: {
device: string
description: string
}
...
}

#UserData: autnums: [string]: {
asn: int
name: string
country: string
}

#Config: {
sampler: #SamplerConfig
userData: #UserData

dataPath: string
risinfoURL: string
}

files: {
// Iterate over our required files from schema, e.g. the protobuf files
for k, v in schema.file {
"\(k)": v
}

// Iterate over all defined files in _files and generate the config files for clickhouse
for k, v in _files {
"\(k).conf": (xml.#Marshal & {in: v._cfg}).out
"\(k).tsv": v.data
}
}
89 changes: 89 additions & 0 deletions deploy/base/clickhouse/external.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package clickhouse

import (
"netmeta.monogon.tech/xml"
)

files: "risinfo.conf": (xml.#Marshal & {in: yandex: dictionary: {
name: "risinfo"
source: http: {
url: "\(#Config.risinfoURL)/rib.tsv"
format: "TabSeparated"
}
lifetime: 3600
layout: ip_trie: access_to_key_from_attributes: true
structure: key: attribute: {
name: "prefix"
type: "String"
}
structure: attribute: {
name: "asnum"
type: "UInt32"
null_value: 0
}
}}).out

files: "autnums.conf": (xml.#Marshal & {in: yandex: dictionary: {
name: "autnums"
source: clickhouse: query:
#"""
SELECT * FROM dictionaries.risinfo_autnums
UNION ALL
SELECT * FROM dictionaries.user_autnums
"""#
lifetime: 3600
layout: flat: null
structure: [{
id: name: "asnum"
}, {
attribute: {
name: "name"
type: "String"
null_value: null
}
}, {
attribute: {
name: "country"
type: "String"
null_value: null
}
}]
}}).out

files: "risinfo_autnums.conf": (xml.#Marshal & {in: yandex: dictionary: {
name: "risinfo_autnums"
source: http: {
url: "\(#Config.risinfoURL)/autnums.tsv"
format: "TabSeparated"
}
lifetime: 86400
layout: flat: null
structure: [{
id: name: "asnum"
}, {
attribute: {
name: "name"
type: "String"
null_value: null
}
}, {
attribute: {
name: "country"
type: "String"
null_value: null
}
}]
}}).out

files: "format_function.xml": (xml.#Marshal & {in: yandex: functions: {
type: "executable"
name: "formatQuery"
return_type: "String"
argument: [{
type: "String"
name: "query"
}]
format: "LineAsString"
command: "clickhouse format --oneline"
execute_direct: "0"
}}).out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package clickhouse
import (
"strings"
"strconv"
"netmeta.monogon.tech/xml"
)

// template for TSV dictionaries
Expand All @@ -20,7 +19,7 @@ _files: [NAME=string]: {
name: NAME
source: [{
file: {
path: "/etc/clickhouse-server/config.d/\(NAME).tsv"
path: "\(#Config.dataPath)/\(NAME).tsv"
format: "TSV"
}
settings: format_tsv_null_representation: "NULL"
Expand All @@ -29,17 +28,9 @@ _files: [NAME=string]: {
}
}

// Iterate over all defined files in _files and generate the config files for clickhouse
ClickHouseInstallation: netmeta: spec: configuration: files: {
for k, v in _files {
"\(k).conf": (xml.#Marshal & {in: v._cfg}).out
"\(k).tsv": v.data
}
}

// Dictionary for user-defined interface name lookup
_files: InterfaceNames: {
data: strings.Join([ for s in #Config.sampler for i in s.interface {
data: strings.Join([for s in #Config.sampler for i in s.interface {
strings.Join([s.device, "\(i.id)", i.description], "\t")
}], "\n")

Expand Down Expand Up @@ -68,7 +59,7 @@ _files: InterfaceNames: {

// Dictionary for user-defined sampler settings lookup
_files: SamplerConfig: {
data: strings.Join([ for s in #Config.sampler {
data: strings.Join([for s in #Config.sampler {
let samplingRate = [
if s.samplingRate == 0 {
"NULL"
Expand Down Expand Up @@ -119,7 +110,7 @@ _files: SamplerConfig: {

// Dictionary for user-defined vlan name lookup
_files: VlanNames: {
data: strings.Join([ for s in #Config.sampler for v in s.vlan {
data: strings.Join([for s in #Config.sampler for v in s.vlan {
strings.Join([s.device, "\(v.id)", v.description], "\t")
}], "\n")

Expand Down Expand Up @@ -148,7 +139,7 @@ _files: VlanNames: {

// Dictionary for user-defined host name lookup
_files: HostNames: {
data: strings.Join([ for s in #Config.sampler for h in s.host {
data: strings.Join([for s in #Config.sampler for h in s.host {
strings.Join([s.device, h.device, h.description], "\t")
}], "\n")

Expand Down Expand Up @@ -176,7 +167,7 @@ _files: HostNames: {
}

_files: user_autnums: {
data: strings.Join([ for _, e in #Config.userData.autnums {
data: strings.Join([for _, e in #Config.userData.autnums {
strings.Join(["\(e.asn)", e.name, e.country], "\t")
}], "\n")

Expand All @@ -199,97 +190,3 @@ _files: user_autnums: {
}]
}
}

ClickHouseInstallation: netmeta: spec: configuration: files: "risinfo.conf": (xml.#Marshal & {in: {
yandex: dictionary: {
name: "risinfo"
source: http: {
url: "http://risinfo/rib.tsv"
format: "TabSeparated"
}
lifetime: 3600
layout: ip_trie: access_to_key_from_attributes: true
structure: key: attribute: {
name: "prefix"
type: "String"
}
structure: attribute: {
name: "asnum"
type: "UInt32"
null_value: 0
}
}
}}).out

ClickHouseInstallation: netmeta: spec: configuration: files: "autnums.conf": (xml.#Marshal & {in: {
yandex: dictionary: {
name: "autnums"
source: clickhouse: {
query:
#"""
SELECT * FROM dictionaries.risinfo_autnums
UNION ALL
SELECT * FROM dictionaries.user_autnums
"""#
}
lifetime: 3600
layout: flat: null
structure: [{
id: name: "asnum"
}, {
attribute: {
name: "name"
type: "String"
null_value: null
}
}, {
attribute: {
name: "country"
type: "String"
null_value: null
}
}]
}
}}).out

ClickHouseInstallation: netmeta: spec: configuration: files: "risinfo_autnums.conf": (xml.#Marshal & {in: {
yandex: dictionary: {
name: "risinfo_autnums"
source: http: {
url: "http://risinfo/autnums.tsv"
format: "TabSeparated"
}
lifetime: 86400
layout: flat: null
structure: [{
id: name: "asnum"
}, {
attribute: {
name: "name"
type: "String"
null_value: null
}
}, {
attribute: {
name: "country"
type: "String"
null_value: null
}
}]
}
}}).out

ClickHouseInstallation: netmeta: spec: configuration: files: "format_function.xml": (xml.#Marshal & {in: {
yandex: functions: {
type: "executable"
name: "formatQuery"
return_type: "String"
argument: [{
type: "String"
name: "query"
}]
format: "LineAsString"
command: "clickhouse format --oneline"
execute_direct: "0"
}
}}).out
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package clickhouse

import (
schema "github.com/monogon-dev/NetMeta/deploy/single-node/schema"
)

// curl -s https://www.iana.org/assignments/protocol-numbers/protocol-numbers-1.csv | awk -F ',' '{ print $1 "\t" $2 }'
// plus some manual post-processing. Most of them are never seen on the wire, but doesn't hurt to have the full list.
_files: IPProtocols: cfg: {
Expand Down Expand Up @@ -252,9 +248,3 @@ _files: TCPFlags: data: #"""
64 ECE
128 CWR
"""#

ClickHouseInstallation: netmeta: spec: configuration: files: {
for k, v in schema.file {
"\(k)": v
}
}
18 changes: 18 additions & 0 deletions deploy/base/reconciler/reconciler.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package reconciler

import (
reconciler "github.com/monogon-dev/NetMeta/reconciler:main"
"encoding/json"
)

#Config: {
files: [string]: string
config: reconciler.#Config
}

files: {
for name, content in #Config.files {
"\(name)": content
}
"config.json": json.Marshal(#Config.config)
}
Loading

0 comments on commit 21861fe

Please sign in to comment.