bashopenshiftopenshift-client-tools

oc + bash - How to execute a local function in Openshift pod with "oc exec"?


I want to execute a local function in my script in each pod of Openshift.

function1() {
    ...
}

function2() {
    ...
}

verify() {
    ... # the function I want to execute in each pod
}

main() {
    # $1: openshift host
    if ! oc projects > /dev/null; then
        oc logout
        oc login "$1"
    fi
 
    while IFS= read -r project;
    do
        oc project $project
        while IFS= read -r pod;
        do
            echo Check pod $pod
            type verify # here it says it's a function
            ## how do we put it here?? this does not work, nothing happens, it just hangs. I expect "type verify" to be a function in the pod, just like above
            oc exec $pod -- bash -s "export -f function1; export -f function2; export -f verify; type verify; verify" 
        done <<< $(oc get pods | awk '/broker*/ {print $1}')
    done <<< $(oc projects | awk '{ some logic here }')
}

setup "$@"
main "$@"

I know that:

But now it's a local function, I don't know.


Solution

  • like ssh, typeset will make your functions available on a remote pod. oc exec $pod -- bash -c "$(typeset -f function1); function1"