kubesql icon indicating copy to clipboard operation
kubesql copied to clipboard

Don't work if context isn't called "context"

Open Mart-Bogdan opened this issue 3 years ago • 5 comments

I've installed from cargo and tried query from readme

# RUST_BACKTRACE=1 kubesql --query "SELECT namespace FROM context WHERE pod.status.phase = 'Running'"
thread 'main' panicked at 'Given contexts ["context"] not found in your KUBECONFIG!', src/validator.rs:29:9
stack backtrace:
   0: rust_begin_unwind
             at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/library/std/src/panicking.rs:483
   1: std::panicking::begin_panic_fmt
             at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/library/std/src/panicking.rs:437
   2: kubesql::validator::validate_contexts
   3: kubesql::main::{{closure}}
   4: std::thread::local::LocalKey<T>::with
   5: tokio::park::thread::CachedParkThread::block_on
   6: tokio::runtime::thread_pool::ThreadPool::block_on
   7: tokio::runtime::Runtime::block_on
   8: kubesql::main

It seems application is trying to find context with name context, not using current-context that is set with kubectl config use-context

~/.kube/config

  11   │ contexts:
  12   │ - context:
  13   │     cluster: XXXXXXXXXXXXXXXXXXX
  14   │     user: XXXXXXXXXXXXXXXX
  15   │   name: org_name-kube-dev
  16   │ - context:
  17   │     cluster: YYYYYYYYYYYYYYYYYYYY
  18   │     user: YYYYYYYYYYYYYYYYY
  19   │   name: org_name-kube-prod
  20   │ current-context: org_name-kube-dev

Mart-Bogdan avatar Apr 23 '21 13:04 Mart-Bogdan

Hey, context is actually represented your contests[*].name. In this case, could you please try:

$ kubesql --query "SELECT default FROM org_name-kube-dev WHERE pod.status.phase = 'Running'"

Dentrax avatar Apr 23 '21 13:04 Dentrax

@Dentrax yay, it works!

would be nice if following could work too SELECT default FROM current-context WHERE pod.status.phase = 'Running'

P.S. strange, - arent's allowed in identifiers in SQL. Would integer expressions be evaluated in your tool? perhaps we should escape "table names" with ` or " ?

Mart-Bogdan avatar Apr 23 '21 14:04 Mart-Bogdan

I see, hyphens are used all across k8s so you are replacing it before SQL parser.

Hm, I'm still wondering is it possible to use math operators inside where?

Mart-Bogdan avatar Apr 23 '21 14:04 Mart-Bogdan

@Mart-Bogdan Using context or current-context in FROM is cool idea, I think. We have to get current-context from KUBECONFIG. And in case if current-context is set to org_name-kube-dev and if we send a query like:

$  SELECT default FROM current-context, org_name-kube-dev WHERE pod.status.phase = 'Running'

...then we should print from only one context since current-context == org_name-kube-dev.

In my theory, you able to evaluate math operators as long as sqlparser-rs are supported and if field-selectors returns numeric values. But needs some implementations. 🤔

Dentrax avatar Apr 23 '21 15:04 Dentrax

Will not work for EKS contexts as they have a '/' inside the name

current-context: arn:aws:eks:us-east-1:11111111111:cluster/cluster-1

Supporting double-quoted identifiers will help here. Have no idea how it's done in Rust though, but it looks like even the GenericParser is capable of correctly working with SELECT * FROM "table"

pkit avatar Sep 02 '21 16:09 pkit