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

@ccremer/kubernetes-client-fetch

v0.2.0

Published

Generic Kubernetes client run in the Browser

Downloads

5

Readme

Kubernetes Client

Query Kubernetes API from the browser. Ideal for SPA-like apps and CRDs.

Features

  • Common CRUD operations on resources:

    • get
    • list
    • watch
    • create
    • update
    • patch
    • delete
  • Support for most query parameters

  • Generic types

    • Some built-in, basic types available, growing as needed.
    • Add your own types
  • Interface-first, includes default implementation using the Fetch API.

  • Authentication with Kubernetes token (JWT). Extensible.

Getting started

Install the client

npm install @ccremer/kubernetes-client @ccremer/kubernetes-client-fetch

Setup the client and make a request

import { KubeClientBuilder } from '@ccremer/kubernetes-client-fetch'
import { SelfSubjectRulesReview } from '@ccremer/kubernetes-client/types/authorization.k8s.io'

// token:
// a valid JWT for Kubernetes.
// This could come from a password-field, oauth service etc.
const token = '...'
// apiUrl:
// Either a CORS-enabled remote URL with https,
// or proxied by the webserver to the actual API.
const apiUrl = '/api'

const client = KubeClientBuilder.DefaultClient(token, apiUrl)
client
  .create<SelfSubjectRulesReview>({
    apiVersion: 'authorization.k8s.io/v1',
    kind: 'SelfSubjectRulesReview',
    spec: {
      namespace: 'default',
    },
  })
  .then((selfSubjectRulesReview) => {
    console.debug('Created client with permissions', selfSubjectRulesReview.status)
  })
  .catch((err) => {
    console.error('could not fetch object', err)
  })

You can also provide your own resource type, as long as it fulfills the KubeMeta interface contract.

import { KubeObject } from '@ccremer/kubernetes-client/types/core'

export interface MyCustomResource extends KubeObject {
  apiVersion: 'customgroup/v1'
  kind: 'CustomResource'
  spec: {
    field: string
  }
}

ℹ️ The client doesn't set a default name or namespace when querying, so be sure you set the correct metadata.

Accessing Kubernetes API

Because of CORS, the default implementation expects the Kubernetes API to be available at /api and /apis, proxied by whatever webserver you are running. You can change the endpoint for it, e.g. set to /kube/ (no trailing slash), so that the endpoints are concatenated to /kube/api and /kube/apis.

Example configuration for Vite in Dev mode:

import { defineConfig } from 'vite'
import 'dotenv/config'

// https://vitejs.dev/config/
export default defineConfig(() => {
  return {
    server: {
      proxy: {
        '/apis': {
          target: process.env.VITE_KUBERNETES_API_URL,
          changeOrigin: true,
          secure: false,
        },
        '/api': {
          target: process.env.VITE_KUBERNETES_API_URL,
          changeOrigin: true,
          secure: false,
        },
      },
    },
  }
})

The /api endpoint is for core resources like Namespaces or Pods, while /apis is for resources under a group like apps for Deployments.

Alternatively, you can set the endpoint to a full URL like https://console.cluster.6443. However, keep CORS and other browser limitations in mind if your app is served under a different domain.

Extension points

The default built-in client can be extended in various points.

  • The KubeClientBuilder accepts various interfaces that are injected into the Client class.
  • Each API endpoint for a resource is generated based on metadata like apiVersion and kind. Implement UrlGenerator interface to provide your own generator and supply it to the builder.
  • Each HTTP request requires authorization. Implement the Authorizer interface and supply your implementation to the builder.
  • You can provide your own custom fetch()-like function to the builder.

Known Issues

  • The Config class returns a KubeConfig-like structure complete with cluster and user information. However, currently only a variant with a JWT token is supported, created with Config.FromToken() combined with DefaultAuthorizer.
  • There is no validation to the passed in payloads or returned results.
  • Many Kubernetes resource types are missing, and they're not (yet?) generated from the Kubernetes API scheme. Implement your own or better yet, contribute to @ccremer/kubernetes-client :)

Production readiness

This library is fairly new. Expect breaking changes as new experience is gained.

Other than that, this package follows SemVer.

Why

There is an official Kubernetes client. However, it's not yet ready for browsers and development seems a bit slow.