# 检查参数数量
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <name_value> <public_key> <private_key>" >&2
exit 1
fi
# 读取参数
NAME="$1"
# 公钥和私钥base64并去掉换行
TLS_CRT=$(echo -n "$2" | base64 | tr -d '\n')
TLS_KEY=$(echo -n "$3" | base64 | tr -d '\n')
# 构建 JSON 数据
JSON_DATA=$(cat <<EOF
{
"metadata": {
"name": "$NAME"
},
"data": {
"tls.crt": "$TLS_CRT",
"tls.key": "$TLS_KEY"
},
"type": "IngressTLS"
}
EOF
)
RESULT=$(curl --cert ./client-cert.pem --key ./client-key.pem -k \
-X PUT -H "Content-Type: application/json" \
-d "$JSON_DATA" \
"https://{k8s服务api}:6443/api/v1/namespaces/default/secrets/$NAME")
# 获取请求结果code
CODE=$(echo $RESULT | jq '.code')
# 判断如果失败,则将结果输出到错误信息
if [ "$CODE" -ne 200 ]; then
echo "$RESULT" >&2
else
echo "$RESULT"
fi