k3s/pkg
Kubernetes Submit Queue 5490273951 Merge pull request #48553 from superbrothers/fix-kubectl-42
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
```
2017-08-06 02:45:49 -07:00
..
api Merge pull request #49607 from dixudx/change_StS_observedGeneration_to_int 2017-08-02 20:07:56 -07:00
apimachinery/tests Move pkg/apimachinery/test to apimachinery 2017-07-08 08:48:38 +08:00
apis Merge pull request #47181 from dims/fail-on-swap-enabled 2017-08-04 14:29:36 -07:00
auth
bootstrap/api
capabilities
client generated 2017-08-02 10:27:36 -07:00
cloudprovider Merge pull request #49805 from nbutton23/nbutton-fix-elb-sg-bug 2017-08-05 12:32:59 -07:00
controller Merge pull request #49915 from caesarxuchao/controller-ignore-initialize-timeout 2017-08-05 19:07:53 -07:00
conversion
credentialprovider azure: acr: support auth to preview ACR w/ MSI+AAD 2017-07-18 15:22:34 -07:00
features Added taints node by condition feature flag. 2017-07-31 19:30:34 +08:00
fieldpath Add Pod UID (metadata.uid) to downward API env var 2017-06-27 16:54:35 +08:00
fields
generated kube-gen: fixup moved tests 2017-08-04 08:03:15 +02:00
hyperkube
kubeapiserver Move remaining cert helper functions to client-go/util/cert 2017-08-03 13:17:07 -07:00
kubectl Merge pull request #48553 from superbrothers/fix-kubectl-42 2017-08-06 02:45:49 -07:00
kubelet Fix typo in variable of remote 2017-08-06 01:05:34 +05:30
kubemark Merge pull request #40050 from mtaufen/standalone-mode 2017-07-25 12:14:43 -07:00
labels
master Don't enable apps/v1beta2 by default 2017-07-20 10:25:21 -07:00
printers Merge pull request #50071 from xiangpengzhao/fix-des-svc 2017-08-05 20:53:19 -07:00
probe update godep 2017-07-20 11:03:49 -07:00
proxy Merge pull request #49300 from tklauser/syscall-to-x-sys-unix 2017-08-03 04:02:12 -07:00
quota use informers for quota evaluation of core resources where possible 2017-07-19 15:52:39 -04:00
registry Merge pull request #49678 from smarterclayton/429_metric 2017-08-05 01:28:00 -07:00
routes
runtime
security allowPrivilegeEscalation: modify api types & add functionality 2017-07-24 12:52:41 -04:00
securitycontext allowPrivilegeEscalation: modify api types & add functionality 2017-07-24 12:52:41 -04:00
serviceaccount Move remaining cert helper functions to client-go/util/cert 2017-08-03 13:17:07 -07:00
ssh
types
util Merge pull request #49300 from tklauser/syscall-to-x-sys-unix 2017-08-03 04:02:12 -07:00
version
volume Merge pull request #49300 from tklauser/syscall-to-x-sys-unix 2017-08-03 04:02:12 -07:00
watch
BUILD generated innocuous change 2017-07-21 14:29:59 -07:00
OWNERS