반응형
이번 주에 저의 실수로 특정 서비스 장애가 있었습니다. 마지막 배포를 하고 테스트를 했었던 걸로 기억하는데 테스트를 잘못한 것 같습니다. ㅜ.ㅜ 오류는 쉽게 발견하여 수정했지만 반성을 하게 된 계기가 되었습니다.
오류내용
오류는 코드 끝에 탭이 있어 발생했습니다. 토큰 끝에 탭이 있어 쿠버네티스 sdk실행 오류가 발생했습니다.
오류재현
오류를 재현하기 위해 serviceaccount와 kubeconfig설정이 필요합니다.
name="developer"
namespace=default
token_name=$(kubectl -n $namespace get serviceaccount $name -o jsonpath='{.secrets[].name}')
token=$(kubectl -n $namespace get secret/$token_name -o jsonpath='{.data.token}' | base64 --decode)
저는 kubeconfig설정을 수동으로 yaml을 수정했습니다.
vi ~/.kube/config
---
apiVersion: v1
kind: Config
clusters:
- name: rancher-desktop
cluster:
server: https://127.0.0.1:6443
certificate-authority-data: ...
insecure-skip-tls-verify: false
users:
- name: developer
user:
token: $token
contexts:
- name: developer
context:
cluster: rancher-desktop
name: developer
user: developer
이제 python 코드를 작성합니다.
from kubernetes import client, config
# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config(context="developer")
v1 = client.CoreV1Api()
print("Listing nodes with their IPs:")
ret = v1.list_node(watch=False)
for i in ret.items:
print("%s\t%s\t%s" % (i.status.addresses[0].address, i.metadata.name, i.status.node_info.kubelet_version))
python code를 실행합니다. kubernetes 패키지가 필요합니다. 실행하면 403 예외가 발생합니다. developer계정에 role이 없기 때문입니다.
pip install kubernetes
python main.py
이제 오류를 재현해볼게요. kubeconfig에서 설정한 토큰 끝에 tab을 추가합니다.
다시 main.py를 실행하면 tab때문에 토큰을 읽을 수 없는 오류가 발생합니다.
python main.py
오류방지
자기도 모르게 줄 끝에 공백 또는 탭이 추가되는 것을 방지하는 방법은 2개가 있는 것 같습니다.
- IDE설정을 통해 파일 저장시 공백제거
- git hook을 사용하여 git commit 전 공백이 있는지 검사
git hook은 git을 사용할 때만 활용할 수 있어 IDE사용방법이 더 좋다고 생각합니다. 대표적으로 vscode에서 “trim trailing whitespace”로 파일 저장 시, 줄 끝 공백을 제거할 수 있습니다.
반응형
'회고모음' 카테고리의 다른 글
토요일에 일하면서 알게된것 (0) | 2023.02.05 |
---|---|
kubernetes feature gates (0) | 2023.01.27 |
iframe을 사용하는 서비스에 만난 x-frame-options deny오류 (0) | 2023.01.17 |
python 자식 쓰레드에서 signal 처리 오류 (0) | 2023.01.15 |
uuid를 잘못 설정하여 오류를 만나다. (0) | 2023.01.09 |