Skip to content

lassoColombo/nuke

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

202 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nuke: Nushell Kubernetes Integration

Nuke – A Nushell-native kubectl toolkit


Why?

Interacting with kubernetes looks too much like this:

# does not actually work
kubectl get po 
| detect columns --guess 
| update AGE {|po|
    $po.AGE 
    | str replace 's' 'sec' 
    | str replace 'm' 'min' 
    | str replace 'h' 'hour' 
    | str replace 'd' 'day' 
    | into datetime 
}
| sort-by AGE

I wish i could just kubectl get po | sort-by age and leverage the power of nushell. Don't you?


So What?

Nuke re-implements some of kubectl commands.
It talks directly with the kube-apiserver to retrieve structured objects and typed data, so we can run things like:

nuke get po -o wide | where node in (
  (nuke top no | sort-by memory -r | first 3).name
) # gets the pods running on the most overloaded nodes
  • Nuke does not aim to reimplement all of kubectl. It focuses on the commands that wuould benefit from nushell's structured data.
  • Nuke tries to mimick kubectl syntax to recreate a familiar environment. No need to learn a new tool.
  • Nuke uses your kubeconfig as configuration. No additional setup is required.
  • Nuke tries to adhere to kubectl semantics, integrating it with richer data.

Implemented Commands

Nuke Command kubectl Equivalent
nuke get kubectl get
nuke http-get kubectl get --raw
nuke api-resources kubectl api-resources
nuke api-versions kubectl api-versions
nuke rollout status kubectl rollout status
nuke rollout history kubectl rollout history
nuke top kubectl top
nuke config kubectl config

How Nuke Works

  1. Reads your kubeconfig
  2. Authenticates against the API server
  3. Performs HTTP requests directly
  4. Applies resource-specific formatter
  5. Returns structured Nushell data

If no formatter is implemented, a default formatter is used.

Authentication

Nuke authenticates with the Kubernetes API server using the credentials defined in your kubeconfig, following a precedence model similar to kubectl.

Supported Methods

  • Bearer token authentication
  • Bearer tokenfile authentication
  • client certificate (mTLS)
  • client certificatefile (mTLS)
  • basic auth

Planned Support

  • exec-plugins

Impersonation

If configured in the kubeconfig, Nuke also supports Kubernetes impersonation via the as and as-groups user fields.

TLS Verification

Cluster TLS settings are also taken from kubeconfig, as well as the insecure-skip-tls-verify option.

Installation

# Clone this repository into one of your NU_LIB_DIRS:
let nuke_basedir = ([($env.NU_LIB_DIRS | first) nuke] | path join)
git clone git@github.com:lassoColombo/nuke.git $nuke_basedir

# Verify installation:
use nuke
nuke api-resources

Dependencies

  • curl — used for HTTP communication with the API server

Formatters

Commands that retrieve objects support three formats:

Format Description
compact minimal view (Default for lists).
wide extended attributes (Default for single objects).
full the complete object from the API

Nuke is currently under active development, so not all resources have a dedicated formatter yet.
When a specific formatter isn’t available, Nuke automatically falls back to the default formatter.

Custom Formatters

You can override formatters or implement new ones using environment variables:

  • NUKE_RESOURCE_FORMATTERS
  • NUKE_ROLLOUTSTATUS_FORMATTERS
  • NUKE_ROLLOUTHISTORY_FORMATTERS
  • NUKE_METRIC_FORMATTERS

Example:

# NUKE_RESOURCE_FORMATTERS, NUKE_ROLLOUTSTATUS_FORMATTERS and NUKE_METRIC_FORMATTERS all adhere to the following interface:
$env.NUKE_RESOURCE_FORMATTERS = { 
  apps: { # api group (see `nuke api-versions | select group name version` for the full list)
    v1: { # api version 
      deployments: { # resource name
        | output?: string = compact | # either compact or wide
        #
        # your formatter takes in input the whole object from the kube-apiserver and must return
        # a formatted object as a nushell record
        let obj = $in
        let res = {
          name: $obj.metadata.name
          namespace: $obj.metadata.namespace
        }
        if $output == compact {
          return ($res
            | insert containers ($obj.spec.template.spec.containers | length)
          )
        }
        $res 
        | insert containers $obj.spec.template.spec.containers
      }
    }
  }
}

# NUKE_ROLLOUTHISTORY_FORMATTERS adhere to the following interface:
$env.NUKE_ROLLOUTHISTORY_FORMATTERS = { 
  apps: { # same structure as above
    v1: {
      deployments: {
      |
      owner: record, # the owner object (eg: the whole deployment/daemonset)
      parents: list, # all RS items in the namespace (eg: repicasets for deployments, controllerrevisions for the others)
      revision: int, # if set, return only this revision 
      output?: string = compact # either compact or wide
      |
        # your formatting logic here
      }
    }
  }
}

Nuke Http-Get

The http-get method performs an authenticated request to the kube API-server and returns the result as structured data without performing any additional parsing.

The request url can be specified as a string or as a record as expected by url-join.

# get pods
nuke http-get api/v1/namespaces/<namespace>/pods 

# get pods by label
nuke http-get {
  path: api/v1/namespaces/<namespace>/pods 
  params: [
    {key: labelSelector, value: 'my-label in (my-value-1, my-value-2)'}
  ]
}

# get aggregated api discovery
nuke http-get apis -H {
   Accept: "application/json;v=v2;g=apidiscovery.k8s.io;as=APIGroupDiscoveryList"
}

Kubeconfig

Nuke provides utilities to manage your kubectl configuration, and to help you switch context swiftly.

Context Switching

Context switching takes inspiration from kubectx and kubens:

nuke config switch-namespace monitoring # explicit switch
nuke config switch-context # interactive switch - triggers input list

Configuration Utilities

Nuke provides structured access to your kubeconfig data:

nuke config # returns the kubeconfig
nuke config path # get the path to the current detected kubeconfig
nuke config get-contexts # get all the contexts
nuke config get-contexts --current # get the current context
nuke config get-current-namespace # get the current namespace
nuke config get-clusters --current # get the current cluster
nuke config get-users --context k8s-001 # get the user of context k8s-001
nuke config get-cluster --context k8s-qa # get the cluster of context k8s-qa

Roadmap

  • Improve coverage of built-in resource formatters
  • Implement nuke describe command
  • Implement --revision flag for rollout command
  • Additional authentication methods
    • Exec plugins
  • Watch functionality

Contributing

Contributions, bug reports, and feature requests are welcome.

Before opening an issue or pull request, please read: CONTRIBUTING.md The contributing guide includes:

  • Development setup
  • How to reproduce bugs
  • KIND cluster configuration
  • Metrics server setup
  • Formatter development guidelines

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors