Hello - does anyone know why the below gives the error ‘error: exactly one NAME . . .

Nat Kus:
Hello - does anyone know why the below gives the error ‘error: exactly one NAME is required, got 2’
k-cm hello whatever=1 a=b

function k-cm() {
    for var in "[email protected]"
        do
            if [ "$var" != "$1" ]; then
                values="$values --from-literal=$var"
            fi
        done
        echo  "kubectl create configmap $1 $values"
    kubectl create configmap $1 $values
}

Alistair Mackay:
Hi @Nat Kus
It works fine

controlplane ~ ➜  function k-cm() {
>     for var in "[email protected]"
>         do
>             if [ "$var" != "$1" ]; then
>                 values="$values --from-literal=$var"
>             fi
>         done
>         echo  "kubectl create configmap $1 $values"
>     kubectl create configmap $1 $values
> }

controlplane ~ ➜  k-cm test-cm x=1 y=2 key1=value1 key2=value2
kubectl create configmap test-cm  --from-literal=x=1 --from-literal=y=2 --from-literal=key1=value1 --from-literal=key2=value2
configmap/test-cm created

controlplane ~ ➜  k get cm test-cm
NAME               DATA   AGE
test-cm            4      10s

Nat Kus:
ok thanks - it might be my zsh

Alistair Mackay:
You’re right. I just tried it in zsh and got that result.

Try this

function k-cm() {
    name=$1
    shift
    values=()
    for var in [email protected]
    do
        values+=("--from-literal=$var")
    done
    echo "kubectl create configmap $name ${values[@]}"
    kubectl create configmap $name $values[@]
}