KubeLibrary icon indicating copy to clipboard operation
KubeLibrary copied to clipboard

How to initialize library without kube config as im already in the current context via sshlibrary

Open ugandhar84 opened this issue 1 year ago • 6 comments

I'm already in the K8S cluster with proper context set via SSHLibrary Keywords in interactive shell. I want to avoid login kube config. how can i skip this. Please suggest.

Initializing library 'KubeLibrary' with arguments [ INIT_FOR_LIBDOC_ONLY=1 ] f ailed: ConfigException: Invalid kube-config file. No configuration found. Traceback (most recent call last): File "/home/xx/.local/lib/python3.10/site-packages/KubeLibrary/KubeLibrary.py", line 97, in init self.reload_config(kube_config=kube_config, context=context, api_url=api_url, bearer_token=bearer_token, File "/home/xx/.local/lib/python3.10/site-packages/KubeLibrary/KubeLibrary.py", line 270, in reload_config config.load_kube_config(kube_config, context) File "/home/user/.local/lib/python3.10/site-packages/kubernetes/config/kube_config.py", line 815, in load_kube_config loader = _get_kube_config_loader( File "/home/xx/.local/lib/python3.10/site-packages/kubernetes/config/kube_config.py", line 772, in _get_kube_config_loader raise ConfigException( kubernetes.config.config_exception.ConfigException: Invalid kube-config file. No configuration found.

ugandhar84 avatar Mar 18 '24 04:03 ugandhar84

Hi @ugandhar84 , you can initialize the KubeLibrary with the paramater incluster=True

Library          KubeLibrary    incluster=True

You can read about it in the keyword documentation here.

Nilsty avatar Mar 18 '24 13:03 Nilsty

Thank you for your response. it didn't help. one more point is I'm running a ROBOT script from a remote SSH session using SSHLIbrary. How do both Libraries sync to send these commands?

Initializing library 'KubeLibrary' with arguments [ incluster=True ] failed: C onfigException: Service host/port is not set. Traceback (most recent call last): File "/home/ugandhar/.local/lib/python3.10/site-packages/KubeLibrary/KubeLibrary.py", line 97, in init self.reload_config(kube_config=kube_config, context=context, api_url=api_url, bearer_token=bearer_token, File "/home/ugandhar/.local/lib/python3.10/site-packages/KubeLibrary/KubeLibrary.py", line 256, in reload_config raise e File "/home/ugandhar/.local/lib/python3.10/site-packages/KubeLibrary/KubeLibrary.py", line 253, in reload_config config.load_incluster_config() File "/home/ugandhar/.local/lib/python3.10/site-packages/kubernetes/config/incluster_config.py", line 121, in load_incluster_config try_refresh_token=try_refresh_token).load_and_set(client_configuration) File "/home/ugandhar/.local/lib/python3.10/site-packages/kubernetes/config/incluster_config.py", line 54, in load_and_set self._load_config() File "/home/ugandhar/.local/lib/python3.10/site-packages/kubernetes/config/incluster_config.py", line 62, in _load_config raise ConfigException("Service host/port is not set.") kubernetes.config.config_exception.ConfigException: Service host/port is not set.

ugandhar84 avatar Mar 18 '24 14:03 ugandhar84

With incluster=True the KubeLibrary calles this function in the python kubernetes client. The error message Service host/port is not set. means that it can not find the environment variables KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT and therefore can not make the connection to the kubernetes API of your cluster. Usually those env variables are available in any container running in a k8s cluster.

So you use the SSH library to connect into a container running in a k8s cluster?

Nilsty avatar Mar 18 '24 16:03 Nilsty

Yes, I'm using the ROBOT from my local and SSH library to connect remote systems and from there exporting the kubeconfig for the cluster. Seems like the SSH library and KubeLibrary is not working in that way.

Write Get Pods Write for Interactive Mode SSH and Get Pods from Kube Lib

ugandhar84 avatar Mar 18 '24 16:03 ugandhar84

hmm... You somehow need to run the robot test with the KubeLibrary inside the host, you are connecting to via SSH. I have not used the SSH lib myself, but I guess you could use Execute Command robot my-kube-libtests.robot to run the tests in the remote host. Not sure if this works for you.

I'm assuming here that you can not access the KubeAPI directly as the cluster is not exposing the access to its API. Maybe a better solution could be to use a proxy inside the cluster to expose the KubeAPI to your local system. Here is an example for that: https://kubernetes.io/docs/tasks/extend-kubernetes/socks5-proxy-access-api/ Then you would not need the SSH tunnel and could directly access the clusters KubeAPI.

Nilsty avatar Mar 19 '24 11:03 Nilsty

I appreciate your input and suggestions I’ve multiple jump servers in between cluster and local 🖥️. I’m just exploring another solution to use fabric .. fabric is doing okay with ssh tunnel with target host here the problem is if I set kubeconfig it is not accessible for the next subsequent command seems every command using different shell session :( would you mind to help here to make session as interactive or holding same session un till connection close?..

Local -> jump -> kubectl (remote)

classRemoteShell:

def__init__(self,jump_host,jump_username,jump_password,host,username, password,jump_port=2212,port=22,timeout=60,interval=1):

self.timeout=timeout self.interval=interval#Defaultintervalforcheckingoutput self.timer=None #CreateaFabricconfigurationforthejumphost jump_config=Config(overrides={'user':jump_username,'connect_kwargs':{ 'password':jump_password}}) try:

#Connecttothejumphost

self.jump_connection=Connection(host=jump_host,user=jump_username, connect_kwargs={'password':jump_password},port=jump_port)

self.jump_connection.open() #Findafreeportonlocalhost(127.0.0.1)

local_port=self._find_free_port()

#Establishaconnectiontotheactualremoteserverfromthejumphost self.remote_connection=Connection(host=host,user=jump_username,port=port,connect_kwargs{'password':jump_password},gateway=self .jump_connection) self.remote_connection.open() except(SSHException,socket.error)as e: raiseConnectionError(f"Failedtoconnecttoremoteserver:{e}") #Storetheoutputofthelastcommand self.last_output=""

def send_command(self,command):

#Startthetimer

self.start_timer() try:

#Executethecommandinteractively

result=self.remote_connection.run(command,pty=True) exceptSSHExceptionase: raiseRuntimeError(f"Errorexecutingcommand:{e}") finally: #Stopthetimer self.stop_timer() #Storetheoutputofthelastcommand

self .last_output=result.stdout.strip(),result.stderr.strip(),result.return_code #ConvertANSIescapesequencestoplaintext self.last_output=self._ansi_to_text(self.last_output[0]),self._ansi_to_text( self.last_output[1]),\

self.last_output[2] #Returntheoutputofthelastcommand returnself.last_output defget_last_output(self):

#Returntheoutputofthelastcommand returnself.last_output defclose(self):

try: self.remote_connection.close() self.jump_connection.close() exceptSSHExceptionase: raiseRuntimeError(f"Errorclosingconnection:{e}")

ugandhar84 avatar Mar 19 '24 12:03 ugandhar84