'Getting unauthorize message while using @kubernetes/client-node

const { v4: uuidv4 } = require("uuid");
const k8s = require("@kubernetes/client-node");
const kc = new k8s.KubeConfig();
kc.loadFromDefault()
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

    let USER_ID=Date.now(), LEVEL=1, DIFFICULTY="start", DRONE="dr1", RESET=false
 const definition = {
    apiVersion: "v1",
    kind: "Pod",
    metadata: {
      annotations: {
        key1: "value1",
        key2: "value2",
      },
      name: USER_ID,
      labels: { app: "simulator-app", name: USER_ID },
    },
    spec: {
      containers: [
        {
          name: "simulator-app",
          image:
            ".....dkr.ecr.eu-central-1.amazonaws.com/simulator:latest",
          ports: [{ containerPort: 8080 }, { containerPort: 9090 }],
          tty: true,
          stdin: true,
          imagePullPolicy: "IfNotPresent",
          command: ["/bin/bash", "-c"],
          args: [
            'echo START....;LEVEL=%s;DIFFICULTY=%s;DRONE=%s;echo "LEVEL=${LEVEL}";echo "DIFFICULTY=${DIFFICULTY}";echo "DRONE=${DRONE}";cd /home/;source ~/.bashrc;echo "Bashrc Sourced";pwd;ls;echo "Starting simulator with ${LEVEL} level, ${DRONE} vehicle and ${DIFFICULTY} difficulty";source "/opt/ros/foxy/setup.bash";source "/home/rossimulator/install/setup.bash";ros2 launch simulator_bringup sim_nodes.launch.py level:=${LEVEL} drone_type:=${DRONE} difficulty:=${DIFFICULTY};echo Done;' %
              (LEVEL, DIFFICULTY, DRONE),
          ],
        },
      ],
    },
  };

k8sApi.createNamespacedPod("Default", definition)
.then(console.log)
.catch(console.log);

Getting the below error on the console

{
  response: {
    statusCode: 401,
    body: {
      kind: "Status",
      apiVersion: "v1",
      metadata: {},
      status: "Failure",
      message: "Unauthorized",
      reason: "Unauthorized",
      code: 401,
    },
    headers: {
      "audit-id": "07eba07d-6121-492f-9993-ece3fa8827c5",
      "cache-control": "no-cache, private",
      "content-type": "application/json",
      date: "Mon, 28 Mar 2022 02:48:59 GMT",
      "content-length": "129",
      connection: "close",
    },
    request: {
      uri: {
        protocol: "https:",
        slashes: true,
        auth: null,
        host: ".....gr7.eu-central-1.eks.amazonaws.com",
        port: 443,
        hostname: "......gr7.eu-central-1.eks.amazonaws.com",
        hash: null,
        search: null,
        query: null,
        pathname: "/api/v1/namespaces/Default/pods",
        path: "/api/v1/namespaces/Default/pods",
        href: "https://..................eu-central-1.eks.amazonaws.com/api/v1/namespaces/Default/pods",
      },
      method: "POST",
      headers: {
        Accept: "application/json",
        Authorization: "Bearer k8s-aws-v1.....",
        "content-type": "application/json",
        "content-length": 506,
      },
    },
  },
  body: {
    kind: "Status",
    apiVersion: "v1",
    metadata: {},
    status: "Failure",
    message: "Unauthorized",
    reason: "Unauthorized",
    code: 401,
  },
  statusCode: 401,
  name: "HttpError",
};

What could be the possible reason for getting this error?

While the it works in the pythong this way

from flask import Flask, jsonify
import time
from kubernetes import client, config
import uuid
from kubernetes.client.rest import ApiException

app = Flask(__name__)

config.load_kube_config(
    context="arn:aws:eks:eu-central-1:9010......:cluster/sim-cluster"
)
v1 = client.CoreV1Api()

# definition - same as above
v1.create_namespaced_pod(body=definition, namespace="default")

I did not found load_kube_config equivalent method in the library



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source