Why don't kubectl commands work when I run them from JavaScript code on Node.js?

79 views Asked by At

I'm trying to execute this code


    const {exec} = require('child_process');

    exec('/snap/bin/kubectl version', (error, stdout, stderr) => {
        if (error) {
            console.error(`Error executing kubectl: ${error}`);
            console.error(`Exit Code: ${error.code}`);
            console.error(`Error Signal: ${error.signal}`);
            return;
        }
        if (stderr) {
            console.error(`stderr: ${stderr}`);
            return;
        }
        console.log(`stdout: ${stdout}`);
    });

and got these console outputs:

  • Command failed: /snap/bin/kubectl version
  • Code: 1
  • Signal: null

Details:

  • OS: Linux (Elementary OS 6)
  • Node.js version is v18.18.0 and node: /snap/bin/node.yarn /snap/bin/node.yarnpkg /snap/bin/node /snap/bin/node.npx /snap/bin/node.npm - paths
  • kubectl is installed and work correctly
  • the full kubectl path is /snap/bin/kubectl
  • In the terminal, all 'kubectl ' commands work correctly
  • The command 'kubect version' had been chosen, for example

I tried to make my node.js script executable (chmode +x <myscript.js>).

DETAILS: I have installed Node.js using Snap. However, I am experiencing errors in the console when attempting to run programs installed through Snap from Node.js. The errors displayed are as follows: Error: Command failed: docker version, Exit Code: 1, Error Signal: null. Furthermore, the stdout is empty. Maybe, it's necessary.

1

There are 1 answers

0
Victor Smol On BEST ANSWER

A snap application cannot run other snap applications. To make this possible, you need to create a snap connection between the required applications. In my case, I need to establish a snap connection between node.js and kubectl, as both are installed as snap applications. However, it is important to note that the node.js snap does not have any interfaces, which means it cannot create connections with other snap applications. Therefore, it is impossible to execute 'kubectl ' from a JavaScript script in this specific scenario.