Prepare for GitHub migration (#24)
Moves node-lookup to IUCLabs/node-lookup on GitHub; it must carry no internal hostnames or Woodpecker release. - Loads PuppetDB, Puppetboard and domain from config (flags > env > ~/.config > /etc) for all 3 tools; errors name the config paths - Ships packaging/config.example.yaml as an RPM doc - Rewrites .goreleaser.yaml: raw binaries + amd64 RPM with completions, signed checksums - Adds GitHub CI/release workflows, CODEOWNERS, zizmor config, README - Makes patch/minor/major draft a GitHub release; make rpm runs a goreleaser snapshot - Removes the Woodpecker release pipeline, nfpm.yaml and build-rpm.sh Reviewed-on: #24 Co-authored-by: unkin-agent <unkin-agent@unkin.net> Co-committed-by: unkin-agent <unkin-agent@unkin.net>
This commit was merged in pull request #24.
This commit is contained in:
+70
-38
@@ -1,48 +1,62 @@
|
||||
// Package puppet holds the small pieces of PuppetDB/Puppetboard plumbing shared
|
||||
// by the node-lookup companion tools (pburl, pblastreport): config loading,
|
||||
// by node-lookup and its companion tools (pburl, pblastreport): config loading,
|
||||
// PuppetDB "nodes" queries, Puppetboard URL construction, and reading hostnames
|
||||
// from piped node-lookup output.
|
||||
//
|
||||
// It intentionally reads the SAME config file, env vars, and defaults as the
|
||||
// node-lookup CLI so a single `~/.config/node-lookup/config.yaml` configures
|
||||
// every tool in the family.
|
||||
// Every tool reads the same config files and env vars, so one config file
|
||||
// configures the whole family.
|
||||
package puppet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultPuppetDBURL is the PuppetDB v4 facts endpoint (shared with node-lookup).
|
||||
DefaultPuppetDBURL = "http://puppetdbapi.service.consul:8080/pdb/query/v4/facts"
|
||||
// DefaultRoleFact is the role fact node-lookup queries with -R.
|
||||
DefaultRoleFact = "enc_role"
|
||||
// DefaultPuppetboardURL is the base URL of the Puppetboard web UI.
|
||||
DefaultPuppetboardURL = "https://puppetboard.k8s.syd1.au.unkin.net"
|
||||
|
||||
appName = "node-lookup"
|
||||
configFileName = "config.yaml"
|
||||
)
|
||||
|
||||
// Config mirrors node-lookup's config plus the puppetboard_url key used by the
|
||||
// companion tools. Fields map 1:1 to config file keys and env vars.
|
||||
// SystemConfigPath is the system-wide config file, read before the user one.
|
||||
var SystemConfigPath = filepath.Join("/etc", appName, configFileName)
|
||||
|
||||
// Config is shared by all tools. Fields map 1:1 to config file keys and
|
||||
// NODE_LOOKUP_* env vars.
|
||||
type Config struct {
|
||||
PuppetDBURL string `yaml:"puppetdb_url"`
|
||||
RoleFact string `yaml:"role_fact"`
|
||||
PuppetboardURL string `yaml:"puppetboard_url"`
|
||||
// Domain is appended to short (dotless) node names; empty disables that.
|
||||
Domain string `yaml:"domain"`
|
||||
}
|
||||
|
||||
// DefaultConfig returns the built-in defaults.
|
||||
func DefaultConfig() Config {
|
||||
return Config{
|
||||
PuppetDBURL: DefaultPuppetDBURL,
|
||||
RoleFact: DefaultRoleFact,
|
||||
PuppetboardURL: DefaultPuppetboardURL,
|
||||
// envVars maps each config key to its env var.
|
||||
var envVars = map[string]string{
|
||||
"puppetdb_url": "NODE_LOOKUP_URL",
|
||||
"role_fact": "NODE_LOOKUP_ROLE_FACT",
|
||||
"puppetboard_url": "NODE_LOOKUP_PUPPETBOARD_URL",
|
||||
"domain": "NODE_LOOKUP_DOMAIN",
|
||||
}
|
||||
|
||||
func (c *Config) field(key string) *string {
|
||||
switch key {
|
||||
case "puppetdb_url":
|
||||
return &c.PuppetDBURL
|
||||
case "role_fact":
|
||||
return &c.RoleFact
|
||||
case "puppetboard_url":
|
||||
return &c.PuppetboardURL
|
||||
case "domain":
|
||||
return &c.Domain
|
||||
}
|
||||
panic("unknown config key " + key)
|
||||
}
|
||||
|
||||
// ConfigDir returns the XDG_CONFIG_HOME/node-lookup directory.
|
||||
@@ -55,36 +69,54 @@ func ConfigDir() string {
|
||||
return filepath.Join(base, appName)
|
||||
}
|
||||
|
||||
// ConfigPath returns the full path to the shared config file.
|
||||
// ConfigPath returns the full path to the user config file.
|
||||
func ConfigPath() string {
|
||||
return filepath.Join(ConfigDir(), configFileName)
|
||||
}
|
||||
|
||||
// Load reads the config file (if present), then applies env var overrides.
|
||||
// Precedence (lowest → highest): defaults < config file < env vars.
|
||||
// Load merges, lowest precedence first: the built-in role fact, the system
|
||||
// config file, the user config file, then NODE_LOOKUP_* env vars; empty values
|
||||
// never override. Flags are
|
||||
// applied by the caller, which then calls Require.
|
||||
func Load() (Config, error) {
|
||||
cfg := DefaultConfig()
|
||||
|
||||
path := ConfigPath()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return cfg, fmt.Errorf("reading config %s: %w", path, err)
|
||||
}
|
||||
if err == nil {
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
cfg := Config{RoleFact: DefaultRoleFact}
|
||||
for _, path := range []string{SystemConfigPath, ConfigPath()} {
|
||||
data, err := os.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return cfg, fmt.Errorf("reading config %s: %w", path, err)
|
||||
}
|
||||
var file Config
|
||||
if err := yaml.Unmarshal(data, &file); err != nil {
|
||||
return cfg, fmt.Errorf("parsing config %s: %w", path, err)
|
||||
}
|
||||
for key := range envVars {
|
||||
if v := *file.field(key); v != "" {
|
||||
*cfg.field(key) = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if v := os.Getenv("NODE_LOOKUP_URL"); v != "" {
|
||||
cfg.PuppetDBURL = v
|
||||
for key, env := range envVars {
|
||||
if v := os.Getenv(env); v != "" {
|
||||
*cfg.field(key) = v
|
||||
}
|
||||
}
|
||||
if v := os.Getenv("NODE_LOOKUP_ROLE_FACT"); v != "" {
|
||||
cfg.RoleFact = v
|
||||
}
|
||||
if v := os.Getenv("NODE_LOOKUP_PUPPETBOARD_URL"); v != "" {
|
||||
cfg.PuppetboardURL = v
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// Require returns an error naming every missing key and where to set it.
|
||||
func (c Config) Require(keys ...string) error {
|
||||
var missing []string
|
||||
for _, k := range keys {
|
||||
if *c.field(k) == "" {
|
||||
missing = append(missing, fmt.Sprintf("%s (env %s)", k, envVars[k]))
|
||||
}
|
||||
}
|
||||
if len(missing) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("missing config: %s; set it in %s or %s, via env, or with a flag",
|
||||
strings.Join(missing, ", "), ConfigPath(), SystemConfigPath)
|
||||
}
|
||||
|
||||
@@ -12,50 +12,106 @@ import (
|
||||
|
||||
// ---- config -----------------------------------------------------------------
|
||||
|
||||
func TestLoad_Defaults(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
t.Setenv("NODE_LOOKUP_URL", "")
|
||||
t.Setenv("NODE_LOOKUP_ROLE_FACT", "")
|
||||
t.Setenv("NODE_LOOKUP_PUPPETBOARD_URL", "")
|
||||
// isolate points both config files into a temp dir and clears the env vars.
|
||||
func isolate(t *testing.T) (system, user string) {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "home"))
|
||||
old := SystemConfigPath
|
||||
SystemConfigPath = filepath.Join(dir, "etc", configFileName)
|
||||
t.Cleanup(func() { SystemConfigPath = old })
|
||||
for _, env := range envVars {
|
||||
t.Setenv(env, "")
|
||||
}
|
||||
return SystemConfigPath, ConfigPath()
|
||||
}
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
func writeFile(t *testing.T, path, body string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.PuppetDBURL != DefaultPuppetDBURL {
|
||||
t.Fatalf("expected default puppetdb url, got %s", cfg.PuppetDBURL)
|
||||
}
|
||||
if cfg.PuppetboardURL != DefaultPuppetboardURL {
|
||||
t.Fatalf("expected default puppetboard url, got %s", cfg.PuppetboardURL)
|
||||
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_FileAndEnvOverride(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Setenv("XDG_CONFIG_HOME", dir)
|
||||
t.Setenv("NODE_LOOKUP_URL", "")
|
||||
t.Setenv("NODE_LOOKUP_ROLE_FACT", "")
|
||||
t.Setenv("NODE_LOOKUP_PUPPETBOARD_URL", "https://env.example.net")
|
||||
func TestLoad_NoConfigHasOnlyRoleFact(t *testing.T) {
|
||||
isolate(t)
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg != (Config{RoleFact: DefaultRoleFact}) {
|
||||
t.Fatalf("expected only the role fact default, got %+v", cfg)
|
||||
}
|
||||
}
|
||||
|
||||
cfgDir := filepath.Join(dir, appName)
|
||||
if err := os.MkdirAll(cfgDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := "puppetdb_url: http://file:8080/pdb/query/v4/facts\npuppetboard_url: https://file.example.net\n"
|
||||
if err := os.WriteFile(filepath.Join(cfgDir, configFileName), []byte(body), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
func TestLoad_Precedence(t *testing.T) {
|
||||
system, user := isolate(t)
|
||||
writeFile(t, system, "puppetdb_url: http://system\npuppetboard_url: https://system\ndomain: system.example.com\nrole_fact: system_role\n")
|
||||
writeFile(t, user, "puppetboard_url: https://user\ndomain: user.example.com\n")
|
||||
t.Setenv("NODE_LOOKUP_DOMAIN", "env.example.com")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.PuppetDBURL != "http://file:8080/pdb/query/v4/facts" {
|
||||
t.Fatalf("file override failed: %s", cfg.PuppetDBURL)
|
||||
want := Config{
|
||||
PuppetDBURL: "http://system", // only in the system file
|
||||
PuppetboardURL: "https://user", // user file beats system file
|
||||
Domain: "env.example.com", // env beats both files
|
||||
RoleFact: "system_role", // file beats the built-in default
|
||||
}
|
||||
// env beats the file for puppetboard_url
|
||||
if cfg.PuppetboardURL != "https://env.example.net" {
|
||||
t.Fatalf("env should beat file: %s", cfg.PuppetboardURL)
|
||||
if cfg != want {
|
||||
t.Fatalf("got %+v, want %+v", cfg, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_EmptyUserValuesKeepSystemValues(t *testing.T) {
|
||||
system, user := isolate(t)
|
||||
writeFile(t, system, "puppetdb_url: http://system\npuppetboard_url: https://system\ndomain: system.example.com\n")
|
||||
writeFile(t, user, "puppetdb_url: \"\"\npuppetboard_url: \"\"\ndomain: \"\"\nrole_fact: enc_role\n")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := Config{PuppetDBURL: "http://system", PuppetboardURL: "https://system", Domain: "system.example.com", RoleFact: DefaultRoleFact}
|
||||
if cfg != want {
|
||||
t.Fatalf("got %+v, want %+v", cfg, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_InvalidYAMLNamesPath(t *testing.T) {
|
||||
system, _ := isolate(t)
|
||||
writeFile(t, system, ":\tinvalid: yaml:\n")
|
||||
_, err := Load()
|
||||
if err == nil || !strings.Contains(err.Error(), system) {
|
||||
t.Fatalf("expected parse error naming %s, got %v", system, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequire_MissingNamesKeysAndPaths(t *testing.T) {
|
||||
system, user := isolate(t)
|
||||
err := Config{PuppetboardURL: "https://board"}.Require("puppetdb_url", "puppetboard_url", "domain")
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
msg := err.Error()
|
||||
for _, want := range []string{"puppetdb_url (env NODE_LOOKUP_URL)", "domain (env NODE_LOOKUP_DOMAIN)", system, user} {
|
||||
if !strings.Contains(msg, want) {
|
||||
t.Fatalf("error %q missing %q", msg, want)
|
||||
}
|
||||
}
|
||||
if strings.Contains(msg, "puppetboard_url") {
|
||||
t.Fatalf("set key reported missing: %q", msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequire_AllSet(t *testing.T) {
|
||||
if err := (Config{PuppetDBURL: "http://pdb"}).Require("puppetdb_url"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,9 +119,9 @@ func TestLoad_FileAndEnvOverride(t *testing.T) {
|
||||
|
||||
func TestNodesEndpoint(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"http://puppetdbapi.service.consul:8080/pdb/query/v4/facts": "http://puppetdbapi.service.consul:8080/pdb/query/v4/nodes",
|
||||
"http://h:8080/pdb/query/v4/facts/": "http://h:8080/pdb/query/v4/nodes",
|
||||
"https://h/facts": "https://h/nodes",
|
||||
"http://puppetdb.example.com:8080/pdb/query/v4/facts": "http://puppetdb.example.com:8080/pdb/query/v4/nodes",
|
||||
"http://h:8080/pdb/query/v4/facts/": "http://h:8080/pdb/query/v4/nodes",
|
||||
"https://h/facts": "https://h/nodes",
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := NodesEndpoint(in); got != want {
|
||||
|
||||
Reference in New Issue
Block a user