• Kubernetes Submit Queue's avatar
    Merge pull request #48553 from superbrothers/fix-kubectl-42 · 54902739
    Kubernetes Submit Queue authored
    Automatic merge from submit-queue
    
    Fix a bug that --flag=val causes completion error in zsh
    
    **What this PR does / why we need it**:
    This PR fixes a bug that flag of syntax like --flag=val causes completion error in zsh.
    
    ```
    kubectl --namespace=foo g__handle_flag:25: bad math expression: operand expected at end of string
    ```
    
    This problem is due to [dynamic scope](https://en.wikipedia.org/wiki/Scope_(computer_science)#Dynamic_scoping) of shell variables. If a variable is declared as local to a function, that scope remains until the function returns.
    
    In kubectl completion zsh, `declare -A flaghash` in __start_kubectl() is replaced with `__kubectl_declare -A flaghash` by __kubectl_convert_bash_to_zsh(). As a result of it, flaghash is declared in __kubectl_declare(), and it can not access to flaghash declared in __kubectl_declare() from __handle_flag(). Therefore an error occurs in __handle_flag().
    
    The following is the minimum reproduction code.
    
    ```sh
    #!/usr/bin/env zsh
    
    set -e
    
    __kubectl_declare() {
        builtin declare "$@"
    }
    
    __handle_flag() {
        local flagname="--namespace="
        local flagval="kube-system"
    
        flaghash[${flagname}]=${flagval}
    
        echo "flaghash[${flagname}]=${flaghash[${flagname}]}"
    }
    
    __handle_word() {
        __handle_flag
    }
    
    __start_kubectl() {
        __kubectl_declare -A flaghash
    
        __handle_word
    }
    
    __start_kubectl
    
    #
    # $ zsh reproduction.zsh
    # __handle_flag:4: bad math expression: operand expected at end of string
    #
    
    # __start_kubectl {
    #
    #     __kubectl_declare {
    #
    #         builtin declare -A flaghash
    #
    #     }
    #
    #     __handle_word {
    #
    #         __handle_flag {
    #
    #             # It is unable to access flaghash declared in __kubectl_declare from here
    #             flaghash[${flagname}]=${flagval}
    #
    #         }
    #
    #     }
    # }
    ```
    
    The following is the fixed code.
    ```sh
    #!/usr/bin/env zsh
    
    set -e
    
    __handle_flag() {
        local flagname="--namespace="
        local flagval="kube-system"
    
        flaghash[${flagname}]=${flagval}
    
        echo "flaghash[${flagname}]=${flaghash[${flagname}]}"
    }
    
    __handle_word() {
        __handle_flag
    }
    
    __start_kubectl() {
        builtin declare -A flaghash
    
        __handle_word
    }
    
    __start_kubectl
    
    #
    # $ zsh fixed.zsh
    # flaghash[--namespace=]=kube-system
    #
    
    # __start_kubectl {
    #
    #     builtin declare -A flaghash
    #
    #     __handle_word {
    #
    #         __handle_flag {
    #
    #             # It is able to access flaghash declared in __start_kubectl from here :)
    #             flaghash[${flagname}]=${flagval}
    #
    #         }
    #
    #     }
    # }
    ```
    https://gist.github.com/superbrothers/0ede4292f6d973f93e54368e227a4902
    
    **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*:
    fixes kubernetes/kubectl#42
    
    **Special notes for your reviewer**:
    @mengqiy
    
    **Release note**:
    
    ```release-note
    NONE
    ```
    54902739
Name
Last commit
Last update
.github Loading commit data...
Godeps Loading commit data...
api Loading commit data...
build Loading commit data...
cluster Loading commit data...
cmd Loading commit data...
docs Loading commit data...
examples Loading commit data...
federation Loading commit data...
hack Loading commit data...
logo Loading commit data...
pkg Loading commit data...
plugin Loading commit data...
staging Loading commit data...
test Loading commit data...
third_party Loading commit data...
translations Loading commit data...
vendor Loading commit data...
.bazelrc Loading commit data...
.generated_files Loading commit data...
.gitattributes Loading commit data...
.gitignore Loading commit data...
.kazelcfg.json Loading commit data...
BUILD.bazel Loading commit data...
CHANGELOG.md Loading commit data...
CONTRIBUTING.md Loading commit data...
LICENSE Loading commit data...
Makefile Loading commit data...
Makefile.generated_files Loading commit data...
OWNERS Loading commit data...
OWNERS_ALIASES Loading commit data...
README.md Loading commit data...
Vagrantfile Loading commit data...
WORKSPACE Loading commit data...
code-of-conduct.md Loading commit data...
labels.yaml Loading commit data...