npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cloudoperators/juno-k8s-client

v1.0.1

Published

JavaScript client for Kubernetes API

Downloads

85

Readme

JavaScript client for Kubernetes API

This client is designed for use in the browser (not tested in Node.js). It utilizes the fetch API and is compatible with all modern browsers. For older browsers, the fetch polyfill (github/fetch) should be used.

The k8sclient facilitates communication with the Kubernetes API. It minimizes data interpretation and adds only essential logic. In addition to standard HTTP methods like GET or POST, it implements the WATCH method, which establishes a stream to the server and reacts to events.

All functions return Promise objects and can therefore be processed with a chain of then calls. Potential errors can be caught using the catch method.

The client requires a Kubernetes API endpoint and a token. Once both parameters are provided, the following functions are available:

  • head
  • get
  • post
  • put
  • patch
  • delete
  • watch

Installation

With npm:

npm install @cloudoperators/juno-k8s-client

With yarn:

yarn add @cloudoperators/juno-k8s-client

Example Code (ES6)

List all pods

In this code snippet, we use the get method to fetch all pods from the Kubernetes API endpoint. The retrieved data is then logged to the console.

import { createClient } from "@cloudoperators/juno-k8s-client"

const apiEndpoint = "https://k8s-api.com"
let token = "BEARER-TOKEN"

const apiClient = createClient({ apiEndpoint, token })

apiClient.get("/api/v1/pods").then((data) => console.log(data))

Create a new namespace

You can use this example to create a new namespace in the Kubernetes cluster.

import { createClient } from "@cloudoperators/juno-k8s-client"

const apiEndpoint = "https://k8s-api.com"
let token = "BEARER-TOKEN"

const apiClient = createClient({ apiEndpoint, token })

apiClient.post("/api/v1/namespaces", {
  apiVersion: "v1",
  kind: "Namespace",
  metadata: {
    name: "my_namespace",
    annotations: { displayName: "My Namespace" },
    labels: { app: "my-app" },
  },
})

Delete a namespace

The following example demonstrates how to delete a namespace in the Kubernetes cluster.

import { createClient } from "@cloudoperators/juno-k8s-client"

const apiEndpoint = "https://k8s-api.com"
let token = "BEARER-TOKEN"

const apiClient = createClient({ apiEndpoint, token })

apiClient.delete("/api/v1/namespaces/my_namespace")

Refresh Token

Use this example to refresh the authentication token.

import { createClient } from "@cloudoperators/juno-k8s-client"

const apiEndpoint = "https://k8s-api.com"
let token = "BEARER-TOKEN"

const apiClient = createClient({ apiEndpoint, token })

apiClient.refreshToken("NEW-TOKEN")

Watch

The watch call establishes a persistent connection to the Kubernetes API server and listens for changes to the specified resource. In this example, we're watching for changes to pods ("/api/v1/pods").

The watch call should be explicitly started using podsWatch.start() to begin listening for changes, and it can be cancelled using podsWatch.cancel() when it's no longer needed.

import { createClient } from "@cloudoperators/juno-k8s-client"

const apiEndpoint = "https://k8s-api.com"
let token = "BEARER-TOKEN"

const apiClient = createClient({ apiEndpoint, token })
const dispatch = (action) => console.log(action)

// Initialize the watch call
const podsWatch = apiClient
  .watch("/api/v1/pods")
  .on(apiClient.WATCH_ERROR, () => console.log("ERROR"))
  .on(apiClient.WATCH_ADDED, (items) => dispatch({ type: "RECEIVE_ITEMS", items }))
  .on(apiClient.WATCH_MODIFIED, (items) => dispatch({ type: "UPDATE_ITEMS", items }))
  .on(apiClient.WATCH_DELETED, (items) => dispatch({ type: "DELETE_ITEMS", items }))

// Start the watch call to begin listening for changes
podsWatch.start()

// Optionally, set a timeout to cancel the watch call after a certain period
setTimeout(podsWatch.cancel, 5 * 60 * 1000) // 5 minutes

Development

All dependencies of this project are expressed in its package.json file. Before you start developing, ensure that you have NPM installed, then run:

npm install
npm run test
npm run build