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

Add tests for project and refactor Makefile #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
94 changes: 88 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,99 @@
.DEFAULT_GOAL := build

$(VERBOSE).SILENT:
.DEFAULT_GOAL := _default
CUR_DIR := $(PWD)
export DOCKER_BUILDKIT=1
platform := $(shell uname -p)
checkarm := arm
EXECUTABLE=dns-inventory
VERSION=$(shell git describe --tags --always)

build-windows:
Color_Off:=\033[0m
# Regular Colors
Black:=\033[0;30m
Red:=\033[0;31m
Green:=\033[0;32m
Yellow:=\033[0;33m
Blue:=\033[0;34m
Purple:=\033[0;35m
Cyan:=\033[0;36m
White:=\033[0;37m


define greeting
@clear
@echo "$(Yellow)"
@echo " ___ _ __ __ ____ __ "
@echo " / | ____ _____(_) /_ / /__ / _/___ _ _____ ____ / /_____ _______ __"
@echo " / /| | / __ \/ ___/ / __ \/ / _ \______ / // __ \ | / / _ \/ __ \/ __/ __ \/ ___/ / / /"
@echo " / ___ |/ / / (__ ) / /_/ / / __/_____// // / / / |/ / __/ / / / /_/ /_/ / / / /_/ / "
@echo "/_/ |_/_/ /_/____/_/_.___/_/\___/ /___/_/ /_/|___/\___/_/ /_/\__/\____/_/ \__, / "
@echo " /____/ "
@echo "$(Color_Off)"
endef

define cleanup
@COMPOSE_PROFILES=dns,etcd docker compose -f docker/docker-compose.yml down
@rm -rf ./$(EXECUTABLE)_$(VERSION)_*
endef

_init:
$(call greeting)

build-windows: ## Make Windows binary
env GOOS=windows GOARCH=amd64 go build -ldflags "-s -w -X 'github.com/NeonSludge/ansible-dns-inventory/internal/build.Version=$(VERSION)' -X 'github.com/NeonSludge/ansible-dns-inventory/internal/build.Time=$(shell date -u +%Y%m%dT%H%M%SZ)'" -o ./$(EXECUTABLE)_$(VERSION)_amd64_windows.exe ./cmd/$(EXECUTABLE)

build-darwin:
build-darwin: ## Make Darwin binary (ARM/AMD64)
env GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w -X 'github.com/NeonSludge/ansible-dns-inventory/internal/build.Version=$(VERSION)' -X 'github.com/NeonSludge/ansible-dns-inventory/internal/build.Time=$(shell date -u +%Y%m%dT%H%M%SZ)'" -o ./$(EXECUTABLE)_$(VERSION)_amd64_darwin ./cmd/$(EXECUTABLE)
env GOOS=darwin GOARCH=arm64 go build -ldflags "-s -w -X 'github.com/NeonSludge/ansible-dns-inventory/internal/build.Version=$(VERSION)' -X 'github.com/NeonSludge/ansible-dns-inventory/internal/build.Time=$(shell date -u +%Y%m%dT%H%M%SZ)'" -o ./$(EXECUTABLE)_$(VERSION)_arm64_darwin ./cmd/$(EXECUTABLE)

build-linux:
build-linux: ## Make Linux binary (ARM/AMD64)
env GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -X 'github.com/NeonSludge/ansible-dns-inventory/internal/build.Version=$(VERSION)' -X 'github.com/NeonSludge/ansible-dns-inventory/internal/build.Time=$(shell date -u +%Y%m%dT%H%M%SZ)'" -o ./$(EXECUTABLE)_$(VERSION)_amd64_linux ./cmd/$(EXECUTABLE)
env GOOS=linux GOARCH=arm64 go build -ldflags "-s -w -X 'github.com/NeonSludge/ansible-dns-inventory/internal/build.Version=$(VERSION)' -X 'github.com/NeonSludge/ansible-dns-inventory/internal/build.Time=$(shell date -u +%Y%m%dT%H%M%SZ)'" -o ./$(EXECUTABLE)_$(VERSION)_arm64_linux ./cmd/$(EXECUTABLE)

build: build-linux build-darwin build-windows
build: build-linux build-darwin build-windows ## Make all binaries

test-dns: _init build ## Run DNS tests
@echo " "
@COMPOSE_PROFILES=dns docker compose -f docker/docker-compose.yml up -d
@sleep 5
@chmod +x ./$(EXECUTABLE)_$(VERSION)_*
@echo " "
@echo "------------- RUN TESTS------------------"
@docker compose -f docker/docker-compose.yml exec -it multitool-dns /bin/bash -c "/app/$(EXECUTABLE)_$(VERSION)_$(platform)64_linux -tree"
$(call cleanup)

test-etcd: _init build ## Run etcd tests
@echo " "
@COMPOSE_PROFILES=etcd docker compose -f docker/docker-compose.yml up -d
@sleep 5
@chmod +x ./$(EXECUTABLE)_$(VERSION)_*
@chmod +x docker/config/etcd/init.sh
@bash docker/config/etcd/init.sh
@echo " "
@echo "------------- RUN TESTS------------------"
@docker compose -f docker/docker-compose.yml exec -it multitool-etcd /bin/bash -c "/app/$(EXECUTABLE)_$(VERSION)_$(platform)64_linux -tree"
$(call cleanup)

test: test-dns test-etcd ## Run all tests

image: build ## Build image
@echo " "

help:
$(call greeting)
grep -E '(^[a-z].*[^:]\s*##)|(^##)' $(MAKEFILE_LIST) | \
perl -pe "s/Makefile://" | perl -pe "s/^##\s*//" | \
awk ' \
BEGIN { FS = ":.*##" } \
$$2 { printf "\033[32m%-30s\033[0m %s\n", $$1, $$2 } \
!$$2 { printf " \033[33m%-30s\033[0m\n", $$1 } \
'

## make exec="command": ## Выполнить команду в контейнере
_default:
if [ '$(exec)' ]; then \
COMPOSE_PROFILES=tools docker compose -f docker/docker-compose.yml up -d; \
docker compose exec -it -f docker/docker-compose.yml multitool-dns -v "$(CUR_DIR):/app" exec -it -c '$(exec)'; \
else \
make help; \
fi
18 changes: 18 additions & 0 deletions docker/Dockerfile.bind9
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#Base Bind9 Image
FROM internetsystemsconsortium/bind9:9.18

#Install required tools and dependencies
RUN apt update && apt install -y \
bind9-doc \
dnsutils \
geoip-bin \
mariadb-server \
net-tools

# Expose Ports
EXPOSE 53/tcp
EXPOSE 53/udp
EXPOSE 953/tcp

# Start the Name Service
CMD ["/usr/sbin/named", "-g", "-c", "/etc/bind/named.conf", "-u", "bind"]
59 changes: 59 additions & 0 deletions docker/config/dns/ansible-dns-inventory.infra.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
$TTL 1d
;
;default expiration time (in seconds) of all RRs without their own
;TTL value
@ IN SOA ns1.infra.local. root.infra.local. (
10 ; Serial
1d ; Refresh
1h ; Retry
1w ; Expire
1h) ; Negative Cache TTL
; name servers - NS records IN
NS ns1.infra.local. ; name servers - A records
ns1 A 172.38.0.5
TXT "OS=linux;ENV=dev;ROLE=platform;SRV=nameservers;VARS=NS=first,START=true"
etcd-1 A 172.38.0.8
TXT "OS=linux;ENV=dev;ROLE=platform;SRV=dbs;VARS=ETCD_VARS=first,START=true"
etcd-2 A 172.38.0.9
TXT "OS=linux;ENV=dev;ROLE=platform;SRV=dbs;VARS=ETCD_VARS=second,START=true"
etcd-3 A 172.38.0.10
TXT "OS=linux;ENV=dev;ROLE=platform;SRV=dbs;VARS=ETCD_VARS=third,START=true"
bastion A 172.38.0.20
TXT "OS=linux;ENV=dev;ROLE=platform;SRV=tools;VARS=key1=value1,key2=value2"
master-1 A 172.38.0.21
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=controlplane;VARS=key1=value1,key2=value2"
master-2 A 172.38.0.22
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=controlplane;VARS=key1=value1,key2=value2"
master-3 A 172.38.0.23
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=controlplane;VARS=key1=value1,key2=value2"
ingress-1 A 172.38.0.25
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=ingresses;VARS=key1=value1,key2=value2"
ingress-2 A 172.38.0.26
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=ingresses;VARS=key1=value1,key2=value2"
ingress-3 A 172.38.0.27
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=ingresses;VARS=key1=value1,key2=value2"
worker-1 A 172.38.0.30
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=workers;VARS=key1=value1,key2=value2"
worker-2 A 172.38.0.31
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=workers;VARS=key1=value1,key2=value2"
worker-3 A 172.38.0.32
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=workers;VARS=key1=value1,key2=value2"
worker-4 A 172.38.0.33
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=workers;VARS=key1=value1,key2=value2"
worker-5 A 172.38.0.34
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=workers;VARS=key1=value1,key2=value2"
observability-1 A 172.38.0.50
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=observability;VARS=key1=value1,key2=value2"
observability-2 A 172.38.0.51
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=observability;VARS=key1=value1,key2=value2"
observability-3 A 172.38.0.52
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=observability;VARS=key1=value1,key2=value2"
logging-1 A 172.38.0.100
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=logs;VARS=key1=value1,key2=value2"
logging-2 A 172.38.0.101
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=logs;VARS=key1=value1,key2=value2"
logging-3 A 172.38.0.102
TXT "OS=linux;ENV=dev;ROLE=k8s;SRV=logs;VARS=key1=value1,key2=value2"
multitool A 172.38.0.250
TXT "OS=linux;ENV=dev;ROLE=platform;SRV=tools;VARS=key1=value1,key2=value2"

4 changes: 4 additions & 0 deletions docker/config/dns/named.conf.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
zone "infra.local" {
type master;
file "/etc/bind/zones/ansible-dns-inventory.infra.local";
};
9 changes: 9 additions & 0 deletions docker/config/dns/named.conf.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
options {
directory "/var/cache/bind";
recursion yes;
listen-on { any; };
forwarders {
8.8.8.8;
8.8.4.4;
};
};
31 changes: 31 additions & 0 deletions docker/config/etcd/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./ns1.infra.local/0 "OS=linux;ENV=dev;ROLE=platform;SRV=nameservers;VARS=NS=first,START=true"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./etcd-1.infra.local/0 "OS=linux;ENV=dev;ROLE=platform;SRV=dbs;VARS=ETCD_VARS=first,START=true"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./etcd-2.infra.local/0 "OS=linux;ENV=dev;ROLE=platform;SRV=dbs;VARS=ETCD_VARS=first,START=true"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./etcd-3.infra.local/0 "OS=linux;ENV=dev;ROLE=platform;SRV=dbs;VARS=ETCD_VARS=first,START=true"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./bastion.infra.local/0 "OS=linux;ENV=dev;ROLE=platform;SRV=tools;VARS=key1=value1,key2=value2"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./master-1.infra.local/0 "OS=linux;ENV=dev;ROLE=k8s;SRV=controlplane;VARS=key1=value1,key2=value2"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./master-2.infra.local/0 "OS=linux;ENV=dev;ROLE=k8s;SRV=controlplane;VARS=key1=value1,key2=value2"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./master-3.infra.local/0 "OS=linux;ENV=dev;ROLE=k8s;SRV=controlplane;VARS=key1=value1,key2=value2"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./ingress-1.infra.local/0 "OS=linux;ENV=dev;ROLE=k8s;SRV=ingresses;VARS=key1=value1,key2=value2"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./ingress-2.infra.local/0 "OS=linux;ENV=dev;ROLE=k8s;SRV=ingresses;VARS=key1=value1,key2=value2"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./ingress-3.infra.local/0 "OS=linux;ENV=dev;ROLE=k8s;SRV=ingresses;VARS=key1=value1,key2=value2"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./worker-1.infra.local/0 "OS=linux;ENV=dev;ROLE=k8s;SRV=workers;VARS=key1=value1,key2=value2"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./worker-2.infra.local/0 "OS=linux;ENV=dev;ROLE=k8s;SRV=workers;VARS=key1=value1,key2=value2"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./observability-1.infra.local/0 "OS=linux;ENV=dev;ROLE=k8s;SRV=observability;VARS=key1=value1,key2=value2"
docker compose -f docker/docker-compose.yml exec -it etcd-1 \
etcdctl put ANSIBLE_INVENTORY/infra.local./logging-1.infra.local/0 "OS=linux;ENV=dev;ROLE=k8s;SRV=logs;VARS=key1=value1,key2=value2"
45 changes: 45 additions & 0 deletions docker/config/inventory/ansible-dns-inventory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Datasource type. Environment variable: ADI_DATASOURCE
datasource: "dns"
dns:
# DNS datasource configuration.
server: "172.38.0.5:53"
timeout: "120s"
zones:
- infra.local.
etcd:
endpoints:
- 172.38.0.8:2379
- 172.38.0.9:2379
- 172.38.0.10:2379
tls:
insecure: true
# Host record parsing configuration.
txt:
# Key/value pair parsing configuration.
kv:
# Separator between k/v pairs found in TXT records. Environment variable: ADI_TXT_KV_SEPARATOR
separator: ";"
# Separator between a key and a value. Environment variable: ADI_TXT_KV_EQUALSIGN
equalsign: "="
# Host variables parsing configuration.
vars:
# Enable host variables support. Environment variable: ADI_TXT_VARS_ENABLED
enabled: false
# Separator between k/v pairs found in the host variables attribute. Environment variable: ADI_TXT_VARS_SEPARATOR
separator: ","
# Separator between a key and a value. Environment variable: ADI_TXT_VARS_EQUALSIGN
equalsign: "="
# Host attributes parsing configuration.
keys:
# Separator between elements of an Ansible group name. Environment variable: ADI_TXT_KEYS_SEPARATOR
separator: "_"
# Key name of the attribute containing the host operating system identifier. Environment variable: ADI_TXT_KEYS_OS
os: "OS"
# Key name of the attribute containing the host environment identifier. Environment variable: ADI_TXT_KEYS_ENV
env: "ENV"
# Key name of the attribute containing the host role identifier. Environment variable: ADI_TXT_KEYS_ROLE
role: "ROLE"
# Key name of the attribute containing the host service identifier. Environment variable: ADI_TXT_KEYS_SRV
srv: "SRV"
# Key name of the attribute containing the host variables. Environment variable: ADI_TXT_KEYS_VARS
vars: "VARS"
50 changes: 50 additions & 0 deletions docker/config/inventory/ansible-etcd-inventory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Datasource type. Environment variable: ADI_DATASOURCE
datasource: "etcd"
dns:
# DNS datasource configuration.
server: "172.38.0.5:53"
timeout: "120s"
zones:
- infra.local.
etcd:
endpoints:
- 172.38.0.8:2379
- 172.38.0.9:2379
- 172.38.0.10:2379
prefix: "ANSIBLE_INVENTORY"
zones:
- infra.local.
tls:
enabled: false
insecure: true

# txt:
# # Key/value pair parsing configuration.
# kv:
# # Separator between k/v pairs found in TXT records. Environment variable: ADI_TXT_KV_SEPARATOR
# separator: ";"
# # Separator between a key and a value. Environment variable: ADI_TXT_KV_EQUALSIGN
# equalsign: "="
# # Host variables parsing configuration.
# vars:
# # Enable host variables support. Environment variable: ADI_TXT_VARS_ENABLED
# enabled: false
# # Separator between k/v pairs found in the host variables attribute. Environment variable: ADI_TXT_VARS_SEPARATOR
# separator: ","
# # Separator between a key and a value. Environment variable: ADI_TXT_VARS_EQUALSIGN
# equalsign: "="
# # Host attributes parsing configuration.
# keys:
# # Separator between elements of an Ansible group name. Environment variable: ADI_TXT_KEYS_SEPARATOR
# separator: "_"
# # Key name of the attribute containing the host operating system identifier. Environment variable: ADI_TXT_KEYS_OS
# os: "OS"
# # Key name of the attribute containing the host environment identifier. Environment variable: ADI_TXT_KEYS_ENV
# env: "ENV"
# # Key name of the attribute containing the host role identifier. Environment variable: ADI_TXT_KEYS_ROLE
# role: "ROLE"
# # Key name of the attribute containing the host service identifier. Environment variable: ADI_TXT_KEYS_SRV
# srv: "SRV"
# # Key name of the attribute containing the host variables. Environment variable: ADI_TXT_KEYS_VARS
# vars: "VARS"

Loading