API and a set of tools to manage Netbox configuration declaratively

declarative-netbox

An experimental project to explore the idea of managing the Netbox state via declarative API. This repo contains the following:

  1. Inside the ./netbox directory -- a Go library implementing declarative management of Netbox devices, built upon the netbox-community/go-netbox package.
  2. Inside the ./cmd directory -- a command-line application nbctl (similar to kubectl) that can retrieve, apply and delete Netbox objects (not relying on k8s cluster).
  3. Inside the ./controllers directory -- a Kubernetes controller that automates the management of Netbox objects via Kubernetes CRDs.

All of the above applications use a common API defined in the ./api directory.

Quick Start

Prerequisites

  1. Prepare the test environment

Build a Kubernetes cluster that will host our controller and the Netbox deployment

kind create cluster --config hack/kind.yaml

Deploy Netbox inside this cluster

make netbox

Expose Netbox on localhost:32178

kubectl apply -f hack/nodeport.yml
  1. Pre-seed Netbox with static configuration data

We will define site and a couple of device roles and types

kubectl exec -it deploy/k8s-netbox bash
source /opt/netbox/venv/bin/activate
/opt/netbox/netbox/manage.py nbshell

Site(name="CITC", status="active").save()

DeviceRole(name="leaf", slug="leaf").save()
DeviceRole(name="spine", slug="spine").save()

Manufacturer(name="nvidia").save()
m = Manufacturer.objects.get(name="nvidia")


DeviceType(model="SN3420", slug="SN3420", manufacturer=m).save()
DeviceType(model="SN3700", slug="SN3700", manufacturer=m).save()

The nbcli UX walkthrough

Build the nbcli binary

make build-cli

Authenticate against a Netbox instance (these details will be stored in ~/.netbox/config)

./bin/nbctl login  http://localhost:32178 0123456789abcdef0123456789abcdef01234567

Get the current list of devices

./bin/nbctl get device
+------+----+------+------+------+
| NAME | ID | TYPE | ROLE | SITE |
+------+----+------+------+------+
+------+----+------+------+------+

Apply the two new devices from ./config/samples/device_create.yml

 ./bin/nbctl apply -f config/samples/device_create.yml

Check the the devices are there (this check can also be done via the web UI)

./bin/nbctl get device
+----------+----+--------+-------+------+
| NAME     | ID | TYPE   | ROLE  | SITE |
+----------+----+--------+-------+------+
| leaf-99  |  1 | SN3420 | leaf  | CITC |
| spine-01 |  2 | SN3700 | spine | CITC |
+----------+----+--------+-------+------+

Optionally, you can apply a -oyaml flag and output those devices in the original YAML format:

./bin/nbctl get device leaf-99 -oyaml
apiVersion: netbox.networkop.co.uk/v1
kind: Device
metadata:
  creationTimestamp: null
  name: leaf-99
spec:
  device_type: SN3420
  role: leaf
  site: CITC
status:
  id: 1
  state: Ready

Apply the new change from ./config/samples/device_update.yml (swapped device type)

./bin/nbctl apply -f config/samples/device_update.yml
./bin/nbctl get device
+----------+----+--------+-------+------+
| NAME     | ID | TYPE   | ROLE  | SITE |
+----------+----+--------+-------+------+
| leaf-99  |  1 | SN3700 | leaf  | CITC |
| spine-01 |  2 | SN3420 | spine | CITC |
+----------+----+--------+-------+------+

Delete all devices

./bin/nbctl delete -f config/samples/device_update.yml
./bin/nbctl get device
+------+----+------+------+------+
| NAME | ID | TYPE | ROLE | SITE |
+------+----+------+------+------+
+------+----+------+------+------+

The Kubernetes controller walkthrough

Install CRDs generated from the API

make install

Deploy the Netbox controller

make deploy

Wait for the controller to transition to ready state

kubectl get deployments.apps -n declarative-netbox-system
NAME                                    READY   UP-TO-DATE   AVAILABLE   AGE
declarative-netbox-controller-manager   1/1     1            1           47s

Apply the device configuration (this is the same YAML that we used in CLI tool)

kubectl apply -f config/samples/device_create.yml
device.netbox.networkop.co.uk/leaf-99 created
device.netbox.networkop.co.uk/spine-01 created

Check the configured devices

kubectl get device
NAME       ID    SITE   TYPE     ROLE
leaf-99    3     CITC   SN3420   leaf
spine-01   4     CITC   SN3700   spine

Update the device configuration

kubectl apply -f config/samples/device_update.yml
device.netbox.networkop.co.uk/leaf-99 configured
device.netbox.networkop.co.uk/spine-01 configured

View details of individual devices

kubectl get device leaf-99 -oyaml                                                                                                    ⎈ kind-kind
apiVersion: netbox.networkop.co.uk/v1
kind: Device
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"netbox.networkop.co.uk/v1","kind":"Device","metadata":{"annotations":{},"name":"leaf-99","namespace":"default"},"spec":{"device_type":"SN3700","role":"leaf","site":"CITC"}}
  creationTimestamp: "2021-12-24T09:51:46Z"
  finalizers:
  - finalizers.netbox.networkop.co.uk
  generation: 2
  name: leaf-99
  namespace: default
  resourceVersion: "4191"
  uid: dab8c5c4-66c8-4a1c-84fb-73a2bf686dc8
spec:
  device_type: SN3700
  role: leaf
  site: CITC
status:
  id: 3
  observedGeneration: 2
  state: Ready

Delete configured devices

kubectl delete -f config/samples/device_update.yml
Owner
Michael Kashin
Michael Kashin
Michael Kashin
Similar Resources

GNU-stow replacement to manage my dotfiles

Basically a replacement to GNU Stow for my use cases: TODO package for Homebrew add --dotfiles true|false flag add --script filename.sh flag that prin

Nov 27, 2022

Gosfdc module - a collection of packages containing the data structures from the various Salesforce APIs and Tools

Gosfdc module - a collection of packages containing the data structures from the various Salesforce APIs and Tools

Jan 21, 2022

Various Dungeons and Dragons Tools. Written in go as an aid to learning the language.

dnd_tools Various Dungeons and Dragons Tools. Written in go as an aid to learning the language. Some tools are generic, while others will target eithe

Jan 28, 2022

The phylosophy behind readyGo is "A Simple configuration should give a working project.".

The phylosophy behind readyGo is

The phylosophy behind readyGo is "A Simple configuration should give a working project.". readyGo is a command line interface( probably the name of re

Oct 30, 2021

Configuration agent for BFE control plane

conf-agent conf-agent 说明 conf-agent 从 api-server 获取最新的配置并触发bfe热加载。 获取方式 获取 conf-agent 工具。获取 conf-agent 有多种方式: 在 releases 页面下载对应平台的可执行文件 通过 go get 工具本地

Oct 28, 2022

Esfmt - An opinionated, zero-configuration formatter for ES/TS/ESX/TSX

esfmt - an opinionated, zero-configuration formatter for ES/TS/ESX/TSX Status: t

Apr 5, 2022

This Go based project of Aadhyarupam Innovators demonstrate the code examples for building microservices, integration with cloud services (Google Cloud Firestore), application configuration management (Viper) etc.

This Go based project of Aadhyarupam Innovators demonstrate the code examples for building microservices, integration with cloud services (Google Cloud Firestore), application configuration management (Viper) etc.

Dec 22, 2022

frida debug tools

frida debug tools

fd windows下frida的调试工具 (单 exe,不需要python和nodejs) 现在他只支持USB english click here 预编译的二进制文件在这里下载 支持列表: 创建一个基于 frida-agent-example的typescript工程(拥有完美的智能提示) 使用

Dec 30, 2022

Go package providing tools for working with Library of Congress data.

go-libraryofcongress Go package providing tools for working with Library of Congress data. Documentation Tools $ make cli go build -mod vendor -o bin

Jan 3, 2023
Set of scripts & tools for converting between numbers and major system encoded words.
Set of scripts & tools for converting between numbers and major system encoded words.

major-system-converter Set of scripts & tools for converting between numbers and major system encoded words. Uses phonetics instead of letters to conv

Aug 9, 2022
set and get github user statuses

gh user-status being an extension for interacting with the status on a GitHub profile. gh user-status set gh user-status set interactively set status

Nov 19, 2022
Tanzu Framework provides a set of building blocks to build atop of the Tanzu platform and leverages Carvel packaging

Tanzu Framework provides a set of building blocks to build atop of the Tanzu platform and leverages Carvel packaging and plugins to provide users with a much stronger, more integrated experience than the loose coupling and stand-alone commands of the previous generation of tools.

Dec 16, 2022
The new home of the CUE language! Validate and define text-based and dynamic configuration

The CUE Data Constraint Language Configure, Unify, Execute CUE is an open source data constraint language which aims to simplify tasks involving defin

Dec 31, 2022
Set of reusable components for Golang projects

go-utils Table of contents go-utils Table of contents 1. Overview 2. Install 3. Utils package 3.1 logger 3.2 error 3.3 datetime 1. Overview In my free

Sep 12, 2022
Generate FIRST/FOLLOW/PREDICT Set from BNF.

Generate FIRST/FOLLOW/PREDICT Set from BNF. We can use it to study parser theory. Feature FirstSet generate. Output pretty. FollowSet generate. Output

Oct 30, 2021
Set of functions/methods that will ease GO code generation

Set of functions/methods that will ease GO code generation

Dec 1, 2021
Bot to manage team members in GitHub organizations

Team manager Team manager is a utility that allows an organization owner to add or remove people from existing teams and / or assign people for GitHub

Oct 29, 2022
Simple application to manage basic deployments

Simple application to manage basic deployments Usage You need to create a deployment yaml file (sample). then run the binary file with --config flag t

Aug 29, 2021
Manage internal technical documentation that is enriched with live data accreted from your environment.

Manage internal technical documentation that is enriched with live data accreted from your environment.

Jan 3, 2023