khronoscope

command module
v0.0.0-...-9966cf0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2025 License: MIT Imports: 33 Imported by: 0

README

khronoscope

Khronoscope

WARNING: You probably don't want to point this to a prod Kubernetes cluster yet. I have made adjustments to make sure it doesn't overload the control plane but haven't tested it's load yet.

Imagine a tool like k9s that also lets you to inspect the state of your Kubernetes cluster BUT also let's you go back in time to see the state as it was at any point in time since you started the app.

This project is in it's VERY early stages, not in Alpha yet really but what it does do at the moment:

  • Connects to your current Kubernetes cluster
  • Supports the following resources: Namespaces, Nodes, Pods, ReplicaSets, DaemonSets, Deployments, and Services
  • Controls
    • Up/Down - Move the resource selection up and down the tree
    • Enter - Toggle folding the tree node you have selected
    • Alt Up/Alt Down - Move by a larger step value
    • Shift Up/Shift Down - Move the detail window up and down
    • Left/Right - Go backwards or forwards in time
    • Esc - Jump back to current time
    • Tab - Switch view orientation
    • Ctrl-C - Exit

Example

Internals

This application create a connection to your kubernetes cluster and starts watching for resource changes. It has a data structure called TemporalMap that is used to manage all the resource changes. The structure allows you to add/update/delete values stored in it and then you can query the map for it's state at a specific point in time. This let's the application show you the state of the cluster at any point since the start of the application.

Along with standard resource information the application also collects metrics data for Pods and Nodes. This is currently done with polling but needs to be switched to a watch as well.

I use BubbleTea for rendering and LipGloss for coloring, both great projects.

Adding new k8s resource types

A good example of how to add a new resource type can be seen in service.go. You implement something like the following, replacing {K8sResourceType} with the resource type you are implementing.

type {K8sResourceType}Renderer struct {
}

func format{K8sResourceType}Details(t *corev1.{K8sResourceType}) []string {
	var result []string

	// Basic details
	result = append(result, fmt.Sprintf("Name:           %s", t.Name))

	return result
}

func (r {K8sResourceType}Renderer) Render(resource Resource, details bool) []string {
	if details {
		return format{K8sResourceType}Details(resource.Object.(*corev1.{K8sResourceType}))
	}

	return []string{resource.Key()}
}

type {K8sResourceType}WatchMe struct {
}

func (n {K8sResourceType}WatchMe) Tick() {
}

func (n {K8sResourceType}WatchMe) Kind() string {
	return "{K8sResourceType}"
}

func (n *{K8sResourceType}WatchMe) Renderer() ResourceRenderer {
	return nil
}

func (n {K8sResourceType}WatchMe) convert(obj runtime.Object) *corev1.{K8sResourceType} {
	ret, ok := obj.(*corev1.{K8sResourceType})
	if !ok {
		return nil
	}
	return ret
}

func (n {K8sResourceType}WatchMe) ToResource(obj runtime.Object) Resource {
	return NewK8sResource(n.Kind(), n.convert(obj), n.Renderer())
}

func watchFor{K8sResourceType}(watcher *K8sWatcher, k KhronosConn) {
	watchChan, err := k.client.CoreV1().{K8sResourceType}("").Watch(context.Background(), v1.ListOptions{})
	if err != nil {
		panic(err)
	}

	go watcher.registerEventWatcher(watchChan.ResultChan(), {K8sResourceType}WatchMe{})
}

In main.go you will need to make a call to watchFor**{K8sResourceType}**. You will see examples of other resources being watched in main as well.

Future

In the future I could see a variation of this application being run as a server and storing it's data long term in a database so that multiple users can connect to it and be able to look through cluster state for longer amounts of time.

I'd also like to support other resources than what is currently supported, possibly a plugin system could be used to support any custom resources users want to add. Ones that I'd like to support out of the box include:

  • StatefulSet
  • ConfigMaps
  • PersistantVolume
  • Secrets

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL