skip to Main Content

Using nodejs trying to parse attribute value(number of replicas) by running child_process kubectl deployment json output
Can you please suggest what I am doing wrong??

** Error: var deploymentInfo = item.spec.map((c) => { return { replicas: c.replicas} }); ^ TypeError: item.spec.map is not a function **

`
{

"apiVersion": "v1",
"items": [
    {
        "apiVersion": "extensions/v1beta1",
        "kind": "Deployment",
        "metadata": {
            "labels": {
                "env": "stage"
            },
            "name": "sl-deployment"
        },
        "spec": {
            "progressDeadlineSeconds": 2147483647,
            "replicas": 3,
            "revisionHistoryLimit": 3,
            "selector": {
                "matchLabels": {
                    "env": "nonprod",
                    "microservice": "svc-microservice",
                    "name": "sl-deployment"
                }
            },
            "strategy": {
                "rollingUpdate": {
                    "maxSurge": 1,
                    "maxUnavailable": 1
                },
                "type": "RollingUpdate"
            },
            "template": {
                "metadata": {
                    "labels": {
                        "env": "stage",
                        "microservice": "svc-microservice",
                        "name": "sl-deployment"
                    }
                },
                "spec": {
                    "containers": [
                        {
                            "image": "myregistry/svc-microservice/sl-deployment:stage.4.30",
                            "name": "sl-deployment",
                            "resources": {
                                "limits": {
                                    "cpu": "6",
                                    "memory": "16Gi"
                                },
                                "requests": {
                                    "cpu": "2",
                                    "memory": "6Gi"
                                }
                            }
                
                        }
                    ]
                   
                }
            }
        }
    } ]`

Tried the code pasted below

exec(`kubectl get deployment -n svc-microservice -o json`, (error, stdout, stderr) => {
        if (error) {
          console.log(`error: ${error.message}`);
          return;
        }
        if (stderr) {
          console.log(`stderr: ${stderr}`);
          return;
        }
        var deployjson = JSON.parse(stdout);
        
        for (let item of deployjson.items) {
          
          var  deploymentInfo = item.spec.map((c) => { return { replicas: c.replicas} });
          for (let info of deploymentInfo) {
            metadata[name][info.name] = { deployment:info.replicas}
          }
        }
        })

**
Error: var deploymentInfo = item.spec.map((c) => { return { replicas: c.replicas} });
^
TypeError: item.spec.map is not a function

**

2

Answers


  1. Chosen as BEST ANSWER

    I agree with dzylich. item.spec is an object

    Below code worked for me

    var deploymentInfo = deployjson.items.map((item) => {
                   return { replicas: item.spec.replicas };
               });
    

  2. It looks like the spec property is an object, so you can’t use the map function on it. The map function is only for arrays.

    If you just want to grab the replicas property, you don’t need to do anything special, just access the property on the spec object:

    item.spec.replicas
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search