2

Our client's core team uses kubeconfig with client-go authentication executing a python script to get token. They all use Macs. I use Windows with mingw64. No matter how I try to specify path to that script: forward slashes with drive letter between slashes or classic Windows style (even though it is present in PATH), even if I put the script into the same directory as kube config file and don't specify any path, I get Unable to connect to the server: getting credentials: exec: executable <our script name> not found. I can execute this script without specifying path to it from CLI just fine. I'm just trying to kubectl get pods. Relevant user: snippet:

- name: someName
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1beta1
      args:
      - --creds
      - '%userprofile%\path\to\credentials.properties'
      command: path\to\script
      env: null

Question: how to specify executable in exec section of kubeconfig in Windows?

Divijan
  • 141
  • 1
  • 6
  • 1
    be aware that windows does not use shebangs, so you will need to alter your kubeconfig to include `python the_script_name` since python is an executable but the script is not (from windows); you'll get more specific help posting the relevant `user:` snippet from your kubeconfig, but that's the general idea – mdaniel Dec 30 '20 at 19:27

2 Answers2

2

Kubernetes client-go already have helper methods for authentication using kubeconfig. It contains two variants with good examples:

I would recommend to use two built-in helper methods instead of rolling a custom variant. You may perhaps want to add an argument so that you can switch between the two, e.g. using out-of-cluster when running locally on Windows and in-cluster when the app (if your code is an app?) is running as a service in the cluster.

Jonas
  • 1,147
  • 5
  • 17
  • 31
  • I don't think this answer is applicable to my situation, because "Make sure your kubectl is configured and pointed to a cluster. Run kubectl get nodes to confirm." is a prerequisite for running an out-of-cluster example and this is precisely what I have trouble with. – Divijan Jan 04 '21 at 16:25
2

There is little documentation on using kubeconfig under Windows, so with a lot trial and error and a little help from @mdaniel's comment I came up with the following solution:

I created a Windows batch file that contains one line: @python <path-to-script> %* and referenced that file with full path in command: under exec:.

I had to use %* because python script takes parameters.

@ prevents Windows from echoing this command in console when running batch.

I couldn't do command: <path-to-python>\python <path-to-script> because kubernetes still complained it couldn't find the executable with such name.

Also, as a side note, if you use Windows environment variables like %userprofile% in kubeconfig, they need to be referenced inside single quotes.

Divijan
  • 141
  • 1
  • 6