cyphernetes

module
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2024 License: MIT

README ΒΆ

Cyphernetes

Go Report Card Go Reference License

Cyphernetes turns this: 😣

# Select Deployments in all Namespaces which are scaled to zero,
# find all Ingresses routing into these deployments -
# finally for each Ingress change it's ingress class to 'inactive':

kubectl get deployments -A -o json | jq -r '.items[] | select(.spec.replicas == 0) | \
[.metadata.namespace, .metadata.name, (.spec.selector | to_entries | map("\(.key)=\(.value)") | \
join(","))] | @tsv' | while read -r ns dep selector; do kubectl get services -n "$ns" -o json | \
jq -r --arg selector "$selector" '.items[] | select((.spec.selector | to_entries | \
map("\(.key)=\(.value)") | join(",")) == $selector) | .metadata.name' | \
while read -r svc; do kubectl get ingresses -n "$ns" -o json | jq -r --arg svc "$svc" '.items[] | \
select(.spec.rules[].http.paths[].backend.service.name == $svc) | .metadata.name' | \
xargs -I {} kubectl patch ingress {} -n "$ns" --type=json -p \
'[{"op": "replace", "path": "/spec/ingressClassName", "value": "inactive"}]'; done; done

Into this: 🀩

# Do the same thing!

MATCH (d:Deployment)->(s:Service)->(i:Ingress)
WHERE d.spec.replicas=0
SET i.spec.ingressClassName="inactive";

How?

Cyphernetes is Cypher repurposed for working with the Kubernetes API. It is a mixture of ascii-art, SQL and JSON and it lets us express Kubernetes operations in an efficeint way that is also fun and creative.

Examples
MATCH (d:Deployment)
RETURN d.spec.replicas AS desiredReplicas, 
       d.status.availableReplicas AS runningReplicas;

{
  "d": [
    {
      "desiredReplicas": 2,
      "name": "coredns",
      "runningReplicas": 2
    }
  ]
}

Query executed in 9.081292ms

Cyphernetes' superpower is understanding the relationships between Kubernetes resource kinds. This feature is expressed using the arrows (->) you see in the example queries. Relationships let us express connected operations in a natural way, and without having to worry about the underlying Kubernetes API:

# This is similar to `kubectl expose`
> MATCH (d:Deployment {name: "nginx"})
  CREATE (d)->(s:Service);

Created services/nginx

Query executed in 30.692208ms
It has macros and graphs too

Macros are minimalistic, user-extensible & batteries included stored procedures. They turn the Cyphernetes shell into a handy kubectl alternative. Many useful macros are included - and it's easy to define your own.

# This macro creates a service and public ingress for a deployment.
# Cyphernetes can optionally draw a graph of affected nodes as ASCII art.

> :expose_public nginx foo.com
Created services/nginx
Created ingresses/nginx

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ *Ingress* nginx β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  β”‚
  β”‚ :ROUTE
  β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ *Service* nginx β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

{
  "ingresses": [
    {
      "Host": "foo.com",
      "Path": "/",
      "Service": "nginx",
      "name": "nginx"
    }
  ],
  "services": [
    {
      "ClusterIP": "10.96.164.152",
      "Type": "ClusterIP",
      "name": "nginx"
    }
  ]
}

Macro executed in 50.305083ms

For more usage examples, please see the Usage Guide.

Get Cyphernetes

Using go:

go install github.com/avitaltamir/cyphernetes/cmd/cyphernetes@latest

Alternatively, grab a binary from the Releases page.

Development

Cyphernetes is written in Go and utilizes a parser generated by goyacc to interpret the custom query language.

Prerequisites
  • Go (1.16 or later)
  • goyacc (for generating the parser)
  • Make (for running make commands)
Getting Started

To get started with development:

Clone the repository:

git clone https://github.com/avitaltamir/cyphernetes.git

Navigate to the project directory:

cd cyphernetes
Building the Project

Use the Makefile commands to build the project:

  • Build & Test:
make
  • To build the binary:
make build
  • To run tests:
make test
  • To generate the grammar parser:
make gen-parser
  • To clean up the build:
make clean
Contributing

Contributions are welcome! Please feel free to submit pull requests, open issues, and provide feedback.

License

Cyphernetes is open-sourced under the MIT license. See the LICENSE file for details.

Acknowledgments

  • Thanks to the Neo4j community for the inspiration behind the query language.

Authors

Directories ΒΆ

Path Synopsis
cmd
operator module
pkg

Jump to

Keyboard shortcuts

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