Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require func is fail #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

Ljohn001
Copy link

Check command is exist

I found require method to detect whether the command exists is invalid, take antisyntax problems
Here's the correct way to write it
require () {
for command in "$@"; do
if [ ! -x "$(command -v "$command")" ]; then
fail "'$command' util not found, please install it first"
fi
done
}

# Check command is exist

I found require method to detect whether the command exists is invalid, take antisyntax problems
Here's the correct way to write it 
require () {
  for command in "$@"; do
    if [ ! -x "$(command -v "$command")" ]; then
      fail "'$command' util not found, please install it first"
    fi
  done
}
@Ljohn001 Ljohn001 closed this Apr 26, 2022
@Ljohn001 Ljohn001 reopened this Apr 26, 2022
@WoozyMasta
Copy link
Owner

@Ljohn001, can you explain why this is an anti-syntax problem?

ABS guide just inclined towards this option

Yes, both work, but is it worth changing something? Can you explain your point of view?

! [ -x "$(command -v /bin/cat)" ] && echo none || echo ok
# ok
! [ -x "$(command -v /bin/cat2)" ] && echo none || echo ok
# none
[ ! -x "$(command -v /bin/cat)" ] && echo none || echo ok
# ok
[ ! -x "$(command -v /bin/cat2)" ] && echo none || echo ok
# none

@Ljohn001
Copy link
Author

Ljohn001 commented Apr 29, 2022

I found that there is this phenomenon, that is, this test sometimes takes effect and sometimes fails.
I guess there is something wrong with the alpine system, but I have no evidence that there is a problem with him.
Here are my dockerfile files. kube-dump version 1.1.1,

(base) ➜  1.1.1 git:(master) cat Dockerfile
FROM alpine:3.15

ARG KUBECTL_VERSION="1.19.15"
ENV KUBE_REPO=http://10.x.x.x:88/k8s

LABEL maintainer="ljohnmail@foxmail.com"

COPY ./kube-dump /opt/kube-dump
# hadolint ignore=DL3018
RUN set -ex && \
    sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
    apk add --update --no-cache \
    bash bind-tools jq yq openssh-client git tar xz gzip bzip2 curl coreutils grep && \
    curl -sLo /usr/bin/kubectl \
    "$KUBE_REPO/release/kubectl/kubectl-v$KUBECTL_VERSION" && \
    chmod +x /usr/bin/kubectl /opt/kube-dump

ENTRYPOINT [ "/opt/kube-dump"  ]
(base) ➜  1.1.1 git:(master) cat kube-dump| grep -C 5 'please install it first'

# Check command is exist
require () {
  for command in "$@"; do
    if [ ! -x "$(command -v "$command")" ]; then
      fail "'$command' util not found, please install it first"
    fi
  done
}

# Usage message

@Ljohn001
Copy link
Author

Ljohn001 commented Apr 29, 2022

I added debugging and rebuilt an image. Two servers executed the same command and returned different results. Here is the execution result.

dockerfile and kube-dump

(base) ➜  1.1.1 git:(master) ✗ cat Dockerfile
FROM alpine:3.15

ARG KUBECTL_VERSION="1.19.15"
ENV KUBE_REPO=http://10.x.x.x:88/k8s

LABEL maintainer="ljohnmail@foxmail.com"

COPY ./kube-dump /opt/kube-dump
# hadolint ignore=DL3018
RUN set -ex && \
    sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
    apk add --update --no-cache \
    bash bind-tools jq yq openssh-client git tar xz gzip bzip2 curl coreutils grep && \
    curl -sLo /usr/bin/kubectl \
    "$KUBE_REPO/release/kubectl/kubectl-v$KUBECTL_VERSION" && \
    chmod +x /usr/bin/kubectl /opt/kube-dump

ENTRYPOINT [ "/opt/kube-dump"  ]
(base) ➜  1.1.1 git:(master) ✗ cat kube-dump| grep -C 5 'please install it first'

# Check command is exist
require () {
  for command in "$@"; do
    if [ ! -x "$(command -v "$command")" ]; then
      fail "'$command' util not found, please install it first"
    fi
  done
}

# Usage message

A server

docker version  | grep 'Version:'
 Version:           19.03.5
  Version:          19.03.5
  Version:          v1.2.10
  Version:          1.0.0-rc8+dev
  Version:          0.18.0
root@SHTL009038079 tmp]# docker run --interactive --rm   --volume $HOME/.kube:/.kube --volume /tmp/dump:/dump   registry.xxx.com/base-image/kube-dump:test   ns -n qycxpt,default -r deployment -d /dump --kube-config /.kube/config
++ pwd
+ working_dir=/
++ date +%Y.%m.%d_%H-%M
+ timestamp=2022.04.29_13-46
+ '[' -f //.env ']'
+ [[ ns =~ ^(dump|all|dump-namespaces|ns|dump-cluster|cls)$ ]]
+ mode=ns
++ getopt -l namespaces:,namespaced-resources:,cluster-resources: -l kube-config:,kube-context:,kube-cluster:,kube-insecure-tls -l help,silent,destination:,force-remove,detailed,output-by-type,flat -l git-commit,git-push,git-branch:,git-commit-user:,git-commit-email: -l git-remote-name:,git-remote-url: -l archivate,archive-rotate-days:,archive-type: -o n:,r:,k:,h,s,d:,f,c,p,b:,a -- -n qycxpt,default -r deployment -d /dump --kube-config /.kube/config
+ args=' -n '\''qycxpt,default'\'' -r '\''deployment'\'' -d '\''/dump'\'' --kube-config '\''/.kube/config'\'' --'
+ eval set -- ' -n '\''qycxpt,default'\'' -r '\''deployment'\'' -d '\''/dump'\'' --kube-config '\''/.kube/config'\'' --'
++ set -- -n qycxpt,default -r deployment -d /dump --kube-config /.kube/config --
+ '[' 9 -ge 1 ']'
+ case "$1" in
+ namespaces+=qycxpt,default,
+ shift
+ shift
+ '[' 7 -ge 1 ']'
+ case "$1" in
+ namespaced_resources+=deployment,
+ shift
+ shift
+ '[' 5 -ge 1 ']'
+ case "$1" in
+ destination_dir=/dump
+ shift
+ shift
+ '[' 3 -ge 1 ']'
+ case "$1" in
+ kube_config=/.kube/config
+ shift
+ shift
+ '[' 1 -ge 1 ']'
+ case "$1" in
+ shift
+ break
+ [[ -n '' ]]
+ : ''
+ : ''
+ : ''
+ : ''
+ : /.kube/config
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ require kubectl jq yq
+ for command in "$@"
++ command -v kubectl
+ '[' '!' -x /usr/bin/kubectl ']'
+ fail ''\''kubectl'\'' util not found, please install it first'
+ '[' -t 1 ']'
+ log ERROR ''\''kubectl'\'' util not found, please install it first'
++ date '+%Y/%m/%d %H:%M:%S'
+ printf '%s [%s] %s\n' '2022/04/29 13:46:18' ERROR ''\''kubectl'\'' util not found, please install it first'
2022/04/29 13:46:18 [ERROR] 'kubectl' util not found, please install it first
+ exit 1

B server:

docker version  | grep 'Version:'
 Version:           20.10.8
  Version:          20.10.8
  Version:          v1.4.9
  Version:          1.0.3
  Version:          0.19.0

[root@SHTL165007016 tmp]# docker run --interactive --rm   --volume $HOME/.kube:/.kube --volume /tmp/dump:/dump   registry.xxxx.com/base-image/kube-dump:test   ns -n qycxpt,default -r deployment -d /dump --kube-config /.kube/config  &>/tmp/ok.log                                                                        
[root@SHTL165007016 tmp]# cat /tmp/ok.log
++ pwd
+ working_dir=/
++ date +%Y.%m.%d_%H-%M
+ timestamp=2022.04.29_13-57
+ '[' -f //.env ']'
+ [[ ns =~ ^(dump|all|dump-namespaces|ns|dump-cluster|cls)$ ]]
+ mode=ns
++ getopt -l namespaces:,namespaced-resources:,cluster-resources: -l kube-config:,kube-context:,kube-cluster:,kube-insecure-tls -l help,silent,destination:,force-remove,detailed,output-by-type,flat -l git-commit,git-push,git-branch:,git-commit-user:,git-commit-email: -l git-remote-name:,git-remote-url: -l archivate,a$
chive-rotate-days:,archive-type: -o n:,r:,k:,h,s,d:,f,c,p,b:,a -- -n qycxpt,default -r deployment -d /dump --kube-config /.kube/config
+ args=' -n '\''qycxpt,default'\'' -r '\''deployment'\'' -d '\''/dump'\'' --kube-config '\''/.kube/config'\'' --'
+ eval set -- ' -n '\''qycxpt,default'\'' -r '\''deployment'\'' -d '\''/dump'\'' --kube-config '\''/.kube/config'\'' --'
++ set -- -n qycxpt,default -r deployment -d /dump --kube-config /.kube/config --
+ '[' 9 -ge 1 ']'
+ case "$1" in
+ namespaces+=qycxpt,default,
+ shift
+ shift
+ '[' 7 -ge 1 ']'
+ case "$1" in
+ namespaced_resources+=deployment,
+ shift
+ shift
+ '[' 5 -ge 1 ']'
+ case "$1" in
+ destination_dir=/dump
+ shift
+ shift
+ '[' 3 -ge 1 ']'
+ case "$1" in
+ kube_config=/.kube/config
+ shift
+ shift
+ '[' 1 -ge 1 ']'
+ case "$1" in
+ shift
+ break
+ [[ -n '' ]]
+ : ''
+ : ''
+ : ''
+ : ''
+ : /.kube/config
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ require kubectl jq yq
+ for command in "$@"
++ command -v kubectl
+ '[' '!' -x /usr/bin/kubectl ']'
+ for command in "$@"
++ command -v jq
+ '[' '!' -x /usr/bin/jq ']'
+ for command in "$@"
++ command -v yq
+ '[' '!' -x /usr/bin/yq ']'
+ '[' '' == true ']'
+ '[' '' == true ']'
+ '[' '' == true ']'
+ '[' '' == true ']'
+ '[' -n /.kube/config ']'
+ k_args+=("--kubeconfig=$kube_config")
+ '[' -n '' ']'
+ '[' -n '' ']'
+ '[' '' == true ']'
+ '[' -n '' ']'
+ '[' -n '' ']'
+ kubectl config current-context --kubeconfig=/.kube/config
++ kubectl config current-context --kubeconfig=/.kube/config
+ context=context-cluster1
+ '[' -n '' ']'
+ kubectl cluster-info --kubeconfig=/.kube/config
...
+ log INFO 'Done! 2 task completed'
++ date '+%Y/%m/%d %H:%M:%S'
+ printf '%s [%s] %s\n' '2022/04/29 13:57:28' INFO 'Done! 2 task completed'
2022/04/29 13:57:28 [INFO] Done! 2 task completed
+ score=3
+ exit 0

@Ljohn001
Copy link
Author

Ljohn001 commented Apr 29, 2022

I added debugging and rebuilt an image. Two servers executed the same command and returned different results. Here is the execution result.

dockerfile and kube-dump

(base) ➜  1.1.1 git:(master) ✗ cat Dockerfile
FROM alpine:3.15

ARG KUBECTL_VERSION="1.19.15"
ENV KUBE_REPO=http://10.x.x.x:88/k8s

LABEL maintainer="ljohnmail@foxmail.com"

COPY ./kube-dump /opt/kube-dump
# hadolint ignore=DL3018
RUN set -ex && \
    sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
    apk add --update --no-cache \
    bash bind-tools jq yq openssh-client git tar xz gzip bzip2 curl coreutils grep && \
    curl -sLo /usr/bin/kubectl \
    "$KUBE_REPO/release/kubectl/kubectl-v$KUBECTL_VERSION" && \
    chmod +x /usr/bin/kubectl /opt/kube-dump

ENTRYPOINT [ "/opt/kube-dump"  ]
(base) ➜  1.1.1 git:(master) ✗ cat kube-dump| grep -C 5 'please install it first'

# Check command is exist
require () {
  for command in "$@"; do
    if [ ! -x "$(command -v "$command")" ]; then
      fail "'$command' util not found, please install it first"
    fi
  done
}

# Usage message

A server

docker version  | grep 'Version:'
 Version:           19.03.5
  Version:          19.03.5
  Version:          v1.2.10
  Version:          1.0.0-rc8+dev
  Version:          0.18.0
root@SHTL009038079 tmp]# docker run --interactive --rm   --volume $HOME/.kube:/.kube --volume /tmp/dump:/dump   registry.xxx.com/base-image/kube-dump:test   ns -n qycxpt,default -r deployment -d /dump --kube-config /.kube/config
++ pwd
+ working_dir=/
++ date +%Y.%m.%d_%H-%M
+ timestamp=2022.04.29_13-46
+ '[' -f //.env ']'
+ [[ ns =~ ^(dump|all|dump-namespaces|ns|dump-cluster|cls)$ ]]
+ mode=ns
++ getopt -l namespaces:,namespaced-resources:,cluster-resources: -l kube-config:,kube-context:,kube-cluster:,kube-insecure-tls -l help,silent,destination:,force-remove,detailed,output-by-type,flat -l git-commit,git-push,git-branch:,git-commit-user:,git-commit-email: -l git-remote-name:,git-remote-url: -l archivate,archive-rotate-days:,archive-type: -o n:,r:,k:,h,s,d:,f,c,p,b:,a -- -n qycxpt,default -r deployment -d /dump --kube-config /.kube/config
+ args=' -n '\''qycxpt,default'\'' -r '\''deployment'\'' -d '\''/dump'\'' --kube-config '\''/.kube/config'\'' --'
+ eval set -- ' -n '\''qycxpt,default'\'' -r '\''deployment'\'' -d '\''/dump'\'' --kube-config '\''/.kube/config'\'' --'
++ set -- -n qycxpt,default -r deployment -d /dump --kube-config /.kube/config --
+ '[' 9 -ge 1 ']'
+ case "$1" in
+ namespaces+=qycxpt,default,
+ shift
+ shift
+ '[' 7 -ge 1 ']'
+ case "$1" in
+ namespaced_resources+=deployment,
+ shift
+ shift
+ '[' 5 -ge 1 ']'
+ case "$1" in
+ destination_dir=/dump
+ shift
+ shift
+ '[' 3 -ge 1 ']'
+ case "$1" in
+ kube_config=/.kube/config
+ shift
+ shift
+ '[' 1 -ge 1 ']'
+ case "$1" in
+ shift
+ break
+ [[ -n '' ]]
+ : ''
+ : ''
+ : ''
+ : ''
+ : /.kube/config
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ require kubectl jq yq
+ for command in "$@"
++ command -v kubectl
+ '[' '!' -x /usr/bin/kubectl ']'
+ fail ''\''kubectl'\'' util not found, please install it first'
+ '[' -t 1 ']'
+ log ERROR ''\''kubectl'\'' util not found, please install it first'
++ date '+%Y/%m/%d %H:%M:%S'
+ printf '%s [%s] %s\n' '2022/04/29 13:46:18' ERROR ''\''kubectl'\'' util not found, please install it first'
2022/04/29 13:46:18 [ERROR] 'kubectl' util not found, please install it first
+ exit 1

B server:

docker version  | grep 'Version:'
 Version:           20.10.8
  Version:          20.10.8
  Version:          v1.4.9
  Version:          1.0.3
  Version:          0.19.0

[root@SHTL165007016 tmp]# docker run --interactive --rm   --volume $HOME/.kube:/.kube --volume /tmp/dump:/dump   registry.xxxx.com/base-image/kube-dump:test   ns -n qycxpt,default -r deployment -d /dump --kube-config /.kube/config  &>/tmp/ok.log                                                                        
[root@SHTL165007016 tmp]# cat /tmp/ok.log
++ pwd
+ working_dir=/
++ date +%Y.%m.%d_%H-%M
+ timestamp=2022.04.29_13-57
+ '[' -f //.env ']'
+ [[ ns =~ ^(dump|all|dump-namespaces|ns|dump-cluster|cls)$ ]]
+ mode=ns
++ getopt -l namespaces:,namespaced-resources:,cluster-resources: -l kube-config:,kube-context:,kube-cluster:,kube-insecure-tls -l help,silent,destination:,force-remove,detailed,output-by-type,flat -l git-commit,git-push,git-branch:,git-commit-user:,git-commit-email: -l git-remote-name:,git-remote-url: -l archivate,a$
chive-rotate-days:,archive-type: -o n:,r:,k:,h,s,d:,f,c,p,b:,a -- -n qycxpt,default -r deployment -d /dump --kube-config /.kube/config
+ args=' -n '\''qycxpt,default'\'' -r '\''deployment'\'' -d '\''/dump'\'' --kube-config '\''/.kube/config'\'' --'
+ eval set -- ' -n '\''qycxpt,default'\'' -r '\''deployment'\'' -d '\''/dump'\'' --kube-config '\''/.kube/config'\'' --'
++ set -- -n qycxpt,default -r deployment -d /dump --kube-config /.kube/config --
+ '[' 9 -ge 1 ']'
+ case "$1" in
+ namespaces+=qycxpt,default,
+ shift
+ shift
+ '[' 7 -ge 1 ']'
+ case "$1" in
+ namespaced_resources+=deployment,
+ shift
+ shift
+ '[' 5 -ge 1 ']'
+ case "$1" in
+ destination_dir=/dump
+ shift
+ shift
+ '[' 3 -ge 1 ']'
+ case "$1" in
+ kube_config=/.kube/config
+ shift
+ shift
+ '[' 1 -ge 1 ']'
+ case "$1" in
+ shift
+ break
+ [[ -n '' ]]
+ : ''
+ : ''
+ : ''
+ : ''
+ : /.kube/config
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ : ''
+ require kubectl jq yq
+ for command in "$@"
++ command -v kubectl
+ '[' '!' -x /usr/bin/kubectl ']'
+ for command in "$@"
++ command -v jq
+ '[' '!' -x /usr/bin/jq ']'
+ for command in "$@"
++ command -v yq
+ '[' '!' -x /usr/bin/yq ']'
+ '[' '' == true ']'
+ '[' '' == true ']'
+ '[' '' == true ']'
+ '[' '' == true ']'
+ '[' -n /.kube/config ']'
+ k_args+=("--kubeconfig=$kube_config")
+ '[' -n '' ']'
+ '[' -n '' ']'
+ '[' '' == true ']'
+ '[' -n '' ']'
+ '[' -n '' ']'
+ kubectl config current-context --kubeconfig=/.kube/config
++ kubectl config current-context --kubeconfig=/.kube/config
+ context=context-cluster1
+ '[' -n '' ']'
+ kubectl cluster-info --kubeconfig=/.kube/config
...
+ log INFO 'Done! 2 task completed'
++ date '+%Y/%m/%d %H:%M:%S'
+ printf '%s [%s] %s\n' '2022/04/29 13:57:28' INFO 'Done! 2 task completed'
2022/04/29 13:57:28 [INFO] Done! 2 task completed
+ score=3
+ exit 0

After I modified the shell of the command, the problem still exists. I think this has something to do with the docker version.

# Check command is exist
require () {
  for cmd in "$@"; do
     if ! test -x "$(which $cmd)";then
       fail "'$cmd' util not found, please install it first"
     fi
  done
}

+ require kubectl jq yq
+ for cmd in "$@"
++ which kubectl
+ test -x /usr/bin/kubectl
+ fail ''\''kubectl'\'' util not found, please install it first'
+ '[' -t 1 ']'
+ printf '\n\e[1;31m%-10s\e[m%s\n' Error: ''\''kubectl'\'' util not found, please install it first'

Error:    'kubectl' util not found, please install it first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants