Using Visual Studio Code to Edit Files in Openshift Pods

Tracing PHP applications are typically done by putting echo statements in code path in a debug session. When PHP applications are deployed into openshift environment, the developer could edit the source code in development app (not production) to add echo statements. Typically this is done using the terminal. However the development experience is not very good.

Background

When debugging or tracing, we developers sometimes need to check multiple files at once, and sometimes need to do a project-wide search to find the source file containing one class or method. Most developers find this is not easy to do  in the terminal interface. They normally use editors such as Visual Studio Code to be able to search quickly and jump between source code files, and using vi in the terminal is somewhat off-putting. A better solution is to run Visual Studio Code remotely in the pod's container.

How to Launch Visual Studio Code in Pods

There are a few prerequisite needed to be able to launch visual studio code in Openshift Container :
Step 1. Install oc client
Check your openshift version and you could find oc client downloads from two major repository in github: the origin repository and the okd repository. The origin is used for 3.x versions, and 4.x later versions are stored in the okd repository. Check the releases page for either repository, and find your version tag there.


Find oc client link matching with your operating system :
If you are unfortunate enough to use Mac OS 12 Monterey but still need to connect to 3.x openshift, then you should check out this link in order to custom-build your oc client : https://inventorsparadox.blogspot.com/2022/01/rebuilding-openshift-oc-client-for-mac.html

Having downloaded the oc client, extract it, and copy the oc binary (executable) to your system path. You need Administrator permission to do that in most cases. In windows, in the Explorer, copy the oc.exe file, and then open C:\Windows\System32 and paste oc.exe there. In Mac OS, do this : cp oc usr/local/bin/oc 
If you are using linux, you should be able to find out on your own how to do that :D
Check the installed oc from command line, and make sure the OS could find your oc binary :


Then, create symbolic link to the oc with the name kubectl : 
ln -s /usr/local/bin/oc /usr/local/bin/kubectl
In windows you might just duplicate the oc.exe and copy it into kubectl.exe.

Step 2. Install Visual Studio Code Extensions for Kubernetes or Openshift
We have two options, that is to use the Kubernetes extension or the Openshift extension. In the old 3.x openshift release only the Kubernetes Extension could be used. First you need to open Extensions, click on the Code menu, then Preferences -> Extensions :

Install the Kubernetes extension :

And, Openshift extension if you have newer Openshift cluster (4.x ) :

3. Login to your openshift cluster
Using openshift 3.x, open up terminal and just :
oc login https://<your cluster API url>:8443

then type in your username and password. 
If using openshift 4.x, you need to log on the web console first, then click on the 'Copy login command'.



After re-login, we will be shown to a Display Token link, which upon clicked will get us this :

Copy and paste the oc login command, and execute it in the terminal/command prompt.


4. Run Visual Studio Code, open up the Kubernetes menu, and make sure you scroll to the part where there is a caret clickable to the left of Openshift Icon :


5. Left Click on the Workloads -> Pods -> Right Click on the Pod you want to run the Visual Studio Code editor  :


6. Then, Visual Studio Code will launch a new window, and it will install Visual Studio Code in the pod. Wait until the Visual Studio download completes.

7. Next, click Explorer icon and Open Folder button. Click ok (or, change the folder path if you want)
8. Now, you could leisurely edit the pod's content using Visual Studio Code editor ! But, still, your mileage may vary, VPN latency will reduce the experience a lot. In my experience, connecting to the openshift cluster in datacenter using our office wi-fi is a whole lot better than connecting using VPN connection from internet tethering.

Conclusion

By combining the oc client, Visual Studio Code Kubernetes and Openshift Extension, we could remotely edit code in the pod using Visual Studio Code GUI.



Comments

Popular posts from this blog

Long running process in Linux using PHP

Reverse Engineering Reptile Kernel module to Extract Authentication code

SAP System Copy Lessons Learned