Files
dsk-iac/scripts/steampipe_iac/steampipe-iac.sh
havelight-ee 74ac1a9b65 update
2023-05-23 16:04:49 +09:00

138 lines
4.3 KiB
Bash
Executable File

#!/bin/bash
#------------------------------------------------------------------------------------------------------
__init (){
pwd
cd ${git_path}
pwd
git pull
datetime=`date "+%Y.%m.%d %H:%M:%S"`
echo -e "*기준 시간 : ${datetime}\n" > ${file}
cat ${origin} >> ${file}
}
#------------------------------------------------------------------------------------------------------
__git_push (){
git add ${file}
git commit -m 'steampipe schedule'
git push
}
#------------------------------------------------------------------------------------------------------
__append (){
line_count=`cat ${exec_log} | grep -v -- -- | grep -v name | wc -l`
echo -e "\n${title} [${line_count}]\n" >> ${file}
cat ${exec_log} >> ${file}
}
#------------------------------------------------------------------------------------------------------
__query_exec (){
steampipe query "${1}" > ${exec_log}
__log_sed
}
#------------------------------------------------------------------------------------------------------
__log_sed (){
sed -i 's/+/|/g' ${exec_log}
sed -i "s/node-role.kubernetes.io\///g" ${exec_log}
sed -i '1d;$d' ${exec_log}
}
#------------------------------------------------------------------------------------------------------
node_query="""
SELECT
name,
annotations ->> 'projectcalico.org/IPv4Address' AS IP,
COALESCE(taints -> 0 ->> 'key', '-') AS Taints_key,
COALESCE(tags ->> 'kops.k8s.io/instancegroup', '-') AS Instance_group,
capacity ->> 'cpu' AS CPU,
CEIL((CAST(regexp_replace(capacity ->> 'memory', 'Ki', '') AS DECIMAL) / POWER(2, 20))) AS Memory,
tags ->> 'topology.kubernetes.io/zone' AS Zone,
tags ->> 'beta.kubernetes.io/instance-type' AS Instance_type,
node_info ->> 'osImage' AS OS,
node_info ->> 'kubeletVersion' AS K8S_ver,
node_info ->> 'containerRuntimeVersion' AS Runtime_ver
FROM
kubernetes_node
ORDER BY
Taints_key
"""
resources_query="""
(SELECT
name,
'Stateful_Set' as kind,
available_replicas as count,
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'requests' ->> 'cpu') AS reqeust_cpu,
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'requests' ->> 'memory') AS reqeust_mem,
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'limits' ->> 'cpu') AS limit_cpu,
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'limits' ->> 'memory') AS limit_mem
FROM
kubernetes_stateful_set
WHERE
name not like 'rel-%')
union
(SELECT
name,
'Deployment' as kind,
available_replicas as count,
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'requests' ->> 'cpu') AS reqeust_cpu,
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'requests' ->> 'memory') AS reqeust_mem,
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'limits' ->> 'cpu') AS limit_cpu,
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'limits' ->> 'memory') AS limit_mem
FROM
kubernetes_deployment
WHERE
name not like 'rel-%')
union
(SELECT
name,
'DaemonSet' as kind,
number_available as count,
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'requests' ->> 'cpu') AS reqeust_cpu,
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'requests' ->> 'memory') AS reqeust_mem,
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'limits' ->> 'cpu') AS limit_cpu,
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'limits' ->> 'memory') AS limit_mem
FROM
kubernetes_daemonset
WHERE
name not like 'rel-%')
order by kind
"""
service_query="""
select
name,
namespace,
type,
lower(p ->> 'nodePort') as Node_Port,
age(current_timestamp, creation_timestamp)
from
kubernetes_service,
jsonb_array_elements(ports) as p
where type='NodePort' and name not like '%rel-%'
order by
Node_Port
"""
#------------------------------------------------------------------------------------------------------
git_path="/home/jhjung/git/dsk-iac"
origin="$(pwd)/org_README.md"
exec_log="${git_path}/query.log"
file="README.md"
#------------------------------------------------------------------------------------------------------
__init
title="## 노드 목록"
__query_exec "${node_query}"
__append
title="## 리소스 목록"
__query_exec "${resources_query}"
__append
title="## 서비스 목록 (NodePort)"
__query_exec "${service_query}"
__append
rm ${exec_log}
__git_push