docker & k8s/helm chart
[Helm Chart] 문자열에서 "파일명" 혹은 "파일경로" 만 추출하기
정선생
2024. 6. 21. 19:00
반응형
helm chart 2.x 버전에서 "파일경로"에서 볼륨마운트 경로를 유도하기위해서 파일경로만 추출하는 필요성이 생겼다.
파일경로가 필요했던 이유는 볼륨마운트할 경로를 유도해야했다. 그럼 어떻게 유도할 수 있을까?
/opt/airflow/template/subject-template.template
1. 파일경로만 추출하기
다음과 같이 helm chart 를 구성하면 파일명을 제거한 "/opt/airflow/template" 값을 유도할 수 있다.
split 한 이후, list 의 마지막값을 제외한 배열을 만들어 합치는 형태로 해결했다.
{{- $my_path := "/opt/airflow/template/subject-template.template" -}}
{{- $token := splitList "/" $my_path -}}
{{- $limit := ( add (len $token) -1 ) | int -}}
{{- if le $limit 0 -}}
{{- fail "Path Depth Error !!!" }}
{{- end -}}
{{- $token := slice $token 0 $limit -}}
{{- printf "%s" (join "/" $token) }}
2. 파일명만 추출하기
list 에서 마지막 index 의 값을 추출하는것이 존재하기 때문에 | last 를 선언하면 쉽게 유도 가능하다.
이렇게 사용하면 "subject-template.tamplte" 값을 유도할 수 있다.
{{- $my_path := "/opt/airflow/template/subject-template.template" -}}
{{- $token := splitList "/" $my_path -}}
{{- printf "%s" ( $token | last ) }}
요즘 helm chart 2.x 환경에서 k8s 배포를 위한 yaml 노가다를 하다보니 간단한 조건이나 로직을 사용해도 한참 헤메는것 같다.
반응형