Improve Makefile. Reformat code.

This commit is contained in:
Carlos de Paula 2020-03-18 18:01:01 -03:00
parent d4be411a81
commit 8c418b73bb
7 changed files with 185 additions and 168 deletions

View File

@ -1,55 +1,65 @@
JSONNET_FMT := jsonnet fmt -n 2 --max-blank-lines 2 --string-style s --comment-style s
GOPATH ?= `$(pwd)` GOPATH ?= `$(pwd)`
JSONNET_BIN := $(GOPATH)/bin/jsonnet JSONNET_BIN := $(GOPATH)/bin/jsonnet
JB_BINARY := $(GOPATH)/bin/jb JB_BINARY := $(GOPATH)/bin/jb
.PHONY: generate vendor fmt manifests JSONNET_FMT := $(GOPATH)/bin/jsonnetfmt -n 2 --max-blank-lines 2 --string-style s --comment-style s
all: manifests .PHONY: generate vendor fmt manifests help
manifests: jsonnet all: manifests ## Builds the manifests
help: # Show help
@echo "Makefile targets:"
@echo ""
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
manifests: $(JSONNET_BIN) ## Builds the manifests
rm -rf manifests rm -rf manifests
./scripts/build.sh main.jsonnet $(JSONNET_BIN) ./scripts/build.sh main.jsonnet $(JSONNET_BIN)
update: jsonnet_bundler update_libs: $(JB_BINARY) ## Updates vendor libs. Require a regeneration of the manifests
jb update $(JB_BINARY) update
vendor: jsonnet_bundler jsonnetfile.json jsonnetfile.lock.json vendor: $(JB_BINARY) jsonnetfile.json jsonnetfile.lock.json ## Download vendor libs
rm -rf vendor rm -rf vendor
$(JB_BINARY) install $(JB_BINARY) install
fmt: fmt: ## Formats all jsonnet and libsonnet files (except on vendor dir)
find . -name 'vendor' -prune -o -name '*.libsonnet' -o -name '*.jsonnet' -print | xargs -n 1 -- $(JSONNET_FMT) -i @echo "Formatting jsonnet files"
@find . -name 'vendor' -prune -o -name '*.libsonnet' -o -name '*.jsonnet' -print | xargs -n 1 -- $(JSONNET_FMT) -i
deploy: manifests deploy: manifests ## Rebuilds manifests and deploy to configured cluster
kubectl apply -f ./manifests/ kubectl apply -f ./manifests/
echo "Will wait 40 seconds to reapply manifests" echo "Will wait 40 seconds to reapply manifests"
sleep 40 sleep 40
kubectl apply -f ./manifests/ kubectl apply -f ./manifests/
teardown: teardown: ## Delete all monitoring stack resources from configured cluster
kubectl delete -f ./manifests/ kubectl delete -f ./manifests/
tar: manifests tar: manifests ## Generates a .tar.gz from manifests dir
rm -rf manifests.tar rm -rf manifests.tar.gz
tar -cf manifests.tar manifests tar -cfz manifests.tar.gz manifests
jsonnet_bundler: $(JB_BINARY): ## Installs jsonnet-bundler utility
ifeq (, $(shell which jb))
@echo "Installing jsonnet-bundler" @echo "Installing jsonnet-bundler"
@go get -u github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb @go get -u github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb
endif
jsonnet: $(JSONNET_BIN): ## Installs jsonnet and jsonnetfmt utility
ifeq (, $(shell which jsonnet))
@echo "Installing jsonnet" @echo "Installing jsonnet"
@go get github.com/google/go-jsonnet/cmd/jsonnet @go get -u github.com/google/go-jsonnet/cmd/jsonnet
@go get github.com/brancz/gojsontoyaml @go get -u github.com/google/go-jsonnet/cmd/jsonnetfmt
endif @go get -u github.com/brancz/gojsontoyaml
change_suffix: update_tools: ## Updates jsonnet, jsonnetfmt and jb utilities
@echo "Updating jsonnet"
@go get -u github.com/google/go-jsonnet/cmd/jsonnet
@go get -u github.com/google/go-jsonnet/cmd/jsonnetfmt
@go get -u github.com/brancz/gojsontoyaml
@go get -u github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb
change_suffix: ## Changes suffix for the ingress
@perl -p -i -e 's/^(\s*)\-\ host:.*/\1- host: alertmanager.${IP}.nip.io/g' manifests/ingress-alertmanager.yaml manifests/ingress-prometheus.yaml manifests/ingress-grafana.yaml @perl -p -i -e 's/^(\s*)\-\ host:.*/\1- host: alertmanager.${IP}.nip.io/g' manifests/ingress-alertmanager.yaml manifests/ingress-prometheus.yaml manifests/ingress-grafana.yaml
@echo "Ingress IPs changed to [service].${IP}.nip.io" @echo "Ingress IPs changed to [service].${IP}.nip.io"
${K3S} kubectl apply -f manifests/ingress-alertmanager.yaml ${K3S} kubectl apply -f manifests/ingress-alertmanager.yaml

View File

@ -20,14 +20,16 @@ local utils = import 'utils.libsonnet';
clusterRole: clusterRole:
utils.newClusterRole('arm-exporter', [ utils.newClusterRole('arm-exporter', [
{apis: ['authentication.k8s.io'], {
apis: ['authentication.k8s.io'],
res: ['tokenreviews'], res: ['tokenreviews'],
verbs: ['create'] verbs: ['create'],
}, },
{apis: ['authorization.k8s.io'], {
apis: ['authorization.k8s.io'],
res: ['subjectaccessreviews'], res: ['subjectaccessreviews'],
verbs: ['create'] verbs: ['create'],
} },
], null), ], null),
clusterRoleBinding: clusterRoleBinding:
@ -86,9 +88,10 @@ local utils = import 'utils.libsonnet';
service.mixin.spec.withClusterIp('None'), service.mixin.spec.withClusterIp('None'),
serviceMonitor: serviceMonitor:
utils.newServiceMonitorHTTPS('arm-exporter', utils.newServiceMonitorHTTPS(
'arm-exporter',
$._config.namespace, $._config.namespace,
{'k8s-app': 'arm-exporter'}, { 'k8s-app': 'arm-exporter' },
$._config.namespace, $._config.namespace,
'https', 'https',
'https', 'https',

View File

@ -82,19 +82,21 @@ local utils = import 'utils.libsonnet';
utils.newServiceMonitor( utils.newServiceMonitor(
'elasticsearch', 'elasticsearch',
$._config.namespace, $._config.namespace,
{'k8s-app': 'elasticsearch-exporter'}, { 'k8s-app': 'elasticsearch-exporter' },
'monitoring', 'monitoring',
'es-metrics', 'es-metrics',
'http'), 'http'
),
serviceMonitorFluentd: serviceMonitorFluentd:
utils.newServiceMonitor( utils.newServiceMonitor(
'fluentd-es', 'fluentd-es',
$._config.namespace, $._config.namespace,
{'k8s-app': 'fluentd-es'}, { 'k8s-app': 'fluentd-es' },
'logging', 'logging',
'metrics', 'metrics',
'http'), 'http'
),
}, },
// Add Prometheus monitoring rules for ElasticSearch // Add Prometheus monitoring rules for ElasticSearch
prometheusRules+:: { prometheusRules+:: {

View File

@ -13,7 +13,7 @@ local utils = import 'utils.libsonnet';
metallbExporter+:: { metallbExporter+:: {
serviceMonitor: serviceMonitor:
utils.newServiceMonitor('metallb', $._config.namespace, {'k8s-app': 'metallb-controller'}, 'metallb-system', 'http', 'http'), utils.newServiceMonitor('metallb', $._config.namespace, { 'k8s-app': 'metallb-controller' }, 'metallb-system', 'http', 'http'),
service: service:
local service = k.core.v1.service; local service = k.core.v1.service;

View File

@ -12,6 +12,6 @@ local utils = import 'utils.libsonnet';
traefikExporter+:: { traefikExporter+:: {
serviceMonitor: serviceMonitor:
utils.newServiceMonitor('traefik', $._config.namespace, {'app': 'traefik'}, 'kube-system', 'metrics', 'http'), utils.newServiceMonitor('traefik', $._config.namespace, { app: 'traefik' }, 'kube-system', 'metrics', 'http'),
}, },
} }

View File

@ -17,7 +17,7 @@ local utils = import 'utils.libsonnet';
upsExporter+:: { upsExporter+:: {
serviceMonitor: serviceMonitor:
utils.newServiceMonitor('ups-exporter', $._config.namespace, {'k8s-app': 'ups-exporter'}, $._config.namespace, 'metrics', 'http'), utils.newServiceMonitor('ups-exporter', $._config.namespace, { 'k8s-app': 'ups-exporter' }, $._config.namespace, 'metrics', 'http'),
service: service:
local service = k.core.v1.service; local service = k.core.v1.service;

View File

@ -5,7 +5,8 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
generate(kp):: ( generate(kp):: (
{ {
[std.asciiLower(module) + '-' + name]: kp[module][name] [std.asciiLower(module) + '-' + name]: kp[module][name]
for module in std.objectFieldsAll(kp) if !std.startsWith(module, '_') for module in std.objectFieldsAll(kp)
if !std.startsWith(module, '_')
for name in std.objectFields(kp[module]) for name in std.objectFields(kp[module])
} }
), ),
@ -42,7 +43,7 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
+ policyRule.withResources([r for r in resources]) + policyRule.withResources([r for r in resources])
+ policyRule.withVerbs([v for v in verbs]); + policyRule.withVerbs([v for v in verbs]);
local r = [ p(pol.apis, pol.res, pol.verbs) for pol in roles ]; local r = [p(pol.apis, pol.res, pol.verbs) for pol in roles];
local rules = r; local rules = r;
@ -63,7 +64,7 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
+ clusterRoleBinding.mixin.roleRef.withApiGroup('rbac.authorization.k8s.io') + clusterRoleBinding.mixin.roleRef.withApiGroup('rbac.authorization.k8s.io')
+ clusterRoleBinding.mixin.roleRef.withName(clusterRole) + clusterRoleBinding.mixin.roleRef.withName(clusterRole)
+ clusterRoleBinding.mixin.roleRef.mixinInstance({ kind: 'ClusterRole' }) + clusterRoleBinding.mixin.roleRef.mixinInstance({ kind: 'ClusterRole' })
+ clusterRoleBinding.withSubjects([{ kind: 'ServiceAccount', 'name': serviceAccount, 'namespace': serviceAccountNamespace }]) + clusterRoleBinding.withSubjects([{ kind: 'ServiceAccount', name: serviceAccount, namespace: serviceAccountNamespace }])
), ),
// Creates endpoint objects // Creates endpoint objects
@ -121,7 +122,7 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
I + ingress.mixin.spec.withTls( I + ingress.mixin.spec.withTls(
ingressTls.new() + ingressTls.new() +
ingressTls.withHosts(host) + ingressTls.withHosts(host) +
(if S != '' then {'secretName': S} else {}) (if S != '' then { secretName: S } else {})
) )
), ),
@ -136,7 +137,8 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
{ {
'tls.crt': std.base64(crt), 'tls.crt': std.base64(crt),
'tls.key': std.base64(key), 'tls.key': std.base64(key),
}) }
)
), ),
// Creates new basic deployments // Creates new basic deployments
@ -152,10 +154,10 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
local c = [con]; local c = [con];
local d = deployment.new(name, 1, c, {'app': name}) local d = deployment.new(name, 1, c, { app: name })
+ deployment.mixin.metadata.withNamespace(namespace) + deployment.mixin.metadata.withNamespace(namespace)
+ deployment.mixin.metadata.withLabels({'app': name}) + deployment.mixin.metadata.withLabels({ app: name })
+ deployment.mixin.spec.selector.withMatchLabels({'app': name}) + deployment.mixin.spec.selector.withMatchLabels({ app: name })
+ deployment.mixin.spec.strategy.withType('RollingUpdate') + deployment.mixin.spec.strategy.withType('RollingUpdate')
+ deployment.mixin.spec.template.spec.withRestartPolicy('Always'); + deployment.mixin.spec.template.spec.withRestartPolicy('Always');
d d
@ -166,9 +168,9 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
local servicePort = k.core.v1.service.mixin.spec.portsType; local servicePort = k.core.v1.service.mixin.spec.portsType;
local p = servicePort.newNamed(name, port, port); local p = servicePort.newNamed(name, port, port);
local s = service.new(name, {'app': name}, p) local s = service.new(name, { app: name }, p)
+ service.mixin.metadata.withNamespace(namespace) + service.mixin.metadata.withNamespace(namespace)
+ service.mixin.metadata.withLabels({'app': name}); + service.mixin.metadata.withLabels({ app: name });
s s
), ),
@ -181,11 +183,11 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
name: name, name: name,
namespace: namespace, namespace: namespace,
labels: { labels: {
'app': name, app: name,
}, },
}, },
spec: { spec: {
jobLabel: name+'-exporter', jobLabel: name + '-exporter',
selector: { selector: {
matchLabels: matchLabel, matchLabels: matchLabel,
}, },
@ -216,20 +218,20 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
bearerTokenFile: token, bearerTokenFile: token,
tlsConfig: { tlsConfig: {
insecureSkipVerify: true, insecureSkipVerify: true,
} },
}], }],
} },
}; };
std.mergePatch(s, t) std.mergePatch(s, t)
// s + t // s + t
), ),
# Adds arguments to a container in a deployment // Adds arguments to a container in a deployment
# args is an array of arguments in the format // args is an array of arguments in the format
# ["arg1","arg2",] // ["arg1","arg2",]
addArguments(deployment, container, args):: ( addArguments(deployment, container, args):: (
{spec+: { { spec+: {
template+: { template+: {
spec+: { spec+: {
containers: containers:
@ -242,14 +244,14 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
), ),
}, },
}, },
}} } }
), ),
# Adds environment variables to a container in a deployment // Adds environment variables to a container in a deployment
# envs is an array of environment variables in the format // envs is an array of environment variables in the format
# [{name: 'VARNAME', value: 'var_value'},{...},] // [{name: 'VARNAME', value: 'var_value'},{...},]
addEnviromnentVars(deployment, container, envs):: ( addEnviromnentVars(deployment, container, envs):: (
{spec+: { { spec+: {
template+: { template+: {
spec+: { spec+: {
containers: containers:
@ -262,6 +264,6 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
), ),
}, },
}, },
}} } }
), ),
} }