Search code examples
openshiftremote-debugging

oc-command to forward local-ports to remote debug ports based on service-name instead of pod-name


To minimize the setup-time for attaching a debug session to the remote pod (microservice deployed on OpenShift) using intelliJ, I am trying to get the most out of the 'Before launch'-setting of the Remote Debug-Configuration.

I use 2 steps before attaching the debugger to the JVM Socket with following command-line arguments (this setup works but needs editing every new deploy);


    -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000

step 1: external tools: oc with arguments:


    login
    https://url.of.openshift.environment
    --username=<login>
    --password=<password>

step 2: external tools: oc with arguments:


    port-forward
    microservice-name-65-6bhz8   -> this needs to be changed after every deploy
    8000
    3000
    3001 

background info: this is the info in the service his YAML under spec>containers>env:

- name: JAVA_TOOL_OPTIONS
              value: >-
                -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n            
                -Dcom.sun.management.jmxremote=true
                -Dcom.sun.management.jmxremote.port=3000            
                -Dcom.sun.management.jmxremote.rmi.port=3001
                -Djava.rmi.server.hostname=127.0.0.1            
                -Dcom.sun.management.jmxremote.authenticate=false
                -Dcom.sun.management.jmxremote.ssl=false

As the name of the pod changes every (re-)deploy I am trying to find a oc-command which can be used to port-forward without having to provide the pod-name.(eg. based on the service-name) Or a completely other solution that allows me to hit 1 button to setup a debug-session (preferably in intelliJ).

> Screenshot IntelliJ settings

----------------------------- edit after tips -------------------------------

For now I made a small batch-script which does the trick: Feel free to help on a even faster solution (I'm checking https://openshiftdo.org/) or other intelliJent solutions


    set /p _username=Type your username:
    set /p _password=Type your password:
    oc login replace-with-openshift-console-url --username=%_username% --password=%_password%
    oc project replace-with-project-name  
    oc get pods --selector app=replace-with-app-name -o jsonpath={.items[?(@.status.phase=='Running')].metadata.name} > temp.txt
    set /p PODNAME= <temp.txt
    del temp.txt
    oc port-forward %PODNAME% 8000 3000 3001


Solution

  • Your going to need the pod name in order to port forward but of course you can fetch that programatically consistantly so you don't need to update in place every time.

    There are a number of ways you can do this, via jsonpath, go template, bash etc. An example would be to use the following, replacing your app name as required:

    oc get pod -l app=replace-me -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'