OpenShift REST Client for Node.js

10.0.0 · active · verified Tue Apr 21

openshift-rest-client is a Node.js library designed to interact with the OpenShift REST API. It provides a client that translates OpenShift API Path Item Objects (like `/apis/project.openshift.io/v1/projects`) into idiomatic JavaScript object chains, allowing developers to interact with resources using methods such as `.get()`, `.post()`, `.delete()`, `.patch()`, and `.put()`. The library is currently stable at version 10.0.0 and follows a release cadence tied to Node.js LTS versions, introducing breaking changes primarily for dropping support for End-of-Life Node.js runtimes. A key differentiator is its integration with the kubernetes-client module for configuration, enabling flexible authentication and cluster context management, similar to the Fabric8 Maven Plugin but tailored for Node.js environments. It simplifies API interaction by providing aliases for common API groups and supports query parameters and path templating, making it a robust tool for programmatic OpenShift management from Node.js applications.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize the OpenShift client, connect to an API endpoint (projects), and fetch resources. It includes basic error handling and uses environment variables for flexible configuration.

import { OpenshiftClient } from 'openshift-rest-client';
import type { OpenShiftClient } from 'openshift-rest-client';

// Configure the client using environment variables for safety, or a specific kubeconfig path.
// process.env.KUBECONFIG can point to a specific file, or OpenshiftClient() will try default paths.
const kubeConfigPath = process.env.KUBECONFIG || undefined;

async function runClientExample() {
  let client: OpenShiftClient;
  try {
    if (kubeConfigPath) {
      console.log(`Attempting to connect using kubeconfig: ${kubeConfigPath}`);
      client = await OpenshiftClient({ config: kubeConfigPath });
    } else {
      console.log('Attempting to connect using default kubeconfig or in-cluster configuration.');
      client = await OpenshiftClient();
    }

    console.log('Successfully connected to OpenShift API.');

    // Fetch all projects accessible by the authenticated user
    console.log('Fetching all projects...');
    const projectList = await client.apis['project.openshift.io'].v1.projects.get();
    console.log(`Found ${projectList.body.items.length} projects.`);

    // Log details of the first project found, if any
    if (projectList.body.items.length > 0) {
      const firstProjectName = projectList.body.items[0].metadata?.name;
      console.log(`First project name: ${firstProjectName}`);
    }

    // Example: List all build configs in the 'default' namespace
    console.log('Fetching build configs in default namespace...');
    const buildConfigs = await client.apis['build.openshift.io'].v1.namespaces('default').buildconfigs.get();
    console.log(`Found ${buildConfigs.body.items.length} build configs in 'default' namespace.`);

  } catch (error: any) {
    console.error('An error occurred:', error.message);
    if (error.response?.body) {
      console.error('API Error Response:', JSON.stringify(error.response.body, null, 2));
    }
  }
}

runClientExample();

view raw JSON →