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

kubernetes-enhanced-informer

v0.2.5

Published

Informer for kubernete based axios

Downloads

11

Readme

Kubernetes Axios Informer

An informer implementation based on axios for @kubernetes/client-node.

Purpose

We found the following issues with current informer implementation of @kubernetes/client-node(v0.14.0):

  • The ongoing request can not be abort, which means connection will hang even if we invoke informer.stop() method manually.
  • The api of informer is not developer-friendly. Current informer api doesn't have some kind of event like sync which will tell us the data in the cache has been sync after error in the connection. In some situation like synchronizing data from cluster to external database, the sync event is very important because we need to know which data should be deleted from database when it is not in the cluster any more.
  • Does not have SharedInformerFactory like client-go. SharedInformer will share the same underlying cache which will reduce a lot of memory use when you use the informer heavily in your application.

What is included:

  • An different informer with some apis not included in its counterpart implementation of @kubernetes/client-node (DONE)
  • A webRequest implementation based on axios for Watch (DONE)
  • A SharedInformerFactory for sharing the same informer of the exact same resource of the same cluser (TO DO)

What is not included

For any other things except the above you want to use to interact with kubernetes cluster, you should use the @kubernetes- node/javascript package.

Installation

// since kubernetes-axios-informer is based on @kubernetes/client-node you need to install it first.
npm install @kubernetes/client-node
npm install kubernetes-axios-informer

Example code

List and watch change of pods resource in the cluster

const k8s = require('@kubernetes/client-node')
const { Watch } = require('@kubernetes/client-node')
const { makeInformer, EVENT, webRequest } = require('kubernetes-axios-informer')

const kc = new k8s.KubeConfig()

kc.loadFromDefault()

const k8sApi = kc.makeApiClient(k8s.CoreV1Api)

const listFn = () => k8sApi.listPodForAllNamespaces()

const podId = (pod) => {
  return `name: ${pod.metadata.name} at namespace ${pod.metadata.namespace}`
}

const informer = makeInformer(kc, '/api/v1/pods', listFn)

informer.on(EVENT.ADD, obj => {
  console.log(`add pod ${podId(obj)}`)
})

informer.on(EVENT.UPDATE, obj => {
  console.log(`updated pod ${podId(obj)}`)
})

informer.on(EVENT.DELETE, obj => {
  console.log(`delete pod ${podId(obj)}`)
})

informer.onSync((pods) => {
  console.log('pod has been resynced, update here')
})

informer.start()
  .then(() => {
    console.log('informer is started')
  })

APIs

Informer

informer.start()

This method will start a informer which means it will start to list and watch in the cluster. Please note that if current informer has been started, this will be a no-ops.

informer.stop()

This method will stop current informer which means no more events will be received.

informer.onSync((objects: T[]) => void)

This method will register an event listener for the resync event. Every time after the informer resync resources from the cluster, the registered event listeners will be called.

informer.on(EVENT: string, callback)

This method is used to register event listeners for informer, the event can be ADD, UPDATE, DELETE or ERROR.

informer.cache

This will return the underlying cache object of this informer.

Cache

cache.list(namespace?: string)

This method will return all of the objects under a namespace. If namespace is not specified, it will return all of the objects in the cache.

cache.get(name: string, namespace?: string)

This method will return the first object with the specified namespace and name.