Skip to content

Commit 349bc64

Browse files
authored
Merge pull request #252 from openinfradev/release
main merge ( 20230831 )
2 parents cc6e26c + 8a6f211 commit 349bc64

File tree

2 files changed

+130
-202
lines changed

2 files changed

+130
-202
lines changed

deploy_apps/tks-lma-federation-wftpl.yaml

-173
Original file line numberDiff line numberDiff line change
@@ -311,179 +311,6 @@ spec:
311311
echo "$thanos_sc_ep:$THANOS_SC_PORT" > /mnt/out/thanos_sc_ep.txt
312312
fi
313313
314-
- name: collectThanosScEndpoints
315-
inputs:
316-
parameters:
317-
- name: tks_info_host
318-
- name: app_group_id
319-
outputs:
320-
parameters:
321-
- name: outwards_cluster_list
322-
valueFrom:
323-
path: /mnt/out/cluster_list.txt
324-
- name: inwards_endpoint_map
325-
valueFrom:
326-
path: /mnt/out/inwards_endpoint.txt
327-
- name: cur_cluster_name
328-
valueFrom:
329-
path: /mnt/out/cur_cluster_name.txt
330-
volumes:
331-
- name: out
332-
emptyDir: {}
333-
script:
334-
name: 'collect'
335-
image: harbor.taco-cat.xyz/tks/centos-tks-api:v1.0
336-
command: ["python"]
337-
envFrom:
338-
- secretRef:
339-
name: "git-svc-token"
340-
- secretRef:
341-
name: "tks-api-secret"
342-
volumeMounts:
343-
- name: out
344-
mountPath: /mnt/out
345-
source: |
346-
import sys
347-
import os
348-
import git
349-
import requests
350-
import json
351-
352-
TKS_API_URL = "{{workflow.parameters.tks_info_host}}"
353-
CLUSTER_ID = "{{workflow.parameters.cluster_id}}"
354-
#CLUSTER_ID = "caldcde6u"
355-
#CLUSTER_ID = "c6fk1w3dm"
356-
357-
def getToken() :
358-
data = {
359-
'organizationId' : os.environ['ORGANIZATION_ID'],
360-
'accountId': os.environ['ACCOUNT_ID'],
361-
'password' : os.environ['PASSWORD']
362-
}
363-
364-
res = requests.post(TKS_API_URL+"/api/1.0/auth/login", json = data )
365-
if res.status_code != 200 :
366-
return ''
367-
resJson = res.json()
368-
return resJson['user']['token']
369-
370-
output_cluster_list = []
371-
temp_map = {}
372-
inwards_endpoint_list = []
373-
inwards_endpoint_map = {}
374-
outwards_endpoint_map = {}
375-
376-
TOKEN=getToken()
377-
378-
res = requests.get(TKS_API_URL+"/api/1.0/clusters/" + CLUSTER_ID,
379-
headers={"Authorization": "Bearer " + TOKEN} )
380-
if res.status_code != 200 :
381-
sys.exit('Failed to get cluster')
382-
383-
cluster = res.json()['cluster']
384-
print( cluster )
385-
organizationId = cluster['organizationId']
386-
cur_cluster_name = cluster['id']
387-
388-
res = requests.get(TKS_API_URL+"/api/1.0/clusters?organizationId=" + organizationId,
389-
headers={"Authorization": "Bearer " + TOKEN} )
390-
if res.status_code != 200 :
391-
sys.exit('Failed to get clusters')
392-
393-
clusters = res.json()['clusters']
394-
395-
print("Iterating over clusters in the same contract...")
396-
397-
# Iterate over cluster list except current cluster #
398-
for cluster in clusters:
399-
if cluster['status'] != "RUNNING":
400-
continue
401-
402-
if cluster['id'] != CLUSTER_ID :
403-
print("*******************************************")
404-
print("Checking cluster: {}".format(cluster['id']))
405-
406-
gitBaseUrl = os.environ['GIT_SVC_URL'].replace("http://","")
407-
print( gitBaseUrl )
408-
409-
410-
print("Checking if corresponding cluster repo exists..")
411-
#url = "@github.com/{{workflow.parameters.github_account}}/{}".format(cluster['id'])
412-
url = "@" + gitBaseUrl + "/" + os.environ['USERNAME'] + "/" + cluster['id']
413-
print( url )
414-
415-
repoUrl = "http://" + os.environ['TOKEN'] + url
416-
try:
417-
repo = git.Repo.clone_from(repoUrl, './tempcluster')
418-
419-
except git.exc.GitCommandError as e:
420-
print(e)
421-
print("Repo {} doesn't exist. Skipping this cluster..".format(repoUrl))
422-
continue
423-
424-
res = requests.get(TKS_API_URL+"/api/1.0/app-groups?clusterId=" + cluster['id'],
425-
headers={"Authorization": "Bearer " + TOKEN} )
426-
if res.status_code != 200 :
427-
print( 'Failed to get appgroups for cluster ')
428-
continue
429-
430-
appGroups = res.json()['appGroups']
431-
print( appGroups )
432-
433-
os.system("rm -rf ./tempcluster")
434-
435-
# Check if LMA group exists.
436-
for appGroup in appGroups:
437-
if appGroup['appGroupType'] == "LMA" :
438-
print("Found LMA appGroup: {}".format(appGroup['name']))
439-
440-
res = requests.get(TKS_API_URL+"/api/1.0/app-groups/" + appGroup['id'] + "/applications?applicationType=PROMETHEUS",
441-
headers={"Authorization": "Bearer " + TOKEN} )
442-
if res.status_code != 200 :
443-
print( 'Failed to get applications for appgroup')
444-
continue
445-
446-
applications = res.json()['applications']
447-
if applications :
448-
# This is based on the premise that there's only one prometheus per appGroup.
449-
endpoint = applications[0]['endpoint']
450-
print("Get Thanos-sc endpoint: {}. Appending it to inward list.".format(endpoint))
451-
452-
# Add this cluster's endpoint to endpoint map
453-
inwards_endpoint_list.append(endpoint)
454-
455-
# Add this cluster to outward list so that current ep is updated to this cluster
456-
temp_map["name"] = cluster['id']
457-
str_json = json.dumps(temp_map)
458-
output_cluster_list.append(str_json)
459-
460-
461-
# Compose profer format to be used as input on next step
462-
inwards_endpoint_map['querier.stores'] = inwards_endpoint_list
463-
464-
###########################
465-
# Construct output params #
466-
###########################
467-
len_list = len(output_cluster_list)
468-
469-
with open("/mnt/out/cluster_list.txt", "w") as f:
470-
f.write('[')
471-
472-
print("*** Outwards Cluster List ***")
473-
for idx, item in enumerate(output_cluster_list, start=1):
474-
print("item {}: {}".format(idx, item))
475-
f.write(item.strip("'"))
476-
if idx < len_list:
477-
f.write(',')
478-
f.write(']')
479-
480-
with open("/mnt/out/inwards_endpoint.txt", "w") as f:
481-
str_inwards_endpoint = repr(inwards_endpoint_map)
482-
f.write(str_inwards_endpoint)
483-
484-
with open("/mnt/out/cur_cluster_name.txt", "w") as f:
485-
f.write(cur_cluster_name)
486-
487314
- name: create-keycloak-client
488315
activeDeadlineSeconds: 600
489316
inputs:

deploy_apps/tks-primary-cluster.yaml

+130-29
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,6 @@ spec:
147147
]
148148
when: "{{workflow.parameters.object_store}} == s3"
149149

150-
# TODO: 전체 완성을 위해서는 아래내역을 구현하여 동적인 bucket을 만드는 방식으로 구현해야 하지만
151-
# 5월 오픈전 가능한 형상을 위해 협의한 바(아래)에 따라 본부분은 기존 준비됀 것을 사용하는 것으로 구현하고 추후 수정하다.
152-
# 1. 사용자가 생성하는 첫번째 클러스터는 primary cluster
153-
# 2. primary cluster는 계약이 종료되기 전까지 임의 삭제불가
154-
# 3. 개별 클러스터에서 수행되는 모니터링은 없고 계약단위에서 수행되어야 함
155-
# 하지만 이부분에 datasource 바꿔주는 부분을 포함하고 있으므로 일단 한번 타야할듯...
156150
- - name: change-target
157151
template: change-logging-target
158152
arguments:
@@ -216,6 +210,28 @@ spec:
216210
- name: primary_cluster
217211
- name: member_clusters
218212
steps:
213+
214+
- - name: change-thanos-sidecar
215+
template: sub-change-thanos-sidecar
216+
arguments:
217+
parameters:
218+
- name: primary_cluster
219+
value: '{{inputs.parameters.primary_cluster}}'
220+
- name: member_clusters
221+
value: '{{inputs.parameters.member_clusters}}'
222+
223+
- - name: render-current-cluster
224+
templateRef:
225+
name: event-gitea-render-manifests
226+
template: main
227+
arguments:
228+
parameters:
229+
- name: decapod_site_repo
230+
value: "{{ workflow.parameters.github_account }}/{{ workflow.parameters.cluster_id }}"
231+
- name: base_repo_branch
232+
value: "{{ workflow.parameters.base_repo_branch }}"
233+
when: "{{steps.change-thanos-sidecar.outputs.parameters.changed}} != 'NO_CHANGE_HERE'" # 이미 변경내역이 반영된 (한번 수행됐던) 클러스터라면 랜더링은 필요없음
234+
219235
- - name: sync-organization-changes
220236
template: sub-sync-organization-changes
221237
arguments:
@@ -232,10 +248,10 @@ spec:
232248
arguments:
233249
parameters:
234250
- name: decapod_site_repo
235-
value: "{{ workflow.parameters.github_account }}/{{steps.sync-organization-changes.outputs.parameters.primary_cluster}}"
251+
value: "{{ workflow.parameters.github_account }}/{{steps.sync-organization-changes.outputs.parameters.changed}}"
236252
- name: base_repo_branch
237253
value: "{{ workflow.parameters.base_repo_branch }}"
238-
when: "{{steps.sync-organization-changes.outputs.parameters.primary_cluster}} != 'NO_CHANGE_HERE'"
254+
when: "{{steps.sync-organization-changes.outputs.parameters.changed}} != 'NO_CHANGE_HERE'"
239255

240256
#######################
241257
# Template Definition #
@@ -258,22 +274,9 @@ spec:
258274
retryStrategy:
259275
limit: 2
260276

261-
# - name: sub-prepare-bucket
262-
# inputs:
263-
# parameters:
264-
# - name: primary_cluster
265-
# container:
266-
# name: prepare-bucket
267-
# image: harbor.taco-cat.xyz/tks/hyperkube:v1.18.6
268-
# command:
269-
# - /bin/bash
270-
# - '-c'
271-
# - |
272-
# echo "prepare bucket for the '{{workflow.parameters.organization_id}}' (clusters: '{{inputs.parameters.primary_cluster}}')"
273-
# activeDeadlineSeconds: 900
274-
# retryStrategy:
275-
# limit: 2
276-
277+
# function sub-pre-change-logging-target
278+
# 1. Change endpoint of fluentbit-output (all in org.)
279+
# 2. Change endpoint of thanos-sidecar in prometheus-pod (all in org.)
277280
- name: sub-pre-change-logging-target
278281
inputs:
279282
parameters:
@@ -546,6 +549,104 @@ spec:
546549
path: /mnt/out/modified_cluster_list.txt
547550
activeDeadlineSeconds: 900
548551

552+
- name: sub-change-thanos-sidecar
553+
inputs:
554+
parameters:
555+
- name: primary_cluster
556+
- name: member_clusters
557+
container:
558+
name: logging-target-changer
559+
image: harbor.taco-cat.xyz/tks/shyaml_jq_yq_kubectl_python:3.11
560+
command:
561+
- /bin/bash
562+
- '-c'
563+
- |
564+
#/bin/bash
565+
566+
set -ex
567+
568+
function log() {
569+
level=$1
570+
msg=$2
571+
date=$(date '+%F %H:%M:%S')
572+
echo "[$date] $level $msg"
573+
}
574+
575+
current_cluster={{workflow.parameters.cluster_id}}
576+
primary_cluster={{inputs.parameters.primary_cluster}}
577+
member_clusters="{{inputs.parameters.member_clusters}}"
578+
empty_char=
579+
580+
if [ -z ${primary_cluster} ] || [ "${primary_cluster}" = "$empty_char" ]; then
581+
primary_cluster=${current_cluster}
582+
fi
583+
584+
S3_Service="s3://ap-northeast-2"
585+
cp /kube/value kubeconfig_adm
586+
export KUBECONFIG=kubeconfig_adm
587+
588+
#################
589+
# updates
590+
#################
591+
GIT_ACCOUNT={{workflow.parameters.github_account}}
592+
if [[ $GIT_SVC_URL == https://* ]]; then
593+
repository_base=https://${TOKEN//[$'\t\r\n ']}@${GIT_SVC_URL/http:\/\//}/${GIT_ACCOUNT}/
594+
else
595+
repository_base=http://${TOKEN//[$'\t\r\n ']}@${GIT_SVC_URL/http:\/\//}/${GIT_ACCOUNT}/
596+
fi
597+
598+
log "INFO" "##### change the loki target to $LOKI_HOST:$LOKI_PORT and $S3_Service (the current target is ${current_cluster})"
599+
[ -d ${current_cluster} ] || git clone ${repository_base}${current_cluster}
600+
cd ${current_cluster}
601+
602+
yq -i e "del(.charts[] | select(.name == \"thanos-config\").override.objectStorage)" ${current_cluster}/lma/site-values.yaml
603+
yq -i e ".charts |= map(select(.name == \"thanos-config\").override.objectStorage.type=\"s3\")" ${current_cluster}/lma/site-values.yaml
604+
yq -i e ".charts |= map(select(.name == \"thanos-config\").override.objectStorage.rawConfig.endpoint=\"s3.ap-northeast-2.amazonaws.com\")" ${current_cluster}/lma/site-values.yaml
605+
yq -i e ".charts |= map(select(.name == \"thanos-config\").override.objectStorage.rawConfig.region=\"ap-northeast-2\")" ${current_cluster}/lma/site-values.yaml
606+
yq -i e ".charts |= map(select(.name == \"thanos-config\").override.objectStorage.rawConfig.bucket=\"${primary_cluster}-tks-thanos\")" ${current_cluster}/lma/site-values.yaml
607+
yq -i e ".charts |= map(select(.name == \"thanos-config\").override.objectStorage.rawConfig.signature_version2=false)" ${current_cluster}/lma/site-values.yaml
608+
609+
git config --global user.name "tks"
610+
git config --global user.email "[email protected]"
611+
612+
if [[ `git status --porcelain` ]]; then
613+
log "INFO" "##### commit changes on ${current_cluster} to use s3"
614+
cmessage="changes on ${current_cluster} to use s3"
615+
git add ${current_cluster}/lma/site-values.yaml
616+
git commit -m "change loki and thanos endpoints. (by set-primary workflow)" -m "$cmessage"
617+
git push
618+
modified_clusters=${current_cluster}
619+
# echo -n "${current_cluster} " >> /mnt/out/modified_cluster_list.txt
620+
else
621+
log "INFO" "No change on the cluster ${current_cluster}"
622+
echo NO_CHANGE_HERE > /mnt/out/modified_cluster_list.txt
623+
fi
624+
cd -
625+
rm -rf ${current_cluster}
626+
627+
jq -n '$ARGS.positional' --args $modified_clusters > /mnt/out/modified_cluster_list.txt
628+
629+
env:
630+
- name: OBJECT_SOTRE
631+
value: "{{workflow.parameters.object_store}}"
632+
envFrom:
633+
- secretRef:
634+
name: "git-svc-token"
635+
volumeMounts:
636+
- name: kubeconfig-adm
637+
mountPath: "/kube"
638+
- name: out
639+
mountPath: /mnt/out
640+
volumes:
641+
- name: out
642+
emptyDir: {}
643+
outputs:
644+
parameters:
645+
- name: changed
646+
valueFrom:
647+
path: /mnt/out/modified_cluster_list.txt
648+
activeDeadlineSeconds: 900
649+
549650
- name: sub-sync-organization-changes
550651
inputs:
551652
parameters:
@@ -658,10 +759,10 @@ spec:
658759
git add ${primary_cluster}/lma/site-values.yaml
659760
git commit -m "change thanos-query stores. (by set-primary workflow)" -m "$cmessage"
660761
git push
661-
echo ${primary_cluster} > /mnt/out/primary_cluster.txt
762+
echo ${primary_cluster} > /mnt/out/changed.txt
662763
else
663764
log "INFO" "No change on the cluster ${member}"
664-
echo NO_CHANGE_HERE > /mnt/out/primary_cluster.txt
765+
echo NO_CHANGE_HERE > /mnt/out/changed.txt
665766
fi
666767
667768
if [ "$OBJECT_SOTRE" != "s3" ]; then
@@ -673,7 +774,7 @@ spec:
673774
git add ${primary_cluster}/lma/site-values.yaml
674775
git commit -m "change iamRoles(s3). (by set-primary workflow)" -m "$cmessage"
675776
git push
676-
echo ${primary_cluster} > /mnt/out/primary_cluster.txt
777+
echo ${primary_cluster} > /mnt/out/changed.txt
677778
else
678779
log "INFO" "(iamRoles) No change on the cluster ${member}"
679780
fi
@@ -702,9 +803,9 @@ spec:
702803
emptyDir: {}
703804
outputs:
704805
parameters:
705-
- name: primary_cluster
806+
- name: changed
706807
valueFrom:
707-
path: /mnt/out/primary_cluster.txt
808+
path: /mnt/out/changed.txt
708809
activeDeadlineSeconds: 900
709810

710811

0 commit comments

Comments
 (0)