Node.js cannot make request to k8s api server

9/13/2019

I'm trying to make an api call to k8s api server to update deployment. I can use below to update deployment.

curl -X PATCH --header "Authorization: Bearer $TOKEN" --insecure -H 'Content-Type: application/strategic-merge-patch+json' --data '
{ "spec": { "template": { "spec": { "containers": [ { "name": "nlu","env": [ { "name": "LAST_UPDATE", "value": "123123" } ] } ] } } } }' \
    'https://10.1.0.4:6443/apis/apps/v1/namespaces/prod/deployments/nlu?fieldManager=strategic'

But when I use the node.js request module to make request, I got an error like below

message:"read ECONNRESET"

Here is my code

restart()
{
    const options = {
        url: "http://10.1.0.4:6443",
        body: JSON.stringify({ 
             "spec": { 
                 "template": { 
                     "spec": { 
                         "containers": [ 
                             { 
                                 "name": "nlu",
                                 "env": [ 
                                     { 
                                         "name": "LAST_UPDATE", 
                                         "value": "20190909" 
                                     } 
                                 ] 
                             } 
                         ] 
                     } 
                 } 
             } 
         }),
        auth: {
            'bearer': "mytoken"
       }
    };
    return new Promise(function(resolve,reject){
        request.patch(options, (err , res)=>{
            if(err)
            {
                reject(err)
            } else 
            {
                resolve('done')
            }
        })
    })
}

How can I fix this problem, any help would be appreciated?

-- Andy Lai
kubernetes
node.js

1 Answer

9/18/2019

I found out I missed something so that the k8s server rejected my request. First, the url should start with https instead of http. Second, I was supposed to add content-type "application/strategic-merge-patch+json" in the request head. Third, I also have to add rejectUnauthorized: false in the request to ignore the error when failing to verify the server's identity.

The correct request should be like

const options = {
    url: "https://10.1.0.4:6443",
    headers: {
            "content-type": "application/strategic-merge-patch+json"
    },
    rejectUnauthorized: false,
    body: JSON.stringify({ 
         "spec": { 
             "template": { 
                 "spec": { 
                     "containers": [ 
                         { 
                             "name": "nlu",
                             "env": [ 
                                 { 
                                     "name": "LAST_UPDATE", 
                                     "value": "20190909" 
                                 } 
                             ] 
                         } 
                     ] 
                 } 
             } 
         } 
     }),
    auth: {
        'bearer': "mytoken"
   }
};
-- Andy Lai
Source: StackOverflow