Search code examples
curlkuberneteskubernetes-secrets

Error while creating k8s secret through rest api


I want to create a secret through kubectl api. Below is the script, I am running but getting error in parsing yaml file. Please help

    curl -vk \
    -X POST \
    -d @- \
    -H "Authorization: Bearer $(cat token)" \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json' \
    https://ip:port/api/v1/namespaces/nginx-ingress/secrets <<'EOF'
{
"apiVersion": "v1",
"kind": "Secret",
"metadata": {
"namespace": "nginx-ingress",
},
"type": "Opaque"
"data": {
"username": "YWRtaW4=",
"password": "MWYyZDFlMmU2N2Rm"
}
EOF

Error:

message": "the object provided is unrecognized (must be of type Secret): couldn't get version/kind; json parse error: invalid character '}' looking for beginning of object key string ({\"apiVersion\": \"v1\",\"kind\": \"S ...)", "reason": "BadRequest",


Solution

  • I changed my JSON structure in which I added the curly brace which I missed in the end and one comma in the type key.

    curl -vk \
        -X POST \
        -d @- \
        -H "Authorization: Bearer $(cat token)" \
        -H 'Accept: application/json' \
        -H 'Content-Type: application/json' \
        https://192.168.2.100:6443/api/v1/namespaces/nginx-ingress/secrets <<'EOF'
    {
     "apiVersion":"v1",
     "kind" :"Secret",
     "metadata" :{"namespace" :"nginx-ingress","name":"mysecret1"},
     "type": "Opaque",
     "data": {"username": "YWRtaW4=","password": "MWYyZDFlMmU2N2Rm"}
    }
    

    It worked.