ksql

command module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2023 License: MIT Imports: 4 Imported by: 0

README

ksql, a SQL-like language tool for kubernetes

Codacy Badge Codacy Badge

Install

go install github.com/imuxin/ksql

Goal #1: bring SQL lanugage for kubernetes command line tool

Rule #1: JSONPath Support in column name and where filter

More jsonpath support details, please check kubernetes kubectl jsonpath support.

Rule #2: NAMESPACE LABEL NAME keyword support in sql statement
  • ps1: NS is abbreviation for NAMESPACE
  • ps2: NAME and LABEL are allowed to have multiple inputs
  • ps3: NS or NAMESPACE is required once NAME is used.
Examples:
  • SELECT
ksql> SELECT * FROM service NAMESPACE default NAME kubernetes
+------------+-----------+
| NAME       | NAMESPACE |
+------------+-----------+
| kubernetes | default   |
+------------+-----------+
ksql> SELECT "{ .metadata.name }" AS NAME, "{ .spec.clusterIP }" AS "CLUSTER-IP", "{ .spec.ports }" FROM svc NAMESPACE default NAME kubernetes
+------------+------------+------------------------------------------------------------------+
| NAME       | CLUSTER-IP | { .SPEC.PORTS }                                                  |
+------------+------------+------------------------------------------------------------------+
| kubernetes | 10.8.0.1   | [{"name":"https","port":443,"protocol":"TCP","targetPort":6443}] |
+------------+------------+------------------------------------------------------------------+
  • DESC
ksql> DESC envoyfilters.networking.istio.io
+-------------------------------------------------------------------------------------------------+----------+
| SCHEMA                                                                                          | VERSION  |
+-------------------------------------------------------------------------------------------------+----------+
| type EnvoyFilter struct {                                                                       | v1alpha3 |
|     // Customizing Envoy configuration generated by Istio. See more details at:                 |          |
|     // https://istio.io/docs/reference/config/networking/envoy-filter.html                      |          |
|     spec struct {                                                                               |          |
|         // One or more patches with match conditions.                                           |          |
|         configPatches []struct {                                                                |          |
|             applyTo string                                                                      |          |
|             // Match on listener/route configuration/cluster.                                   |          |
|             match struct {                                                                      |          |
|                 // Match on envoy cluster attributes.                                           |          |
|                 cluster struct {                                                                |          |
|                     // The exact name of the cluster to match.                                  |          |
|                     name string                                                                 |          |
|                     // The service port for which this cluster was generated.                   |          |
|                     portNumber int                                                              |          |
|                     // The fully qualified service name for this cluster.                       |          |
|                     service string                                                              |          |
|                     // The subset associated with the service.                                  |          |
|                     subset string                                                               |          |
|                 }                                                                               |          |
|                                                                                                 |          |
|                 ...                                                                             |          |
|                                                                                                 |          |
|     status map[string]interface{}                                                               |          |
| }                                                                                               |          |
+-------------------------------------------------------------------------------------------------+----------+

more usages, see EBNF description: https://github.com/imuxin/ksql/blob/86e62709a6f3f1d7d6da94b02232623b8df04426/pkg/parser/parser_test.go#L11-L23

Goal #2: make code easier to maintain

client-go ksql
func list() ([]T, error) {
    kubeConfig := getKubeConfig()

    client, err := dynamic.
        NewForConfig(kubeConfig)
    if err != nil {
        return nil, err
    }

    gvr :=
        schema.GroupVersionResource{
            Group:    "k8s.io",
            Version:  "v1alpha1",
            Resource: "tttt",
        }

    s := labels.NewSelector()
    req, err := labels.NewRequirement(
        "key", selection.Equals, []string{"val"})
    if err != nil {
        return nil, err
    }
    s = s.Add(*req)

    us, err := client.
        Resource(gvr).
        List(context.TODO(), metav1.ListOptions{
            LabelSelector: s.String(),
        })
    if err != nil {
        return nil, err
    }

    var results []T
    for _, item := range us.Items {
        obj := &T{}
        if err := runtime.DefaultUnstructuredConverter.
            FromUnstructured(item.Object, obj); err != nil {
            return nil, err
        }
        results = append(results, *obj)
    }
    return results, nil
}
import "github.com/imuxin/ksql/pkg/executor"

func list() ([]T, error) {
    kubeConfig := getKubeConfig()
    sql := `SELECT * FROM tttt.v1alpha1.k8s.io LABEL key = val`
    return executor.Execute[T](sql, kubeConfig)
}

Roadmap

  • Support SELECT stat
  • Support FROM
  • Support AS LABEL NAMESPACE NAME
  • Support WHERE expr
  • Support DESC expr
  • Support USE stat
  • Support DELETE stat
  • Support UPDATE stat
  • Support custom TABLE extensions
  • ...

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pkg
cmd
ext

Jump to

Keyboard shortcuts

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