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'], {
res: ['tokenreviews'], apis: ['authentication.k8s.io'],
verbs: ['create'] res: ['tokenreviews'],
verbs: ['create'],
},
{
apis: ['authorization.k8s.io'],
res: ['subjectaccessreviews'],
verbs: ['create'],
}, },
{apis: ['authorization.k8s.io'],
res: ['subjectaccessreviews'],
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])
} }
), ),
@ -21,11 +22,11 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
// Creates serviceaccount // Creates serviceaccount
newServiceAccount(name, namespace, labels):: ( newServiceAccount(name, namespace, labels):: (
local serviceAccount = k.core.v1.serviceAccount; local serviceAccount = k.core.v1.serviceAccount;
serviceAccount.new(name) serviceAccount.new(name)
+ (if labels != null then serviceAccount.mixin.metadata.withLabels(labels) else {}) + (if labels != null then serviceAccount.mixin.metadata.withLabels(labels) else {})
+ serviceAccount.mixin.metadata.withNamespace(namespace) + serviceAccount.mixin.metadata.withNamespace(namespace)
), ),
// Creates ClusterRoles // Creates ClusterRoles
@ -33,38 +34,38 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
// res: ['tokenreviews'], // res: ['tokenreviews'],
// verbs: ['create'] // verbs: ['create']
// },[{...}]] // },[{...}]]
newClusterRole(name, roles, labels):: ( newClusterRole(name, roles, labels):: (
local clusterRole = k.rbac.v1.clusterRole; local clusterRole = k.rbac.v1.clusterRole;
local policyRule = clusterRole.rulesType; local policyRule = clusterRole.rulesType;
local p(apigroups, resources, verbs) = policyRule.new() local p(apigroups, resources, verbs) = policyRule.new()
+ policyRule.withApiGroups([a for a in apigroups]) + policyRule.withApiGroups([a for a in apigroups])
+ 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;
local c = clusterRole.new() local c = clusterRole.new()
+ (if labels != null then clusterRole.mixin.metadata.withLabels(labels) else {}) + (if labels != null then clusterRole.mixin.metadata.withLabels(labels) else {})
+ clusterRole.mixin.metadata.withName(name) + clusterRole.mixin.metadata.withName(name)
+ clusterRole.withRules(rules); + clusterRole.withRules(rules);
c c
), ),
// Creates a ClusterRoleBinding between a `clusterRole` and a `serviceAccount` on `serviceAccountNamespace` // Creates a ClusterRoleBinding between a `clusterRole` and a `serviceAccount` on `serviceAccountNamespace`
newClusterRoleBinding(name, serviceAccount, serviceAccountNamespace, clusterRole, labels):: ( newClusterRoleBinding(name, serviceAccount, serviceAccountNamespace, clusterRole, labels):: (
local clusterRoleBinding = k.rbac.v1.clusterRoleBinding; local clusterRoleBinding = k.rbac.v1.clusterRoleBinding;
clusterRoleBinding.new() clusterRoleBinding.new()
+ clusterRoleBinding.mixin.metadata.withName(name) + clusterRoleBinding.mixin.metadata.withName(name)
+ (if labels != null then clusterRoleBinding.mixin.metadata.withLabels(labels) else {}) + (if labels != null then clusterRoleBinding.mixin.metadata.withLabels(labels) else {})
+ 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
newEndpoint(name, namespace, ips, portName, portNumber):: ( newEndpoint(name, namespace, ips, portName, portNumber):: (
@ -72,22 +73,22 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
local endpointSubset = endpoints.subsetsType; local endpointSubset = endpoints.subsetsType;
local endpointPort = endpointSubset.portsType; local endpointPort = endpointSubset.portsType;
local Port = endpointPort.new() local Port = endpointPort.new()
+ endpointPort.withName(portName) + endpointPort.withName(portName)
+ endpointPort.withPort(portNumber) + endpointPort.withPort(portNumber)
+ endpointPort.withProtocol('TCP'); + endpointPort.withProtocol('TCP');
local subset = endpointSubset.new() local subset = endpointSubset.new()
+ endpointSubset.withAddresses([ + endpointSubset.withAddresses([
{ ip: IP } { ip: IP }
for IP in ips for IP in ips
]) ])
+ endpointSubset.withPorts(Port); + endpointSubset.withPorts(Port);
endpoints.new() endpoints.new()
+ endpoints.mixin.metadata.withName(name) + endpoints.mixin.metadata.withName(name)
+ endpoints.mixin.metadata.withNamespace(namespace) + endpoints.mixin.metadata.withNamespace(namespace)
+ endpoints.mixin.metadata.withLabels({ 'k8s-app': name }) + endpoints.mixin.metadata.withLabels({ 'k8s-app': name })
+ endpoints.withSubsets(subset) + endpoints.withSubsets(subset)
), ),
// Creates ingress objects // Creates ingress objects
newIngress(name, namespace, host, path, serviceName, servicePort):: ( newIngress(name, namespace, host, path, serviceName, servicePort):: (
@ -120,8 +121,8 @@ 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 {})
) )
), ),
@ -130,13 +131,14 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
local secret = k.core.v1.secret; local secret = k.core.v1.secret;
secret.new('ingress-secret') + secret.new('ingress-secret') +
secret.mixin.metadata.withNamespace(namespace) + secret.mixin.metadata.withNamespace(namespace) +
secret.withType('kubernetes.io/tls') + secret.withType('kubernetes.io/tls') +
secret.withData( secret.withData(
{ {
'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,12 +154,12 @@ 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,40 +168,40 @@ 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
), ),
// Creates http ServiceMonitor objects // Creates http ServiceMonitor objects
newServiceMonitor(name, namespace, matchLabel, matchNamespace, portName, portScheme, path='metrics'):: ( newServiceMonitor(name, namespace, matchLabel, matchNamespace, portName, portScheme, path='metrics'):: (
{ {
apiVersion: 'monitoring.coreos.com/v1', apiVersion: 'monitoring.coreos.com/v1',
kind: 'ServiceMonitor', kind: 'ServiceMonitor',
metadata: { metadata: {
name: name, name: name,
namespace: namespace, namespace: namespace,
labels: { labels: {
'app': name, app: name,
},
}, },
spec: { },
jobLabel: name+'-exporter', spec: {
selector: { jobLabel: name + '-exporter',
matchLabels: matchLabel, selector: {
}, matchLabels: matchLabel,
endpoints: [
{
port: portName,
scheme: portScheme,
interval: '30s',
},
],
namespaceSelector: {
matchNames: [matchNamespace],
},
}, },
endpoints: [
{
port: portName,
scheme: portScheme,
interval: '30s',
},
],
namespaceSelector: {
matchNames: [matchNamespace],
},
},
} }
), ),
@ -210,58 +212,58 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
local t = { local t = {
spec: { spec: {
endpoints: [{ endpoints: [{
port: portName, port: portName,
scheme: portScheme, scheme: portScheme,
interval: '30s', interval: '30s',
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:
std.map( std.map(
function(c) function(c)
if c.name == container then if c.name == container then
c { args+: args } c { args+: args }
else c, else c,
super.containers super.containers
), ),
},
}, },
}} },
), } }
),
# 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:
std.map( std.map(
function(c) function(c)
if c.name == container then if c.name == container then
c { env+: envs } c { env+: envs }
else c, else c,
super.containers super.containers
), ),
},
}, },
}} },
), } }
),
} }