Skip to content

Latest commit

 

History

History
187 lines (138 loc) · 6.75 KB

README.md

File metadata and controls

187 lines (138 loc) · 6.75 KB
Screenshot 2024-10-15 at 0 03 00

Cyphernetes Logo (3 5 x 1 2 in)

Go Report Card Go Reference License Artifact Hub

Cyphernetes turns this: 😣

# Select all zero-scaled Deployments in all namespaces,
# find all Ingresses routing to these deployments -
# 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 a Cypher-inspired query language for Kubernetes. 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.

There are multiple ways to run Cyphernetes queries:

  1. Using the web client by running cyphernetes web from your terminal, then visiting http://localhost:8080
  2. Using the interactive shell by running cyphernetes shell in your terminal
  3. Running a single query from the command line by running cyphernetes query "your query" - great for scripting and CI/CD pipelines
  4. Creating a Cyphernetes DynamicOperator using the cyphernetes-operator which lets you define powerful Kubernetes workflows on-the-fly
  5. Using the Cyphernetes API in your own Go programs

To learn more about how to use Cyphernetes, refer to these documents:

  • LANGUAGE.md - a crash-course in Cyphernetes language syntax
  • CLI.md - a guide to using Cyphernetes shell, query command and macros
  • OPERATOR.md - a guide to using Cyphernetes DynamicOperator

Examples (from the Cyphernetes Shell)

# Get the desired and running replicas for all deployments
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

Get Cyphernetes

Using Homebrew:

brew install cyphernetes

Using go:

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

Alternatively, grab a binary from the Releases page.

Development

The Cyphernetes monorepo is a multi-package project that includes the core Cyphernetes Go package, a CLI, a web client, and an operator. Additionally, there's a grammer folder which contains the yacc grammar for generating the parser.

.
├── cmd # The CLI (this is where the cyphernetes binary lives)
│   └── cyphernetes
│       └── ...
├── grammar # The yacc grammar for generating the parser
│   └── ...
├── operator # The operator
│   └── ...
├── pkg # The core Cyphernetes package (and parser)
│   └── parser
│       └── ...
├── web # The web client
│   └── src
│       └── ...

Prerequisites

  • Go (Latest)
  • goyacc (for generating the parser)
  • Make (for running make commands)
  • NodeJS (Latest, for building the web client)
  • pnpm (9+, for building the web client)

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 Core Project

Running make will build the operator manifests and web client static assets, then build the binary and run the tests.

make

Building the Web Client

make web-build

Building the Operator

make operator-build

Contributing

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

License

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

Acknowledgments

Authors