Skip to content

Commit

Permalink
feat(opencost-report-cronjob): Added opencost report cronjob (#2046)
Browse files Browse the repository at this point in the history
Co-authored-by: Edward Malinowski <[email protected]>
  • Loading branch information
emalinowski and Edward Malinowski authored Oct 3, 2022
1 parent 2fcbd2e commit 6dd5983
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
45 changes: 40 additions & 5 deletions gen3/bin/kube-setup-kubecost.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ gen3_setup_kubecost_service_account() {
aws iam attach-role-policy --role-name "$roleName" --policy-arn "arn:aws:iam::$accountID:policy/$vpc_name-Kubecost-CUR-policy" 1>&2
#gen3 awsrole sa-annotate "$saName" "$roleName" "kubecost"
kubectl delete sa -n kubecost $saName
#thanosRoleName="$vpc_name-thanos-user"
#thanosSaName="thanos-service-account"
#gen3 awsrole create "$thanosRoleName" "$thanosSaName" "kubecost" || return 1
#aws iam attach-role-policy --role-name "$thanosRoleName" --policy-arn "arn:aws:iam::$accountID:policy/$vpc_name-Kubecost-Thanos-policy" 1>&2
#gen3 awsrole sa-annotate "$thanosSaName" "$thanosRoleName" "kubecost"
reportsRoleName="$vpc_name-opencost-report-role"
reportsSaName="reports-service-account"
gen3 awsrole create "$reportsRoleName" "$reportsSaName" "kubecost" || return 1
aws iam attach-role-policy --role-name "$reportsRoleName" --policy-arn "arn:aws:iam::$accountID:policy/$vpc_name-Kubecost-Thanos-policy" 1>&2
gen3 awsrole sa-annotate "$reportsSaName" "$reportsRoleName" "kubecost"
}

gen3_delete_kubecost_service_account() {
Expand Down Expand Up @@ -120,6 +120,11 @@ gen3_setup_kubecost() {
else
gen3_log_info "kube-setup-kubecost exiting - kubecost already deployed, use --force true to redeploy"
fi
gen3_setup_reports_cronjob
}

gen3_setup_reports_cronjob {
gen3 job cron opencost-report '0 0 * * 0' BUCKET_NAME $s3Bucket
}

if [[ -z "$GEN3_SOURCE_ONLY" ]]; then
Expand Down Expand Up @@ -295,6 +300,36 @@ if [[ -z "$GEN3_SOURCE_ONLY" ]]; then
;;
esac
;;
"cronjob")
subcommand=""
if [[ $# -gt 0 ]]; then
subcommand="$1"
shift
fi
case "$subcommand" in
"create")
for flag in $@; do
if [[ $# -gt 0 ]]; then
flag="$1"
shift
fi
case "$flag" in
"--s3-bucket")
s3Bucket="$1"
;;
esac
done
if [[ -z $s3Bucket ]]; then
gen3_log_err "Please ensure you set the s3Bucket for setting up cronjob without full opencost deployment."
exit 1
fi
gen3_setup_reports_cronjob
;;
*)
gen3_log_err "gen3_logs" "invalid history subcommand $subcommand - try: gen3 help kube-setup-kubecost"
;;
esac
;;
"delete")
gen3_delete_kubecost
;;
Expand Down
36 changes: 36 additions & 0 deletions kube/services/jobs/opencost-report-job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
apiVersion: batch/v1
kind: Job
metadata:
name: opencost-report
namespace: kubecost
spec:
template:
metadata:
labels:
app: gen3job
spec:
serviceAccountName: report-service-account
containers:
- name: send-report
image: quay.io/cdis/awshelper:master
imagePullPolicy: Always
env:
- name: gen3Env
valueFrom:
configMapKeyRef:
name: global
key: environment
- name: JENKINS_HOME
value: "devterm"
- name: GEN3_HOME
value: /home/ubuntu/cloud-automation
- name: bucketName
GEN3_BUCKET_NAME|-value: ""-|
command: [ "/bin/bash" ]
args:
- "-c"
- |
curl -k "https://kubecost-cost-analyzer.kubecost/model/allocation/summary?aggregate=label%3Agen3username&window=7d&accumulate=true&shareIdle=false&idleByNode=false&shareTenancyCosts=true&shareNamespaces=&shareLabels=&shareCost=NaN&shareSplit=weighted" | jq -r . > "report-$(date +"%m-%d-%y").json"
aws s3 cp ./report*.json s3://$bucketName
restartPolicy: Never
2 changes: 1 addition & 1 deletion kube/services/monitoring/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2482,7 +2482,7 @@ prometheus:

## How long to retain metrics
##
retention: 2d
retention: 90d

## Maximum size of metrics
##
Expand Down

0 comments on commit 6dd5983

Please sign in to comment.